Revision af6433c6 lib/rapi/baserlib.py
b/lib/rapi/baserlib.py | ||
---|---|---|
38 | 38 |
from ganeti import errors |
39 | 39 |
|
40 | 40 |
|
41 |
# Dummy value to detect unchanged parameters |
|
42 |
_DEFAULT = object() |
|
43 |
|
|
44 |
|
|
41 | 45 |
def BuildUriList(ids, uri_format, uri_fields=("name", "uri")): |
42 | 46 |
"""Builds a URI list as used by index resources. |
43 | 47 |
|
... | ... | |
213 | 217 |
logging.info("%s: %s", log_type, log_msg) |
214 | 218 |
|
215 | 219 |
|
220 |
def CheckType(value, exptype, descr): |
|
221 |
"""Abort request if value type doesn't match expected type. |
|
222 |
|
|
223 |
@param value: Value |
|
224 |
@type exptype: type |
|
225 |
@param exptype: Expected type |
|
226 |
@type descr: string |
|
227 |
@param descr: Description of value |
|
228 |
@return: Value (allows inline usage) |
|
229 |
|
|
230 |
""" |
|
231 |
if not isinstance(value, exptype): |
|
232 |
raise http.HttpBadRequest("%s: Type is '%s', but '%s' is expected" % |
|
233 |
(descr, type(value).__name__, exptype.__name__)) |
|
234 |
|
|
235 |
return value |
|
236 |
|
|
237 |
|
|
238 |
def CheckParameter(data, name, default=_DEFAULT, exptype=_DEFAULT): |
|
239 |
"""Check and return the value for a given parameter. |
|
240 |
|
|
241 |
If no default value was given and the parameter doesn't exist in the input |
|
242 |
data, an error is raise. |
|
243 |
|
|
244 |
@type data: dict |
|
245 |
@param data: Dictionary containing input data |
|
246 |
@type name: string |
|
247 |
@param name: Parameter name |
|
248 |
@param default: Default value (can be None) |
|
249 |
@param exptype: Expected type (can be None) |
|
250 |
|
|
251 |
""" |
|
252 |
try: |
|
253 |
value = data[name] |
|
254 |
except KeyError: |
|
255 |
if default is not _DEFAULT: |
|
256 |
return default |
|
257 |
|
|
258 |
raise http.HttpBadRequest("Required parameter '%s' is missing" % |
|
259 |
name) |
|
260 |
|
|
261 |
if exptype is _DEFAULT: |
|
262 |
return value |
|
263 |
|
|
264 |
return CheckType(value, exptype, "'%s' parameter" % name) |
|
265 |
|
|
266 |
|
|
216 | 267 |
class R_Generic(object): |
217 | 268 |
"""Generic class for resources. |
218 | 269 |
|
... | ... | |
279 | 330 |
@param name: the required parameter |
280 | 331 |
|
281 | 332 |
""" |
282 |
if name in self.req.request_body: |
|
283 |
return self.req.request_body[name] |
|
284 |
elif args: |
|
285 |
return args[0] |
|
286 |
else: |
|
287 |
raise http.HttpBadRequest("Required parameter '%s' is missing" % |
|
288 |
name) |
|
333 |
if args: |
|
334 |
return CheckParameter(self.req.request_body, name, default=args[0]) |
|
335 |
|
|
336 |
return CheckParameter(self.req.request_body, name) |
|
289 | 337 |
|
290 | 338 |
def useLocking(self): |
291 | 339 |
"""Check if the request specifies locking. |
Also available in: Unified diff