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

refactor options usage illustration
This commit is contained in:
beelit94 2017-01-04 01:02:11 +08:00
parent c6ab9087b8
commit dbd2f13384
2 changed files with 25 additions and 16 deletions

View file

@ -16,7 +16,7 @@ python-terraform is a python module provide a wrapper of `terraform` command lin
t = Terraform() t = Terraform()
return_code, stdout, stderr = t.<cmd_name>(*arguments, **options) return_code, stdout, stderr = t.<cmd_name>(*arguments, **options)
####For any parameter ####For any argument
simply pass the string to arguments of the method, for example, simply pass the string to arguments of the method, for example,
terraform apply target_dir terraform apply target_dir
@ -80,6 +80,12 @@ or
tf = Terraform() tf = Terraform()
tf.apply('/home/test', no_color=IsFlagged, refresh=False, var={'a':'b', 'c':'d'}) tf.apply('/home/test', no_color=IsFlagged, refresh=False, var={'a':'b', 'c':'d'})
or
from python_terraform import Terraform
tf = Terraform(working_dir='/home/test', variables={'a':'b', 'c':'d'})
tf.apply(no_color=IsFlagged, refresh=False)
#### 2. fmt command, diff=true #### 2. fmt command, diff=true
In shell: In shell:
@ -92,6 +98,12 @@ In python-terraform:
tf = terraform(working_dir='/home/test') tf = terraform(working_dir='/home/test')
tf.fmt(diff=True) tf.fmt(diff=True)
## default values
for apply/plan/destroy command, assign with following default value to make
caller easier in python
1. ```input=False```, in this case process won't hang because you missing a variable
1. ```no_color=IsFlagged```, in this case, stdout of result is easier for parsing
## Implementation ## Implementation
IMHO, how terraform design boolean options is confusing. IMHO, how terraform design boolean options is confusing.
Take `input=True` and `-no-color` option of `apply` command for example, Take `input=True` and `-no-color` option of `apply` command for example,
@ -99,7 +111,7 @@ 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 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. a exhaustive method implementation which I don't prefer to.
Therefore I end-up with using `IsFlagged` or `IsNotFlagged` as value of option Therefore I end-up with using `IsFlagged` or `IsNotFlagged` as value of option
like `-no-color` and `True/False` value reserved for option like like `-no-color` and `True/False` value reserved for option like `refresh=true`

View file

@ -16,10 +16,6 @@ class IsFlagged:
pass pass
class IsNotFlagged:
pass
class Terraform(object): class Terraform(object):
""" """
Wrapper of terraform command line tool Wrapper of terraform command line tool
@ -34,13 +30,17 @@ class Terraform(object):
var_file=None, var_file=None,
terraform_bin_path=None): terraform_bin_path=None):
""" """
:param working_dir: the folder of the working folder, if not given, will be where python :param working_dir: the folder of the working folder, if not given,
will be current working folder
:param targets: list of target :param targets: list of target
:param state: path of state file relative to working folder as default value of apply/destroy/plan command
:param variables: variables for apply/destroy/plan command :param state: path of state file relative to working folder,
:param parallelism: parallelism for apply/destroy command as a default value of apply/destroy/plan command
:param var_file: passed as value of -var-file option, could be string or list :param variables: default variables for apply/destroy/plan command,
list stands for multiple -var-file option will be override by variable passing by apply/destroy/plan method
:param parallelism: default parallelism value for apply/destroy command
:param var_file: passed as value of -var-file option,
could be string or list, list stands for multiple -var-file option
:param terraform_bin_path: binary path of terraform :param terraform_bin_path: binary path of terraform
""" """
self.working_dir = working_dir self.working_dir = working_dir
@ -148,15 +148,12 @@ class Terraform(object):
cmds += ['-{k}'.format(k=k)] cmds += ['-{k}'.format(k=k)]
continue continue
if v is IsNotFlagged: if v is None:
continue continue
if type(v) is bool: if type(v) is bool:
v = 'true' if v else 'false' v = 'true' if v else 'false'
if not v:
continue
cmds += ['-{k}={v}'.format(k=k, v=v)] cmds += ['-{k}={v}'.format(k=k, v=v)]
cmds += args cmds += args