All files / modules/75-ce/components/COGatewayConfig helper.ts

85.14% Statements 63/74
48.57% Branches 34/70
82.35% Functions 14/17
83.82% Lines 57/68

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              4x     4x 4x                           1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x       1x 1x       1x 1x       1x 1x       1x 1x                 1x 1x       1x 1x       1x 1x                 4x 1x 1x                 1x     1x     1x     4x 155x 155x 124x   31x 31x   31x     4x                 4x 17x 17x                             4x 14x 14x 1x   14x     14x     4x 14x 14x 1x   14x     14x    
/*
 * 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 { isEmpty as _isEmpty, defaultTo } from 'lodash-es'
import type { Resource } from 'services/lw'
import type { GatewayDetails, InstanceDetails } from '@ce/components/COCreateGateway/models'
import { RESOURCES } from '@ce/constants'
import { Utils } from '@ce/common/Utils'
 
class InstanceData implements InstanceDetails {
  name: string
  id: string
  ipv4: string
  region: string
  type: string
  tags: string
  launch_time: string
  status: string
  vpc: string
  metadata: { [key: string]: any } | undefined
  constructor() {
    this.name = ''
    this.id = ''
    this.ipv4 = ''
    this.region = ''
    this.type = ''
    this.tags = ''
    this.launch_time = ''
    this.status = ''
    this.vpc = ''
  }
 
  setName(name: string) {
    this.name = name
    return this
  }
 
  setId(id: string) {
    this.id = id
    return this
  }
 
  setIp(ip: string) {
    this.ipv4 = ip
    return this
  }
 
  setRegion(region: string) {
    this.region = region
    return this
  }
 
  setType(type: string) {
    this.type = type
    return this
  }
 
  setTags(tags: string) {
    this.tags = tags
    return this
  }
 
  setLaunchTime(time: string) {
    this.launch_time = time
    return this
  }
 
  setStatus(status: string) {
    this.status = status
    return this
  }
 
  setVpc(vpc: string) {
    this.vpc = vpc
    return this
  }
 
  setMetadata(meta: { [key: string]: any }) {
    this.metadata = meta
    return this
  }
}
 
export const fromResourceToInstanceDetails = (item: Resource, resourceType: { isAzure: boolean; isGcp: boolean }) => {
  const ip = resourceType.isGcp ? defaultTo(item.ipv4, item.private_ipv4)?.[0] : item.ipv4?.[0]
  const instanceDetails = new InstanceData()
    .setName(item.name as string)
    .setId(item.id as string)
    .setIp(ip as string)
    .setRegion(item.region as string)
    .setType(item.type as string)
    .setLaunchTime(item.launch_time as string)
    .setStatus(item.status as string)
    .setVpc(item.metadata?.['vpcID'] as string)
  Iif (resourceType.isAzure) {
    instanceDetails.setMetadata({ resourceGroup: item.metadata?.resourceGroup })
  }
  Iif (resourceType.isGcp) {
    instanceDetails.setMetadata({ availabilityZone: item.availability_zone })
  }
  return instanceDetails
}
 
export const isFFEnabledForResource = (flag: string | undefined | null, featureFlagsMap: Record<string, boolean>) => {
  let enableStatus = true
  if (_isEmpty(flag)) {
    return enableStatus
  }
  Eif (!featureFlagsMap[flag as string]) {
    enableStatus = false
  }
  return enableStatus
}
 
export const isActiveStep = (stepEl: HTMLElement, parentEl: HTMLDivElement) => {
  const parentElVal = parentEl.getBoundingClientRect()
  const { top, bottom, height } = stepEl.getBoundingClientRect()
  return (
    (top > parentElVal.top || bottom - parentElVal.top > 100) &&
    (bottom <= parentElVal.bottom || (height >= parentElVal.height ? bottom > parentElVal.bottom : false))
  )
}
 
export const getSelectedResourceFromGatewayDetails = (gatewayDetails: GatewayDetails) => {
  const isGcpProvider = Utils.isProviderGcp(gatewayDetails.provider)
  return !_isEmpty(gatewayDetails.selectedInstances)
    ? RESOURCES.INSTANCES
    : isGcpProvider && !_isEmpty(gatewayDetails.routing?.instance?.scale_group)
    ? RESOURCES.IG
    : !_isEmpty(gatewayDetails.routing?.instance?.scale_group)
    ? RESOURCES.ASG
    : Utils.isK8sRule(gatewayDetails)
    ? RESOURCES.KUBERNETES
    : !_isEmpty(gatewayDetails.routing.container_svc)
    ? RESOURCES.ECS
    : !_isEmpty(gatewayDetails.routing.database)
    ? RESOURCES.RDS
    : null
}
 
export const getTitle = (selectedResource?: RESOURCES | null) => {
  let title = 'ce.co.autoStoppingRule.configuration.step3.title'
  if (selectedResource === RESOURCES.ASG) {
    title = 'ce.co.autoStoppingRule.configuration.step3.asgTitle'
  }
  Iif (selectedResource === RESOURCES.ECS) {
    title = 'ce.co.autoStoppingRule.configuration.step3.desiredTaskCount'
  }
  return title
}
 
export const getSubTitle = (selectedResource?: RESOURCES | null) => {
  let subStr = 'ce.co.autoStoppingRule.configuration.step3.subTitle'
  if (selectedResource === RESOURCES.ASG) {
    subStr = 'ce.co.autoStoppingRule.configuration.step3.asgSubTitle'
  }
  Iif (selectedResource === RESOURCES.ECS) {
    subStr = 'ce.co.autoStoppingRule.configuration.step3.ecsSubTitle'
  }
  return subStr
}