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

refactor default values
This commit is contained in:
beelit94 2017-01-04 01:34:37 +08:00
parent dbd2f13384
commit 29d391664a

View file

@ -16,6 +16,10 @@ 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
@ -64,20 +68,28 @@ class Terraform(object):
return wrapper return wrapper
def apply(self, dir_or_plan=None, **kwargs): def apply(self, dir_or_plan=None, input=False, no_color=IsFlagged, **kwargs):
""" """
refer to https://terraform.io/docs/commands/apply.html refer to https://terraform.io/docs/commands/apply.html
no-color is flagged by default no-color is flagged by default
:param no_color: disable color of stdout
:param input: disable prompt for a missing variable
:param dir_or_plan: folder relative to working folder :param dir_or_plan: folder relative to working folder
:param kwargs: same as kwags in method 'cmd' :param kwargs: same as kwags in method 'cmd'
:returns return_code, stdout, stderr :returns return_code, stdout, stderr
""" """
default = dict() default = kwargs
args, option_dict = self._generate_default_args(dir_or_plan, default, kwargs) default['input'] = input
default['no_color'] = no_color
option_dict = self._generate_default_options(default)
args = self._generate_default_args(dir_or_plan)
return self.cmd('apply', *args, **option_dict) return self.cmd('apply', *args, **option_dict)
def _generate_default_args(self, dir_or_plan, default_dict, kwargs): def _generate_default_args(self, dir_or_plan):
option_dict = default_dict return [dir_or_plan] if dir_or_plan else []
def _generate_default_options(self, input_options):
option_dict = dict()
option_dict['state'] = self.state option_dict['state'] = self.state
option_dict['target'] = self.targets option_dict['target'] = self.targets
option_dict['var'] = self.variables option_dict['var'] = self.variables
@ -85,18 +97,19 @@ class Terraform(object):
option_dict['parallelism'] = self.parallelism option_dict['parallelism'] = self.parallelism
option_dict['no_color'] = IsFlagged option_dict['no_color'] = IsFlagged
option_dict['input'] = False option_dict['input'] = False
option_dict.update(kwargs) option_dict.update(input_options)
args = [dir_or_plan] if dir_or_plan else [] return option_dict
return args, option_dict
def destroy(self, dir_or_plan=None, **kwargs): def destroy(self, dir_or_plan=None, force=IsFlagged, **kwargs):
""" """
refer to https://www.terraform.io/docs/commands/destroy.html refer to https://www.terraform.io/docs/commands/destroy.html
force/no-color option is flagged by default force/no-color option is flagged by default
:return: ret_code, stdout, stderr :return: ret_code, stdout, stderr
""" """
default = {'force': IsFlagged} default = kwargs
args, option_dict = self._generate_default_args(dir_or_plan, default, kwargs) default['force'] = force
option_dict = self._generate_default_options(default)
args = self._generate_default_args(dir_or_plan)
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):
@ -148,7 +161,7 @@ class Terraform(object):
cmds += ['-{k}'.format(k=k)] cmds += ['-{k}'.format(k=k)]
continue continue
if v is None: if v is None or v is IsNotFlagged:
continue continue
if type(v) is bool: if type(v) is bool: