All files / modules/75-ce/components/COGatewayConfig KubernetesRuleYamlEditor.tsx

65.38% Statements 17/26
16.67% Branches 4/24
50% Functions 1/2
65.38% Lines 17/26

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              5x 5x 5x 5x   5x 5x 5x 5x                 5x 4x 4x 4x 4x 4x   4x                               4x                                                       5x  
/*
 * 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 { parse } from 'yaml'
import { Button } from '@wings-software/uicore'
import YAMLBuilder from '@common/components/YAMLBuilder/YamlBuilder'
import type { YamlBuilderHandlerBinding } from '@common/interfaces/YAMLBuilderProps'
import { useToaster } from '@common/exports'
import { useGatewayContext } from '@ce/context/GatewayContext'
import { getK8sYamlSchema } from './GetK8sYamlSchema'
import css from './COGatewayConfig.module.scss'
 
interface KubernetesRuleYamlEditorProps {
  handleSave?: (data: any) => void
  existingData?: Record<any, any>
  mode?: 'read' | 'edit'
  fileName?: string
}
 
const KubernetesRuleYamlEditor: React.FC<KubernetesRuleYamlEditorProps> = props => {
  const { showError } = useToaster()
  const { isEditFlow } = useGatewayContext()
  const [yamlHandler, setYamlHandler] = useState<YamlBuilderHandlerBinding | undefined>()
  const [hasYamlEditorChanged, setHasYamlEditorChanged] = useState<boolean>(false)
  const isReadMode = props.mode === 'read'
 
  const onSave = () => {
    try {
      const errorMap = yamlHandler?.getYAMLValidationErrorMap()
      if (errorMap?.size) {
        throw new Error(errorMap.entries().next().value)
      }
      const data = yamlHandler?.getLatestYaml()
      const dataToSave = parse(data || '')
      props.handleSave?.(dataToSave)
    } catch (err) {
      showError(err.message)
    } finally {
      setHasYamlEditorChanged(false)
    }
  }
 
  return (
    <div className={css.yamlEditorContainer}>
      <YAMLBuilder
        schema={getK8sYamlSchema({ isEdit: isEditFlow })}
        showSnippetSection={false}
        fileName={props.fileName || 'harness-ccm-autostopping-connector.yaml'}
        entityType="Service"
        bind={setYamlHandler}
        height="400px"
        onChange={setHasYamlEditorChanged}
        existingJSON={props.existingData}
        yamlSanityConfig={{ removeEmptyObject: false, removeEmptyString: false, removeEmptyArray: false }}
        isReadOnlyMode={isReadMode}
        isEditModeSupported={!isReadMode}
      />
      {!isReadMode && (
        <Button
          text={'Validate YAML'}
          intent={'primary'}
          disabled={!hasYamlEditorChanged}
          onClick={onSave}
          style={{ marginTop: 20 }}
        />
      )}
    </div>
  )
}
 
export default KubernetesRuleYamlEditor