All files / modules/75-cd/components/PipelineSteps/Common/Terraform TerraformInterfaces.ts

84.51% Statements 60/71
53.04% Branches 488/920
100% Functions 6/6
84.06% Lines 58/69

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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481              108x 108x                                             108x                                                                                                                                                                           108x           108x                                                                                                                                                                                                                                                                                                     108x 3x 1x 1x 1x 1x 1x                   1x 1x 1x 1x 1x           1x   1x       1x               1x     1x       1x   1x       1x 1x         1x         1x                                 1x                           2x                       108x 1x 1x 1x 1x 1x 1x                 1x 1x 1x 1x 1x 1x         1x   1x           1x 1x               1x 1x         1x 1x         1x 1x   1x         1x                                   1x 1x         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 { unset } from 'lodash-es'
import { getMultiTypeFromValue, MultiTypeInputType } from '@wings-software/uicore'
import type { Scope } from '@common/interfaces/SecretsInterface'
import type { GitFilterScope } from '@common/components/GitFilters/GitFilters'
import type { StepViewType } from '@pipeline/components/AbstractSteps/Step'
import type { ListType, SelectOption } from '@pipeline/components/PipelineSteps/Steps/StepsTypes'
 
import type {
  InlineTerraformVarFileSpec,
  StepElementConfig,
  StringNGVariable,
  TerraformApplyStepInfo,
  TerraformBackendConfig,
  TerraformConfigFilesWrapper,
  TerraformDestroyStepInfo,
  TerraformExecutionData,
  TerraformPlanExecutionData,
  TerraformPlanStepInfo,
  TerraformRollbackStepInfo,
  TerraformStepConfiguration,
  TerraformVarFileWrapper
} from 'services/cd-ng'
import type { VariableMergeServiceResponse } from 'services/pipeline-ng'
 
export const TerraformStoreTypes = {
  Inline: 'Inline',
  Remote: 'Remote'
}
export interface TerraformProps<T = TerraformData> {
  initialValues: T
  onUpdate?: (data: T) => void
  onChange?: (data: T) => void
  allowableTypes: MultiTypeInputType[]
  stepViewType?: StepViewType
  configTypes?: SelectOption[]
  isNewStep?: boolean
  inputSetData?: {
    template?: T
    path?: string
  }
  readonly?: boolean
  path?: string
  stepType?: string
  gitScope?: GitFilterScope
  allValues?: T
}
 
export interface TerraformPlanProps {
  initialValues: TFPlanFormData
  onUpdate?: (data: TFPlanFormData) => void
  onChange?: (data: TFPlanFormData) => void
  allowableTypes: MultiTypeInputType[]
  stepViewType?: StepViewType
  configTypes?: SelectOption[]
  isNewStep?: boolean
  inputSetData?: {
    template?: TFPlanFormData
    path?: string
  }
  path?: string
  readonly?: boolean
  gitScope?: GitFilterScope
  stepType?: string
  allValues?: TFPlanFormData
}
 
export interface RemoteVar {
  varFile: {
    identifier?: string
    spec?: {
      store?: {
        spec?: {
          gitFetchType?: string
          repoName?: string
          branch?: string
          commitId?: string
          connectorRef?: {
            label: string
            value: string
            scope: Scope
            live: boolean
            connector: { type: string; spec: { val: string } }
          }
          paths?: PathInterface[]
          content?: string
        }
      }
    }
  }
}
 
export interface TerraformPlanVariableStepProps {
  initialValues: TFPlanFormData
  originalData?: TFPlanFormData
  stageIdentifier?: string
  onUpdate?(data: TFPlanFormData): void
  metadataMap: Required<VariableMergeServiceResponse>['metadataMap']
  variablesData?: TFPlanFormData
}
 
export interface TerraformVariableStepProps {
  initialValues: TerraformData
  originalData?: TerraformData
  stageIdentifier?: string
  onUpdate?(data: TerraformData): void
  metadataMap: Required<VariableMergeServiceResponse>['metadataMap']
  variablesData?: TerraformData
  stepType?: string
}
 
export const ConfigurationTypes: Record<TerraformStepConfiguration['type'], TerraformStepConfiguration['type']> = {
  Inline: 'Inline',
  InheritFromPlan: 'InheritFromPlan',
  InheritFromApply: 'InheritFromApply'
}
 
export const CommandTypes = {
  Apply: 'Apply',
  Destroy: 'Destroy'
}
export interface PathInterface {
  [x: string]: any
  path: string
}
 
export interface EnvironmentVar {
  key: string
  value: string
}
 
export interface BackendConfig {
  type: string
  spec: {
    content?: string
  }
}
export interface VarFileArray {
  varFile: {
    type?: string
    store?: {
      spec?: {
        gitFetchType?: string
        repoName?: string
        branch?: string
        commitId?: string
        connectorRef?: {
          label: string
          value: string
          scope: Scope
          live: boolean
          connector: { type: string; spec: { val: string } }
        }
        paths?: PathInterface[]
        content?: string
      }
    }
  }
}
 
export interface ConfigFileData {
  spec?: {
    configuration?: {
      spec?: TerraformApplyStepInfo
    }
  }
}
 
export interface TFPlanConfig {
  spec?: {
    configuration?: TerraformPlanExecutionData
  }
}
 
export interface Connector {
  label: string
  value: string
  scope: Scope
  live: boolean
  connector: {
    type: string
    identifier: string
    name: string
    spec: { val: string; url: string; connectionType?: string; type?: string }
  }
}
export interface TerraformData extends StepElementConfig {
  spec?: {
    provisionerIdentifier?: string
    configuration?: {
      type?: 'Inline' | 'InheritFromPlan' | 'InheritFromApply'
 
      spec?: TFDataSpec
    }
  }
}
 
export interface TerraformPlanData extends StepElementConfig {
  spec?: TerraformPlanStepInfo
}
 
export interface TFDataSpec {
  workspace?: string
  backendConfig?: TerraformBackendConfig
  targets?: any
 
  environmentVariables?: any
  configFiles?: {
    store?: {
      type?: string
      spec?: {
        gitFetchType?: string
        branch?: string
        commitId?: string
        folderPath?: string
        connectorRef?: string | Connector
        repositoryName?: string
        artifactPaths?: string
      }
    }
  }
  varFiles?: TerraformVarFileWrapper[]
}
 
export interface TFFormData extends StepElementConfig {
  spec?: TerraformApplyStepInfo
}
 
export interface TFDestroyData extends StepElementConfig {
  spec?: TerraformDestroyStepInfo
}
 
export interface TFRollbackData extends StepElementConfig {
  spec: TerraformRollbackStepInfo
}
 
export interface TFPlanFormData extends StepElementConfig {
  spec?: Omit<TerraformPlanStepInfo, 'configuration'> & {
    configuration: Omit<TerraformPlanExecutionData, 'environmentVariables' | 'targets'> & {
      targets?: Array<{ id: string; value: string }> | string[] | string
      environmentVariables?: Array<{ key: string; id: string; value: string }> | string
    }
  }
}
 
export interface TerraformFormData extends StepElementConfig {
  delegateSelectors: string[]
  spec?: TerraformPlanStepInfo
}
 
export interface TfVar {
  type?: string
  connectorRef?: {
    label: string
    scope: Scope
    value: string
  }
  gitFetchType?: string
  repoName?: string
  branch?: string
  commitId?: string
  paths?: string[]
}
 
export const onSubmitTerraformData = (values: any): TFFormData => {
  if (values?.spec?.configuration?.type === 'Inline') {
    const envVars = values.spec?.configuration?.spec?.environmentVariables
    const envMap: StringNGVariable[] = []
    Eif (Array.isArray(envVars)) {
      envVars.forEach(mapValue => {
        Iif (mapValue.value) {
          envMap.push({
            name: mapValue.key,
            value: mapValue.value,
            type: 'String'
          })
        }
      })
    }
 
    const targets = values?.spec?.configuration?.spec?.targets as MultiTypeInputType
    const targetMap: ListType = []
    Eif (Array.isArray(targets)) {
      targets.forEach(target => {
        Iif (target.value) {
          targetMap.push(target.value)
        }
      })
    }
 
    const connectorValue = values?.spec?.configuration?.spec?.configFiles?.store?.spec?.connectorRef as any
 
    const configObject: TerraformExecutionData = {
      workspace: values?.spec?.configuration?.spec?.workspace,
      configFiles: {} as any
    }
    Iif (values?.spec?.configuration?.spec?.backendConfig?.spec?.content) {
      configObject['backendConfig'] = {
        type: 'Inline',
        spec: {
          content: values?.spec?.configuration?.spec?.backendConfig?.spec?.content
        }
      }
    } else {
      unset(values?.spec?.configuration?.spec, 'backendConfig')
    }
 
    Iif (envMap.length) {
      configObject['environmentVariables'] = envMap
    }
 
    Iif (targetMap.length) {
      configObject['targets'] = targetMap
    } else Iif (getMultiTypeFromValue(values?.spec?.configuration?.spec?.targets) === MultiTypeInputType.RUNTIME) {
      configObject['targets'] = values?.spec?.configuration?.spec?.targets
    }
 
    Eif (values?.spec?.configuration?.spec?.varFiles?.length) {
      configObject['varFiles'] = values?.spec?.configuration?.spec?.varFiles
    } else {
      unset(values?.spec?.configuration?.spec, 'varFiles')
    }
 
    Eif (
      connectorValue ||
      getMultiTypeFromValue(values?.spec?.configuration?.spec?.configFiles?.store?.spec?.connectorRef) ===
        MultiTypeInputType.RUNTIME
    ) {
      configObject['configFiles'] = {
        ...values.spec?.configuration?.spec?.configFiles,
        store: {
          ...values.spec?.configuration?.spec?.configFiles?.store,
          type: connectorValue?.connector?.type || values?.spec?.configuration?.spec?.configFiles?.store?.type,
          spec: {
            ...values.spec?.configuration?.spec?.configFiles?.store?.spec,
            connectorRef: values?.spec?.configuration?.spec?.configFiles?.store?.spec?.connectorRef
              ? getMultiTypeFromValue(values?.spec?.configuration?.spec?.configFiles?.store?.spec?.connectorRef) ===
                  MultiTypeInputType.RUNTIME || !connectorValue?.value
                ? values?.spec?.configuration?.spec?.configFiles?.store?.spec?.connectorRef
                : connectorValue?.value
              : ''
          }
        }
      }
    }
    return {
      ...values,
      spec: {
        provisionerIdentifier: values.spec?.provisionerIdentifier,
        configuration: {
          type: values?.spec?.configuration?.type,
          spec: {
            ...configObject
          }
        }
      }
    }
  }
 
  return {
    ...values,
    spec: {
      ...values.spec,
      provisionerIdentifier: values?.spec?.provisionerIdentifier,
      configuration: {
        type: values?.spec?.configuration?.type
      }
    }
  }
}
 
export const onSubmitTFPlanData = (values: any): TFPlanFormData => {
  const envVars = values.spec?.configuration?.environmentVariables
  const envMap: StringNGVariable[] = []
  Eif (Array.isArray(envVars)) {
    envVars.forEach(mapValue => {
      Eif (mapValue.value) {
        envMap.push({
          name: mapValue.key,
          value: mapValue.value,
          type: 'String'
        })
      }
    })
  }
 
  const targets = values?.spec?.configuration?.targets as MultiTypeInputType
  const targetMap: ListType = []
  Eif (Array.isArray(targets)) {
    targets.forEach(target => {
      Eif (target.value) {
        targetMap.push(target.value)
      }
    })
  }
 
  const connectorValue = values?.spec?.configuration?.configFiles?.store?.spec?.connectorRef
 
  const configObject: TerraformPlanExecutionData = {
    command: values?.spec?.configuration?.command,
    workspace: values?.spec?.configuration?.workspace,
    configFiles: {} as TerraformConfigFilesWrapper,
    secretManagerRef: ''
  }
  Eif (values?.spec?.configuration?.backendConfig?.spec?.content) {
    configObject['backendConfig'] = {
      type: 'Inline',
      spec: {
        content: values?.spec?.configuration?.backendConfig?.spec?.content
      }
    }
  }
 
  Eif (envMap.length) {
    configObject['environmentVariables'] = envMap
  } else if (getMultiTypeFromValue(values?.spec?.configuration?.environmentVariables) === MultiTypeInputType.RUNTIME) {
    configObject['environmentVariables'] = values?.spec?.configuration?.environmentVariables
  }
 
  Eif (targetMap.length) {
    configObject['targets'] = targetMap
  } else if (getMultiTypeFromValue(values?.spec?.configuration?.targets) === MultiTypeInputType.RUNTIME) {
    configObject['targets'] = values?.spec?.configuration?.targets
  }
 
  Eif (values?.spec?.configuration?.varFiles?.length) {
    configObject['varFiles'] = values?.spec?.configuration?.varFiles
  }
  Eif (
    connectorValue ||
    getMultiTypeFromValue(values?.spec?.configuration?.configFiles?.store?.spec?.connectorRef) ===
      MultiTypeInputType.RUNTIME
  ) {
    configObject['configFiles'] = {
      ...values.spec?.configuration?.configFiles,
      store: {
        ...values.spec?.configuration?.configFiles?.store,
        type: connectorValue?.connector?.type || values?.spec?.configuration?.configFiles?.store?.type,
        spec: {
          ...values.spec?.configuration?.configFiles?.store?.spec,
          connectorRef: values?.spec?.configuration?.configFiles?.store?.spec?.connectorRef
            ? getMultiTypeFromValue(values?.spec?.configuration?.spec?.configFiles?.store?.spec?.connectorRef) ===
                MultiTypeInputType.RUNTIME || !connectorValue?.value
              ? values?.spec?.configuration?.configFiles?.store?.spec?.connectorRef
              : connectorValue?.value
            : ''
        }
      }
    }
  }
 
  Eif (values?.spec?.configuration?.secretManagerRef) {
    configObject['secretManagerRef'] = values?.spec?.configuration?.secretManagerRef
      ? values?.spec?.configuration?.secretManagerRef
      : ''
  }
 
  return {
    ...values,
    spec: {
      ...values.spec,
 
      configuration: {
        ...configObject
      }
    }
  }
}
export interface InlineVar {
  varFile: {
    identifier: string
    spec: InlineTerraformVarFileSpec
  }
}