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

Merge pull request #11032 from resmo/fix/value_error

basic: fix ValueError if value of a type='int' is not an int
This commit is contained in:
Brian Coca 2015-05-19 11:47:40 -04:00
commit 9ec5299c12

View file

@ -1015,57 +1015,60 @@ class AnsibleModule(object):
value = self.params[k] value = self.params[k]
is_invalid = False is_invalid = False
if wanted == 'str': try:
if not isinstance(value, basestring): if wanted == 'str':
self.params[k] = str(value) if not isinstance(value, basestring):
elif wanted == 'list': self.params[k] = str(value)
if not isinstance(value, list): elif wanted == 'list':
if isinstance(value, basestring): if not isinstance(value, list):
self.params[k] = value.split(",") if isinstance(value, basestring):
elif isinstance(value, int) or isinstance(value, float): self.params[k] = value.split(",")
self.params[k] = [ str(value) ] elif isinstance(value, int) or isinstance(value, float):
else: self.params[k] = [ str(value) ]
is_invalid = True
elif wanted == 'dict':
if not isinstance(value, dict):
if isinstance(value, basestring):
if value.startswith("{"):
try:
self.params[k] = json.loads(value)
except:
(result, exc) = self.safe_eval(value, dict(), include_exceptions=True)
if exc is not None:
self.fail_json(msg="unable to evaluate dictionary for %s" % k)
self.params[k] = result
elif '=' in value:
self.params[k] = dict([x.strip().split("=", 1) for x in value.split(",")])
else: else:
self.fail_json(msg="dictionary requested, could not parse JSON or key=value") is_invalid = True
else: elif wanted == 'dict':
is_invalid = True if not isinstance(value, dict):
elif wanted == 'bool': if isinstance(value, basestring):
if not isinstance(value, bool): if value.startswith("{"):
if isinstance(value, basestring): try:
self.params[k] = self.boolean(value) self.params[k] = json.loads(value)
else: except:
is_invalid = True (result, exc) = self.safe_eval(value, dict(), include_exceptions=True)
elif wanted == 'int': if exc is not None:
if not isinstance(value, int): self.fail_json(msg="unable to evaluate dictionary for %s" % k)
if isinstance(value, basestring): self.params[k] = result
self.params[k] = int(value) elif '=' in value:
else: self.params[k] = dict([x.strip().split("=", 1) for x in value.split(",")])
is_invalid = True else:
elif wanted == 'float': self.fail_json(msg="dictionary requested, could not parse JSON or key=value")
if not isinstance(value, float): else:
if isinstance(value, basestring): is_invalid = True
self.params[k] = float(value) elif wanted == 'bool':
else: if not isinstance(value, bool):
is_invalid = True if isinstance(value, basestring):
else: self.params[k] = self.boolean(value)
self.fail_json(msg="implementation error: unknown type %s requested for %s" % (wanted, k)) else:
is_invalid = True
elif wanted == 'int':
if not isinstance(value, int):
if isinstance(value, basestring):
self.params[k] = int(value)
else:
is_invalid = True
elif wanted == 'float':
if not isinstance(value, float):
if isinstance(value, basestring):
self.params[k] = float(value)
else:
is_invalid = True
else:
self.fail_json(msg="implementation error: unknown type %s requested for %s" % (wanted, k))
if is_invalid: if is_invalid:
self.fail_json(msg="argument %s is of invalid type: %s, required: %s" % (k, type(value), wanted)) self.fail_json(msg="argument %s is of invalid type: %s, required: %s" % (k, type(value), wanted))
except ValueError, e:
self.fail_json(msg="value of argument %s is not of type %s and we were unable to automatically convert" % (k, wanted))
def _set_defaults(self, pre=True): def _set_defaults(self, pre=True):
for (k,v) in self.argument_spec.iteritems(): for (k,v) in self.argument_spec.iteritems():