1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

subspec protptype

This commit is contained in:
Brian Coca 2017-02-08 18:45:11 -05:00 committed by Brian Coca
parent ed413f2b28
commit 25de905c6e

View file

@ -1374,10 +1374,12 @@ class AnsibleModule(object):
e = get_exception() e = get_exception()
self.fail_json(msg="An unknown error was encountered while attempting to validate the locale: %s" % e) self.fail_json(msg="An unknown error was encountered while attempting to validate the locale: %s" % e)
def _handle_aliases(self): def _handle_aliases(self, spec=None):
# this uses exceptions as it happens before we can safely call fail_json # this uses exceptions as it happens before we can safely call fail_json
aliases_results = {} #alias:canon aliases_results = {} #alias:canon
for (k,v) in self.argument_spec.items(): if spec is None:
spec = self.argument_spec
for (k,v) in spec.items():
self._legal_inputs.append(k) self._legal_inputs.append(k)
aliases = v.get('aliases', None) aliases = v.get('aliases', None)
default = v.get('default', None) default = v.get('default', None)
@ -1479,10 +1481,12 @@ class AnsibleModule(object):
if 0 in counts: if 0 in counts:
self.fail_json(msg="parameters are required together: %s" % (check,)) self.fail_json(msg="parameters are required together: %s" % (check,))
def _check_required_arguments(self): def _check_required_arguments(self, spec=None):
''' ensure all required arguments are present ''' ''' ensure all required arguments are present '''
missing = [] missing = []
for (k,v) in self.argument_spec.items(): if spec is None:
spec = self.argument_spec
for (k,v) in spec.items():
required = v.get('required', False) required = v.get('required', False)
if required and k not in self.params: if required and k not in self.params:
missing.append(k) missing.append(k)
@ -1515,9 +1519,11 @@ class AnsibleModule(object):
if len(missing) and len(missing) >= max_missing_count: if len(missing) and len(missing) >= max_missing_count:
self.fail_json(msg="%s is %s but the following are missing: %s" % (key, val, ','.join(missing))) self.fail_json(msg="%s is %s but the following are missing: %s" % (key, val, ','.join(missing)))
def _check_argument_values(self): def _check_argument_values(self, spec=None):
''' ensure all arguments have the requested values, and there are no stray arguments ''' ''' ensure all arguments have the requested values, and there are no stray arguments '''
for (k,v) in self.argument_spec.items(): if spec is None:
spec = self.argument_spec
for (k,v) in spec.items():
choices = v.get('choices',None) choices = v.get('choices',None)
if choices is None: if choices is None:
continue continue
@ -1700,9 +1706,12 @@ class AnsibleModule(object):
except ValueError: except ValueError:
raise TypeError('%s cannot be converted to a Bit value' % type(value)) raise TypeError('%s cannot be converted to a Bit value' % type(value))
def _check_argument_types(self): def _check_argument_types(self, spec=None):
''' ensure all arguments have the requested type ''' ''' ensure all arguments have the requested type '''
for (k, v) in self.argument_spec.items():
if spec is None:
spec = self.argument_spec
for (k, v) in spec.items():
wanted = v.get('type', None) wanted = v.get('type', None)
if k not in self.params: if k not in self.params:
continue continue
@ -1728,6 +1737,15 @@ class AnsibleModule(object):
e = get_exception() e = get_exception()
self.fail_json(msg="argument %s is of type %s and we were unable to convert to %s: %s" % (k, type(value), wanted, e)) self.fail_json(msg="argument %s is of type %s and we were unable to convert to %s: %s" % (k, type(value), wanted, e))
spec = None
# deal with subspecs
if wanted == 'dict' or (wanted == 'list' and v.get('elements', '') == 'dict'):
spec = v.get('spec', None)
if spec:
self._check_required_arguments(spec)
self._check_argument_types(spec)
self._check_argument_values(spec)
def _set_defaults(self, pre=True): def _set_defaults(self, pre=True):
for (k,v) in self.argument_spec.items(): for (k,v) in self.argument_spec.items():
default = v.get('default', None) default = v.get('default', None)