diff --git a/terraform_compliance/common/exceptions.py b/terraform_compliance/common/exceptions.py new file mode 100644 index 00000000..18e14396 --- /dev/null +++ b/terraform_compliance/common/exceptions.py @@ -0,0 +1,4 @@ + + +class TerraformComplianceInvalidConfig(Exception): + pass diff --git a/terraform_compliance/common/pyhcl_helper.py b/terraform_compliance/common/pyhcl_helper.py index 08f4d4e2..6b52b6ba 100644 --- a/terraform_compliance/common/pyhcl_helper.py +++ b/terraform_compliance/common/pyhcl_helper.py @@ -2,6 +2,7 @@ from os.path import isdir from terraform_compliance import Validator from terraform_validate.terraform_validate import TerraformSyntaxException +from terraform_compliance.common.exceptions import TerraformComplianceInvalidConfig from shutil import rmtree @@ -16,14 +17,18 @@ def load_tf_files(tf_directory): try: Validator(tf_directory) print('All HCL files look good.') - result = True + return True except ValueError: print('Unable to validate Terraform Files.') print('ERROR: {}'.format(exc_info()[1])) exit(1) + except TerraformSyntaxException: - pad_invalid_tf_files(exc_info()[1]) + result = pad_invalid_tf_files(exc_info()[1]) is True + + except TerraformComplianceInvalidConfig: + pass return result @@ -35,10 +40,12 @@ def pad_invalid_tf_files(exception_message): print('Invalid HCL file: {}. Fixing it.'.format(filename)) pad_tf_file(filename) return True + elif 'Invalid terraform configuration in ' in exception_message[0]: + raise TerraformComplianceInvalidConfig('ERROR: Invalid terraform configuration {}'.format(exception_message[1])) return False def pad_tf_file(file): with open(file, 'a') as f: - f.write('variable {}') + f.write('\n\nvariable {}') diff --git a/terraform_compliance/main.py b/terraform_compliance/main.py index 3df53423..c9008acb 100644 --- a/terraform_compliance/main.py +++ b/terraform_compliance/main.py @@ -1,20 +1,20 @@ import os from argparse import ArgumentParser +from sys import exc_info, exit, executable try: from radish.main import main as call_radish except ImportError as e: import subprocess - import sys def pip(action, package, params=None): print '{}ing {}..'.format(action, package) if action == 'uninstall': - cmds = [sys.executable, "-m", "pip", action, '--yes', package] + cmds = [executable, "-m", "pip", action, '--yes', package] else: - cmds = [sys.executable, "-m", "pip", action, package] + cmds = [executable, "-m", "pip", action, package] subprocess.call(cmds) @@ -27,7 +27,7 @@ def pip(action, package, params=None): print "~"*40 print " Please run terraform-compliance again." print "~"*40 - sys.exit(1) + exit(1) from tempfile import mkdtemp from git import Repo @@ -35,10 +35,11 @@ def pip(action, package, params=None): from distutils.dir_util import copy_tree from shutil import rmtree from terraform_compliance.common.readable_dir import ReadableDir +from terraform_compliance.common.exceptions import TerraformComplianceInvalidConfig __app_name__ = "terraform-compliance" -__version__ = "0.4.8" +__version__ = "0.4.9" class ArgHandling(object): @@ -100,7 +101,12 @@ def cli(arghandling=ArgHandling(), argparser=ArgumentParser(prog=__app_name__, '--user-data=tf_dir={}'.format(tf_directory)] commands.extend(radish_arguments) - load_tf_files(tf_directory) + try: + load_tf_files(tf_directory) + except TerraformComplianceInvalidConfig: + print exc_info()[1] + exit(1) + print('Running tests.') result = call_radish(args=commands[1:]) diff --git a/tests/terraform_compliance/common/test_pyhcl_helper.py b/tests/terraform_compliance/common/test_pyhcl_helper.py index 5d2b934d..76df30d6 100644 --- a/tests/terraform_compliance/common/test_pyhcl_helper.py +++ b/tests/terraform_compliance/common/test_pyhcl_helper.py @@ -16,7 +16,7 @@ def test_pad_tf_file(self): pad_tf_file(tmpFile) contents = open(tmpFile, 'r').read() remove(tmpFile) - self.assertEqual(contents, 'variable {}') + self.assertEqual(contents, '\n\nvariable {}') @patch('terraform_compliance.common.pyhcl_helper.pad_tf_file', return_value=None) def test_pad_invalid_tf_files(self, *args):