All files / modules/35-connectors/components/CreateConnector/CENGAwsConnector/steps CrossAccountRoleStep1.tsx

74.6% Statements 47/63
59.52% Branches 25/42
55.56% Functions 10/18
76.27% Lines 45/59

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              219x 219x 219x 219x 219x   219x   219x 219x 219x 219x                                   219x 1x 1x                                                                                                             1x 1x 1x           1x     1x     219x 1x 1x 1x 1x   1x   1x 1x 1x 1x         1x 1x 1x     1x       1x                       1x                                             2x                                       219x 3x 3x                               11x                                               219x 1x 1x                               1x           1x         219x  
/*
 * 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, { useRef, useState } from 'react'
import { Button, Heading, Layout, StepProps, CardSelect, Icon, IconName, Container, Text } from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
import { CE_AWS_CONNECTOR_CREATION_EVENTS } from '@connectors/trackingConstants'
import { useStepLoadTelemetry } from '@connectors/common/useTrackStepLoad/useStepLoadTelemetry'
import type { CEAwsConnectorDTO } from './OverviewStep'
import css from '../CreateCeAwsConnector.module.scss'
 
enum Features {
  VISIBILITY,
  OPTIMIZATION,
  BILLING
}
 
export type FeaturesString = keyof typeof Features
interface CardData {
  icon: IconName
  text: string
  value: FeaturesString
  heading: string
  prefix: string
  features: string[]
  footer: React.ReactNode
}
 
interface DefaultCardType extends Omit<CardData, 'value'> {
  value: 'DEFAULT'
}
 
const useSelectedCards = (featuresEnabled: FeaturesString[]) => {
  const { getString } = useStrings()
  const FeatureCards = useRef<CardData[]>([
    {
      icon: 'ce-visibility',
      text: getString('connectors.ceAzure.chooseRequirements.visibilityCardDesc'),
      value: 'VISIBILITY',
      heading: getString('connectors.ceAws.crossAccountRoleStep1.visible.heading'),
      prefix: getString('connectors.ceAws.crossAccountRoleStep1.visible.prefix'),
      features: [
        getString('connectors.ceAws.crossAccountRoleStep1.visible.feat1'),
        getString('connectors.ceAws.crossAccountRoleStep1.visible.feat2')
      ],
      footer: (
        <>
          {getString('connectors.ceAzure.chooseRequirements.optimization.footer1')}{' '}
          <a
            href="https://ngdocs.harness.io/article/80vbt5jv0q-set-up-cost-visibility-for-aws#aws_ecs_and_resource_inventory_management"
            target="_blank"
            rel="noreferrer"
            onClick={e => e.stopPropagation()}
          >
            {getString('permissions').toLowerCase()}
          </a>{' '}
          {getString('connectors.ceAws.crossAccountRoleStep1.optimize.footer')}
        </>
      )
    },
    {
      icon: 'nav-settings',
      text: getString('connectors.ceAzure.chooseRequirements.optimizationCardDesc'),
      value: 'OPTIMIZATION',
      heading: getString('common.ce.autostopping'),
      prefix: getString('connectors.ceAws.crossAccountRoleStep1.optimize.prefix'),
      features: [
        getString('connectors.ceAws.crossAccountRoleStep1.optimize.feat1'),
        getString('connectors.ceAzure.chooseRequirements.optimization.feat2'),
        getString('connectors.ceAzure.chooseRequirements.optimization.feat3'),
        getString('connectors.ceAzure.chooseRequirements.optimization.feat4')
      ],
      footer: (
        <>
          {getString('connectors.ceAzure.chooseRequirements.optimization.footer1')}{' '}
          <a
            href="https://ngdocs.harness.io/article/80vbt5jv0q-set-up-cost-visibility-for-aws#aws_resource_optimization_using_auto_stopping_rules"
            target="_blank"
            rel="noreferrer"
            onClick={e => e.stopPropagation()}
          >
            {getString('permissions').toLowerCase()}
          </a>{' '}
          {getString('connectors.ceAws.crossAccountRoleStep1.optimize.footer')}
        </>
      )
    }
  ]).current
 
  const [selectedCards, setSelectedCards] = useState<CardData[]>(() => {
    const initialSelectedCards = []
    for (const fe of featuresEnabled) {
      const card = FeatureCards.find(c => c.value === fe)
      if (card) {
        initialSelectedCards.push(card)
      }
    }
    return initialSelectedCards
  })
 
  return { selectedCards, setSelectedCards, FeatureCards }
}
 
const CrossAccountRoleStep1: React.FC<StepProps<CEAwsConnectorDTO>> = props => {
  const { getString } = useStrings()
  const { prevStepData, nextStep, previousStep } = props
  const featuresEnabled = prevStepData?.spec?.featuresEnabled || []
  const { selectedCards, setSelectedCards, FeatureCards } = useSelectedCards(featuresEnabled)
 
  useStepLoadTelemetry(CE_AWS_CONNECTOR_CREATION_EVENTS.LOAD_CHOOSE_REQUIREMENTS)
 
  const handleSubmit = () => {
    const features: FeaturesString[] = selectedCards.map(card => card.value)
    Eif (prevStepData?.includeBilling) features.push('BILLING')
    const newspec = {
      crossAccountAccess: { crossAccountRoleArn: '' },
      ...prevStepData?.spec,
      featuresEnabled: features
    }
    const payload = prevStepData
    Eif (payload) payload.spec = newspec
    nextStep?.(payload)
  }
 
  const handleprev = () => {
    previousStep?.({ ...(prevStepData as CEAwsConnectorDTO) })
  }
 
  const handleCardSelection = (item: CardData) => {
    const sc = [...selectedCards]
    const index = sc.indexOf(item)
    if (index > -1) {
      sc.splice(index, 1)
    } else {
      sc.push(item)
    }
 
    setSelectedCards(sc)
  }
 
  return (
    <Layout.Vertical className={css.stepContainer}>
      <Heading level={2} className={css.header}>
        {getString('connectors.ceAws.crossAccountRoleStep1.heading')}
        <span>{getString('connectors.ceAws.crossAccountRoleStep1.choosePermissions')}</span>
      </Heading>
      <Text color="grey800">{getString('connectors.ceAws.crossAccountRoleStep1.description')}</Text>
      <Container>
        <Text font={{ italic: true }} className={css.mtblarge}>
          {getString('connectors.ceAws.crossAccountRoleStep1.info')}
        </Text>
        <div style={{ flex: 1 }}>
          <div className={css.cards}>
            <DefaultCard />
            <CardSelect
              data={FeatureCards}
              selected={selectedCards}
              multi={true}
              className={css.grid}
              onChange={item => {
                handleCardSelection(item)
              }}
              cornerSelected={true}
              renderItem={item => <Card {...item} />}
            />
          </div>
          <Layout.Horizontal className={css.buttonPanel} spacing="small">
            <Button text={getString('previous')} icon="chevron-left" onClick={handleprev}></Button>
            <Button
              type="submit"
              intent="primary"
              text={getString('continue')}
              rightIcon="chevron-right"
              onClick={handleSubmit}
              disabled={!prevStepData?.includeBilling && selectedCards.length == 0}
            />
          </Layout.Horizontal>
        </div>
      </Container>
    </Layout.Vertical>
  )
}
 
const Card = (props: CardData | DefaultCardType) => {
  const { prefix, icon, heading, features, footer } = props
  return (
    <Container className={css.featureCard}>
      <Layout.Vertical spacing="medium" padding={{ left: 'large', right: 'large' }}>
        <Layout.Horizontal spacing="small">
          <Icon name={icon} size={32} />
          <Container>
            <Text color="grey900" style={{ fontSize: 9, fontWeight: 500 }}>
              {prefix.toUpperCase()}
            </Text>
            <Text color="grey900" style={{ fontSize: 16, fontWeight: 500 }}>
              {heading}
            </Text>
          </Container>
        </Layout.Horizontal>
        <ul className={css.features}>
          {features.map((feat, idx) => {
            return (
              <li key={idx}>
                <Text
                  icon="main-tick"
                  iconProps={{ color: 'green600', size: 12, padding: { right: 'small' } }}
                  font="small"
                  style={{ lineHeight: '20px' }}
                >
                  {feat}
                </Text>
              </li>
            )
          })}
        </ul>
      </Layout.Vertical>
      <Container className={css.footer}>
        <Text font={{ size: 'small', italic: true }} color="grey400">
          {footer}
        </Text>
      </Container>
    </Container>
  )
}
 
const DefaultCard = () => {
  const { getString } = useStrings()
  const card: DefaultCardType = {
    icon: 'ce-visibility',
    text: getString('connectors.ceAzure.chooseRequirements.visibilityCardDesc'),
    value: 'DEFAULT',
    heading: getString('connectors.costVisibility'),
    prefix: getString('common.aws'),
    features: [
      getString('connectors.ceAws.crossAccountRoleStep1.default.feat1'),
      getString('connectors.ceAzure.chooseRequirements.visibility.feat2'),
      getString('connectors.ceAzure.chooseRequirements.visibility.feat3'),
      getString('connectors.ceAzure.chooseRequirements.visibility.feat4'),
      getString('connectors.ceAzure.chooseRequirements.visibility.feat5')
    ],
    footer: getString('connectors.ceAws.crossAccountRoleStep1.default.footer')
  }
 
  return (
    <CardSelect
      data={[card]}
      selected={card}
      onChange={() => void 0}
      cornerSelected={true}
      renderItem={item => <Card {...item} />}
    />
  )
}
 
export default CrossAccountRoleStep1