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

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

View file

@ -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):

View file

@ -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 == ''