From dbd2f13384ad8278858211093e5be6de345b0c0c Mon Sep 17 00:00:00 2001 From: beelit94 Date: Wed, 4 Jan 2017 01:02:11 +0800 Subject: [PATCH] python-terraform-3 Refactor readme and how default value being passed refactor options usage illustration --- README.md | 16 ++++++++++++++-- python_terraform/__init__.py | 25 +++++++++++-------------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index e039c56..4987754 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ python-terraform is a python module provide a wrapper of `terraform` command lin t = Terraform() return_code, stdout, stderr = t.(*arguments, **options) -####For any parameter +####For any argument simply pass the string to arguments of the method, for example, terraform apply target_dir @@ -79,6 +79,12 @@ or from python_terraform import Terraform tf = Terraform() 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 In shell: @@ -91,6 +97,12 @@ In python-terraform: from python_terraform import Terraform tf = terraform(working_dir='/home/test') 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 IMHO, how terraform design boolean options is confusing. @@ -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 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 +like `-no-color` and `True/False` value reserved for option like `refresh=true` diff --git a/python_terraform/__init__.py b/python_terraform/__init__.py index 5f5395c..86bcb7d 100644 --- a/python_terraform/__init__.py +++ b/python_terraform/__init__.py @@ -16,10 +16,6 @@ class IsFlagged: pass -class IsNotFlagged: - pass - - class Terraform(object): """ Wrapper of terraform command line tool @@ -34,13 +30,17 @@ class Terraform(object): var_file=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 state: path of state file relative to working folder - :param variables: variables for apply/destroy/plan command - :param parallelism: parallelism 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 + as default value of apply/destroy/plan command + :param state: path of state file relative to working folder, + as a default value of apply/destroy/plan command + :param variables: default variables for apply/destroy/plan command, + 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 """ self.working_dir = working_dir @@ -148,15 +148,12 @@ class Terraform(object): cmds += ['-{k}'.format(k=k)] continue - if v is IsNotFlagged: + if v is None: continue if type(v) is bool: v = 'true' if v else 'false' - if not v: - continue - cmds += ['-{k}={v}'.format(k=k, v=v)] cmds += args