From 57f8503f7ebc413ebce1f3eccec12af11ab9b80e Mon Sep 17 00:00:00 2001 From: Balwant Date: Tue, 24 Aug 2021 19:21:18 +1000 Subject: [PATCH 1/2] docs: added install instruction in Readme.md --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e381f51..f00ed6e 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,25 @@ # Devops Scripts Collection of scripts to ease out some tasks duriing devops pipelines. +## Instalation + +```shell +npm i devops-scripts +``` +Or Install globally +> Recomended way is to install this module globally in your dev or ci/cd environment. +```shell +npm i -g devops-scripts +``` + --- ## `terraform-cloud` / `tfc` Terraform cloud scripts +### Environment Variables + - `TFC_TOKEN` : Terraform cloud Access Token + - `TFC_WORKSPACE` : Terraform cloud worksapce id. e.g. `ws-xxxxxxxxxx` -Usage: +### Usage: ```shell terraform-cloud [arguments] From 596f680bc71932f58d8c8d744ebc056f3baf52e2 Mon Sep 17 00:00:00 2001 From: Balwant Date: Tue, 24 Aug 2021 23:19:56 +1000 Subject: [PATCH 2/2] feat(tfc-workspace): added terraform cloud workspace command --- README.md | 6 ++-- scripts/shared/environment.js | 4 +-- scripts/terraform/cli.js | 20 +++++++---- scripts/terraform/help.js | 4 +-- scripts/terraform/resources/output.js | 4 +-- scripts/terraform/resources/workspace.js | 46 ++++++++++++++++++++++++ 6 files changed, 70 insertions(+), 14 deletions(-) create mode 100755 scripts/terraform/resources/workspace.js diff --git a/README.md b/README.md index f00ed6e..994e2ee 100644 --- a/README.md +++ b/README.md @@ -22,18 +22,20 @@ Terraform cloud scripts ### Usage: ```shell -terraform-cloud [arguments] +terraform-cloud [arguments] ``` OR ```shell -tfc [arguments] +tfc [arguments] ``` ### Available Allowed Resources: + - `workspace` - `output` - `state` ### Available Allowed Actions: - `get` - `set` + - `list` --- \ No newline at end of file diff --git a/scripts/shared/environment.js b/scripts/shared/environment.js index ecfe7fa..14656ce 100644 --- a/scripts/shared/environment.js +++ b/scripts/shared/environment.js @@ -9,8 +9,8 @@ const Environment = (render) => { TFC_TOKEN: process.env.TFC_TOKEN, TFC_WORKSPACE: process.env.TFC_WORKSPACE, }, - action: process.argv[2] || '', - resource: process.argv[3] || '', + action: process.argv[3] || '', + resource: process.argv[2] || '', args: process.argv.slice(4) } if (render) { diff --git a/scripts/terraform/cli.js b/scripts/terraform/cli.js index 775b5a7..0b71261 100755 --- a/scripts/terraform/cli.js +++ b/scripts/terraform/cli.js @@ -3,6 +3,7 @@ const Environment = require("../shared/environment"); const Help = require("./help"); const OUTPUT = require("./resources/output"); const STATE = require("./resources/state"); +const WORKSPACE = require("./resources/workspace"); Object.byString = function(o, s) { s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties @@ -19,14 +20,16 @@ Object.byString = function(o, s) { return o; } -const env = Environment(true); +const env = Environment(); const ACTIONS = [ 'get', - 'set' + 'set', + 'list' ] const RESOURCES = [ + 'workspace', 'output', 'state' ] @@ -38,19 +41,24 @@ if ( try { let mod = null switch (env.resource) { + case 'workspace': + mod = WORKSPACE; + break; case 'output': - mod = OUTPUT + mod = OUTPUT; break; case 'state': - mod = STATE + mod = STATE; break; default: break; } if (mod && typeof mod[env.action]) { - mod[env.action](env.args).then(()=> { - console.log("ok"); + mod[env.action](env.args).then((value)=> { + if (value) { + console.log(JSON.stringify(value, null, ' ')); + } }).catch(err => { Help(err); }); diff --git a/scripts/terraform/help.js b/scripts/terraform/help.js index 1bbf12b..3e3721b 100644 --- a/scripts/terraform/help.js +++ b/scripts/terraform/help.js @@ -9,9 +9,9 @@ function Help(errorMessage) { Github: https://github.com/phenixcoder/devops-scripts Usage: - terraform-cloud [arguments] + terraform-cloud [arguments] OR - tfc [arguments] + tfc [arguments] `); } diff --git a/scripts/terraform/resources/output.js b/scripts/terraform/resources/output.js index d4d3fdb..6e89885 100755 --- a/scripts/terraform/resources/output.js +++ b/scripts/terraform/resources/output.js @@ -12,7 +12,7 @@ const OUTPUT = { Help('Missing TFC_WORKSPACE.'); } const state = await Request('app.terraform.io', 'GET', `/api/v2/workspaces/${env.secrets.TFC_WORKSPACE}/current-state-version`, { - 'Authorization': `Bearer ${env.secrets.TF_TOKEN}` + 'Authorization': `Bearer ${env.secrets.TFC_TOKEN}` }); console.log(state); @@ -20,7 +20,7 @@ const OUTPUT = { const output_id = JSON.parse(state).data.relationships.outputs.data[0].id; let outputs = await Request('app.terraform.io', 'GET', `/api/v2/state-version-outputs/${output_id}`, { - 'Authorization': `Bearer ${env.secrets.TF_TOKEN}` + 'Authorization': `Bearer ${env.secrets.TFC_TOKEN}` }); outputs = JSON.parse(outputs).data.attributes.value; diff --git a/scripts/terraform/resources/workspace.js b/scripts/terraform/resources/workspace.js new file mode 100755 index 0000000..007a4be --- /dev/null +++ b/scripts/terraform/resources/workspace.js @@ -0,0 +1,46 @@ +const Environment = require("../../shared/environment"); +const Request = require("../../shared/request"); + +const WORKSPACE = { + get: async (args) => { + if (!args[0]) { + throw 'Workspace Name Missing' + } + + const [org, ws] = args[0].split('/'); + + const workspaces = await WORKSPACE.list([org], true); + + const returnValue = workspaces.find(workspace => { + return workspace.attributes.name.toLowerCase() === ws.toLowerCase(); + }); + return returnValue; + }, + + list: async (args, returnOnly) => { + if (!args[0]) { + throw 'Organisation Missing' + } + const org = args[0]; + + let response = await Request('app.terraform.io', 'GET', `/api/v2/organizations/${org}/workspaces`, { + 'Authorization': `Bearer ${Environment().secrets.TFC_TOKEN}` + }) + response = JSON.parse(response); + if (response.errors) { + response.errors.forEach(error => { + console.log(`error: [${error.status}] ${error.title}`); + }); + throw ""; + } + if (!returnOnly) { + response.data.forEach(workspace => { + console.log(`${workspace.id}\t${workspace.attributes.name} (${workspace.attributes.environment}) - ${workspace.attributes.description}`); + }) + } else { + return response.data; + } + } +} + +module.exports = WORKSPACE; \ No newline at end of file