Skip to content

Commit 7f0fb6a

Browse files
authored
Configuration.tfvars (#26)
* 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!
1 parent c64461b commit 7f0fb6a

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.8.4
2+
current_version = 0.9.0
33
commit = True
44
tag = False
55

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def parse_requirements(req_file):
2525
if __name__ == "__main__":
2626
setup(
2727
name="terraform-ci",
28-
version="0.8.4",
28+
version="0.9.0",
2929
description="Terraform CI runs terraform in Travis-CI",
3030
long_description=dedent(
3131
"""

terraform_ci/__init__.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import boto3
1111

1212

13-
__version__ = "0.8.4"
13+
__version__ = "0.9.0"
1414

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

394394
def terraform_output(path):
395395
"""
396-
Run terraform output and return the json results as a dict
396+
Run terraform output and return the json results as a dict.
397+
397398
:param path: Path to directory with terraform module.
398399
:type path: str
399400
:return: dict from terraform output
401+
:rtype: dict
400402
"""
401403
cmd = "terraform output -json"
402404
ret, cout, cerr = execute(cmd.split(), stdout=PIPE, stderr=None, cwd=path)
@@ -406,7 +408,12 @@ def terraform_output(path):
406408

407409

408410
@contextmanager
409-
def terraform_apply(path, destroy_after=True, json_output=False):
411+
def terraform_apply(
412+
path, # pylint: disable=bad-continuation
413+
destroy_after=True, # pylint: disable=bad-continuation
414+
json_output=False, # pylint: disable=bad-continuation
415+
var_file="configuration.tfvars", # pylint: disable=bad-continuation
416+
):
410417
"""
411418
Run terraform init and apply, then return a generator.
412419
If destroy_after is True, run terraform destroy afterwards.
@@ -417,6 +424,8 @@ def terraform_apply(path, destroy_after=True, json_output=False):
417424
:type destroy_after: bool
418425
:param json_output: Yield terraform output result as a dict (available in the context)
419426
:type json_output: bool
427+
:param var_file: Path to a file with terraform variables.
428+
:type var_file: str
420429
:return: If json_output is true then yield the result from terraform_output otherwise nothing.
421430
Use it in the ``with`` block.
422431
:raise CalledProcessError: if either of terraform commands (except ``terraform destroy``)
@@ -426,8 +435,8 @@ def terraform_apply(path, destroy_after=True, json_output=False):
426435
"terraform init -no-color",
427436
"terraform get -update=true -no-color",
428437
(
429-
"terraform apply -var-file=configuration.tfvars -input=false "
430-
"-auto-approve"
438+
"terraform apply -var-file={var_file} -input=false "
439+
"-auto-approve".format(var_file=var_file)
431440
),
432441
]
433442
try:
@@ -445,8 +454,8 @@ def terraform_apply(path, destroy_after=True, json_output=False):
445454
finally:
446455
if destroy_after:
447456
execute(
448-
"terraform destroy -var-file=configuration.tfvars "
449-
"-input=false -auto-approve".split(),
457+
"terraform destroy -var-file={var_file} "
458+
"-input=false -auto-approve".format(var_file=var_file).split(),
450459
stdout=None,
451460
stderr=None,
452461
cwd=path,

0 commit comments

Comments
 (0)