Skip to content

Commit 5ea6d6f

Browse files
authored
fix(terraform): misc cleanup [khcp-12445] (#1569)
Misc fixes for terraform, part of [KHCP-12445](https://konghq.atlassian.net/browse/KHCP-12445). Fixes: - `tags = []` not appearing on it's own line - correctly handle terraform on consumer credential form - add `other` type for unsupported terraform entities (Vault secrets) - terraform tab should NOT be displayed - fix handling for key -> key_set relationship (use `set` instead)
1 parent 5c2de50 commit 5ea6d6f

File tree

5 files changed

+36
-20
lines changed

5 files changed

+36
-20
lines changed

packages/entities/entities-plugins/src/components/PluginForm.vue

+3
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
</template>
127127
<template #terraform>
128128
<TerraformCodeBlock
129+
:credential-type="credentialType"
129130
:entity-record="getRequestBody"
130131
:entity-type="SupportedEntityType.Plugin"
131132
/>
@@ -1240,6 +1241,7 @@ watch(actionsDivRef, (newVal) => {
12401241
}
12411242
})
12421243
1244+
const credentialType = ref('')
12431245
const schemaLoading = ref(false)
12441246
const fetchSchemaError = ref('')
12451247
onBeforeMount(async () => {
@@ -1251,6 +1253,7 @@ onBeforeMount(async () => {
12511253
// credential schema endpoints don't exist for Konnect, so we use hard-coded schemas
12521254
const pluginType = CREDENTIAL_METADATA[props.pluginType]?.schemaEndpoint
12531255
const data = CREDENTIAL_SCHEMAS[pluginType]
1256+
credentialType.value = pluginType
12541257
12551258
schema.value = buildFormSchema('', data, {})
12561259
schemaLoading.value = false

packages/entities/entities-shared/src/components/common/TerraformCodeBlock.vue

+19-15
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,20 @@ const props = defineProps({
2727
required: true,
2828
validator: (val: SupportedEntityType) => SupportedEntityTypesArray.includes(val),
2929
},
30+
credentialType: {
31+
type: String,
32+
default: '',
33+
},
3034
})
3135
3236
const buildBasicValString = (value: string | number | boolean, key: string): string => {
3337
const indent = SINGLE_INDENT
3438
let content = ''
3539
3640
if (typeof value === 'string') {
37-
content += `\n${indent}${key} = "${value}"`
41+
content += `${indent}${key} = "${value}"\n`
3842
} else { // boolean or number
39-
content += `\n${indent}${key} = ${String(value !== undefined && value !== null ? value : '')}`
43+
content += `${indent}${key} = ${String(value !== undefined && value !== null ? value : '')}\n`
4044
}
4145
4246
return content
@@ -52,7 +56,7 @@ const buildObjectStr = (value: Record<string, any>, key?: string, additionalInde
5256
let content = ''
5357
5458
if (key) {
55-
content += `\n${indent}${key} = {\n`
59+
content += `${indent}${key} = {\n`
5660
}
5761
5862
if (value === null) {
@@ -94,7 +98,7 @@ const buildObjectStr = (value: Record<string, any>, key?: string, additionalInde
9498
content += `${indent}${SINGLE_INDENT}${k} = ${valueContent}\n`
9599
}
96100
97-
return key ? content += `${indent}}` : content
101+
return key ? content += `${indent}}\n` : content
98102
}
99103
100104
const buildArrayStr = (arr: any[], key?: string, additionalIndent = ''): string => {
@@ -108,7 +112,7 @@ const buildArrayStr = (arr: any[], key?: string, additionalIndent = ''): string
108112
if (arr.length === 0) {
109113
content += `${indent}${key} = [`
110114
} else {
111-
content += `\n${indent}${key} = [\n`
115+
content += `${indent}${key} = [\n`
112116
}
113117
}
114118
@@ -135,9 +139,9 @@ const buildArrayStr = (arr: any[], key?: string, additionalIndent = ''): string
135139
136140
if (key) {
137141
if (arr.length === 0) {
138-
content += ']'
142+
content += ']\n'
139143
} else {
140-
content += `${indent}]`
144+
content += `${indent}]\n`
141145
}
142146
}
143147
@@ -201,33 +205,33 @@ const terraformContent = computed((): string => {
201205
// snis can be a child of certificate
202206
parentEntityType = 'certificate'
203207
delete modifiedRecord.certificate
204-
} else if (modifiedRecord.key_set?.id) {
208+
} else if (modifiedRecord.set?.id) {
205209
// keys can be a child of key_set
206-
parentEntityType = 'key_set'
207-
delete modifiedRecord.key_set
210+
parentEntityType = 'set'
211+
delete modifiedRecord.set
208212
}
209213
210214
// special handling for plugins
211215
if (props.entityType === 'plugin') {
212216
// plugin type is specified separately
213217
//clone and convert '-' to '_' since terraform doesn't allow '-'
214-
const pluginType = (modifiedRecord.name + '').replace(/-/g, '_')
218+
const pluginType = props.credentialType.replace(/-/g, '_') || (modifiedRecord.name + '').replace(/-/g, '_')
215219
delete modifiedRecord.name
216220
217-
content += `resource "konnect_gateway_plugin_${pluginType}" "my_${pluginType}" {`
221+
content += `resource "konnect_gateway_plugin_${pluginType}" "my_${pluginType}" {\n`
218222
} else { // generic entity
219-
content += `resource "konnect_gateway_${props.entityType}" "my_${props.entityType}" {`
223+
content += `resource "konnect_gateway_${props.entityType}" "my_${props.entityType}" {\n`
220224
}
221225
222226
// main config
223227
content += generateConfig(modifiedRecord)
224228
225229
// control plane id
226-
content += `\n${SINGLE_INDENT}control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id\n`
230+
content += `${SINGLE_INDENT}control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id\n`
227231
228232
// parent entity information if scoped
229233
if (parentEntityType) {
230-
content += `\n${SINGLE_INDENT}${parentEntityType} = {\n`
234+
content += `${SINGLE_INDENT}${parentEntityType} = {\n`
231235
content += `${SINGLE_INDENT}${SINGLE_INDENT}id = konnect_gateway_${parentEntityType}.my_${parentEntityType}.id\n`
232236
content += `${SINGLE_INDENT}}\n`
233237
}

packages/entities/entities-shared/src/components/entity-base-form/EntityBaseForm.vue

+3-3
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ import type { PropType } from 'vue'
115115
import { computed, ref, onBeforeMount, watch } from 'vue'
116116
import { useRouter } from 'vue-router'
117117
import type { AxiosError } from 'axios'
118-
import type { KonnectBaseFormConfig, KongManagerBaseFormConfig, SupportedEntityType } from '../../types'
119-
import { SupportedEntityTypesArray } from '../../types'
118+
import type { KonnectBaseFormConfig, KongManagerBaseFormConfig } from '../../types'
119+
import { SupportedEntityTypesArray, SupportedEntityType } from '../../types'
120120
import composables from '../../composables'
121121
import type { Tab } from '@kong/kongponents'
122122
import JsonCodeBlock from '../common/JsonCodeBlock.vue'
@@ -291,7 +291,7 @@ const tabs = ref<Tab[]>([
291291
},
292292
])
293293
294-
if (props.enableTerraform) {
294+
if (props.enableTerraform && props.entityType !== SupportedEntityType.Other) {
295295
// insert terraform as the third option
296296
tabs.value.splice(1, 0, {
297297
title: t('baseForm.configuration.terraform'),

packages/entities/entities-shared/src/types/entity-base-config-card.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
import type { KonnectConfig, KongManagerConfig } from './index'
22

3+
/**
4+
* These entity strings are used for generating terraform scripts.
5+
* Only add entity types that are supported by terraform. Use '_' instead of '-'.
6+
* DO NOT MODIFY THE VALUES OF THESE STRINGS.
7+
*/
38
export enum SupportedEntityType {
49
CaCertificate = 'ca_certificate',
510
Certificate = 'certificate',
611
Consumer = 'consumer',
712
ConsumerGroup = 'consumer_group',
813
GatewayService = 'service',
914
Key = 'key',
10-
KeySet = 'key_set',
15+
KeySet = 'set',
1116
Plugin = 'plugin',
1217
Route = 'route',
1318
SNI = 'sni',
1419
Upstream = 'upstream',
1520
Target = 'target',
1621
Vault = 'vault',
22+
// Use this for any entity type that is not supported by terraform
23+
// If entityType is 'other' terraform scripts will not be available
24+
// Note: This is currently only supported by EntityBaseForm not EntityBaseConfigCard!!
25+
Other = 'other',
1726
}
1827

1928
export const SupportedEntityTypesArray = Object.values(SupportedEntityType)

packages/entities/entities-vaults/src/components/SecretForm.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
:config="config"
66
:edit-id="secretId"
77
:enable-terraform="enableTerraform"
8-
:entity-type="SupportedEntityType.Vault"
8+
:entity-type="SupportedEntityType.Other"
99
:error-message="state.errorMessage"
1010
:fetch-url="fetchUrl"
1111
:form-fields="payload"

0 commit comments

Comments
 (0)