Commit af6433c6 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

rapi.baserlib: Add function to check variable type



Also add a separate function to retrieve body parameters. Having
it separate (independent of a class instance) will make it easier
to unittest users of this function.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarGuido Trotter <ultrotter@google.com>
parent 7eac4a4d
...@@ -38,6 +38,10 @@ from ganeti import opcodes ...@@ -38,6 +38,10 @@ from ganeti import opcodes
from ganeti import errors from ganeti import errors
# Dummy value to detect unchanged parameters
_DEFAULT = object()
def BuildUriList(ids, uri_format, uri_fields=("name", "uri")): def BuildUriList(ids, uri_format, uri_fields=("name", "uri")):
"""Builds a URI list as used by index resources. """Builds a URI list as used by index resources.
...@@ -213,6 +217,53 @@ def FeedbackFn(ts, log_type, log_msg): # pylint: disable-msg=W0613 ...@@ -213,6 +217,53 @@ def FeedbackFn(ts, log_type, log_msg): # pylint: disable-msg=W0613
logging.info("%s: %s", log_type, log_msg) logging.info("%s: %s", log_type, log_msg)
def CheckType(value, exptype, descr):
"""Abort request if value type doesn't match expected type.
@param value: Value
@type exptype: type
@param exptype: Expected type
@type descr: string
@param descr: Description of value
@return: Value (allows inline usage)
"""
if not isinstance(value, exptype):
raise http.HttpBadRequest("%s: Type is '%s', but '%s' is expected" %
(descr, type(value).__name__, exptype.__name__))
return value
def CheckParameter(data, name, default=_DEFAULT, exptype=_DEFAULT):
"""Check and return the value for a given parameter.
If no default value was given and the parameter doesn't exist in the input
data, an error is raise.
@type data: dict
@param data: Dictionary containing input data
@type name: string
@param name: Parameter name
@param default: Default value (can be None)
@param exptype: Expected type (can be None)
"""
try:
value = data[name]
except KeyError:
if default is not _DEFAULT:
return default
raise http.HttpBadRequest("Required parameter '%s' is missing" %
name)
if exptype is _DEFAULT:
return value
return CheckType(value, exptype, "'%s' parameter" % name)
class R_Generic(object): class R_Generic(object):
"""Generic class for resources. """Generic class for resources.
...@@ -279,13 +330,10 @@ class R_Generic(object): ...@@ -279,13 +330,10 @@ class R_Generic(object):
@param name: the required parameter @param name: the required parameter
""" """
if name in self.req.request_body: if args:
return self.req.request_body[name] return CheckParameter(self.req.request_body, name, default=args[0])
elif args:
return args[0] return CheckParameter(self.req.request_body, name)
else:
raise http.HttpBadRequest("Required parameter '%s' is missing" %
name)
def useLocking(self): def useLocking(self):
"""Check if the request specifies locking. """Check if the request specifies locking.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment