Skip to content

Commit

Permalink
BUGFIX: Fixed the issue where terraform-compliance was hanging (#47)
Browse files Browse the repository at this point in the history
* #46: Handled exceptions properly for the invalid terraform configurations where it was hanging before. Thanks to @joshpavel

* Fixed a general issue where main.tf is full of comments without a new line in the EOF.
  • Loading branch information
eerkunt authored Nov 27, 2018
1 parent 4bd0a90 commit ba28690
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
4 changes: 4 additions & 0 deletions terraform_compliance/common/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@


class TerraformComplianceInvalidConfig(Exception):
pass
13 changes: 10 additions & 3 deletions terraform_compliance/common/pyhcl_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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

Expand All @@ -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 {}')
18 changes: 12 additions & 6 deletions terraform_compliance/main.py
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -27,18 +27,19 @@ 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
from terraform_compliance.common.pyhcl_helper import load_tf_files
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):
Expand Down Expand Up @@ -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:])

Expand Down
2 changes: 1 addition & 1 deletion tests/terraform_compliance/common/test_pyhcl_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit ba28690

Please sign in to comment.