module_utils/basic.py: Support logical or condition in required_if (#20220)

* Support logical or condition in required_if

Add logical 'or' condition support in 'required_if'
for requirements.
* If requirements is a list all parameters within it should
  be present.
* If requirements is a set atleast one parameter should
  be present

* Fix review comment
pull/20915/head
Ganesh Nalawade 8 years ago committed by Toshio Kuratomi
parent bbe2903d63
commit 01f4d4a666

@ -1467,14 +1467,26 @@ class AnsibleModule(object):
''' ensure that parameters which conditionally required are present ''' ''' ensure that parameters which conditionally required are present '''
if spec is None: if spec is None:
return return
for (key, val, requirements) in spec: for sp in spec:
missing = [] missing = []
max_missing_count = 0
is_one_of = False
if len(sp) == 4:
key, val, requirements, is_one_of = sp
else:
key, val, requirements = sp
# is_one_of is True at least one requirement should be
# present, else all requirements should be present.
if is_one_of:
max_missing_count = len(requirements)
if key in self.params and self.params[key] == val: if key in self.params and self.params[key] == val:
for check in requirements: for check in requirements:
count = self._count_terms((check,)) count = self._count_terms((check,))
if count == 0: if count == 0:
missing.append(check) missing.append(check)
if len(missing) > 0: 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):

Loading…
Cancel
Save