All files / modules/35-connectors/components/CreateConnector/SplunkConnector CreateSplunkConnector.tsx

90.91% Statements 20/22
80.77% Branches 21/26
60% Functions 3/5
90.91% Lines 20/22

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              219x 219x 219x 219x 219x   219x 219x   219x 219x 219x 219x     4x 4x 4x   4x 3x   4x   4x                         22x                                                   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 from 'react'
import { Layout, Button, FormInput, FormikForm } from '@wings-software/uicore'
import { Formik } from 'formik'
import * as Yup from 'yup'
import { useStrings } from 'framework/strings'
import type { ConnectorConfigDTO } from 'services/cd-ng'
import { buildSplunkPayload } from '@connectors/pages/connectors/utils/ConnectorUtils'
import { cvConnectorHOC } from '../CommonCVConnector/CVConnectorHOC'
import type { ConnectionConfigProps } from '../CommonCVConnector/constants'
import { ConnectorSecretField } from '../CommonCVConnector/components/ConnectorSecretField/ConnectorSecretField'
import { initializeSplunkConnector } from './utils'
import { StepDetailsHeader } from '../CommonCVConnector/components/CredentialsStepHeader/CredentialsStepHeader'
import css from '../AppDynamicsConnector/CreateAppDynamicsConnector.module.scss'
 
function SplunkConfigStep(props: ConnectionConfigProps): JSX.Element {
  const { nextStep, prevStepData, connectorInfo, accountId, projectIdentifier, orgIdentifier } = props
  const { getString } = useStrings()
  const initialValues = initializeSplunkConnector({ prevStepData, accountId, projectIdentifier, orgIdentifier })
 
  const handleSubmit = (formData: ConnectorConfigDTO) => {
    nextStep?.({ ...connectorInfo, ...prevStepData, ...formData })
  }
  const secretValue = prevStepData?.passwordRef?.referenceString || prevStepData?.spec?.passwordRef
 
  return (
    <Formik
      initialValues={{
        ...initialValues
      }}
      validationSchema={Yup.object().shape({
        url: Yup.string().trim().required(getString('common.validation.urlIsRequired')),
        username: Yup.string().trim().required(getString('validation.username')),
        passwordRef: Yup.string().trim().required(getString('validation.password'))
      })}
      onSubmit={handleSubmit}
    >
      {formikProps => (
        <FormikForm className={css.connectionForm}>
          <Layout.Vertical spacing="large" className={css.appDContainer}>
            <StepDetailsHeader connectorTypeLabel={getString('connectors.splunkLabel')} />
            <FormInput.Text label={getString('UrlLabel')} name="url" />
            <FormInput.Text name="username" label={getString('username')} />
            <ConnectorSecretField
              secretInputProps={{ name: 'passwordRef', label: getString('password') }}
              secretFieldValue={secretValue}
              accountIdentifier={accountId}
              projectIdentifier={projectIdentifier}
              orgIdentifier={orgIdentifier}
              onSuccessfulFetch={result => {
                formikProps.setFieldValue('passwordRef', result)
              }}
            />
          </Layout.Vertical>
          <Layout.Horizontal spacing="large">
            <Button onClick={() => props.previousStep?.({ ...props.prevStepData })} text={getString('back')} />
            <Button type="submit" text={getString('connectors.connectAndSave')} />
          </Layout.Horizontal>
        </FormikForm>
      )}
    </Formik>
  )
}
 
export default cvConnectorHOC({
  connectorType: 'Splunk',
  ConnectorCredentialsStep: SplunkConfigStep,
  buildSubmissionPayload: buildSplunkPayload
})