All files / modules/75-ce/components/COAsgSelector index.tsx

75.61% Statements 62/82
47.27% Branches 52/110
82.35% Functions 14/17
75.31% Lines 61/81

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              5x 5x   5x 5x 5x                     5x   5x 5x 5x 5x                             22x             11x             5x   5x 10x 9x 9x 9x 9x 9x   9x     18x     2x         9x             1x         1x         1x 1x   1x 1x               1x     9x 1x     9x 1x 1x 1x               1x     9x                             9x   9x                                                                                                                                                                                                                                   5x 14x 14x 14x     14x       14x 14x 14x           14x                   14x 6x                 6x           14x 6x 6x       14x 6x         14x                                                     14x     5x  
/*
 * 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 type { CellProps } from 'react-table'
import { isEmpty as _isEmpty, defaultTo } from 'lodash-es'
import { Radio } from '@blueprintjs/core'
import {
  Text,
  Container,
  ExpandingSearchInput,
  Layout,
  Button,
  Icon,
  TableV2,
  Select,
  SelectOption
} from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import type { GatewayDetails } from '@ce/components/COCreateGateway/models'
import { useStrings } from 'framework/strings'
import { ASGMinimal, PortConfig, TargetGroupMinimal, useAllZones } from 'services/lw'
import { Utils } from '@ce/common/Utils'
import useRegionsForSelection from '@ce/common/hooks/useRegionsForSelection'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
 
interface COAsgSelectorprops {
  scalingGroups: ASGMinimal[]
  selectedScalingGroup: ASGMinimal | undefined
  setSelectedAsg: (asg: ASGMinimal) => void
  search: (t: string) => void
  gatewayDetails: GatewayDetails
  onAsgAddSuccess?: (updatedGatewayDetails: GatewayDetails) => void
  loading: boolean
  refresh?: (text?: string) => void
}
 
function TableCell(tableProps: CellProps<ASGMinimal>): JSX.Element {
  return (
    <Text lineClamp={3} color={Color.BLACK}>
      {tableProps.value}
    </Text>
  )
}
function NameCell(tableProps: CellProps<ASGMinimal>): JSX.Element {
  return (
    <Text lineClamp={1} color={Color.BLACK}>
      {`${tableProps.value} ${tableProps.row.original.id}`}
    </Text>
  )
}
 
const TOTAL_ITEMS_PER_PAGE = 5
 
const COAsgSelector: React.FC<COAsgSelectorprops> = props => {
  const [selectedAsg, setSelectedAsg] = useState<ASGMinimal | undefined>(props.selectedScalingGroup)
  const [pageIndex, setPageIndex] = useState<number>(0)
  const [gcpFilters, setGcpFilters] = useState<GCPFiltersProps>()
  const [isLoading, setIsLoading] = useState<boolean>(false)
  const isAsgSelected = !_isEmpty(selectedAsg)
  const { getString } = useStrings()
 
  const isGcpProvider = Utils.isProviderGcp(props.gatewayDetails.provider)
 
  function TableCheck(tableProps: CellProps<ASGMinimal>): JSX.Element {
    return (
      <Radio
        checked={selectedAsg?.name === tableProps.row.original.name}
        onClick={_ => setSelectedAsg(tableProps.row.original)}
      />
    )
  }
 
  const addAsg = () => {
    /**
     * desired capacity can't be 0
     * it can be either > 0 or
     * summation of od + spot or
     * equal to max capacity
     *  */
    const desiredCapacityValue = isGcpProvider
      ? selectedAsg?.desired || 1
      : selectedAsg?.desired ||
        defaultTo(selectedAsg?.on_demand, 0) + defaultTo(selectedAsg?.spot, 0) ||
        selectedAsg?.max
    const newAsg = {
      ...selectedAsg,
      desired: desiredCapacityValue,
      on_demand: defaultTo(selectedAsg?.on_demand, desiredCapacityValue)
    }
    const ports = (newAsg as ASGMinimal).target_groups?.map((tg: TargetGroupMinimal) =>
      Utils.getTargetGroupObject(tg.port as number, tg.protocol as string)
    ) as PortConfig[]
    props.setSelectedAsg(newAsg)
    const updatedGatewayDetails = {
      ...props.gatewayDetails,
      routing: {
        ...props.gatewayDetails.routing,
        instance: { ...props.gatewayDetails.routing.instance, scale_group: newAsg },
        ports
      }
    }
    props.onAsgAddSuccess?.(updatedGatewayDetails)
  }
 
  const refreshPageParams = () => {
    setPageIndex(0)
  }
 
  const handleRefresh = () => {
    refreshPageParams()
    let filterText = ''
    Iif (isGcpProvider) {
      if (gcpFilters?.region) {
        filterText += `regions=['${gcpFilters.region.label}']`
      }
      if (gcpFilters?.zone) {
        filterText += `\n zones=['${gcpFilters.zone.label}']`
      }
    }
    props.refresh?.(filterText)
  }
 
  const onGcpFiltersChange = (filters: GCPFiltersProps, loadingFilters: boolean) => {
    setGcpFilters(filters)
    let filterText = ''
    if (filters.region) {
      filterText = `regions=['${filters.region.label}']`
    }
    if (filters.zone) {
      filterText += `\n zones=['${filters.zone.label}']`
    }
    if (!_isEmpty(filterText) && !loadingFilters) {
      props.refresh?.(filterText)
    }
    setIsLoading(loadingFilters)
  }
 
  const loadingEnabled = props.loading || isLoading
 
  return (
    <Container>
      <Layout.Vertical spacing="large">
        <Container style={{ paddingBottom: 20, borderBottom: '1px solid #CDD3DD' }}>
          <Text font={'large'}>
            {Utils.getConditionalResult(
              isGcpProvider,
              getString('ce.co.autoStoppingRule.configuration.igModal.title'),
              getString('ce.co.autoStoppingRule.configuration.asgModal.title')
            )}
          </Text>
        </Container>
        <Layout.Vertical style={{ paddingBottom: 20, paddingTop: 20, borderBottom: '1px solid #CDD3DD' }}>
          <Layout.Horizontal style={{ justifyContent: 'space-between' }}>
            <Layout.Horizontal flex={{ alignItems: 'center' }}>
              <Button
                onClick={addAsg}
                disabled={!isAsgSelected}
                style={{
                  backgroundColor: Utils.getConditionalResult(isAsgSelected, '#0278D5', 'inherit'),
                  color: Utils.getConditionalResult(isAsgSelected, '#F3F3FA', 'inherit'),
                  marginRight: 20
                }}
              >
                {'Add selected'}
              </Button>
              <div onClick={handleRefresh}>
                <Icon name="refresh" color="primary7" size={14} />
                <span style={{ color: 'var(--primary-7)', margin: '0 5px', cursor: 'pointer' }}>Refresh</span>
              </div>
            </Layout.Horizontal>
            <ExpandingSearchInput onChange={(text: string) => props.search(text)} />
          </Layout.Horizontal>
          <GroupsFilter
            gatewayDetails={props.gatewayDetails}
            isEditFlow={false}
            onGcpFiltersChangeCallback={onGcpFiltersChange}
          />
        </Layout.Vertical>
        <Container style={{ minHeight: 250 }}>
          {loadingEnabled && (
            <Layout.Horizontal flex={{ justifyContent: 'center' }}>
              <Icon name="spinner" size={24} color="blue500" />
            </Layout.Horizontal>
          )}
          {!loadingEnabled && _isEmpty(gcpFilters?.region) ? (
            <Layout.Horizontal flex={{ justifyContent: 'center' }}>
              <Text icon={'execution-warning'} font={{ size: 'medium' }} iconProps={{ size: 20 }}>
                {getString('ce.co.autoStoppingRule.configuration.igModal.gcpFiltersNotSelectedDescription')}
              </Text>
            </Layout.Horizontal>
          ) : null}
          {!loadingEnabled && !_isEmpty(props.scalingGroups) && (
            <TableV2<ASGMinimal>
              data={defaultTo(props.scalingGroups, []).slice(
                pageIndex * TOTAL_ITEMS_PER_PAGE,
                pageIndex * TOTAL_ITEMS_PER_PAGE + TOTAL_ITEMS_PER_PAGE
              )}
              pagination={{
                pageSize: TOTAL_ITEMS_PER_PAGE,
                pageIndex: pageIndex,
                pageCount: Math.ceil(defaultTo(props.scalingGroups, []).length / TOTAL_ITEMS_PER_PAGE),
                itemCount: defaultTo(props.scalingGroups, []).length,
                gotoPage: newPageIndex => setPageIndex(newPageIndex)
              }}
              columns={[
                {
                  Header: '',
                  id: 'selected',
                  Cell: TableCheck,
                  width: '5%',
                  disableSortBy: true
                },
                {
                  accessor: 'name',
                  Header: getString('ce.co.instanceSelector.name'),
                  width: '70%',
                  Cell: NameCell,
                  disableSortBy: true
                },
                {
                  accessor: 'desired',
                  Header: 'Instances',
                  width: '10%',
                  Cell: TableCell,
                  disableSortBy: true
                },
                {
                  accessor: 'region',
                  Header: getString('regionLabel'),
                  width: '10%',
                  Cell: TableCell,
                  disableSortBy: true
                }
              ]}
            />
          )}
        </Container>
      </Layout.Vertical>
    </Container>
  )
}
 
interface GCPFiltersProps {
  region?: SelectOption
  zone?: SelectOption | null
}
 
interface GroupsFilterProps {
  gatewayDetails: GatewayDetails
  onGcpFiltersChangeCallback: (values: GCPFiltersProps, loading: boolean) => void
  isEditFlow: boolean
}
 
const GroupsFilter: React.FC<GroupsFilterProps> = ({ gatewayDetails, onGcpFiltersChangeCallback, isEditFlow }) => {
  const { accountId } = useParams<AccountPathProps>()
  const { getString } = useStrings()
  const isGcpProvider = Utils.isProviderGcp(gatewayDetails.provider)
 
  // GCP filters data
  const { data: regionsData, loading: regionsLoading } = useRegionsForSelection({
    cloudAccountId: gatewayDetails.cloudAccount.id,
    additionalProps: {}
  })
  const [selectedRegion, setSelectedRegion] = useState<SelectOption>()
  const [zonesData, setZonesData] = useState<SelectOption[]>([])
  const [selectedZone, setSelectedZone] = useState<SelectOption | null | undefined>()
 
  const {
    data: zones,
    loading: zonesLoading,
    refetch: fetchZones
  } = useAllZones({
    account_id: accountId,
    queryParams: {
      cloud_account_id: gatewayDetails.cloudAccount.id,
      accountIdentifier: accountId,
      region: ''
    },
    lazy: true
  })
 
  useEffect(() => {
    Iif (selectedRegion) {
      fetchZones({
        queryParams: {
          cloud_account_id: gatewayDetails.cloudAccount.id,
          accountIdentifier: accountId,
          region: selectedRegion.label
        }
      })
    }
    Iif (isGcpProvider) {
      setSelectedZone(null)
      onGcpFiltersChangeCallback({ region: selectedRegion }, regionsLoading)
    }
  }, [selectedRegion, regionsLoading])
 
  useEffect(() => {
    Eif (zones?.response) {
      setZonesData(zones.response.map(z => ({ label: z, value: z })))
    }
  }, [zones?.response])
 
  useEffect(() => {
    Iif (selectedRegion) {
      onGcpFiltersChangeCallback({ region: selectedRegion, zone: selectedZone }, zonesLoading)
    }
  }, [selectedZone, zonesLoading])
 
  Iif (isGcpProvider) {
    return (
      <Layout.Horizontal flex={{ justifyContent: 'flex-start' }} spacing={'large'} style={{ maxWidth: '40%' }}>
        <Select
          disabled={regionsLoading || isEditFlow}
          items={regionsData}
          onChange={setSelectedRegion}
          value={selectedRegion}
          inputProps={{
            placeholder: getString('ce.allRegions')
          }}
          name="regionsSelector"
        />
        <Select
          disabled={zonesLoading || isEditFlow}
          items={zonesData}
          onChange={setSelectedZone}
          value={selectedZone}
          inputProps={{
            placeholder: getString('ce.co.accessPoint.zone')
          }}
          name="zoneSelector"
        />
      </Layout.Horizontal>
    )
  }
 
  return null
}
 
export default COAsgSelector