mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
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.
This commit is contained in:
parent
190d72761f
commit
8700de964c
3 changed files with 36 additions and 9 deletions
|
@ -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 '''
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in a new issue