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

95.83% Statements 46/48
93.75% Branches 15/16
90.91% Functions 10/11
95.65% Lines 44/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 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              2x 2x 2x                   2x   2x 2x   2x 2x   2x 2x 2x 2x 2x 2x 2x                 2x                                       17x     2x 9x 9x 9x 9x     9x 9x   9x 8x 7x       9x 3x 1x 1x   3x 1x   3x         9x 18x       9x                                                                 3x 3x 3x 3x     18x                 18x                                                               2x  
/*
 * 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 {
  CardSelect,
  Layout,
  CardBody,
  Button,
  Heading,
  Container,
  Text,
  HarnessDocTooltip
} from '@wings-software/uicore'
import { omit as _omit } from 'lodash-es'
import type { IconName } from '@wings-software/uicore'
import routes from '@common/RouteDefinitions'
import { useStrings } from 'framework/strings'
import type { GatewayDetails, Provider, ProviderWithDependencies } from '@ce/components/COCreateGateway/models'
import { useTelemetry } from '@common/hooks/useTelemetry'
import { Breadcrumbs } from '@common/components/Breadcrumbs/Breadcrumbs'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { useFeatureFlags } from '@common/hooks/useFeatureFlag'
import { USER_JOURNEY_EVENTS } from '@ce/TrackingEventsConstants'
import { Utils } from '@ce/common/Utils'
import COGatewayBasics from '../COGatewayBasics/COGatewayBasics'
import COFixedDrawer from '../COGatewayAccess/COFixedDrawer'
import COHelpSidebar from '../COHelpSidebar/COHelpSidebar'
import css from './COProviderSelector.module.scss'
 
interface COProviderSelectorProps {
  nextTab: () => void
  setGatewayDetails: (gatewayDetails: GatewayDetails) => void
  gatewayDetails: GatewayDetails
  provider?: string
}
 
const data: ProviderWithDependencies[] = [
  {
    name: 'AWS',
    value: 'aws',
    icon: 'service-aws'
  },
  {
    name: 'Azure',
    value: 'azure',
    icon: 'service-azure'
  },
  {
    name: 'GCP',
    value: 'gcp',
    icon: 'gcp',
    ffDependencies: ['CE_AS_KUBERNETES_ENABLED']
  }
]
 
function getProvider(name: string | unknown): Provider | undefined {
  return data.find(p => p.name === name)
}
 
const COProviderSelector: React.FC<COProviderSelectorProps> = props => {
  const { getString } = useStrings()
  const { trackEvent } = useTelemetry()
  const { CE_AS_KUBERNETES_ENABLED } = useFeatureFlags()
  const [selectedCard, setSelectedCard] = useState<Provider | undefined>(
    getProvider(props.gatewayDetails.provider.name)
  )
  const [cloudAccountID, setCloudAccountID] = useState<string>(props.gatewayDetails.cloudAccount.id)
  const { accountId } = useParams<ProjectPathProps>()
 
  useEffect(() => {
    if (selectedCard) {
      trackEvent(USER_JOURNEY_EVENTS.SELECT_CLOUD_PROVIDER, { cloudProvider: selectedCard.name })
    }
  }, [selectedCard])
 
  const clearCloudAccountDetails = (_gatewayDetails: GatewayDetails): void => {
    if (_gatewayDetails.cloudAccount.id) {
      _gatewayDetails.cloudAccount.id = ''
      setCloudAccountID('')
    }
    if (_gatewayDetails.cloudAccount.name) {
      _gatewayDetails.cloudAccount.name = ''
    }
    Iif (_gatewayDetails.metadata.cloud_provider_details) {
      delete _gatewayDetails.metadata.cloud_provider_details
    }
  }
 
  const providerData = React.useMemo(
    () => data.filter(p => Utils.isFFEnabledForResource(p.ffDependencies, { CE_AS_KUBERNETES_ENABLED })),
    [CE_AS_KUBERNETES_ENABLED]
  )
 
  return (
    <>
      <Breadcrumbs
        className={css.breadCrumb}
        links={[
          {
            url: routes.toCECORules({ accountId }),
            label: getString('ce.co.breadCrumb.rules')
          },
          {
            url: '',
            label: props.gatewayDetails.name || ''
          }
        ]}
      />
      <COFixedDrawer
        topMargin={35}
        content={<COHelpSidebar pageName={'provider-selector'} activeSectionNames={[]} />}
      />
      <Container style={{ margin: '0 auto', paddingTop: 100, paddingLeft: 50 }}>
        <Layout.Vertical spacing="large" padding="large">
          <Heading className={css.title} data-tooltip-id="coGetStarted">
            {getString('common.letsGetYouStarted')}
            <HarnessDocTooltip tooltipId="coGetStarted" useStandAlone={true} />
          </Heading>
          <Heading level={2}>{getString('ce.co.autoStoppingRule.providerSelector.description')}</Heading>
          <Layout.Vertical spacing="small">
            <Layout.Horizontal spacing="small" style={{ paddingTop: '29px' }}>
              <CardSelect
                data={providerData}
                selected={selectedCard}
                className={css.providersViewGrid}
                onChange={item => {
                  setSelectedCard(item)
                  const updatedGatewayDetails = { ...props.gatewayDetails, provider: _omit(item, 'ffDependencies') }
                  clearCloudAccountDetails(updatedGatewayDetails)
                  props.setGatewayDetails(updatedGatewayDetails)
                }}
                renderItem={item => (
                  <Layout.Vertical spacing="small">
                    <CardBody.Icon className={css.card} icon={item.icon as IconName} iconSize={28}></CardBody.Icon>
                  </Layout.Vertical>
                )}
                cornerSelected={true}
              ></CardSelect>
            </Layout.Horizontal>
            <Layout.Horizontal spacing="medium" className={css.instanceTypeNameGrid}>
              {providerData.map(provider => {
                return (
                  <Text font={{ align: 'center' }} style={{ fontSize: 11 }} key={provider.value}>
                    {provider.name}
                  </Text>
                )
              })}
            </Layout.Horizontal>
          </Layout.Vertical>
          {selectedCard || props.provider ? (
            <COGatewayBasics
              gatewayDetails={props.gatewayDetails}
              setGatewayDetails={props.setGatewayDetails}
              setCloudAccount={setCloudAccountID}
              provider={props.provider}
            ></COGatewayBasics>
          ) : null}
        </Layout.Vertical>
        <Button
          className={css.footer}
          intent="primary"
          text="Next"
          icon="chevron-right"
          onClick={() => {
            props.nextTab()
          }}
          disabled={!(selectedCard && cloudAccountID)}
        />
      </Container>
    </>
  )
}
 
export default COProviderSelector