Skip to content

Commit

Permalink
Configuration.tfvars (#26)
Browse files Browse the repository at this point in the history
* var_file argument to terraform_apply()

By default terraform_apply() read terraform variables from a file `configuration.tfvars` in the same directory as `path`.
Argument `var_file` allows to specify where terraform should read variables from.

* Bump version: 0.8.4 → 0.9.0

* Make black and pylint happy

This is funny - black makes changes pylint doesn't like.
There is an open bug pylint-dev/pylint#289 which is unlikely to be fixed since it hasn't been closed for 5+ years.

There can be only one!
  • Loading branch information
akuzminsky committed Dec 25, 2019
1 parent c64461b commit 7f0fb6a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.8.4
current_version = 0.9.0
commit = True
tag = False

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -25,7 +25,7 @@ def parse_requirements(req_file):
if __name__ == "__main__":
setup(
name="terraform-ci",
version="0.8.4",
version="0.9.0",
description="Terraform CI runs terraform in Travis-CI",
long_description=dedent(
"""
Expand Down
23 changes: 16 additions & 7 deletions terraform_ci/__init__.py
Expand Up @@ -10,7 +10,7 @@
import boto3


__version__ = "0.8.4"
__version__ = "0.9.0"

DEFAULT_TERRAFORM_VARS = ".env/tf_env.json"
LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -393,10 +393,12 @@ def setup_logging(logger, debug=False): # pragma: no cover

def terraform_output(path):
"""
Run terraform output and return the json results as a dict
Run terraform output and return the json results as a dict.
:param path: Path to directory with terraform module.
:type path: str
:return: dict from terraform output
:rtype: dict
"""
cmd = "terraform output -json"
ret, cout, cerr = execute(cmd.split(), stdout=PIPE, stderr=None, cwd=path)
Expand All @@ -406,7 +408,12 @@ def terraform_output(path):


@contextmanager
def terraform_apply(path, destroy_after=True, json_output=False):
def terraform_apply(
path, # pylint: disable=bad-continuation
destroy_after=True, # pylint: disable=bad-continuation
json_output=False, # pylint: disable=bad-continuation
var_file="configuration.tfvars", # pylint: disable=bad-continuation
):
"""
Run terraform init and apply, then return a generator.
If destroy_after is True, run terraform destroy afterwards.
Expand All @@ -417,6 +424,8 @@ def terraform_apply(path, destroy_after=True, json_output=False):
:type destroy_after: bool
:param json_output: Yield terraform output result as a dict (available in the context)
:type json_output: bool
:param var_file: Path to a file with terraform variables.
:type var_file: str
:return: If json_output is true then yield the result from terraform_output otherwise nothing.
Use it in the ``with`` block.
:raise CalledProcessError: if either of terraform commands (except ``terraform destroy``)
Expand All @@ -426,8 +435,8 @@ def terraform_apply(path, destroy_after=True, json_output=False):
"terraform init -no-color",
"terraform get -update=true -no-color",
(
"terraform apply -var-file=configuration.tfvars -input=false "
"-auto-approve"
"terraform apply -var-file={var_file} -input=false "
"-auto-approve".format(var_file=var_file)
),
]
try:
Expand All @@ -445,8 +454,8 @@ def terraform_apply(path, destroy_after=True, json_output=False):
finally:
if destroy_after:
execute(
"terraform destroy -var-file=configuration.tfvars "
"-input=false -auto-approve".split(),
"terraform destroy -var-file={var_file} "
"-input=false -auto-approve".format(var_file=var_file).split(),
stdout=None,
stderr=None,
cwd=path,
Expand Down

0 comments on commit 7f0fb6a

Please sign in to comment.