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

100% Statements 7/7
100% Branches 0/0
100% Functions 0/0
100% Lines 7/7

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              3x 3x   3x     3x     3x                                     3x                               3x                                                                                                                                                            
/*
 * 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 as _defaultTo } from 'lodash-es'
import { ASRuleTabs, GatewayKindType } from '@ce/constants'
import type { RoutingData, Service } from 'services/lw'
import { Utils } from '@ce/common/Utils'
import type { GatewayDetails } from '../COCreateGateway/models'
 
const tabs = [ASRuleTabs.CONFIGURATION, ASRuleTabs.SETUP_ACCESS, ASRuleTabs.REVIEW]
 
/* istanbul ignore next */
export const trackPrimaryBtnClick = (
  selectedTabId: string,
  dataToTrack: Record<ASRuleTabs, any>,
  trackEvent: (eventName: string, properties: Record<string, string>) => void
) => {
  switch (selectedTabId) {
    case tabs[0]:
      trackEvent('VisitedSetupAccessPage', {})
      break
    case tabs[1]:
      trackEvent('CompletedSetupAccess', dataToTrack[selectedTabId] || {})
      break
    case tabs[2]:
      trackEvent('AutoStoppingRuleCompleted', {})
      break
  }
}
 
/* istanbul ignore next */
export const isPrimaryBtnDisable = (
  selectedTabId: string,
  validTabs: { config: boolean; setupAccess: boolean },
  bypassCheck = false
) => {
  if (bypassCheck) {
    return true
  }
  return (
    (selectedTabId === tabs[0] && !validTabs.config) ||
    (selectedTabId === tabs[1] && !validTabs.setupAccess) ||
    (selectedTabId === tabs[2] && (!validTabs.setupAccess || !validTabs.config))
  )
}
 
/* istanbul ignore next */
export const getServiceObjectFromgatewayDetails = (
  gatewayDetails: GatewayDetails,
  orgIdentifier: string,
  projectIdentifier: string,
  accountId: string,
  serverNames: string[] = []
): Service => {
  const hasInstances = !_isEmpty(gatewayDetails.selectedInstances)
  const isK8sRule = Utils.isK8sRule(gatewayDetails)
  const isGcpProvider = Utils.isProviderGcp(gatewayDetails.provider)
  const routing: RoutingData = { ports: gatewayDetails.routing.ports, lb: undefined }
  let kind = GatewayKindType.INSTANCE
  if (isK8sRule) {
    // k8s rule check
    kind = GatewayKindType.KUBERNETES
    routing.k8s = gatewayDetails.routing.k8s
  } else if (hasInstances) {
    // VMs rule check
    const instanceIDs = gatewayDetails.selectedInstances.map(instance => `'${instance.id}'`).join(',')
    let filterText = `id = [${instanceIDs}]`
    if (isGcpProvider) {
      filterText += `\n regions=['${_defaultTo(gatewayDetails.selectedInstances[0].metadata?.availabilityZone, '')}']`
    }
    routing.instance = {
      filter_text: filterText
    }
  } else if (!_isEmpty(gatewayDetails.routing.container_svc)) {
    // ECS rule check
    kind = GatewayKindType.CONTAINERS
    routing.container_svc = gatewayDetails.routing.container_svc
  } else if (!_isEmpty(gatewayDetails.routing.database)) {
    // RDS rule check
    kind = GatewayKindType.DATABASE
    routing.database = gatewayDetails.routing.database
  } else {
    let filterText = ''
    if (isGcpProvider) {
      filterText = `id = ['${gatewayDetails.routing.instance.scale_group?.id}'] \n regions = ['${gatewayDetails.routing.instance.scale_group?.region}']`
      if (!_isEmpty(gatewayDetails.routing.instance.scale_group?.availability_zones)) {
        filterText += `\n zones = ['${gatewayDetails.routing.instance.scale_group?.availability_zones?.[0]}']`
      }
      kind = GatewayKindType.CLUSTERS
    }
    // ASG rule check
    routing.instance = {
      filter_text: filterText,
      scale_group: gatewayDetails.routing.instance.scale_group
    }
  }
 
  routing.custom_domain_providers = gatewayDetails.routing.custom_domain_providers
  const gateway: Service = {
    name: gatewayDetails.name,
    org_id: orgIdentifier,
    project_id: projectIdentifier,
    account_identifier: accountId,
    fulfilment: Utils.getConditionalResult(isK8sRule, 'kubernetes', gatewayDetails.fullfilment || 'ondemand'),
    kind,
    cloud_account_id: gatewayDetails.cloudAccount.id,
    idle_time_mins: gatewayDetails.idleTimeMins,
    custom_domains: Utils.getAllCustomDomains(serverNames, gatewayDetails.customDomains),
    health_check: gatewayDetails.healthCheck,
    routing,
    opts: {
      ...gatewayDetails.opts,
      preserve_private_ip: false,
      always_use_private_ip: false
    },
    metadata: gatewayDetails.metadata,
    disabled: gatewayDetails.disabled,
    match_all_subdomains: gatewayDetails.matchAllSubdomains,
    access_point_id: gatewayDetails.accessPointID
  }
  if (gatewayDetails.id) {
    gateway.id = gatewayDetails.id
  }
  return gateway
}