All files / modules/75-ce/components/Connectors/AWSCOConnector/steps OverviewStep.tsx

90% Statements 36/40
41.18% Branches 14/34
100% Functions 6/6
90% Lines 36/40

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              1x 1x 1x 1x                       1x 1x 1x 1x   1x 1x                                             1x 7x 6x 6x 6x         6x 6x 6x         6x 1x   6x 1x 1x 1x 1x               1x   1x 1x 1x             1x               1x                       6x                                 1x         8x                   9x                                                   1x  
/*
 * 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, { useState } from 'react'
import { useParams } from 'react-router-dom'
import * as Yup from 'yup'
import {
  Layout,
  Button,
  StepProps,
  Heading,
  Formik,
  FormikForm,
  Container,
  ModalErrorHandler,
  ModalErrorHandlerBinding
} from '@wings-software/uicore'
import type { ConnectorInfoDTO, ConnectorConfigDTO } from 'services/cd-ng'
import { validateTheIdentifierIsUniquePromise, Failure } from 'services/cd-ng'
import { useStrings } from 'framework/strings'
import { StringUtils } from '@common/exports'
import { AddDescriptionAndKVTagsWithIdentifier } from '@common/components/AddDescriptionAndTags/AddDescriptionAndTags'
import type { permission as PermissionType } from '../constants'
import { CO_PERMISSION, CE_PERMISSION, COCE_PERMISSION } from '../constants'
import css from './Steps.module.scss'
 
interface OverviewDetails {
  name: string
  identifier: string
  description: string
  tags: Record<string, any>
  billingPermission?: boolean
  eventsPermission?: boolean
  optimizationPermission?: boolean
}
 
export type OverviewForm = Pick<
  OverviewDetails,
  'name' | 'identifier' | 'description' | 'tags' | 'billingPermission' | 'eventsPermission' | 'optimizationPermission'
>
 
interface OverviewStepProps extends StepProps<ConnectorInfoDTO> {
  type: ConnectorInfoDTO['type']
  name: string
  permission: PermissionType
}
 
const OverviewStep: React.FC<StepProps<ConnectorConfigDTO> & OverviewStepProps> = props => {
  const { nextStep, permission } = props
  const { getString } = useStrings()
  const cePermission = permission === CE_PERMISSION || permission === COCE_PERMISSION
  const coPermission = permission === CO_PERMISSION || permission === COCE_PERMISSION
  // const [billing, setBilling] = useState(cePermission)
  // const [events, setEvents] = useState(cePermission)
  // const toggleBilling = (): void => setBilling(!billing)
  // const toggleEvents = (): void => setEvents(!events)
  const [loading, setLoading] = useState(false)
  const [modalErrorHandler, setModalErrorHandler] = useState<ModalErrorHandlerBinding | undefined>()
  const { accountId, projectIdentifier, orgIdentifier } = useParams<{
    accountId: string
    projectIdentifier: string
    orgIdentifier: string
  }>()
  const randomString = (): string => {
    return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
  }
  const handleSubmit = async (values: OverviewDetails): Promise<void> => {
    setLoading(true)
    try {
      modalErrorHandler?.hide()
      const response = await validateTheIdentifierIsUniquePromise({
        queryParams: {
          identifier: values.identifier,
          accountIdentifier: accountId,
          orgIdentifier: orgIdentifier,
          projectIdentifier: projectIdentifier
        }
      })
      setLoading(false)
 
      Eif ('SUCCESS' === response.status) {
        Eif (response.data) {
          const spec: ConnectorConfigDTO = {
            billingPermission: values.billingPermission,
            eventsPermission: values.eventsPermission,
            optimizationPermission: values.optimizationPermission,
            roleARN: '',
            externalID: randomString()
          }
          const connectorDetails: ConnectorInfoDTO = {
            name: values.name,
            identifier: values.identifier,
            description: values.description,
            tags: values.tags,
            type: 'CEAws',
            spec: spec
          }
          nextStep?.(connectorDetails)
        } else {
          modalErrorHandler?.showDanger('ID uniqueness check failed')
        }
      } else {
        throw response as Failure
      }
    } catch (error) {
      setLoading(false)
      modalErrorHandler?.showDanger(error.data?.message || error.message)
    }
  }
  return (
    <Layout.Vertical data-id={'todo'} spacing="xlarge" className={css.containerLayout}>
      <Heading level={2} font={{ weight: 'bold' }}>
        {getString('ce.connector.AWS.overview.title')}
      </Heading>
      <Formik<OverviewForm>
        initialValues={{
          name: '',
          description: '',
          identifier: '',
          tags: {},
          billingPermission: cePermission,
          eventsPermission: cePermission,
          optimizationPermission: coPermission
        }}
        formName="connectorOverview"
        onSubmit={values => {
          handleSubmit(values)
        }}
        validationSchema={Yup.object().shape({
          name: Yup.string().trim().required(getString('ce.connector.AWS.overview.validation.name')),
          identifier: Yup.string().when('name', {
            is: val => val?.length,
            then: Yup.string()
              .trim()
              .required(getString('ce.connector.AWS.overview.validation.identifier.required'))
              .matches(/^(?![0-9])[0-9a-zA-Z_$]*$/, getString('ce.connector.AWS.overview.validation.identifier.format'))
              .notOneOf(StringUtils.illegalIdentifiers)
          })
        })}
      >
        {formikProps => {
          return (
            <FormikForm className={css.fullHeight}>
              <ModalErrorHandler bind={setModalErrorHandler} />
              <Container padding={{ top: 'large', bottom: 'large' }} className={css.fullHeight}>
                <AddDescriptionAndKVTagsWithIdentifier
                  formikProps={formikProps}
                  identifierProps={{ inputName: 'name', inputLabel: getString('ce.connector.AWS.overview.label') }}
                />
              </Container>
              <Button
                intent="primary"
                type="submit"
                text={getString('ce.connector.AWS.overview.submitText')}
                rightIcon="chevron-right"
                loading={loading}
                disabled={loading}
                className={css.nextButton}
              />
            </FormikForm>
          )
        }}
      </Formik>
    </Layout.Vertical>
  )
}
 
export default OverviewStep