Skip to content

Commit ce4629b

Browse files
committed
fix: create correct HCL for remote backend
1 parent a36c83b commit ce4629b

File tree

2 files changed

+151
-9
lines changed

2 files changed

+151
-9
lines changed

packages/cdktf/lib/backends/remote-backend.ts

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,22 @@ export class RemoteBackend extends TerraformBackend {
1919
}
2020

2121
protected synthesizeAttributes(): { [name: string]: any } {
22-
return keysToSnakeCase({ ...this.props });
22+
return {
23+
...keysToSnakeCase({ ...this.props }),
24+
workspaces: this.props.workspaces.toTerraform(),
25+
};
2326
}
2427

2528
protected synthesizeHclAttributes(): { [name: string]: any } {
26-
return keysToSnakeCase({ ...this.props });
29+
return {
30+
...keysToSnakeCase(this.props),
31+
workspaces: {
32+
value: this.props.workspaces.toHclTerraform(),
33+
isBlock: true,
34+
type: "map",
35+
storageClassType: "stringmap",
36+
},
37+
};
2738
}
2839

2940
public toMetadata() {
@@ -53,26 +64,79 @@ export class DataTerraformRemoteState extends TerraformRemoteState {
5364
}
5465
}
5566

67+
export interface DataRemoteBackendConfig {
68+
readonly hostname?: string;
69+
readonly organization: string;
70+
readonly token?: string;
71+
workspaces: IRemoteWorkspace;
72+
}
73+
5674
export interface RemoteBackendConfig {
5775
readonly hostname?: string;
5876
readonly organization: string;
5977
readonly token?: string;
60-
readonly workspaces: IRemoteWorkspace;
78+
/**
79+
* A nested block that specifies which remote Terraform workspaces to use for the current configuration.
80+
*/
81+
readonly workspaces: NamedRemoteWorkspace | PrefixedRemoteWorkspaces;
6182
}
6283

6384
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
6485
export interface IRemoteWorkspace {}
6586

87+
/**
88+
* A cloud workspace can either be a single named workspace, or a list of tagged workspaces.
89+
*/
90+
export abstract class RemoteWorkspace {
91+
public abstract toTerraform(): any;
92+
}
93+
6694
// eslint-disable-next-line jsdoc/require-jsdoc
67-
export class NamedRemoteWorkspace implements IRemoteWorkspace {
68-
public constructor(public name: string) {}
95+
export class NamedRemoteWorkspace extends RemoteWorkspace {
96+
public constructor(public name: string) {
97+
super();
98+
}
99+
100+
public toHclTerraform(): any {
101+
return {
102+
name: {
103+
value: this.name,
104+
type: "simple",
105+
storageClassType: "string",
106+
},
107+
};
108+
}
109+
110+
public toTerraform(): any {
111+
return {
112+
name: this.name,
113+
};
114+
}
69115
}
70116

71117
// eslint-disable-next-line jsdoc/require-jsdoc
72-
export class PrefixedRemoteWorkspaces implements IRemoteWorkspace {
73-
public constructor(public prefix: string) {}
118+
export class PrefixedRemoteWorkspaces extends RemoteWorkspace {
119+
public constructor(public prefix: string) {
120+
super();
121+
}
122+
123+
public toHclTerraform(): any {
124+
return {
125+
prefix: {
126+
value: this.prefix,
127+
type: "simple",
128+
storageClassType: "string",
129+
},
130+
};
131+
}
132+
133+
public toTerraform(): any {
134+
return {
135+
prefix: this.prefix,
136+
};
137+
}
74138
}
75139

76140
export interface DataTerraformRemoteStateRemoteConfig
77141
extends DataTerraformRemoteStateConfig,
78-
RemoteBackendConfig {}
142+
DataRemoteBackendConfig {}

packages/cdktf/test/json-to-hcl.test.ts

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ describe("backends", () => {
663663
`);
664664
});
665665

666-
test("remote", async () => {
666+
test("remote as datasource", async () => {
667667
const app = Testing.app();
668668
const stack = new TerraformStack(app, "test");
669669

@@ -676,6 +676,7 @@ describe("backends", () => {
676676
},
677677
});
678678

679+
// Data for terraform_remote_state is different than remote backend configuration
679680
expect(Testing.synthHcl(stack)).toMatchInlineSnapshot(`
680681
"
681682
data "terraform_remote_state" "remote" {
@@ -691,6 +692,59 @@ describe("backends", () => {
691692
`);
692693
});
693694

695+
test("remote as backend", async () => {
696+
const app = Testing.app();
697+
const stack = new TerraformStack(app, "test");
698+
699+
new b.RemoteBackend(stack, {
700+
organization: "test-organization",
701+
workspaces: new b.NamedRemoteWorkspace("test-stack"),
702+
hostname: "test.artifactory.local",
703+
});
704+
705+
expect(Testing.synthHcl(stack)).toMatchInlineSnapshot(`
706+
"terraform {
707+
required_providers {
708+
709+
}
710+
backend "remote" {
711+
organization = "test-organization"
712+
workspaces {
713+
name = "test-stack"
714+
}
715+
hostname = "test.artifactory.local"
716+
}
717+
718+
719+
}"
720+
`);
721+
});
722+
723+
test("cloud as backend", async () => {
724+
const app = Testing.app();
725+
const stack = new TerraformStack(app, "test");
726+
727+
new b.CloudBackend(stack, {
728+
organization: "test-organization",
729+
workspaces: new b.NamedCloudWorkspace("test-stack"),
730+
});
731+
732+
expect(Testing.synthHcl(stack)).toMatchInlineSnapshot(`
733+
"terraform {
734+
required_providers {
735+
736+
}
737+
cloud {
738+
organization = "test-organization"
739+
workspaces {
740+
name = "test-stack"
741+
}
742+
}
743+
744+
}"
745+
`);
746+
});
747+
694748
test("azurerm", async () => {
695749
const app = Testing.app();
696750
const stack = new TerraformStack(app, "test");
@@ -1264,3 +1318,27 @@ test("import block", () => {
12641318
}"
12651319
`);
12661320
});
1321+
1322+
test("lifecycle ignore", () => {
1323+
const app = Testing.app();
1324+
const stack = new TerraformStack(app, "test");
1325+
1326+
new TestResource(stack, "test", {
1327+
name: "test",
1328+
lifecycle: {
1329+
ignoreChanges: ["tags"],
1330+
},
1331+
});
1332+
1333+
expect(Testing.synthHcl(stack)).toMatchInlineSnapshot(`
1334+
"
1335+
resource "test_resource" "test" {
1336+
name = "test"
1337+
lifecycle {
1338+
ignore_changes = [
1339+
"tags",
1340+
]
1341+
}
1342+
}"
1343+
`);
1344+
});

0 commit comments

Comments
 (0)