Commit 3d103742 authored by Iustin Pop's avatar Iustin Pop
Browse files

rapi: fix 'bulk' processing and add locking option

This patch fixes the 'bulk' parameter (before any non-empty
specification was considered True, in conflict with the documentation,
i.e. bulk=0 still did bulk queries).

The patch also adds optional locking on the instance/node listing (does
not have effect when we only list names).

Reviewed-by: imsnah
parent 9031ee8e
......@@ -28,6 +28,7 @@ import ganeti.opcodes
from ganeti import luxi
from ganeti import rapi
from ganeti import http
def BuildUriList(ids, uri_format, uri_fields=("name", "uri")):
......@@ -173,3 +174,32 @@ class R_Generic(object):
"""
return self.sn
def _checkIntVariable(self, name):
"""Return the parsed value of an int argument.
"""
val = self.queryargs.get(name, 0)
if isinstance(val, list):
if val:
val = val[0]
else:
val = 0
try:
val = int(val)
except (ValueError, TypeError), err:
raise http.HttpBadRequest(message="Invalid value for the"
" '%s' parameter" % (name,))
return val
def useLocking(self):
"""Check if the request specifies locking.
"""
return self._checkIntVariable('lock')
def useBulk(self):
"""Check if the request specifies bulk querying.
"""
return self._checkIntVariable('bulk')
......@@ -217,7 +217,7 @@ class R_2_nodes(baserlib.R_Generic):
"""
client = luxi.Client()
if 'bulk' in self.queryargs:
if self.useBulk():
bulkdata = client.QueryNodes([], N_FIELDS, False)
return baserlib.MapBulkFields(bulkdata, N_FIELDS)
else:
......@@ -240,7 +240,7 @@ class R_2_nodes_name(baserlib.R_Generic):
node_name = self.items[0]
client = luxi.Client()
result = client.QueryNodes(names=[node_name], fields=N_FIELDS,
use_locking=False)
use_locking=self.useLocking())
return baserlib.MapFields(N_FIELDS, result[0])
......@@ -301,12 +301,12 @@ class R_2_instances(baserlib.R_Generic):
"""
client = luxi.Client()
if 'bulk' in self.queryargs:
bulkdata = client.QueryInstances([], I_FIELDS, False)
use_locking = self.useLocking()
if self.useBulk():
bulkdata = client.QueryInstances([], I_FIELDS, use_locking)
return baserlib.MapBulkFields(bulkdata, I_FIELDS)
else:
instancesdata = client.QueryInstances([], ["name"], False)
instancesdata = client.QueryInstances([], ["name"], use_locking)
instanceslist = [row[0] for row in instancesdata]
return baserlib.BuildUriList(instanceslist, "/2/instances/%s",
uri_fields=("id", "uri"))
......@@ -362,7 +362,7 @@ class R_2_instances_name(baserlib.R_Generic):
client = luxi.Client()
instance_name = self.items[0]
result = client.QueryInstances(names=[instance_name], fields=I_FIELDS,
use_locking=False)
use_locking=self.useLocking())
return baserlib.MapFields(I_FIELDS, result[0])
......
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