All files / modules/75-ce/components/COGatewayBasics COGatewayBasics.tsx

69.57% Statements 32/46
48.39% Branches 15/31
50% Functions 5/10
69.57% Lines 32/46

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              3x 3x 3x   3x   3x 3x 3x 3x                 3x 9x     9x       9x         9x 9x   9x   9x 6x         9x                   9x 10x   4x   2x   3x       1x       9x 1x   1x             9x 6x 1x         9x                                                                                                         3x  
/*
 * 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 { Layout } from '@wings-software/uicore'
import { useParams } from 'react-router-dom'
import type { ConnectorInfoDTO } from 'services/cd-ng'
import { useTelemetry } from '@common/hooks/useTelemetry'
import type { GatewayDetails } from '@ce/components/COCreateGateway/models'
import { ConnectorReferenceField } from '@connectors/components/ConnectorReferenceField/ConnectorReferenceField'
import { useStrings } from 'framework/strings'
import useCreateConnectorModal from '@connectors/modals/ConnectorModal/useCreateConnectorModal'
import { Connectors } from '@connectors/constants'
 
interface COGatewayBasicsProps {
  gatewayDetails: GatewayDetails
  setGatewayDetails: (gwDetails: GatewayDetails) => void
  setCloudAccount: (s: string) => void
  provider?: string
}
 
const COGatewayBasics: React.FC<COGatewayBasicsProps> = props => {
  const { accountId } = useParams<{
    accountId: string
  }>()
  const handleConnectorCreationSuccess = (data: ConnectorInfoDTO | undefined): void => {
    handleConnectorSelection(data as ConnectorInfoDTO)
  }
 
  const { openConnectorModal } = useCreateConnectorModal({
    onSuccess: data => {
      handleConnectorCreationSuccess(data?.connector)
    }
  })
  const { getString } = useStrings()
  const { trackEvent } = useTelemetry()
 
  const [selectedConnector, setSelectedConnector] = useState<ConnectorInfoDTO | null>(null)
 
  useEffect(() => {
    Iif (selectedConnector) {
      setSelectedConnector(null)
    }
  }, [props.gatewayDetails.provider.value])
 
  const handleConnectorSelection = (data: ConnectorInfoDTO): void => {
    setSelectedConnector(data)
    const updatedGatewayDetails = { ...props.gatewayDetails }
    updatedGatewayDetails.cloudAccount = { id: data.identifier?.toString(), name: data.name }
    updatedGatewayDetails.metadata.cloud_provider_details = { name: data.name }
    props.setGatewayDetails(updatedGatewayDetails)
    props.setCloudAccount(updatedGatewayDetails.cloudAccount.id)
    trackEvent('SelectingExistingConnector', {})
  }
 
  const getConnectorType = (provider: string) => {
    switch (provider) {
      case 'AWS':
        return Connectors.CEAWS
      case 'GCP':
        return Connectors.CE_GCP
      case 'Azure':
        return Connectors.CE_AZURE
      case 'Kubernetes':
        return Connectors.CE_KUBERNETES
      default:
        return null
    }
  }
 
  const handleConnectorCreation = (selectedProvider: string) => {
    const connectorType = getConnectorType(selectedProvider)
 
    Iif (connectorType) {
      openConnectorModal(false, connectorType, {
        connectorInfo: { orgIdentifier: '', projectIdentifier: '' } as unknown as ConnectorInfoDTO
      })
    }
  }
 
  useEffect(() => {
    if (props.provider) {
      handleConnectorCreation(props.provider)
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [props.provider])
 
  return (
    <div>
      <Layout.Vertical spacing="large">
        <ConnectorReferenceField
          name="cloudAccount"
          selected={props.gatewayDetails.cloudAccount.id || selectedConnector?.identifier}
          label={[
            getString('ce.co.gatewayBasics.connect'),
            props.gatewayDetails.provider.name,
            getString('rbac.account')
          ].join(' ')}
          placeholder={getString('ce.co.gatewayBasics.select')}
          accountIdentifier={accountId}
          onChange={handleConnectorSelection}
          type={
            props.gatewayDetails.provider.value
              ? (getConnectorType(props.gatewayDetails.provider.name) as ConnectorInfoDTO['type'])
              : undefined
          }
        />
      </Layout.Vertical>
      {localStorage.CENGAWSCONNECTOR && (
        <div>
          <span
            onClick={() => {
              openConnectorModal(false, Connectors.CEAWS, {
                connectorInfo: { orgIdentifier: '', projectIdentifier: '' } as unknown as ConnectorInfoDTO
              })
            }}
            style={{ fontSize: '13px', color: '#0278D5', lineHeight: '20px', cursor: 'pointer' }}
          >
            Try AWS New Connector
          </span>
        </div>
      )}
      {localStorage.CEGCPCONNECTOR && (
        <div>
          <span
            onClick={() => {
              openConnectorModal(false, Connectors.CE_GCP, {
                connectorInfo: { orgIdentifier: '', projectIdentifier: '' } as unknown as ConnectorInfoDTO
              })
            }}
            style={{ fontSize: '13px', color: '#0278D5', lineHeight: '20px', cursor: 'pointer' }}
          >
            Try GCP New Connector
          </span>
        </div>
      )}
    </div>
  )
}
 
export default COGatewayBasics