Skip to content

Commit

Permalink
chore(cli): replace terraform cloud implementation with terraform CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Nov 17, 2022
1 parent a33dde9 commit 88ca0d0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 699 deletions.
40 changes: 1 addition & 39 deletions packages/cdktf-cli/src/lib/cdktf-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { Terraform, TerraformPlan } from "./models/terraform";
import { getConstructIdsForOutputs, NestedTerraformOutputs } from "./output";
import { logger } from "./logging";
import { extractJsonLogIfPresent } from "./server/terraform-logs";
import { TerraformJson } from "./terraform-json";
import { TerraformCloud } from "./models/terraform-cloud";
import { TerraformCli } from "./models/terraform-cli";
import { Errors } from "./errors";
import * as fs from "fs";
import * as path from "path";
import { ProviderConstraint } from "./dependencies/dependency-manager";
Expand Down Expand Up @@ -78,46 +75,11 @@ export type StackApprovalUpdate = {
async function getTerraformClient(
abortSignal: AbortSignal,
stack: SynthesizedStack,
isSpeculative: boolean,
_isSpeculative: boolean, // TODO: remove
createTerraformLogHandler: (
phase: string
) => (message: string, isError?: boolean) => void
): Promise<Terraform> {
const parsedStack = JSON.parse(stack.content) as TerraformJson;

if (parsedStack.terraform?.backend?.remote) {
const tfClient = new TerraformCloud(
abortSignal,
stack,
parsedStack.terraform.backend.remote,
isSpeculative,
createTerraformLogHandler
);
if (await tfClient.isRemoteWorkspace()) {
return tfClient;
}
}

if (parsedStack.terraform?.cloud) {
const workspaces = parsedStack.terraform.cloud.workspaces;
if (!("name" in workspaces)) {
throw Errors.Usage(
"The Cloud backend can not used with the cdktf-cli unless specified with a workspace name."
);
}

const tfClient = new TerraformCloud(
abortSignal,
stack,
{ ...parsedStack.terraform.cloud, workspaces },
isSpeculative,
createTerraformLogHandler
);
if (await tfClient.isRemoteWorkspace()) {
return tfClient;
}
}

return new TerraformCli(abortSignal, stack, createTerraformLogHandler);
}

Expand Down
42 changes: 38 additions & 4 deletions packages/cdktf-cli/src/lib/models/terraform-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "./terraform";
import { SynthesizedStack } from "../synth-stack";
import { terraformBinaryName } from "../terraform";
import { TerraformJson } from "../terraform-json";

export class TerraformCliPlan
extends AbstractTerraformPlan
Expand All @@ -18,7 +19,7 @@ export class TerraformCliPlan
public readonly planFile: string,
public readonly plan: { [key: string]: any }
) {
super(planFile, plan.resource_changes, plan.output_changes);
super(planFile, plan?.resource_changes, plan?.output_changes);
}
}

Expand Down Expand Up @@ -50,6 +51,26 @@ export class TerraformCli implements Terraform {
args.push("-upgrade");
}

if (this.isTFCPlan) {
await exec(
terraformBinaryName,
[
"providers",
"lock",
"-platform=windows_amd64",
"-platform=darwin_amd64",
"-platform=linux_amd64",
],
{
cwd: this.workdir,
env: process.env,
signal: this.abortSignal,
},
this.onStdout("init"),
this.onStderr("init")
);
}

await exec(
terraformBinaryName,
args,
Expand All @@ -63,13 +84,26 @@ export class TerraformCli implements Terraform {
);
}

private get isTFCPlan(): boolean {
const content = JSON.parse(this.stack.content) as TerraformJson;
if (content.terraform?.backend?.remote) {
return true;
}

return false;
}

public async plan(
destroy = false,
refreshOnly = false,
parallelism = -1
): Promise<TerraformPlan> {
const planFile = "plan";
const options = ["plan", "-input=false", "-out", planFile];
const options = ["plan", "-input=false"];

if (!this.isTFCPlan) {
options.push("-out", planFile);
}
if (destroy) {
options.push("-destroy");
}
Expand Down Expand Up @@ -104,7 +138,7 @@ export class TerraformCli implements Terraform {
}

public async deploy(
planFile: string,
planFile?: string,
refreshOnly = false,
parallelism = -1,
extraOptions: string[] = []
Expand All @@ -121,7 +155,7 @@ export class TerraformCli implements Terraform {
...(refreshOnly ? ["-refresh-only"] : []),
...(parallelism > -1 ? [`-parallelism=${parallelism}`] : []),
// only appends planFile if not empty
// this allows deploying without a plan (as used in watch)
// this allows deploying without a plan (as used in watch and for TFC)
...(planFile ? [planFile] : []),
],
{ cwd: this.workdir, env: process.env, signal: this.abortSignal },
Expand Down

0 comments on commit 88ca0d0

Please sign in to comment.