Skip to content

Commit

Permalink
subspec protptype
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoca committed Mar 24, 2017
1 parent ed413f2 commit 25de905
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions lib/ansible/module_utils/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1374,10 +1374,12 @@ def _check_locale(self):
e = get_exception()
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
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)
aliases = v.get('aliases', None)
default = v.get('default', None)
Expand Down Expand Up @@ -1479,10 +1481,12 @@ def _check_required_together(self, spec):
if 0 in counts:
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 '''
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)
if required and k not in self.params:
missing.append(k)
Expand Down Expand Up @@ -1515,9 +1519,11 @@ def _check_required_if(self, spec):
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)))

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 '''
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)
if choices is None:
continue
Expand Down Expand Up @@ -1700,9 +1706,12 @@ def _check_type_bits(self, value):
except ValueError:
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 '''
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)
if k not in self.params:
continue
Expand All @@ -1728,6 +1737,15 @@ def _check_argument_types(self):
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))

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):
for (k,v) in self.argument_spec.items():
default = v.get('default', None)
Expand Down

0 comments on commit 25de905

Please sign in to comment.