Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pull request to add terraform issue #107 #118

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ const commands = prog
.option('--include-modules [modules]', 'List of modules to run', prog.LIST, [])
.option('--custom-config', 'Custom configuration', prog.LIST, [])
.option('--tf-vars', 'Extend terraform variables', prog.LIST, [])
.option('--tf-workspace', 'Terraform workspace', /.*/, undefined)
.option('--tf-varfiles', 'Terraform variables', prog.LIST, [])
.option('--tf-version', 'Terraform version', /.*/, undefined)
.option('-s <component>', 'Skip component', prog.REPEATABLE)
.complete(() => [ 'preprocess', 'cache', 'emit', 'npm', 'e2e', 'test', 'coverage' ])
.option('-c <component>', 'Use 3\'rd party component', prog.REPEATABLE)
Expand Down
40 changes: 40 additions & 0 deletions bin/commands/run/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,46 @@ module.exports = (args, options, logger) => {
}
}

if (options.tfWorkspace) {
for (let module in modules) {

dot.str(`${modules[module]}.terraform.current_workspace`, trimBoth(options.tfWorkspace, '"'), config);

if (config[modules[module]]['terraform']['available_workspaces']) {
let availableWorkspaces = config[modules[module]]['terraform']['available_workspaces'];

for (let property in availableWorkspaces[options.tfWorkspace]) {

if (availableWorkspaces[options.tfWorkspace].hasOwnProperty(property)) {
dot.str(`${modules[module]}.terraform.${property}`, availableWorkspaces[options.tfWorkspace][property], config);
}

}

}

}
}

if (options.tfVarfiles.length > 0) {
for (let module in modules) {
dot.str(`${modules[module]}.terraform.var-files`, [], config);
for (let property in options.tfVarfiles) {

if (options.tfVarfiles.hasOwnProperty(property)) {
dot.str(`${modules[module]}.terraform.var-files.${property}`, trimBoth(options.tfVarfiles[property], '"'), config);
}

}
}
}

if (options.tfVersion) {
for (let module in modules) {
dot.str(`${modules[module]}.terraform.version`, options.tfVersion, config);
}
}

let excludeModules = cleanList(options.excludeModules, modules);
let includeModules = cleanList(options.includeModules, modules);

Expand Down
18 changes: 18 additions & 0 deletions components/terraform/src/terraform-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ class TerraformComponent extends DependantConfigBasedComponent {

return terraform.ensure(version)
.then(() => this._init(terraform, emitModule))
.then(() => this._workspace(terraform,emitModule))
.then(() => this._plan(terraform, emitModule))
.then(() => this._apply(terraform, emitModule))
.then(() => this._destroy(terraform, emitModule));
Expand Down Expand Up @@ -474,6 +475,23 @@ class TerraformComponent extends DependantConfigBasedComponent {
.catch(error => this._handleError(emitModule, 'init', error));
}

/**
* @param {Terraform} terraform
* @param {EmitModule} emitModule
* @returns {Promise}
* @private
*/
_workspace(terraform, emitModule){
let workspace = this._parameterFromConfig(emitModule, 'current_workspace', 'default')
this.logger.info(
this.logger.emoji.magic,
`Running "terraform workspace" in "${ emitModule.name }".`
);
return terraform
.workspace(this._moduleRoot(emitModule), workspace)
.catch(error => this._handleError(emitModule, 'workspace', error));
}

/**
* @param {Terraform} terraform
* @param {EmitModule} emitModule
Expand Down
25 changes: 25 additions & 0 deletions components/terraform/src/terraform.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,31 @@ class Terraform {
.then(() => Promise.resolve());
}

/**
* https://www.terraform.io/docs/state/workspaces.html
* @param {string} dir
* @param {string} workspace
* @returns {Promise}
*/
workspace(dir, workspace) {
return this._ensureResourceDir(dir).then(() => {
const regex = RegExp('(\\*\\s|\\s.)' + workspace + '$','m');
let options;
this.run('workspace', ['list'], dir).then(result => {
if (regex.exec(result.output) != null){
options = ['select', workspace,'-no-color'];
} else {
options = ['new', workspace,'-no-color'];
}
if (fse.existsSync(`${dir}/terraform.tfstate.d`)) {
this._resource = 'terraform.tfstate.d/' + workspace;
}
return this.run('workspace', options, dir)
});
});
}


/**
* Check if remote state configured
* @param {String} dir
Expand Down