All files / modules/35-connectors/components/CreateConnector/CEGcpConnector/steps GrantPermission.tsx

89.36% Statements 42/47
50% Branches 30/60
57.14% Functions 4/7
89.36% Lines 42/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189              219x 219x 219x 219x                   219x 219x 219x 219x 219x 219x 219x   219x   219x 4x 3x   3x   3x     3x 3x 3x   3x     3x               3x         3x 3x 1x 1x       3x 1x   1x 1x 1x               1x       1x       1x         1x     3x       3x 3x                             3x                                                                                                                               219x               219x  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React, { useEffect, useState } from 'react'
import { useParams } from 'react-router-dom'
import { pick } from 'lodash-es'
import {
  Button,
  Container,
  Heading,
  Layout,
  StepProps,
  ModalErrorHandler,
  ModalErrorHandlerBinding,
  Icon
} from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
import { useGcpserviceaccount } from 'services/ce'
import { useCreateConnector, useUpdateConnector, Failure } from 'services/cd-ng'
import CopyToClipboard from '@common/components/CopyToClipBoard/CopyToClipBoard'
import { CE_GCP_CONNECTOR_CREATION_EVENTS } from '@connectors/trackingConstants'
import { useStepLoadTelemetry } from '@connectors/common/useTrackStepLoad/useStepLoadTelemetry'
import { useFeatureFlags } from '@common/hooks/useFeatureFlag'
import type { CEGcpConnectorDTO } from './OverviewStep'
import css from '../CreateCeGcpConnector.module.scss'
 
const GrantPermission: React.FC<StepProps<CEGcpConnectorDTO>> = props => {
  const { getString } = useStrings()
  const { CE_AS_GCP_VM_SUPPORT } = useFeatureFlags()
 
  useStepLoadTelemetry(CE_GCP_CONNECTOR_CREATION_EVENTS.LOAD_GRANT_PERMISSIONS)
 
  const { accountId } = useParams<{
    accountId: string
  }>()
  const { prevStepData, nextStep, previousStep } = props
  const [isSubmitLoading, setIsSubmitLoading] = useState<boolean>(false)
  const [modalErrorHandler, setModalErrorHandler] = useState<ModalErrorHandlerBinding | undefined>()
 
  const { mutate: createConnector } = useCreateConnector({
    queryParams: { accountIdentifier: accountId }
  })
  const { mutate: updateConnector } = useUpdateConnector({
    queryParams: { accountIdentifier: accountId }
  })
 
  const {
    data,
    loading: loadingServiceAccount,
    refetch: refetchServiceAccount
  } = useGcpserviceaccount({
    queryParams: { accountIdentifier: accountId },
    lazy: true
  })
 
  const serviceAccount = prevStepData?.serviceAccount || data?.data
  useEffect(() => {
    Eif (!prevStepData?.serviceAccount) {
      refetchServiceAccount()
    }
  }, [])
 
  const handleSubmit = async () => {
    setIsSubmitLoading(true)
 
    try {
      Eif (prevStepData) {
        const connectorInfo: CEGcpConnectorDTO = {
          ...pick(prevStepData, ['name', 'identifier', 'description', 'tags', 'spec', 'type']),
          spec: {
            ...prevStepData.spec,
            serviceAccountEmail: serviceAccount || ''
          }
        }
 
        const response = await (prevStepData.isEditMode
          ? updateConnector({ connector: connectorInfo })
          : createConnector({ connector: connectorInfo }))
 
        Iif (response.status !== 'SUCCESS') {
          throw response as Failure
        }
 
        nextStep?.({ ...prevStepData, serviceAccount })
      }
    } catch (e) {
      modalErrorHandler?.showDanger(e?.data?.message)
    }
    setIsSubmitLoading(false)
  }
 
  const handlePrev = () => {
    previousStep?.({ ...(prevStepData as CEGcpConnectorDTO), serviceAccount })
  }
 
  const renderOptimizationSteps = () => {
    return CE_AS_GCP_VM_SUPPORT && props.prevStepData?.spec.featuresEnabled?.includes('OPTIMIZATION') ? (
      <>
        <li>
          <div>{getString('connectors.ceGcp.grantPermission.optimization.step1')}</div>
        </li>
        <li>
          <div>{getString('connectors.ceGcp.grantPermission.optimization.step2', { serviceAccount })}</div>
        </li>
        <li>
          <div>{getString('connectors.ceGcp.grantPermission.optimization.step3')}</div>
        </li>
      </>
    ) : null
  }
 
  return (
    <Layout.Vertical className={css.stepContainer}>
      <Heading level={2} className={css.header}>
        {getString('connectors.ceGcp.grantPermission.heading')}
      </Heading>
      <ModalErrorHandler bind={setModalErrorHandler} />
      <ol className={css.grantPermissionSteps}>
        <li>
          <Layout.Vertical spacing="small">
            <div>{getString('connectors.ceGcp.grantPermission.step1')}</div>
            <Button
              className={css.bigQueryPage}
              text={getString('connectors.ceGcp.grantPermission.bigQueryButtonText')}
              rightIcon="chevron-right"
              onClick={() => {
                window.open('https://console.cloud.google.com/bigquery')
              }}
            />
          </Layout.Vertical>
        </li>
        <li>
          <div>{getString('connectors.ceGcp.grantPermission.step2')}</div>
        </li>
        <li>
          <div>{getString('connectors.ceGcp.grantPermission.step3')}</div>
        </li>
        <li>
          <div>{getString('connectors.ceGcp.grantPermission.step4')}</div>
        </li>
        <li>
          <Layout.Vertical spacing="small">
            <div>{getString('connectors.ceGcp.grantPermission.step5')}</div>
            <Container className={css.commandsContainer}>
              <Container className={css.command}>
                {loadingServiceAccount ? <Loader /> : <pre>{serviceAccount}</pre>}
                <CopyToClipboard showFeedback content={serviceAccount || ''} />
              </Container>
            </Container>
          </Layout.Vertical>
        </li>
        <li>
          <div>{getString('connectors.ceGcp.grantPermission.step6')}</div>
        </li>
        {renderOptimizationSteps()}
        <li>
          <div>{getString('connectors.ceGcp.grantPermission.step7')}</div>
        </li>
      </ol>
 
      <Layout.Horizontal className={css.buttonPanel} spacing="small">
        <Button text={getString('previous')} icon="chevron-left" onClick={handlePrev} disabled={isSubmitLoading} />
        <Button
          type="submit"
          intent="primary"
          text={getString('continue')}
          rightIcon="chevron-right"
          onClick={handleSubmit}
          disabled={isSubmitLoading}
        />
      </Layout.Horizontal>
    </Layout.Vertical>
  )
}
 
const Loader = () => {
  return (
    <Container className={css.serviceAccountLoader}>
      <Icon name="spinner" color="primary7" size={18} />
    </Container>
  )
}
 
export default GrantPermission