public setOutputVariableToPlanFilePath()

in Extensions/Terraform/Src/Tasks/TerraformTask/TerraformTaskV2/src/base-terraform-command-handler.ts [135:203]


    public setOutputVariableToPlanFilePath() {
        // Do terraform version to check if version is >= 0.12.0
        if (this.checkIfShowCommandSupportsJsonOutput() >= 0) {
            let terraformTool;
            let fileStream;

            // Do terraform plan with -out flag to output the binary plan file
            const binaryPlanFilePath = path.resolve(`plan-binary-${uuidV4()}.tfplan`);
            const tempFileForPlanOutput = path.resolve(`temp-plan-${uuidV4()}.txt`);

            let serviceName = `environmentServiceName${this.getServiceProviderNameFromProviderInput()}`;
            let planCommand = new TerraformAuthorizationCommandInitializer(
                "plan",
                tasks.getInput("workingDirectory"),
                tasks.getInput(serviceName, true),
                `${tasks.getInput("commandOptions")} -out=${binaryPlanFilePath}`
            );
            terraformTool = this.terraformToolHandler.createToolRunner(planCommand);
            this.handleProvider(planCommand);
            fileStream = fs.createWriteStream(tempFileForPlanOutput);
            terraformTool.execSync(<IExecSyncOptions>{
                cwd: planCommand.workingDirectory,
                outStream: fileStream
            });

            // Do terraform show with -json flag to output the json plan file
            const jsonPlanFilePath = path.resolve(`plan-json-${uuidV4()}.json`);
            const tempFileForJsonPlanOutput = path.resolve(`temp-plan-json-${uuidV4()}.json`)
            let commandOutput: IExecSyncResult;
            let showCommand = new TerraformBaseCommandInitializer(
                "show",
                tasks.getInput("workingDirectory"),
                `-json ${binaryPlanFilePath}`
            );
            terraformTool = this.terraformToolHandler.createToolRunner(showCommand);
            fileStream = fs.createWriteStream(tempFileForJsonPlanOutput);
            commandOutput = terraformTool.execSync(<IExecSyncOptions>{
                cwd: showCommand.workingDirectory,
                outStream: fileStream
            });

            // Write command output to the json plan file
            tasks.writeFile(jsonPlanFilePath, commandOutput.stdout);
            // Set the output variable to the json plan file path
            tasks.setVariable('jsonPlanFilePath', jsonPlanFilePath);

            // Delete all the files that are not needed any further
            if (tasks.exist(binaryPlanFilePath)) {
                (async () => {
                    await del([binaryPlanFilePath]);
                })();
            }

            if (tasks.exist(tempFileForPlanOutput)) {
                (async () => {
                    await del([tempFileForPlanOutput]);
                })();
            }

            if (tasks.exist(tempFileForJsonPlanOutput)) {
                (async () => {
                    await del([tempFileForJsonPlanOutput]);
                })();
            }

        } else {
            tasks.warning("Terraform show command does not support -json flag for terraform versions older than 0.12.0. The output variable named 'jsonPlanFilePath' was not set.")
        }
    }