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

77.5% Statements 31/40
33.33% Branches 8/24
42.86% Functions 3/7
81.58% Lines 31/38

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              1x 1x 1x 1x   1x 1x 1x 1x   1x 1x 1x 1x 1x       2x 2x     2x     1x 1x 1x           1x                                     1x 1x 1x 1x 1x   1x                                                                                 1x 1x           1x             1x                                             1x  
/*
 * 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, { useState } from 'react'
import { Container } from '@wings-software/uicore'
import { useParams } from 'react-router-dom'
import { Color } from '@harness/design-system'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import COProviderSelector from '@ce/components/COProviderSelector/COProviderSelector'
import { useQueryParams } from '@common/hooks'
import COGatewayDetails from '@ce/components/COGatewayDetails/COGatewayDetails'
import { Utils } from '@ce/common/Utils'
// import type { Provider } from '@ce/components/COCreateGateway/models'
import { useFeature } from '@common/hooks/useFeatures'
import { FeatureIdentifier } from 'framework/featureStore/FeatureIdentifier'
import { useStrings } from 'framework/strings'
import { GatewayKindType } from '@ce/constants'
import FeatureWarningBanner from '@common/components/FeatureWarning/FeatureWarningBanner'
import type { GatewayDetails } from './models'
 
function getStringVal(val: string | undefined): string {
  const stringVal = val?.toString()
  Iif (stringVal != undefined) {
    return stringVal
  }
  return ''
}
 
const COCreateGatewayContainer: React.FC = () => {
  const { getString } = useStrings()
  const { enabled, featureDetail } = useFeature({
    featureRequest: {
      featureName: FeatureIdentifier.RESTRICTED_AUTOSTOPPING_RULE_CREATION
    }
  })
 
  return (
    <Container background={Color.WHITE} height="100vh">
      {!enabled ? (
        <>
          <FeatureWarningBanner
            featureName={FeatureIdentifier.RESTRICTED_AUTOSTOPPING_RULE_CREATION}
            warningMessage={getString('ce.co.autoStoppingRule.limitWarningMessage', {
              limit: featureDetail?.limit,
              count: featureDetail?.count
            })}
          />
        </>
      ) : (
        <COCreateGateway />
      )}
    </Container>
  )
}
 
const COCreateGateway: React.FC = () => {
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
  const { provider } = useQueryParams<{ provider: string }>()
  const gatewayCreationTabs = ['providerSelector', 'gatewayConfig']
  const [currentTab, setCurrentTab] = useState<string | 'providerSelector'>('providerSelector')
 
  const initialGatewayDetails: GatewayDetails = {
    name: '',
    cloudAccount: {
      id: '',
      name: ''
    },
    idleTimeMins: 15,
    fullfilment: '',
    filter: '',
    kind: GatewayKindType.INSTANCE,
    orgID: getStringVal(orgIdentifier),
    projectID: getStringVal(projectIdentifier),
    accountID: accountId,
    hostName: '',
    customDomains: [],
    matchAllSubdomains: false,
    disabled: false,
    routing: {
      instance: {
        filterText: ''
      },
      lb: '',
      ports: []
    },
    healthCheck: Utils.getDefaultRuleHealthCheck(),
    opts: {
      preservePrivateIP: false,
      deleteCloudResources: false,
      alwaysUsePrivateIP: false,
      hide_progress_page: false
    },
    provider: {
      icon: '',
      name: '',
      value: ''
    },
    selectedInstances: [],
    accessPointID: '',
    metadata: {},
    deps: []
  }
  const [gatewayDetails, setGatewayDetails] = useState<GatewayDetails>(initialGatewayDetails)
  const nextTab = (): void => {
    const tabIndex = gatewayCreationTabs.findIndex(t => t == currentTab)
    if (tabIndex < gatewayCreationTabs.length - 1) {
      setCurrentTab(gatewayCreationTabs[tabIndex + 1])
    }
  }
  const previousTab = (): void => {
    const tabIndex = gatewayCreationTabs.findIndex(t => t == currentTab)
    if (tabIndex > 0) {
      setCurrentTab(gatewayCreationTabs[tabIndex - 1])
    }
  }
 
  return (
    <Container background={Color.WHITE} height="100vh">
      {currentTab === 'providerSelector' && (
        <COProviderSelector
          nextTab={nextTab}
          setGatewayDetails={setGatewayDetails}
          gatewayDetails={gatewayDetails}
          provider={provider}
        />
      )}
 
      {currentTab == 'gatewayConfig' && (
        <COGatewayDetails
          previousTab={previousTab}
          gatewayDetails={gatewayDetails}
          setGatewayDetails={setGatewayDetails}
          isEditFlow={false}
        />
      )}
    </Container>
  )
}
 
export default COCreateGatewayContainer