@ibm-cloud/cd-tools 1.15.2 → 1.15.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cmd/utils/import-terraform.js +22 -3
- package/cmd/utils/terraform.js +18 -14
- package/package.json +1 -1
|
@@ -15,7 +15,7 @@ import { jsonToTf } from 'json-to-tf';
|
|
|
15
15
|
import { getPipelineData, getToolchainTools } from './requests.js';
|
|
16
16
|
import { runTerraformPlanGenerate, setTerraformEnv } from './terraform.js';
|
|
17
17
|
import { getRandChars, isSecretReference, normalizeName } from './utils.js';
|
|
18
|
-
import { logger } from './logger.js';
|
|
18
|
+
import { LOG_STAGES, logger } from './logger.js';
|
|
19
19
|
|
|
20
20
|
import { SECRET_KEYS_MAP, SUPPORTED_TOOLS_MAP } from '../../config.js';
|
|
21
21
|
|
|
@@ -160,9 +160,24 @@ export async function importTerraform(token, apiKey, region, toolchainId, toolch
|
|
|
160
160
|
|
|
161
161
|
// STEP 2/2: run terraform import and post-processing
|
|
162
162
|
setTerraformEnv(apiKey, verbosity);
|
|
163
|
-
await runTerraformPlanGenerate(dir, 'generated/draft.tf').catch((err) => { DEBUG_MODE && logger.dump(`\n[DEBUG_MODE=true] Draft errors: ${err}`) }); // temp fix for errors before post-processing
|
|
164
163
|
|
|
165
|
-
|
|
164
|
+
let draftErrors = '';
|
|
165
|
+
await runTerraformPlanGenerate(dir, 'generated/draft.tf').catch((err) => {
|
|
166
|
+
if (DEBUG_MODE) logger.dump(`\n[DEBUG_MODE=true] Draft errors: ${err}`);
|
|
167
|
+
draftErrors = err;
|
|
168
|
+
});
|
|
169
|
+
// above is a temp fix for errors before post-processing
|
|
170
|
+
// empty pipeline_id and trigger_id are expected and is a known provider bug
|
|
171
|
+
|
|
172
|
+
let generatedFile = '';
|
|
173
|
+
try {
|
|
174
|
+
generatedFile = fs.readFileSync(`${dir}/generated/draft.tf`);
|
|
175
|
+
} catch (err) {
|
|
176
|
+
// if draft file is missing, then something went wrong
|
|
177
|
+
// also, no need to log draft errors twice
|
|
178
|
+
throw new Error(err + (!DEBUG_MODE ? ('\nDraft errors: ' + draftErrors) : '\nDraft errors already logged'));
|
|
179
|
+
}
|
|
180
|
+
|
|
166
181
|
const generatedFileJson = await tfToJson('draft.tf', generatedFile.toString());
|
|
167
182
|
|
|
168
183
|
const newTfFileObj = { 'resource': {} }
|
|
@@ -236,6 +251,10 @@ export async function importTerraform(token, apiKey, region, toolchainId, toolch
|
|
|
236
251
|
const propValue = newTfFileObj['resource'][key][k]['value'];
|
|
237
252
|
if (newTfFileObj['resource'][key][k]['type'] === 'integration' && propValue in toolIdMap) {
|
|
238
253
|
newTfFileObj['resource'][key][k]['value'] = `\${${toolIdMap[propValue].type}.${toolIdMap[propValue].name}.tool_id}`;
|
|
254
|
+
} else if (propValue) {
|
|
255
|
+
// escape newlines, double quotes and backslashes
|
|
256
|
+
if (propValue.includes('\n')) logger.warn(`Warning! Multi-line values for pipeline and trigger properties are not yet supported in the provider, newlines will be replaced with '\\\\n': "${k}"`, LOG_STAGES.import, true);
|
|
257
|
+
newTfFileObj['resource'][key][k]['value'] = propValue.replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/"/g, '\\"');
|
|
239
258
|
}
|
|
240
259
|
}
|
|
241
260
|
|
package/cmd/utils/terraform.js
CHANGED
|
@@ -334,13 +334,15 @@ async function setupTerraformFiles(config) {
|
|
|
334
334
|
if (isCompact || resourceName === 'ibm_cd_tekton_pipeline_property') {
|
|
335
335
|
for (const [k, v] of Object.entries(newTfFileObj['resource']['ibm_cd_tekton_pipeline_property'])) {
|
|
336
336
|
try {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
337
|
+
const thisValue = v['value'];
|
|
338
|
+
|
|
339
|
+
if (v['type'] === 'integration' && thisValue in toolIdToTfName) {
|
|
340
|
+
const thisTfName = toolIdToTfName[thisValue];
|
|
341
|
+
newTfFileObj['resource']['ibm_cd_tekton_pipeline_property'][k]['value'] = `\${ibm_cd_toolchain_tool_githubconsolidated.${thisTfName}.tool_id}`;
|
|
342
|
+
} else if (thisValue) {
|
|
343
|
+
// escape newlines, double quotes and backslashes
|
|
344
|
+
// TODO: remove extra backslash in newline replacement once provider is updated
|
|
345
|
+
newTfFileObj['resource']['ibm_cd_tekton_pipeline_property'][k]['value'] = thisValue.replace(/\\/g, '\\\\').replace(/\n/g, '\\\\n').replace(/\r/g, '\\\\r').replace(/"/g, '\\"');
|
|
344
346
|
}
|
|
345
347
|
}
|
|
346
348
|
catch {
|
|
@@ -354,13 +356,15 @@ async function setupTerraformFiles(config) {
|
|
|
354
356
|
if (isCompact || resourceName === 'ibm_cd_tekton_pipeline_trigger_property') {
|
|
355
357
|
for (const [k, v] of Object.entries(newTfFileObj['resource']['ibm_cd_tekton_pipeline_trigger_property'])) {
|
|
356
358
|
try {
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
359
|
+
const thisValue = v['value'];
|
|
360
|
+
|
|
361
|
+
if (v['type'] === 'integration' && thisValue in toolIdToTfName) {
|
|
362
|
+
const thisTfName = toolIdToTfName[thisValue];
|
|
363
|
+
newTfFileObj['resource']['ibm_cd_tekton_pipeline_trigger_property'][k]['value'] = `\${ibm_cd_toolchain_tool_githubconsolidated.${thisTfName}.tool_id}`;
|
|
364
|
+
} else if (thisValue) {
|
|
365
|
+
// escape newlines, double quotes and backslashes
|
|
366
|
+
// TODO: remove extra backslash in newline replacement once provider is updated
|
|
367
|
+
newTfFileObj['resource']['ibm_cd_tekton_pipeline_trigger_property'][k]['value'] = thisValue.replace(/\\/g, '\\\\').replace(/\n/g, '\\\\n').replace(/\r/g, '\\\\r').replace(/"/g, '\\"');
|
|
364
368
|
}
|
|
365
369
|
}
|
|
366
370
|
catch {
|