From 8700de964ce247023ef9da8925cdae410baf2b39 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Tue, 31 Jul 2012 21:23:34 -0400 Subject: [PATCH] Teach the common module code to warn users about typo'd arguments and also set everything to None automatically such that code doesn't have to do a lot of params.get('foo', None) everywhere. --- lib/ansible/module_common.py | 33 ++++++++++++++++++++++++++++----- library/command | 6 ++++++ library/yum | 6 ++---- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/lib/ansible/module_common.py b/lib/ansible/module_common.py index e8d38631d5..23b9ddfe3e 100644 --- a/lib/ansible/module_common.py +++ b/lib/ansible/module_common.py @@ -64,26 +64,43 @@ class AnsibleModule(object): self.argument_spec = argument_spec (self.params, self.args) = self._load_params() + self._legal_inputs = [] self._handle_aliases() - self._set_defaults() + self._check_invalid_arguments() + self._set_defaults(pre=True) if not bypass_checks: self._check_required_arguments() self._check_argument_types() + + self._set_defaults(pre=False) if not no_log: self._log_invocation() + def _handle_aliases(self): for (k,v) in self.argument_spec.iteritems(): + self._legal_inputs.append(k) aliases = v.get('aliases', None) + default = v.get('default', None) + required = v.get('required', False) + if default is not None and required: + # not alias specific but this is a good place to check this + self.fail_json(msg="internal error: required and default are mutally exclusive for %s" % k) if aliases is None: continue if type(aliases) != list: self.fail_json(msg='internal error: aliases must be a list') for alias in aliases: + self._legal_inputs.append(alias) if alias in self.params: self.params[k] = self.params[alias] + def _check_invalid_arguments(self): + for (k,v) in self.params.iteritems(): + if k not in self._legal_inputs: + self.fail_json(msg="unsupported parameter for module: %s" % k) + def _check_required_arguments(self): ''' ensure all required arguments are present ''' missing = [] @@ -109,11 +126,17 @@ class AnsibleModule(object): else: self.fail_json(msg="internal error: do not know how to interpret argument_spec") - def _set_defaults(self): + def _set_defaults(self, pre=True): for (k,v) in self.argument_spec.iteritems(): - default = v.get('default', '__NO_DEFAULT__') - if default != '__NO_DEFAULT__' and k not in self.params: - self.params[k] = default + default = v.get('default', None) + if pre == True: + # this prevents setting defaults on required items + if default and k not in self.params: + self.params[k] = default + else: + # make sure things without a default still get set None + if k not in self.params: + self.params[k] = default def _load_params(self): ''' read the input and return a dictionary and the arguments string ''' diff --git a/library/command b/library/command index 754f75110b..48dd0f500a 100755 --- a/library/command +++ b/library/command @@ -76,6 +76,12 @@ def main(): class CommandModule(AnsibleModule): + def _handle_aliases(self): + pass + + def _check_invalid_arguments(self): + pass + def _load_params(self): ''' read the input and return a dictionary and the arguments string ''' args = base64.b64decode(MODULE_ARGS) diff --git a/library/yum b/library/yum index f62cb8a2f0..10a623c803 100755 --- a/library/yum +++ b/library/yum @@ -280,15 +280,13 @@ def main(): # removed==absent, installed==present, these are accepted as aliases state=dict(default='installed', choices=['absent','present','installed','removed','latest']), list=dict(choices=['installed','updates','available','repos','pkgspec']), + conf_file=dict() ) ) params = module.params - if 'conf_file' not in params: - params['conf_file'] = None - - if 'list' in params and 'pkg' in params: + if params['list'] and params['pkg']: module.fail_json(msg="expected 'list=' or 'name=', but not both") if 'list' in params: