python-terraform-3 Refactor readme and how default value being passed

reorder readme, refactor name
This commit is contained in:
beelit94 2017-01-03 23:09:23 +08:00
parent ed03239db2
commit 144b4c61c4
3 changed files with 27 additions and 22 deletions

View file

@ -9,15 +9,6 @@ python-terraform is a python module provide a wrapper of `terraform` command lin
## Installation ## Installation
pip install python-terraform 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 ## Usage
For any terraform command For any terraform command
@ -68,6 +59,19 @@ In python-terraform:
tf = terraform(working_dir='/home/test') tf = terraform(working_dir='/home/test')
tf.fmt(diff=True) 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

View file

@ -73,11 +73,10 @@ class Terraform(object):
:returns return_code, stdout, stderr :returns return_code, stdout, stderr
""" """
default = dict() 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) 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 = default_dict
option_dict['state'] = self.state option_dict['state'] = self.state
option_dict['target'] = self.targets option_dict['target'] = self.targets
@ -97,7 +96,7 @@ class Terraform(object):
:return: ret_code, stdout, stderr :return: ret_code, stdout, stderr
""" """
default = {'force': IsFlagged} 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) return self.cmd('destroy', *args, **option_dict)
def generate_cmd_string(self, cmd, *args, **kwargs): def generate_cmd_string(self, cmd, *args, **kwargs):

View file

@ -79,16 +79,18 @@ class TestTerraform(object):
assert ret == 0 assert ret == 0
@pytest.mark.parametrize( @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",
("var_to_output", {'test_list_var': ['c', 'd']}, None, "test_list_output=[c,d]"), {'test_var': 'test'}, None, "test_output=test", {}),
("var_to_output", {'test_map_var': {"c": "c", "d": "d"}}, None, "test_map_output={a=ab=bc=cd=d}"), ("var_to_output", {'test_list_var': ['c', 'd']}, None, "test_list_output=[c,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_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) 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 ret == 0
assert expected_output in out.replace('\n', '').replace(' ', '') assert expected_output in out.replace('\n', '').replace(' ', '')
assert err == '' assert err == ''