From f7691f7d3c083cbf6033726569315d030dca28f5 Mon Sep 17 00:00:00 2001 From: Wythe Lin Date: Tue, 1 Aug 2017 17:51:12 -0700 Subject: [PATCH 01/14] fix https://github.com/beelit94/python-terraform/issues/12 --- python_terraform/__init__.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/python_terraform/__init__.py b/python_terraform/__init__.py index 896d2e0..f1141c4 100644 --- a/python_terraform/__init__.py +++ b/python_terraform/__init__.py @@ -35,7 +35,7 @@ class Terraform(object): var_file=None, terraform_bin_path=None, is_env_vars_included=True): - """ + """ :param working_dir: the folder of the working folder, if not given, will be current working folder :param targets: list of target @@ -49,7 +49,7 @@ class Terraform(object): could be string or list, list stands for multiple -var-file option :param terraform_bin_path: binary path of terraform :type is_env_vars_included: bool - :param is_env_vars_included: included env variables when calling terraform cmd + :param is_env_vars_included: included env variables when calling terraform cmd """ self.is_env_vars_included = is_env_vars_included self.working_dir = working_dir @@ -250,13 +250,19 @@ class Terraform(object): else: return ret_code, None, None - def output(self, name): + def output(self, name, **kwargs): """ https://www.terraform.io/docs/commands/output.html :param name: name of output :return: output value """ - ret, out, err = self.cmd('output', name, json=IsFlagged) + options = kwargs + options['detailed_exitcode'] = detailed_exitcode + options = self._generate_default_options(options) + args = self._generate_default_args(dir_or_plan) + + ret, out, err = self.cmd( + 'output', name, json=IsFlagged, *args, **options) log.debug('output raw string: {0}'.format(out)) if ret != 0: From 6fdd205527ea2ec0212a2c2492044467708a8f61 Mon Sep 17 00:00:00 2001 From: Wythe Lin Date: Wed, 2 Aug 2017 10:29:54 -0700 Subject: [PATCH 02/14] remove --- python_terraform/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python_terraform/__init__.py b/python_terraform/__init__.py index f1141c4..38408c2 100644 --- a/python_terraform/__init__.py +++ b/python_terraform/__init__.py @@ -257,7 +257,6 @@ class Terraform(object): :return: output value """ options = kwargs - options['detailed_exitcode'] = detailed_exitcode options = self._generate_default_options(options) args = self._generate_default_args(dir_or_plan) From 5b6f4c133ed409fa7f586c2b773bfcfe8d14124a Mon Sep 17 00:00:00 2001 From: Wythe Lin Date: Wed, 2 Aug 2017 10:59:01 -0700 Subject: [PATCH 03/14] add dir_or_plan parameter --- python_terraform/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_terraform/__init__.py b/python_terraform/__init__.py index 38408c2..67bbd3b 100644 --- a/python_terraform/__init__.py +++ b/python_terraform/__init__.py @@ -250,7 +250,7 @@ class Terraform(object): else: return ret_code, None, None - def output(self, name, **kwargs): + def output(self, name, dir_or_plan=None, **kwargs): """ https://www.terraform.io/docs/commands/output.html :param name: name of output From 966f2d9f6463798f26cf594a550b538454efbc62 Mon Sep 17 00:00:00 2001 From: Wythe Lin Date: Wed, 2 Aug 2017 15:18:11 -0700 Subject: [PATCH 04/14] remove wrong variables --- python_terraform/__init__.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/python_terraform/__init__.py b/python_terraform/__init__.py index 67bbd3b..78b513c 100644 --- a/python_terraform/__init__.py +++ b/python_terraform/__init__.py @@ -250,15 +250,12 @@ class Terraform(object): else: return ret_code, None, None - def output(self, name, dir_or_plan=None, **kwargs): + def output(self, name, *args, **kwargs): """ https://www.terraform.io/docs/commands/output.html :param name: name of output :return: output value """ - options = kwargs - options = self._generate_default_options(options) - args = self._generate_default_args(dir_or_plan) ret, out, err = self.cmd( 'output', name, json=IsFlagged, *args, **options) From 8214c6ac61d2817122c0a196bc9c95e6cf315add Mon Sep 17 00:00:00 2001 From: Wythe Lin Date: Wed, 2 Aug 2017 16:11:54 -0700 Subject: [PATCH 05/14] fix typo --- python_terraform/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_terraform/__init__.py b/python_terraform/__init__.py index 78b513c..35c6e3b 100644 --- a/python_terraform/__init__.py +++ b/python_terraform/__init__.py @@ -258,7 +258,7 @@ class Terraform(object): """ ret, out, err = self.cmd( - 'output', name, json=IsFlagged, *args, **options) + 'output', name, json=IsFlagged, *args, **kwargs) log.debug('output raw string: {0}'.format(out)) if ret != 0: From 12d57862ed218d593383fd4d8694eedbec3ea9bd Mon Sep 17 00:00:00 2001 From: beelit94 Date: Fri, 4 Aug 2017 14:14:57 -0700 Subject: [PATCH 06/14] add more parameter test for output --- test/test_terraform.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/test/test_terraform.py b/test/test_terraform.py index 0a2487d..78c41e3 100644 --- a/test/test_terraform.py +++ b/test/test_terraform.py @@ -71,9 +71,10 @@ def string_logger(request): def td(): root_logger.removeHandler(handler) + log_stream.close() request.addfinalizer(td) - return log_stream + return lambda: str(log_stream.getvalue()) class TestTerraform(object): @@ -105,7 +106,7 @@ class TestTerraform(object): def test_cmd(self, method, expected_output, expected_ret_code, expected_logs, string_logger): tf = Terraform(working_dir=current_path) ret, out, err = method(tf) - logs = str(string_logger.getvalue()) + logs = string_logger() logs = logs.replace('\n', '') assert expected_output in out assert expected_ret_code == ret @@ -167,10 +168,22 @@ class TestTerraform(object): out = tf.output('test_output') assert 'test2' in out - def test_get_output(self): + @pytest.mark.parametrize( + ("param", "param_str"), + [ + ({}, ""), + ({'module': 'test2'}, "-module=test2"), + ({'module': 'test2', 'no_color': IsFlagged}, "-no-color -module=test2") + ] + ) + def test_output(self, param, param_str, string_logger): tf = Terraform(working_dir=current_path, variables={'test_var': 'test'}) tf.apply('var_to_output') - assert tf.output('test_output') == 'test' + result = tf.output('test_output', **param) + if param: + assert 'terraform output -json {0} test_output'.format(param_str) in string_logger() + else: + assert result == 'test' def test_destroy(self): tf = Terraform(working_dir=current_path, variables={'test_var': 'test'}) @@ -197,6 +210,4 @@ class TestTerraform(object): def test_import(self, string_logger): tf = Terraform(working_dir=current_path) tf.import_cmd('aws_instance.foo', 'i-abc1234', no_color=IsFlagged) - logs = string_logger.getvalue() - print(logs) - assert 'command: terraform import -no-color aws_instance.foo i-abc1234' in logs + assert 'command: terraform import -no-color aws_instance.foo i-abc1234' in string_logger() From 965a39e3bf3f789e5080425c3326839fe5e204e9 Mon Sep 17 00:00:00 2001 From: beelit94 Date: Fri, 4 Aug 2017 15:36:11 -0700 Subject: [PATCH 07/14] 1. test with more felxible string order 2. test with latest terraform --- .travis.yml | 2 +- test/test_terraform.py | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9843ea0..bb62e9c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ python: - '3.6' before_install: sudo apt-get install unzip before_script: - - export TFVER=0.9.5 + - export TFVER=0.10.0 - export TFURL=https://releases.hashicorp.com/terraform/ - TFURL+=$TFVER - TFURL+="/terraform_" diff --git a/test/test_terraform.py b/test/test_terraform.py index 78c41e3..c7e5a3a 100644 --- a/test/test_terraform.py +++ b/test/test_terraform.py @@ -8,11 +8,10 @@ import os import logging import re import shutil +from itertools import combinations logging.basicConfig(level=logging.DEBUG) root_logger = logging.getLogger() -# ch = logging.StreamHandler(sys.stdout) -# root_logger.addHandler(ch) current_path = os.path.dirname(os.path.realpath(__file__)) STRING_CASES = [ @@ -169,19 +168,20 @@ class TestTerraform(object): assert 'test2' in out @pytest.mark.parametrize( - ("param", "param_str"), + ("param"), [ - ({}, ""), - ({'module': 'test2'}, "-module=test2"), - ({'module': 'test2', 'no_color': IsFlagged}, "-no-color -module=test2") + ({}), + ({'module': 'test2'}), ] ) - def test_output(self, param, param_str, string_logger): + def test_output(self, param, string_logger): tf = Terraform(working_dir=current_path, variables={'test_var': 'test'}) tf.apply('var_to_output') result = tf.output('test_output', **param) + regex = re.compile('terraform output (-module=test2 -json|-json -module=test2) test_output') + log_str = string_logger() if param: - assert 'terraform output -json {0} test_output'.format(param_str) in string_logger() + assert re.search(regex, log_str), log_str else: assert result == 'test' From 3b5fe8203e31f3515d3805188d6566cce103d971 Mon Sep 17 00:00:00 2001 From: beelit94 Date: Fri, 4 Aug 2017 16:02:49 -0700 Subject: [PATCH 08/14] clean up files --- test/test_terraform.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/test/test_terraform.py b/test/test_terraform.py index c7e5a3a..8342f93 100644 --- a/test/test_terraform.py +++ b/test/test_terraform.py @@ -8,7 +8,7 @@ import os import logging import re import shutil -from itertools import combinations +import fnmatch logging.basicConfig(level=logging.DEBUG) root_logger = logging.getLogger() @@ -83,12 +83,21 @@ class TestTerraform(object): """ def purge(dir, pattern): - for f in os.listdir(dir): - if re.search(pattern, f): - if os.path.isfile(f): - os.remove(os.path.join(dir, f)) + # for f in os.listdir(dir): + # # if re.search(pattern, f): + # glob.glob() + # if os.path.isfile(f): + # os.remove(os.path.join(dir, f)) + for root, dirnames, filenames in os.walk(dir): + for filename in fnmatch.filter(filenames, pattern): + f = os.path.join(root, filename) + os.remove(f) + for dirname in fnmatch.filter(dirnames, pattern): + d = os.path.join(root, dirname) + shutil.rmtree(d) - purge('.', '.tfstate') + purge('.', '*.tfstate') + purge('.', '*.terraform') @pytest.mark.parametrize([ "method", "expected" From 3c700b9830cc1f2821d6478d742b68d13a620d09 Mon Sep 17 00:00:00 2001 From: beelit94 Date: Fri, 4 Aug 2017 16:06:55 -0700 Subject: [PATCH 09/14] add compatabilty for 0.10.0 --- test/test_terraform.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/test_terraform.py b/test/test_terraform.py index 8342f93..8422823 100644 --- a/test/test_terraform.py +++ b/test/test_terraform.py @@ -132,6 +132,8 @@ class TestTerraform(object): ]) def test_apply(self, folder, variables, var_files, expected_output, options): tf = Terraform(working_dir=current_path, variables=variables, var_file=var_files) + # after 0.10.0 we always need to init + tf.init(folder) ret, out, err = tf.apply(folder, **options) assert ret == 0 assert expected_output in out.replace('\n', '').replace(' ', '') From 4c65c1bbbefc1c3eb70c8194bdeae7208e20a29f Mon Sep 17 00:00:00 2001 From: beelit94 Date: Fri, 4 Aug 2017 16:17:52 -0700 Subject: [PATCH 10/14] add compatabilty for 0.10.0 --- test/test_terraform.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/test/test_terraform.py b/test/test_terraform.py index 8422823..b9581d4 100644 --- a/test/test_terraform.py +++ b/test/test_terraform.py @@ -29,20 +29,22 @@ STRING_CASES = [ ] CMD_CASES = [ - ['method', 'expected_output', 'expected_ret_code', 'expected_logs'], + ['method', 'expected_output', 'expected_ret_code', 'expected_logs', 'folder'], [ [ lambda x: x.cmd('plan', 'var_to_output', no_color=IsFlagged, var={'test_var': 'test'}) , "doesn't need to do anything", 0, - '' + '', + 'var_to_output' ], # try import aws instance [ lambda x: x.cmd('import', 'aws_instance.foo', 'i-abcd1234', no_color=IsFlagged), '', 1, - 'command: terraform import -no-color aws_instance.foo i-abcd1234' + 'command: terraform import -no-color aws_instance.foo i-abcd1234', + '' ] ] ] @@ -83,11 +85,6 @@ class TestTerraform(object): """ def purge(dir, pattern): - # for f in os.listdir(dir): - # # if re.search(pattern, f): - # glob.glob() - # if os.path.isfile(f): - # os.remove(os.path.join(dir, f)) for root, dirnames, filenames in os.walk(dir): for filename in fnmatch.filter(filenames, pattern): f = os.path.join(root, filename) @@ -111,8 +108,9 @@ class TestTerraform(object): assert s in result @pytest.mark.parametrize(*CMD_CASES) - def test_cmd(self, method, expected_output, expected_ret_code, expected_logs, string_logger): + def test_cmd(self, method, expected_output, expected_ret_code, expected_logs, string_logger, folder): tf = Terraform(working_dir=current_path) + tf.init(folder) ret, out, err = method(tf) logs = string_logger() logs = logs.replace('\n', '') @@ -171,6 +169,7 @@ class TestTerraform(object): ) def test_override_default(self, folder, variables): tf = Terraform(working_dir=current_path, variables=variables) + tf.init(folder) ret, out, err = tf.apply(folder, var={'test_var': 'test2'}, no_color=IsNotFlagged) out = out.replace('\n', '') @@ -187,6 +186,7 @@ class TestTerraform(object): ) def test_output(self, param, string_logger): tf = Terraform(working_dir=current_path, variables={'test_var': 'test'}) + tf.init('var_to_output') tf.apply('var_to_output') result = tf.output('test_output', **param) regex = re.compile('terraform output (-module=test2 -json|-json -module=test2) test_output') @@ -198,6 +198,7 @@ class TestTerraform(object): def test_destroy(self): tf = Terraform(working_dir=current_path, variables={'test_var': 'test'}) + tf.init('var_to_output') ret, out, err = tf.destroy('var_to_output') assert ret == 0 assert 'Destroy complete! Resources: 0 destroyed.' in out From 4f20dfab754a09db66741b84a413e667b9882c78 Mon Sep 17 00:00:00 2001 From: beelit94 Date: Tue, 8 Aug 2017 14:58:33 -0700 Subject: [PATCH 11/14] handle space or special characters in windows & linux --- python_terraform/__init__.py | 9 ++++----- test/test_terraform.py | 12 +++++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/python_terraform/__init__.py b/python_terraform/__init__.py index 35c6e3b..19ca603 100644 --- a/python_terraform/__init__.py +++ b/python_terraform/__init__.py @@ -192,8 +192,7 @@ class Terraform(object): cmds += ['-{k}={v}'.format(k=k, v=v)] cmds += args - cmd = ' '.join(cmds) - return cmd + return cmds def cmd(self, cmd, *args, **kwargs): """ @@ -224,8 +223,8 @@ class Terraform(object): stderr = sys.stderr stdout = sys.stdout - cmd_string = self.generate_cmd_string(cmd, *args, **kwargs) - log.debug('command: {c}'.format(c=cmd_string)) + cmds = self.generate_cmd_string(cmd, *args, **kwargs) + log.debug('command: {c}'.format(c=' '.join(cmds))) working_folder = self.working_dir if self.working_dir else None @@ -233,7 +232,7 @@ class Terraform(object): if self.is_env_vars_included: environ_vars = os.environ.copy() - p = subprocess.Popen(cmd_string, stdout=stdout, stderr=stderr, shell=True, + p = subprocess.Popen(cmds, stdout=stdout, stderr=stderr, cwd=working_folder, env=environ_vars) out, err = p.communicate() ret_code = p.returncode diff --git a/test/test_terraform.py b/test/test_terraform.py index b9581d4..84ae447 100644 --- a/test/test_terraform.py +++ b/test/test_terraform.py @@ -14,6 +14,7 @@ logging.basicConfig(level=logging.DEBUG) root_logger = logging.getLogger() current_path = os.path.dirname(os.path.realpath(__file__)) +FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS = "test 'test.out!" STRING_CASES = [ [ lambda x: x.generate_cmd_string('apply', 'the_folder', @@ -45,6 +46,14 @@ CMD_CASES = [ 1, 'command: terraform import -no-color aws_instance.foo i-abcd1234', '' + ], + # test with space and special character in file path + [ + lambda x: x.cmd('plan', 'var_to_output', out=FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS), + '', + 0, + '', + 'var_to_output' ] ] ] @@ -95,6 +104,7 @@ class TestTerraform(object): purge('.', '*.tfstate') purge('.', '*.terraform') + purge('.', FILE_PATH_WITH_SPACE_AND_SPACIAL_CHARS) @pytest.mark.parametrize([ "method", "expected" @@ -189,7 +199,7 @@ class TestTerraform(object): tf.init('var_to_output') tf.apply('var_to_output') result = tf.output('test_output', **param) - regex = re.compile('terraform output (-module=test2 -json|-json -module=test2) test_output') + regex = re.compile("terraform output (-module=test2 -json|-json -module=test2) test_output") log_str = string_logger() if param: assert re.search(regex, log_str), log_str From 64a06f9bd723f7d745b9226558a8e749f93e385c Mon Sep 17 00:00:00 2001 From: beelit94 Date: Tue, 8 Aug 2017 15:06:06 -0700 Subject: [PATCH 12/14] ignore dropbox files --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 901668c..7b47fe6 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ .cache /.pypirc /.tox/ +.dropbox +Icon \ No newline at end of file From 6d1f1be9ec174403566dfc27e36e14587166feef Mon Sep 17 00:00:00 2001 From: beelit94 Date: Tue, 8 Aug 2017 15:12:48 -0700 Subject: [PATCH 13/14] add changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..18de51f --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,6 @@ +# Changelog +## [0.9.0] +### Fixed +1. [#12] Output function doesn't accept parameter 'module' +1. [#16] Handle empty space/special characters when passing string to command line options +1. Tested with terraform 0.10.0 \ No newline at end of file From 2e9c6d3b922c45e7a910de35be158185c82a0121 Mon Sep 17 00:00:00 2001 From: beelit94 Date: Tue, 8 Aug 2017 15:12:59 -0700 Subject: [PATCH 14/14] =?UTF-8?q?Bump=20version:=200.8.6=20=E2=86=92=200.9?= =?UTF-8?q?.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .bumpversion.cfg | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 8c1f761..35141c7 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.8.6 +current_version = 0.9.0 commit = True tag = False diff --git a/setup.py b/setup.py index 988cc76..b4fb40e 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ except IOError: setup( name=module_name, - version='0.8.6', + version='0.9.0', url='https://github.com/beelit94/python-terraform', license='MIT', author='Freddy Tan',