terraform-plus
Version:
Terraform Plus
131 lines (116 loc) • 3.5 kB
JavaScript
;
const treeify = require('treeify');
const fs = require('fs');
const yaml = require('js-yaml');
const ProgressBar = require('progress');
const TerraformCommand = require('../terraform-command');
const Terraform = require('../terraform/terraform');
class RefreshCommand extends TerraformCommand {
/**
* @desc Validates options passed via cli
*
* @returns {Boolean} - Returns true if options given from cli are valid.
*
* @private
*/
_validateOptions() {
return super._validateOptions();
}
/**
* @desc Executes `terraform refresh` across multiple terraform scripts
*/
run() {
this._layers=[[]];
this._logger.debug(`templates : ${this.templates}`);
this._folders = {};
this._graph = {};
this.getFilesPath([this._directory]).forEach(element => {
if (fs.existsSync(element)) {
let tfsConf = yaml.safeLoad(fs.readFileSync(`${element}/${this._paramFile}`, 'utf8'));
this._folders[tfsConf.id] = element;
if (tfsConf && tfsConf.parent) {
this._graph[tfsConf.id] = tfsConf.parent ;
} else {
if (!this._graph[tfsConf.id]) {
this._graph[tfsConf.id] = null;
this._layers[0].push(tfsConf.id);
}
}
}
});
console.log('Building tree:');
let graph = this._graph;
if (this._cli.branch) {
graph = this.findBranch(this._graph);
console.log(treeify.asTree(this.listToTree(graph), true));
} else {
console.log(treeify.asTree(this.listToTree(graph), true));
}
console.log('Start terraform refresh\n');
//console.log(this._cli.verbose2);
if (!this._verbose) {
this.bar = new ProgressBar('Run refresh [:bar] :percent :elapseds',
{ total: Object.keys(graph).length * 2, width: 30 });
this.bar.tick(0);
}
this.refresh();
return Promise.resolve();
}
/**
*
* @return {Promise<void>}
*/
async refresh() {
let bar = this.bar;
let verbose = this._verbose;
let tf = this.terraform();
for (let id in this._layers) {
let folders = this._folders;
let promises = this._layers[id].map(async function(element) {
let elementPath = folders[element];
if (!verbose) {
bar.tick();
}
await RefreshCommand.execPromise(tf, elementPath);
if (!verbose) {
bar.tick();
}
});
await Promise.all(promises);
}
}
static execPromise(tf, elementPath) {
return new Promise(function(resolve, reject) {
tf.then((terraform) => {
terraform.refresh(elementPath).then(() => {
resolve();
});
});
});
}
/**
* @returns {String}
*/
static get DESCRIPTION() {
return 'run `terraform refresh` across multiple terraform scripts'
}
/**
* @returns {String}
*/
static get OPTIONS() {
return [{
opt: '-p, --provider [provider]',
desc: 'terraform provider name (e.g. aws, azurerm, google)'
}, {
opt: '-i, --include [comma_separated_values]',
desc: 'comma separated values of terraform scripts or folders to be included'
}, {
opt: '-e, --exclude [comma_separated_values]',
desc: 'comma separated values of terraform scripts or folders to be excluded'
}, {
opt: '-d, --directory [directory]',
desc: 'path where terraform will be executed (default value - current working directory)'
}];
}
}
module.exports = RefreshCommand;