Skip to content

Commit 596f680

Browse files
committed
feat(tfc-workspace): added terraform cloud workspace command
1 parent 57f8503 commit 596f680

File tree

6 files changed

+70
-14
lines changed

6 files changed

+70
-14
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@ Terraform cloud scripts
2222
### Usage:
2323

2424
```shell
25-
terraform-cloud <action> <resource> [arguments]
25+
terraform-cloud <resource> <action> [arguments]
2626
```
2727
OR
2828
```shell
29-
tfc <action> <resource> [arguments]
29+
tfc <resource> <action> [arguments]
3030
```
3131
### Available Allowed Resources:
32+
- `workspace`
3233
- `output`
3334
- `state`
3435

3536
### Available Allowed Actions:
3637
- `get`
3738
- `set`
39+
- `list`
3840

3941
---

scripts/shared/environment.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const Environment = (render) => {
99
TFC_TOKEN: process.env.TFC_TOKEN,
1010
TFC_WORKSPACE: process.env.TFC_WORKSPACE,
1111
},
12-
action: process.argv[2] || '',
13-
resource: process.argv[3] || '',
12+
action: process.argv[3] || '',
13+
resource: process.argv[2] || '',
1414
args: process.argv.slice(4)
1515
}
1616
if (render) {

scripts/terraform/cli.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const Environment = require("../shared/environment");
33
const Help = require("./help");
44
const OUTPUT = require("./resources/output");
55
const STATE = require("./resources/state");
6+
const WORKSPACE = require("./resources/workspace");
67

78
Object.byString = function(o, s) {
89
s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
@@ -19,14 +20,16 @@ Object.byString = function(o, s) {
1920
return o;
2021
}
2122

22-
const env = Environment(true);
23+
const env = Environment();
2324

2425
const ACTIONS = [
2526
'get',
26-
'set'
27+
'set',
28+
'list'
2729
]
2830

2931
const RESOURCES = [
32+
'workspace',
3033
'output',
3134
'state'
3235
]
@@ -38,19 +41,24 @@ if (
3841
try {
3942
let mod = null
4043
switch (env.resource) {
44+
case 'workspace':
45+
mod = WORKSPACE;
46+
break;
4147
case 'output':
42-
mod = OUTPUT
48+
mod = OUTPUT;
4349
break;
4450
case 'state':
45-
mod = STATE
51+
mod = STATE;
4652
break;
4753
default:
4854
break;
4955
}
5056

5157
if (mod && typeof mod[env.action]) {
52-
mod[env.action](env.args).then(()=> {
53-
console.log("ok");
58+
mod[env.action](env.args).then((value)=> {
59+
if (value) {
60+
console.log(JSON.stringify(value, null, ' '));
61+
}
5462
}).catch(err => {
5563
Help(err);
5664
});

scripts/terraform/help.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ function Help(errorMessage) {
99
Github: https://github.com/phenixcoder/devops-scripts
1010
1111
Usage:
12-
terraform-cloud <action> <resource> [arguments]
12+
terraform-cloud <resource> <action> [arguments]
1313
OR
14-
tfc <action> <resource> [arguments]
14+
tfc <resource> <action> [arguments]
1515
1616
`);
1717
}

scripts/terraform/resources/output.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ const OUTPUT = {
1212
Help('Missing TFC_WORKSPACE.');
1313
}
1414
const state = await Request('app.terraform.io', 'GET', `/api/v2/workspaces/${env.secrets.TFC_WORKSPACE}/current-state-version`, {
15-
'Authorization': `Bearer ${env.secrets.TF_TOKEN}`
15+
'Authorization': `Bearer ${env.secrets.TFC_TOKEN}`
1616
});
1717

1818
console.log(state);
1919

2020
const output_id = JSON.parse(state).data.relationships.outputs.data[0].id;
2121

2222
let outputs = await Request('app.terraform.io', 'GET', `/api/v2/state-version-outputs/${output_id}`, {
23-
'Authorization': `Bearer ${env.secrets.TF_TOKEN}`
23+
'Authorization': `Bearer ${env.secrets.TFC_TOKEN}`
2424
});
2525

2626
outputs = JSON.parse(outputs).data.attributes.value;
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const Environment = require("../../shared/environment");
2+
const Request = require("../../shared/request");
3+
4+
const WORKSPACE = {
5+
get: async (args) => {
6+
if (!args[0]) {
7+
throw 'Workspace Name Missing'
8+
}
9+
10+
const [org, ws] = args[0].split('/');
11+
12+
const workspaces = await WORKSPACE.list([org], true);
13+
14+
const returnValue = workspaces.find(workspace => {
15+
return workspace.attributes.name.toLowerCase() === ws.toLowerCase();
16+
});
17+
return returnValue;
18+
},
19+
20+
list: async (args, returnOnly) => {
21+
if (!args[0]) {
22+
throw 'Organisation Missing'
23+
}
24+
const org = args[0];
25+
26+
let response = await Request('app.terraform.io', 'GET', `/api/v2/organizations/${org}/workspaces`, {
27+
'Authorization': `Bearer ${Environment().secrets.TFC_TOKEN}`
28+
})
29+
response = JSON.parse(response);
30+
if (response.errors) {
31+
response.errors.forEach(error => {
32+
console.log(`error: [${error.status}] ${error.title}`);
33+
});
34+
throw "";
35+
}
36+
if (!returnOnly) {
37+
response.data.forEach(workspace => {
38+
console.log(`${workspace.id}\t${workspace.attributes.name} (${workspace.attributes.environment}) - ${workspace.attributes.description}`);
39+
})
40+
} else {
41+
return response.data;
42+
}
43+
}
44+
}
45+
46+
module.exports = WORKSPACE;

0 commit comments

Comments
 (0)