Skip to content

Commit

Permalink
Fixed a few issues with --apply and getTargets()
Browse files Browse the repository at this point in the history
  • Loading branch information
nitrocode committed Jan 9, 2019
1 parent 7926a79 commit a8d0e30
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 154 deletions.
27 changes: 4 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Setup `apply.yml`

The following will allow an asg named blue for any service to have an additional subnet using the regex for new. Since it's adding the subnet, the old value will be empty. Running the script using this yaml file will return a list of targets or it can be configured to run `terraform apply -auto-approve`. If any of the attribute criteria fails, the target resource will be omitted.

```
```yaml
changedResources:
- path: 'module\..*\.module\.service\.module\.blue\.aws_autoscaling_group\..*'
attributes:
Expand All @@ -22,7 +22,7 @@ changedResources:
new: subnet-ac7b95a3|subnet-d74eeda1
```
```
```bash
$ terraform-plan-applier -h
Usage: terraform-plan-applier [options]

Expand Down Expand Up @@ -50,28 +50,9 @@ If you have time, please contribute! Standard fork, branch, PR model.
## TODO
### High

* [x] read yml apply config
* [x] at the end should collect all validated targets and run
* [x] check for modified asg.default.image_id that matches a value
* [x] read plan from stdin
* [x] cli args
* [x] required argument to read from the apply file
* [x] help info with examples
* [x] option for dryrun where it shows the cmd but doesnt run it
* [x] option for verbosity
* [x] run `terraform plan / apply` commands on targets
* [x] package into installable cli app
* [x] installable via npm
* [x] ensure that targets do not include other targets when planning
* terraform plan first with the found targets and make sure the returned targets match. if not, do not apply.
* [x] option to `-auto-approve` by using `--apply`

### Low

* [ ] option to save plan output check to local file
* [ ] option to save applied output to local file
* [ ] allow action to be set e.g. update, replace, destroy
* also account for plan numbers like `Plan: 2 to add, 2 to change, 2 to destroy.` for more precise matching. If this fails, do not continue.
* [ ] investigate support for module / type attribute searching
* how much more value does this bring?
* [ ] support json file type in addition to yaml for config criteria
File renamed without changes.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "terraform-plan-applier",
"version": "0.2.0",
"version": "0.2.1",
"repository": {
"type": "git",
"url": "https://github.com/nitrocode/terraform-plan-applier"
Expand All @@ -14,6 +14,7 @@
"terraform",
"terraform-plan",
"terraform-apply",
"terraform-plan-applier",
"terraform-applier",
"terraform-parser"
],
Expand Down
19 changes: 5 additions & 14 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@ const createLogger = require('./logger');
// the meat
const applier = require('./terraform-plan-apply');

/**
* Wrapper around terraform-plan-apply with a catch statement
*
* @param {string} stdin standard input
* @param {object} program commander parsed object
*/
function apply(stdin, program) {
return applier(stdin, program)
.catch(console.error);
}

/**
* Read the yaml file safely and destructure it into an object
*
Expand Down Expand Up @@ -91,10 +80,12 @@ if (!program.plan) {
});
// once stdin is finished, call main
process.stdin.on('end', () => {
apply(stdin, configFile, applyOption, verbose);
applier(stdin, configFile, applyOption, verbose)
.catch(console.error);
});
} else {
// read the plan file
const stdin = fs.readFileSync(program.plan, { encoding: 'utf8' });
apply(stdin, configFile, applyOption, verbose);
}
applier(stdin, configFile, applyOption, verbose)
.catch(console.error);
}
7 changes: 3 additions & 4 deletions src/terraform-plan-apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function getTargets(plan, apply) {
// then do an early return
if ((planAttribKey.match(validAttrib.name))
// and there is a change in values and
&& (planAttrib.old.value !== planAttrib.new.value)
&& ('old' in planAttrib && planAttrib.old.value !== planAttrib.new.value)
// and values are NOT validated
&& (!validateAttributeValues(validAttrib, planAttrib))) {
logger.info('This target was rejected for one of these reasons.');
Expand All @@ -74,7 +74,7 @@ function getTargets(plan, apply) {
}
}
} else {
logger.log('no attributes');
logger.info('no attributes');
}
}
// if all the validation for this target did not fail, then return true
Expand Down Expand Up @@ -149,7 +149,7 @@ async function applier(stdin, config, apply, verbose) {
logger.msg(`Rerunning plan with matching targets (${targets.length}) to confirm targets...`);
logger.msg(`\n\t$ ${cmd}\n`);
return terraformExec(cmd)
.then(async (output) => {
.then((output) => {
console.log(output.stdout);
// check if the targets are the same
targetsCheck = getTargets(parser.parseStdout(output.stdout), config);
Expand All @@ -163,7 +163,6 @@ async function applier(stdin, config, apply, verbose) {
logger.msg(`Running terraform apply -auto-approve with matching targets...`);
logger.msg(`\n\t$ ${cmd}\n`);
// only apply and auto approve if option has been set
// if ('apply' in program) {
if (apply) {
return terraformExec(cmd)
.then((output) => {
Expand Down
3 changes: 0 additions & 3 deletions test/00-terraform-plan/main.tf

This file was deleted.

109 changes: 0 additions & 109 deletions test/00-terraform-plan/terraform-plan.stdout

This file was deleted.

0 comments on commit a8d0e30

Please sign in to comment.