9
9
10
10
import boto3
11
11
12
- __version__ = "0.8.3"
12
+
13
+ __version__ = "0.8.4"
13
14
14
15
DEFAULT_TERRAFORM_VARS = ".env/tf_env.json"
15
16
LOG = logging .getLogger (__name__ )
@@ -390,8 +391,22 @@ def setup_logging(logger, debug=False): # pragma: no cover
390
391
logger .setLevel (logging .DEBUG )
391
392
392
393
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
+
393
408
@contextmanager
394
- def terraform_apply (path , destroy_after = True ):
409
+ def terraform_apply (path , destroy_after = True , json_output = False ):
395
410
"""
396
411
Run terraform init and apply, then return a generator.
397
412
If destroy_after is True, run terraform destroy afterwards.
@@ -400,8 +415,10 @@ def terraform_apply(path, destroy_after=True):
400
415
:type path: str
401
416
:param destroy_after: Run terraform destroy after context it returned back.
402
417
: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.
405
422
:raise CalledProcessError: if either of terraform commands (except ``terraform destroy``)
406
423
exits with non-zero.
407
424
"""
@@ -420,7 +437,10 @@ def terraform_apply(path, destroy_after=True):
420
437
raise CalledProcessError (
421
438
returncode = ret , cmd = cmd , output = cout , stderr = cerr
422
439
)
423
- yield
440
+ if json_output :
441
+ yield terraform_output (path )
442
+ else :
443
+ yield
424
444
425
445
finally :
426
446
if destroy_after :
0 commit comments