python-terraform-3 Refactor readme and how default value being passed
refactor default values
This commit is contained in:
parent
dbd2f13384
commit
29d391664a
1 changed files with 25 additions and 12 deletions
|
@ -16,6 +16,10 @@ class IsFlagged:
|
|||
pass
|
||||
|
||||
|
||||
class IsNotFlagged:
|
||||
pass
|
||||
|
||||
|
||||
class Terraform(object):
|
||||
"""
|
||||
Wrapper of terraform command line tool
|
||||
|
@ -64,20 +68,28 @@ class Terraform(object):
|
|||
|
||||
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
|
||||
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 kwargs: same as kwags in method 'cmd'
|
||||
:returns return_code, stdout, stderr
|
||||
"""
|
||||
default = dict()
|
||||
args, option_dict = self._generate_default_args(dir_or_plan, default, kwargs)
|
||||
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)
|
||||
|
||||
def _generate_default_args(self, dir_or_plan, default_dict, kwargs):
|
||||
option_dict = default_dict
|
||||
def _generate_default_args(self, dir_or_plan):
|
||||
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['target'] = self.targets
|
||||
option_dict['var'] = self.variables
|
||||
|
@ -85,18 +97,19 @@ class Terraform(object):
|
|||
option_dict['parallelism'] = self.parallelism
|
||||
option_dict['no_color'] = IsFlagged
|
||||
option_dict['input'] = False
|
||||
option_dict.update(kwargs)
|
||||
args = [dir_or_plan] if dir_or_plan else []
|
||||
return args, option_dict
|
||||
option_dict.update(input_options)
|
||||
return 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
|
||||
force/no-color option is flagged by default
|
||||
:return: ret_code, stdout, stderr
|
||||
"""
|
||||
default = {'force': IsFlagged}
|
||||
args, option_dict = self._generate_default_args(dir_or_plan, default, kwargs)
|
||||
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)
|
||||
|
||||
def generate_cmd_string(self, cmd, *args, **kwargs):
|
||||
|
@ -148,7 +161,7 @@ class Terraform(object):
|
|||
cmds += ['-{k}'.format(k=k)]
|
||||
continue
|
||||
|
||||
if v is None:
|
||||
if v is None or v is IsNotFlagged:
|
||||
continue
|
||||
|
||||
if type(v) is bool:
|
||||
|
|
Loading…
Reference in a new issue