From b415e775890367a28e63ff1090351c83f636dee4 Mon Sep 17 00:00:00 2001 From: Aleksandr Kuzminsky Date: Wed, 25 Dec 2019 08:43:18 -0800 Subject: [PATCH 1/3] 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. --- terraform_ci/__init__.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/terraform_ci/__init__.py b/terraform_ci/__init__.py index de6b8b8..374ddb3 100644 --- a/terraform_ci/__init__.py +++ b/terraform_ci/__init__.py @@ -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) @@ -406,7 +408,9 @@ def terraform_output(path): @contextmanager -def terraform_apply(path, destroy_after=True, json_output=False): +def terraform_apply( + path, destroy_after=True, json_output=False, var_file="configuration.tfvars" +): """ Run terraform init and apply, then return a generator. If destroy_after is True, run terraform destroy afterwards. @@ -417,6 +421,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``) @@ -426,8 +432,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: @@ -445,8 +451,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, From 778d1a63f46c80cd5805ebe4318ffada347abe50 Mon Sep 17 00:00:00 2001 From: Aleksandr Kuzminsky Date: Wed, 25 Dec 2019 08:46:29 -0800 Subject: [PATCH 2/3] =?UTF-8?q?Bump=20version:=200.8.4=20=E2=86=92=200.9.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setup.cfg | 2 +- setup.py | 2 +- terraform_ci/__init__.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/setup.cfg b/setup.cfg index b6d2d5c..097a727 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.8.4 +current_version = 0.9.0 commit = True tag = False diff --git a/setup.py b/setup.py index 2854ca3..3aa8dfc 100644 --- a/setup.py +++ b/setup.py @@ -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( """ diff --git a/terraform_ci/__init__.py b/terraform_ci/__init__.py index 374ddb3..88feee0 100644 --- a/terraform_ci/__init__.py +++ b/terraform_ci/__init__.py @@ -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__) From 3882e11a84755fa75d8d0e61b68112b8d9a4cf50 Mon Sep 17 00:00:00 2001 From: Aleksandr Kuzminsky Date: Wed, 25 Dec 2019 08:52:54 -0800 Subject: [PATCH 3/3] Make black and pylint happy This is funny - black makes changes pylint doesn't like. There is an open bug https://github.com/PyCQA/pylint/issues/289 which is unlikely to be fixed since it hasn't been closed for 5+ years. There can be only one! --- terraform_ci/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/terraform_ci/__init__.py b/terraform_ci/__init__.py index 88feee0..0a63c3f 100644 --- a/terraform_ci/__init__.py +++ b/terraform_ci/__init__.py @@ -409,7 +409,10 @@ def terraform_output(path): @contextmanager def terraform_apply( - path, destroy_after=True, json_output=False, var_file="configuration.tfvars" + 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.