Skip to content

Commit e0b6111

Browse files
committed
chore: Directly call Terraform Cloud API instead of using third-party library
1 parent 0bfc97c commit e0b6111

File tree

1 file changed

+49
-39
lines changed
  • test/typescript/terraform-cloud

1 file changed

+49
-39
lines changed

test/typescript/terraform-cloud/test.ts

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,48 @@ describe("full integration test", () => {
3131
let workspaceName: string;
3232
const orgName = "cdktf";
3333

34+
async function createWorkspace(token: string, name: string) {
35+
const response = await fetch(
36+
`https://app.terraform.io/api/v2/organizations/${orgName}/workspaces`,
37+
{
38+
method: "POST",
39+
headers: {
40+
"Content-Type": "application/vnd.api+json",
41+
Authorization: `Bearer ${token}`,
42+
},
43+
body: JSON.stringify({
44+
data: {
45+
attributes: {
46+
name,
47+
executionMode: "remote",
48+
terraformVersion: TERRAFORM_VERSION,
49+
},
50+
},
51+
type: "workspaces",
52+
}),
53+
},
54+
);
55+
const res = await response.json();
56+
57+
return res.data.id;
58+
}
59+
60+
async function deleteWorkspace(token: string, name: string) {
61+
const response = await fetch(
62+
`https://app.terraform.io/api/v2/organizations/${orgName}/workspaces/${name}`,
63+
{
64+
method: "DELETE",
65+
headers: {
66+
"Content-Type": "application/vnd.api+json",
67+
Authorization: `Bearer ${token}`,
68+
},
69+
},
70+
);
71+
const res = await response.json();
72+
73+
return res.data.id;
74+
}
75+
3476
beforeEach(async () => {
3577
workspaceName = `${GITHUB_RUN_NUMBER}-${crypto
3678
.randomBytes(10)
@@ -45,36 +87,15 @@ describe("full integration test", () => {
4587
});
4688

4789
withAuth("deploy in Terraform Cloud", async () => {
48-
const client = new TerraformCloud(TERRAFORM_CLOUD_TOKEN);
49-
50-
await client.Workspaces.create(orgName, {
51-
data: {
52-
attributes: {
53-
name: workspaceName,
54-
executionMode: "remote",
55-
terraformVersion: TERRAFORM_VERSION,
56-
},
57-
type: "workspaces",
58-
},
59-
});
90+
await createWorkspace(TERRAFORM_CLOUD_TOKEN, workspaceName);
6091

6192
expect(await driver.deploy(["source-stack"])).toContain("Apply complete!");
62-
await client.Workspaces.deleteByName(orgName, workspaceName);
93+
94+
await deleteWorkspace(TERRAFORM_CLOUD_TOKEN, workspaceName);
6395
});
6496

6597
withAuth("deploy locally and then in Terraform Cloud", async () => {
66-
const client = new TerraformCloud(TERRAFORM_CLOUD_TOKEN);
67-
68-
await client.Workspaces.create(orgName, {
69-
data: {
70-
attributes: {
71-
name: workspaceName,
72-
executionMode: "remote",
73-
terraformVersion: TERRAFORM_VERSION,
74-
},
75-
type: "workspaces",
76-
},
77-
});
98+
await createWorkspace(TERRAFORM_CLOUD_TOKEN, workspaceName);
7899

79100
process.env.TF_EXECUTE_LOCAL = "true";
80101
await driver.deploy(["source-stack"], "before-migration.json");
@@ -87,31 +108,20 @@ describe("full integration test", () => {
87108
readFileSync("after-migration.json"),
88109
);
89110

90-
await client.Workspaces.deleteByName(orgName, workspaceName);
111+
await deleteWorkspace(TERRAFORM_CLOUD_TOKEN, workspaceName);
91112
});
92113

93114
// Only the origin stack is in TFC, the consumer stack is local
94115
withAuth(
95116
"deploy with cross stack reference origin in Terraform Cloud",
96117
async () => {
97-
const client = new TerraformCloud(TERRAFORM_CLOUD_TOKEN);
98-
99-
await client.Workspaces.create(orgName, {
100-
data: {
101-
attributes: {
102-
name: workspaceName,
103-
executionMode: "remote",
104-
terraformVersion: TERRAFORM_VERSION,
105-
},
106-
type: "workspaces",
107-
},
108-
});
118+
await createWorkspace(TERRAFORM_CLOUD_TOKEN, workspaceName);
109119

110120
await driver.deploy(["source-stack", "consumer-stack"]);
111121
driver.output("source-stack", "outputs.tmp.json", true);
112122
const outputs = JSON.parse(readFileSync("outputs.tmp.json").toString());
113123

114-
await client.Workspaces.deleteByName(orgName, workspaceName);
124+
await deleteWorkspace(TERRAFORM_CLOUD_TOKEN, workspaceName);
115125

116126
expect(driver.readLocalFile("consumer-file.txt")).toEqual(
117127
outputs["source-stack"].password_output,

0 commit comments

Comments
 (0)