Commit 8700de96 by Michael DeHaan

Teach the common module code to warn users about typo'd arguments and also set…

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.
parent 190d7276
...@@ -64,26 +64,43 @@ class AnsibleModule(object): ...@@ -64,26 +64,43 @@ class AnsibleModule(object):
self.argument_spec = argument_spec self.argument_spec = argument_spec
(self.params, self.args) = self._load_params() (self.params, self.args) = self._load_params()
self._legal_inputs = []
self._handle_aliases() self._handle_aliases()
self._set_defaults() self._check_invalid_arguments()
self._set_defaults(pre=True)
if not bypass_checks: if not bypass_checks:
self._check_required_arguments() self._check_required_arguments()
self._check_argument_types() self._check_argument_types()
self._set_defaults(pre=False)
if not no_log: if not no_log:
self._log_invocation() self._log_invocation()
def _handle_aliases(self): def _handle_aliases(self):
for (k,v) in self.argument_spec.iteritems(): for (k,v) in self.argument_spec.iteritems():
self._legal_inputs.append(k)
aliases = v.get('aliases', None) 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: if aliases is None:
continue continue
if type(aliases) != list: if type(aliases) != list:
self.fail_json(msg='internal error: aliases must be a list') self.fail_json(msg='internal error: aliases must be a list')
for alias in aliases: for alias in aliases:
self._legal_inputs.append(alias)
if alias in self.params: if alias in self.params:
self.params[k] = self.params[alias] 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): def _check_required_arguments(self):
''' ensure all required arguments are present ''' ''' ensure all required arguments are present '''
missing = [] missing = []
...@@ -109,11 +126,17 @@ class AnsibleModule(object): ...@@ -109,11 +126,17 @@ class AnsibleModule(object):
else: else:
self.fail_json(msg="internal error: do not know how to interpret argument_spec") 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(): for (k,v) in self.argument_spec.iteritems():
default = v.get('default', '__NO_DEFAULT__') default = v.get('default', None)
if default != '__NO_DEFAULT__' and k not in self.params: if pre == True:
self.params[k] = default # 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): def _load_params(self):
''' read the input and return a dictionary and the arguments string ''' ''' read the input and return a dictionary and the arguments string '''
......
...@@ -76,6 +76,12 @@ def main(): ...@@ -76,6 +76,12 @@ def main():
class CommandModule(AnsibleModule): class CommandModule(AnsibleModule):
def _handle_aliases(self):
pass
def _check_invalid_arguments(self):
pass
def _load_params(self): def _load_params(self):
''' read the input and return a dictionary and the arguments string ''' ''' read the input and return a dictionary and the arguments string '''
args = base64.b64decode(MODULE_ARGS) args = base64.b64decode(MODULE_ARGS)
......
...@@ -280,15 +280,13 @@ def main(): ...@@ -280,15 +280,13 @@ def main():
# removed==absent, installed==present, these are accepted as aliases # removed==absent, installed==present, these are accepted as aliases
state=dict(default='installed', choices=['absent','present','installed','removed','latest']), state=dict(default='installed', choices=['absent','present','installed','removed','latest']),
list=dict(choices=['installed','updates','available','repos','pkgspec']), list=dict(choices=['installed','updates','available','repos','pkgspec']),
conf_file=dict()
) )
) )
params = module.params params = module.params
if 'conf_file' not in params: if params['list'] and params['pkg']:
params['conf_file'] = None
if 'list' in params and 'pkg' in params:
module.fail_json(msg="expected 'list=' or 'name=', but not both") module.fail_json(msg="expected 'list=' or 'name=', but not both")
if 'list' in params: if 'list' in params:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment