Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Jul 18, 2022
1 parent 5addec6 commit f61e342
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 573 deletions.
18 changes: 1 addition & 17 deletions packages/cdktf-cli/lib/cdktf-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ 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";

export type StackUpdate =
Expand Down Expand Up @@ -72,25 +70,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;
}
}
return new TerraformCli(abortSignal, stack, createTerraformLogHandler);
}

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

export class TerraformCliPlan
extends AbstractTerraformPlan
Expand All @@ -16,7 +18,29 @@ 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);
}
}

export class TerraformCloudPlan implements TerraformPlan {
public get resources(): PlannedResource[] {
return [];
}

public get applyableResources(): PlannedResource[] {
return [];
}

public get outputs(): PlannedResource[] {
return [];
}

public get changingOutputs(): PlannedResource[] {
return [];
}

public get needsApply(): boolean {
return true;
}
}

Expand All @@ -42,6 +66,27 @@ export class TerraformCli implements Terraform {

public async init(): Promise<void> {
await this.setUserAgent();

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,
["init", "-input=false"],
Expand All @@ -55,12 +100,25 @@ 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
): 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 All @@ -80,6 +138,10 @@ export class TerraformCli implements Terraform {
this.onStderr("plan")
);

if (this.isTFCPlan) {
return new TerraformCloudPlan();
}

const jsonPlan = await exec(
terraformBinaryName,
["show", "-json", planFile],
Expand All @@ -92,7 +154,7 @@ export class TerraformCli implements Terraform {
}

public async deploy(
planFile: string,
planFile?: string,
refreshOnly = false,
extraOptions: string[] = []
): Promise<void> {
Expand All @@ -106,7 +168,7 @@ export class TerraformCli implements Terraform {

...extraOptions,
// 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] : []),
...(refreshOnly ? ["-refresh-only"] : []),
],
Expand Down

0 comments on commit f61e342

Please sign in to comment.