Skip to content

Commit c64461b

Browse files
authored
Merge pull request #25 from tamaskozak/terraform-output-json1
Terraform output json1
2 parents 5369a3b + 9df0942 commit c64461b

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
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.3
2+
current_version = 0.8.4
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.3",
28+
version="0.8.4",
2929
description="Terraform CI runs terraform in Travis-CI",
3030
long_description=dedent(
3131
"""

terraform_ci/__init__.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
import boto3
1111

12-
__version__ = "0.8.3"
12+
13+
__version__ = "0.8.4"
1314

1415
DEFAULT_TERRAFORM_VARS = ".env/tf_env.json"
1516
LOG = logging.getLogger(__name__)
@@ -390,8 +391,22 @@ def setup_logging(logger, debug=False): # pragma: no cover
390391
logger.setLevel(logging.DEBUG)
391392

392393

394+
def terraform_output(path):
395+
"""
396+
Run terraform output and return the json results as a dict
397+
:param path: Path to directory with terraform module.
398+
:type path: str
399+
:return: dict from terraform output
400+
"""
401+
cmd = "terraform output -json"
402+
ret, cout, cerr = execute(cmd.split(), stdout=PIPE, stderr=None, cwd=path)
403+
if ret:
404+
raise CalledProcessError(returncode=ret, cmd=cmd, output=cout, stderr=cerr)
405+
return json.loads(cout)
406+
407+
393408
@contextmanager
394-
def terraform_apply(path, destroy_after=True):
409+
def terraform_apply(path, destroy_after=True, json_output=False):
395410
"""
396411
Run terraform init and apply, then return a generator.
397412
If destroy_after is True, run terraform destroy afterwards.
@@ -400,8 +415,10 @@ def terraform_apply(path, destroy_after=True):
400415
:type path: str
401416
:param destroy_after: Run terraform destroy after context it returned back.
402417
:type destroy_after: bool
403-
:return: Nothing. The function just yields to use it in the ``with``
404-
block.
418+
:param json_output: Yield terraform output result as a dict (available in the context)
419+
:type json_output: bool
420+
:return: If json_output is true then yield the result from terraform_output otherwise nothing.
421+
Use it in the ``with`` block.
405422
:raise CalledProcessError: if either of terraform commands (except ``terraform destroy``)
406423
exits with non-zero.
407424
"""
@@ -420,7 +437,10 @@ def terraform_apply(path, destroy_after=True):
420437
raise CalledProcessError(
421438
returncode=ret, cmd=cmd, output=cout, stderr=cerr
422439
)
423-
yield
440+
if json_output:
441+
yield terraform_output(path)
442+
else:
443+
yield
424444

425445
finally:
426446
if destroy_after:

0 commit comments

Comments
 (0)