All files / modules/75-ce/components/AccessPoint/GCPAccessPoint GCPAccessPointForm.tsx

100% Statements 72/72
87.8% Branches 108/123
100% Functions 23/23
100% Lines 72/72

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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356              10x 10x 10x 10x 10x 10x               10x   10x   10x 10x 10x                                             10x               17x 17x 17x   17x 17x 17x 17x 17x 17x 17x 17x 17x       17x                   17x                   17x                   17x                     17x                   17x 8x 2x 2x 2x       17x 8x 2x       17x 7x   6x             7x     17x 7x   6x             7x     17x 7x   6x             7x     17x 7x   6x             7x     17x 7x   6x             7x     17x 8x 1x       17x                 6x                 15x               1x 25x                 1x                       1x                           1x 1x                   30x                                                                                                     2x                                         10x  
/*
 * Copyright 2022 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 { Menu } from '@blueprintjs/core'
import { isEmpty as _isEmpty, defaultTo as _defaultTo } from 'lodash-es'
import { Button, Container, Formik, FormikForm, FormInput, Icon, Layout, SelectOption } from '@harness/uicore'
import {
  AccessPoint,
  useAllSecurityGroups,
  useAllSubnets,
  useAllVPCs,
  useAllZones,
  useGetMachineListForZone
} from 'services/lw'
import { useStrings } from 'framework/strings'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import useRegionsForSelection from '@ce/common/hooks/useRegionsForSelection'
import type { AccessPointScreenMode } from '@ce/types'
import { useFeatureFlag } from '@common/hooks/useFeatureFlag'
import { FeatureFlag } from '@common/featureFlags'
import css from './GCPAccessPoint.module.scss'
 
export interface GcpApFormValue {
  region?: string
  securityGroups?: Array<SelectOption>
  certificate?: string
  vpc?: string
  zone?: string
  machine_type?: string
  subnet_name?: string
  cert_secret_id?: string
  key_secret_id?: string
}
 
interface GCPAccessPointFormProps {
  handleSubmit?: (values: GcpApFormValue) => void
  loadBalancer: AccessPoint
  cloudAccountId: string
  handlePreviousClick: (values: GcpApFormValue) => void
  isSaving: boolean
  mode: AccessPointScreenMode
}
 
const GCPAccessPointForm: React.FC<GCPAccessPointFormProps> = ({
  loadBalancer,
  cloudAccountId,
  handlePreviousClick,
  handleSubmit,
  // mode,
  isSaving
}) => {
  const { getString } = useStrings()
  const { accountId } = useParams<AccountPathProps>()
  const tlsSupportEnabled = useFeatureFlag(FeatureFlag.CCM_GCP_TLS_ENABLED)
 
  const [selectedRegion, setSelectedRegion] = useState<string | undefined>(loadBalancer.region)
  const [selectedVpc, setSelectedVpc] = useState<string | undefined>(loadBalancer.vpc)
  const [selectedZone, setSelectedZone] = useState<string | undefined>(loadBalancer.metadata?.zone)
  const [zonesOptions, setZonesOptions] = useState<SelectOption[]>([])
  const [vpcOptions, setvpcOptions] = useState<SelectOption[]>([])
  const [sgOptions, setSGOptions] = useState<SelectOption[]>([])
  const [machineTypesOptions, setMachineTypesOptions] = useState<SelectOption[]>([])
  const [subnetOptions, setSubnetOptions] = useState<SelectOption[]>([])
  const { data: regionOptions } = useRegionsForSelection({ cloudAccountId, additionalProps: {} })
 
  // const isEditMode = mode === 'edit' || !_isEmpty(loadBalancer.id)
 
  const { data: zones, refetch: fetchZones } = useAllZones({
    account_id: accountId,
    queryParams: {
      cloud_account_id: cloudAccountId,
      accountIdentifier: accountId,
      region: _defaultTo(selectedRegion, '')
    },
    lazy: true
  })
 
  const { data: vpcs, refetch: vpcsReload } = useAllVPCs({
    account_id: accountId, // eslint-disable-line
    queryParams: {
      region: _defaultTo(selectedRegion, ''),
      cloud_account_id: cloudAccountId, // eslint-disable-line
      accountIdentifier: accountId
    },
    lazy: true
  })
 
  const { data: machinesData, refetch: fetchMachines } = useGetMachineListForZone({
    account_id: accountId,
    queryParams: {
      accountIdentifier: accountId,
      cloud_account_id: cloudAccountId,
      zone: _defaultTo(selectedZone, '')
    },
    lazy: true
  })
 
  const { data: securityGroups, refetch: sgsReload } = useAllSecurityGroups({
    account_id: accountId, // eslint-disable-line
    queryParams: {
      region: _defaultTo(selectedRegion, ''),
      vpc_id: _defaultTo(selectedVpc, ''),
      cloud_account_id: cloudAccountId,
      accountIdentifier: accountId
    },
    lazy: true
  })
 
  const { data: subnets, refetch: subnetsReload } = useAllSubnets({
    account_id: accountId, // eslint-disable-line
    queryParams: {
      cloud_account_id: cloudAccountId,
      region: _defaultTo(selectedRegion, ''),
      accountIdentifier: accountId
    },
    lazy: true
  })
 
  useEffect(() => {
    if (selectedRegion) {
      vpcsReload()
      fetchZones()
      subnetsReload()
    }
  }, [selectedRegion])
 
  useEffect(() => {
    if (selectedVpc) {
      sgsReload()
    }
  }, [selectedVpc])
 
  useEffect(() => {
    const loaded: SelectOption[] = _defaultTo(
      zones?.response?.map(z => {
        return {
          label: z,
          value: z
        }
      }),
      []
    )
    setZonesOptions(loaded)
  }, [zones])
 
  useEffect(() => {
    const loaded: SelectOption[] = _defaultTo(
      vpcs?.response?.map(v => {
        return {
          label: v.name as string,
          value: v.name as string
        }
      }),
      []
    )
    setvpcOptions(loaded)
  }, [vpcs])
 
  useEffect(() => {
    const loaded: SelectOption[] = _defaultTo(
      securityGroups?.response?.map(v => {
        return {
          label: v.name as string,
          value: v.id as string
        }
      }),
      []
    )
    setSGOptions(loaded)
  }, [securityGroups])
 
  useEffect(() => {
    const loaded: SelectOption[] = _defaultTo(
      machinesData?.response?.map(m => {
        return {
          label: m.name as string,
          value: m.name as string
        }
      }),
      []
    )
    setMachineTypesOptions(loaded)
  }, [machinesData])
 
  useEffect(() => {
    const loaded: SelectOption[] = _defaultTo(
      subnets?.response?.map(s => {
        return {
          label: s.name as string,
          value: s.name as string
        }
      }),
      []
    )
    setSubnetOptions(loaded)
  }, [subnets])
 
  useEffect(() => {
    if (selectedZone) {
      fetchMachines()
    }
  }, [selectedZone])
 
  return (
    <Container>
      <Formik<GcpApFormValue>
        initialValues={{
          region: _defaultTo(loadBalancer.region, selectedRegion),
          zone: _defaultTo(loadBalancer.metadata?.zone, selectedZone),
          vpc: _defaultTo(loadBalancer.vpc, selectedVpc),
          securityGroups: _defaultTo(
            loadBalancer.metadata?.security_groups?.map(x => {
              return {
                value: x,
                label: x
              }
            }),
            []
          ),
          machine_type: _defaultTo(
            loadBalancer.metadata?.machine_type,
            machinesData?.response?.find(m => m.is_default)?.name
          ),
          subnet_name: loadBalancer.metadata?.subnet_name,
          cert_secret_id: _defaultTo(loadBalancer.metadata?.certificates?.[0]?.cert_secret_id, ''),
          key_secret_id: _defaultTo(loadBalancer.metadata?.certificates?.[0]?.key_secret_id, '')
        }}
        enableReinitialize
        formName="lbFormSecond"
        onSubmit={values => handleSubmit?.(values)}
        render={({ submitForm, setFieldValue, values }) => (
          <FormikForm>
            <Layout.Horizontal className={css.formFieldRow}>
              <FormInput.Select
                name="region"
                label={getString('regionLabel')}
                placeholder={getString('pipeline.regionPlaceholder')}
                items={regionOptions}
                onChange={item => {
                  setSelectedRegion(item.value as string)
                }}
                // disabled={
                //   isEditMode ? !editableFieldsMap['accessPointRegion'] : regionsLoading || regionOptions.length === 0
                // }
              />
              <FormInput.Select
                name="zone"
                label={getString('ce.co.accessPoint.zone')}
                placeholder={getString('ce.co.accessPoint.select.zone')}
                items={zonesOptions}
                onChange={item => {
                  setSelectedZone(item.value as string)
                }}
                // disabled={
                //   isEditMode ? !editableFieldsMap['accessPointRegion'] : regionsLoading || regionOptions.length === 0
                // }
              />
            </Layout.Horizontal>
            <Layout.Horizontal className={css.formFieldRow}>
              <FormInput.Select
                name="vpc"
                label={getString('ce.co.accessPoint.vpcLabel')}
                placeholder={getString('ce.co.accessPoint.select.vpc')}
                items={vpcOptions}
                onChange={e => {
                  setFieldValue('vpc', e.value)
                  setSelectedVpc(e.value as string)
                }}
                // disabled={isEditMode ? !editableFieldsMap['vpc'] : vpcsLoading || vpcOptions.length === 0}
              />
              <FormInput.MultiSelect
                name="securityGroups"
                label={getString('ce.co.accessPoint.securityGroupsLabel')}
                placeholder={getString('ce.co.accessPoint.select.securityGroups')}
                multiSelectProps={{
                  itemRender: (_item, { handleClick }) => (
                    <Menu.Item
                      key={_item.label}
                      style={{
                        whiteSpace: 'nowrap',
                        overflow: 'hidden',
                        textOverflow: 'ellipsis',
                        padding: '5px 10px'
                      }}
                      onClick={handleClick}
                      text={_item.label}
                    />
                  )
                }}
                items={sgOptions}
                // disabled={isEditMode ? !editableFieldsMap['securityGroups'] : sgsLoading || sgOptions.length === 0}
              />
            </Layout.Horizontal>
            <Layout.Horizontal className={css.formFieldRow}>
              <FormInput.Select
                label={getString('ce.co.accessPoint.subnet')}
                placeholder={getString('ce.co.accessPoint.select.subnet')}
                name="subnet_name"
                items={subnetOptions}
                // disabled={
                //   isEditMode
                //     ? !editableFieldsMap['subnet']
                //     : !_isEmpty(values.subnet)
                //     ? false
                //     : subnetsLoading || !selectedVpc
                // }
              />
              <FormInput.Select
                name="machine_type"
                label={getString('ce.co.accessPoint.machineType')}
                placeholder={getString('ce.co.accessPoint.select.machineType')}
                items={machineTypesOptions}
                // disabled={
                //   isEditMode ? !editableFieldsMap['accessPointRegion'] : regionsLoading || regionOptions.length === 0
                // }
              />
            </Layout.Horizontal>
            {tlsSupportEnabled && (
              <Layout.Horizontal className={css.formFieldRow}>
                <FormInput.Text name="cert_secret_id" label={getString('ce.co.accessPoint.gcpCertificateId')} />
                <FormInput.Text name="key_secret_id" label={getString('ce.co.accessPoint.gcpSecretId')} />
              </Layout.Horizontal>
            )}
            <Layout.Horizontal style={{ marginTop: 220 }}>
              <Button
                text={'Back'}
                icon={'chevron-left'}
                onClick={() => handlePreviousClick(values)}
                data-testid="previousButton"
                style={{ marginRight: 'var(--spacing-large)' }}
              ></Button>
              {isSaving && <Icon name="spinner" size={24} color="blue500" style={{ alignSelf: 'center' }} />}
              {!isSaving && (
                <Button
                  intent="primary"
                  text={'Save Load Balancer'}
                  onClick={submitForm}
                  disabled={!selectedRegion && !selectedVpc && _isEmpty(loadBalancer.metadata?.security_groups)}
                ></Button>
              )}
            </Layout.Horizontal>
          </FormikForm>
        )}
      ></Formik>
    </Container>
  )
}
 
export default GCPAccessPointForm