From 144b4c61c407b7c52d8912e51b258fd3ab8ca4dd Mon Sep 17 00:00:00 2001 From: beelit94 Date: Tue, 3 Jan 2017 23:09:23 +0800 Subject: [PATCH] python-terraform-3 Refactor readme and how default value being passed reorder readme, refactor name --- README.md | 26 +++++++++++++++----------- python_terraform/__init__.py | 7 +++---- test/test_terraform.py | 16 +++++++++------- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 31fbd21..ec3a8bc 100644 --- a/README.md +++ b/README.md @@ -8,16 +8,7 @@ python-terraform is a python module provide a wrapper of `terraform` command lin ## Installation pip install python-terraform - -## Implementation -IMHO, how terraform design boolean options is confusing. -Take `input=True` and `-no-color` option of `apply` command for example, -they're all boolean value but with different option type. -This make api caller don't have a general rule to follow but to do -a exhaustive method implementation which I don't prefer to. -Therefore I end-up with using `IsFlagged` or `IsNotFlagged` as value of option -like `-no-color` and `True/False` value reserved for option like - + ## Usage For any terraform command @@ -35,7 +26,7 @@ For any options if it's a flag could be used multiple times, assign list to it's value if it's a "var" variable flag, assign dictionary to it if a value is None, will skip this option - + ## Examples ### Have a test.tf file under folder "/home/test" #### 1. apply with variables a=b, c=d, refresh=false, no color in the output @@ -67,6 +58,19 @@ In python-terraform: from python_terraform import Terraform tf = terraform(working_dir='/home/test') tf.fmt(diff=True) + +## Implementation +IMHO, how terraform design boolean options is confusing. +Take `input=True` and `-no-color` option of `apply` command for example, +they're all boolean value but with different option type. +This make api caller don't have a general rule to follow but to do +a exhaustive method implementation which I don't prefer to. +Therefore I end-up with using `IsFlagged` or `IsNotFlagged` as value of option +like `-no-color` and `True/False` value reserved for option like + + + + diff --git a/python_terraform/__init__.py b/python_terraform/__init__.py index e61ff00..5f5395c 100644 --- a/python_terraform/__init__.py +++ b/python_terraform/__init__.py @@ -73,11 +73,10 @@ class Terraform(object): :returns return_code, stdout, stderr """ default = dict() - args, option_dict = self._create_cmd_args(dir_or_plan, default, kwargs) - + args, option_dict = self._generate_default_args(dir_or_plan, default, kwargs) return self.cmd('apply', *args, **option_dict) - def _create_cmd_args(self, dir_or_plan, default_dict, kwargs): + def _generate_default_args(self, dir_or_plan, default_dict, kwargs): option_dict = default_dict option_dict['state'] = self.state option_dict['target'] = self.targets @@ -97,7 +96,7 @@ class Terraform(object): :return: ret_code, stdout, stderr """ default = {'force': IsFlagged} - args, option_dict = self._create_cmd_args(dir_or_plan, default, kwargs) + args, option_dict = self._generate_default_args(dir_or_plan, default, kwargs) return self.cmd('destroy', *args, **option_dict) def generate_cmd_string(self, cmd, *args, **kwargs): diff --git a/test/test_terraform.py b/test/test_terraform.py index a190b15..f55323c 100644 --- a/test/test_terraform.py +++ b/test/test_terraform.py @@ -79,16 +79,18 @@ class TestTerraform(object): assert ret == 0 @pytest.mark.parametrize( - ("folder", "variables", "var_files", "expected_output"), + ("folder", "variables", "var_files", "expected_output", "options"), [ - ("var_to_output", {'test_var': 'test'}, None, "test_output=test"), - ("var_to_output", {'test_list_var': ['c', 'd']}, None, "test_list_output=[c,d]"), - ("var_to_output", {'test_map_var': {"c": "c", "d": "d"}}, None, "test_map_output={a=ab=bc=cd=d}"), - ("var_to_output", {'test_map_var': {"c": "c", "d": "d"}}, 'var_to_output/test_map_var.json', "test_map_output={a=ab=bc=cd=de=ef=f}") + ("var_to_output", + {'test_var': 'test'}, None, "test_output=test", {}), + ("var_to_output", {'test_list_var': ['c', 'd']}, None, "test_list_output=[c,d]", {}), + ("var_to_output", {'test_map_var': {"c": "c", "d": "d"}}, None, "test_map_output={a=ab=bc=cd=d}", {}), + ("var_to_output", {'test_map_var': {"c": "c", "d": "d"}}, 'var_to_output/test_map_var.json', "test_map_output={a=ab=bc=cd=de=ef=f}", {}), + ("var_to_output", {}, None, "\x1b[0m\x1b[1m\x1b[32mApplycomplete!", {"no_color": IsNotFlagged}) ]) - def test_apply(self, folder, variables, var_files, expected_output): + def test_apply(self, folder, variables, var_files, expected_output, options): tf = Terraform(working_dir=current_path, variables=variables, var_file=var_files) - ret, out, err = tf.apply(folder) + ret, out, err = tf.apply(folder, **options) assert ret == 0 assert expected_output in out.replace('\n', '').replace(' ', '') assert err == ''