From 29d391664ae719c42b8d9c54e79e5da1f249728a Mon Sep 17 00:00:00 2001 From: beelit94 Date: Wed, 4 Jan 2017 01:34:37 +0800 Subject: [PATCH] python-terraform-3 Refactor readme and how default value being passed refactor default values --- python_terraform/__init__.py | 37 ++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/python_terraform/__init__.py b/python_terraform/__init__.py index 86bcb7d..8de4c0b 100644 --- a/python_terraform/__init__.py +++ b/python_terraform/__init__.py @@ -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: