Revision 3d8548c4 lib/luxi.py
b/lib/luxi.py | ||
---|---|---|
40 | 40 |
from ganeti import constants |
41 | 41 |
|
42 | 42 |
|
43 |
KEY_REQUEST = 'request' |
|
44 |
KEY_DATA = 'data' |
|
43 |
KEY_METHOD = 'method' |
|
44 |
KEY_ARGS = 'args' |
|
45 |
KEY_SUCCESS = "success" |
|
46 |
KEY_RESULT = "result" |
|
47 |
|
|
45 | 48 |
REQ_SUBMIT = 'submit' |
46 | 49 |
REQ_ABORT = 'abort' |
47 | 50 |
REQ_QUERY = 'query' |
... | ... | |
82 | 85 |
|
83 | 86 |
""" |
84 | 87 |
|
88 |
|
|
85 | 89 |
class NoMasterError(ProtocolError): |
86 | 90 |
"""The master cannot be reached |
87 | 91 |
|
... | ... | |
261 | 265 |
address = constants.MASTER_SOCKET |
262 | 266 |
self.transport = transport(address, timeouts=timeouts) |
263 | 267 |
|
264 |
def SendRequest(self, request, data):
|
|
268 |
def CallMethod(self, method, args):
|
|
265 | 269 |
"""Send a generic request and return the response. |
266 | 270 |
|
267 | 271 |
""" |
268 |
msg = {KEY_REQUEST: request, KEY_DATA: data} |
|
269 |
result = self.transport.Call(serializer.DumpJson(msg, indent=False)) |
|
272 |
# Build request |
|
273 |
request = { |
|
274 |
KEY_METHOD: method, |
|
275 |
KEY_ARGS: args, |
|
276 |
} |
|
277 |
|
|
278 |
# Send request and wait for response |
|
279 |
result = self.transport.Call(serializer.DumpJson(request, indent=False)) |
|
270 | 280 |
try: |
271 | 281 |
data = serializer.LoadJson(result) |
272 | 282 |
except Exception, err: |
273 | 283 |
raise ProtocolError("Error while deserializing response: %s" % str(err)) |
284 |
|
|
285 |
# Validate response |
|
274 | 286 |
if (not isinstance(data, dict) or |
275 |
'success' not in data or
|
|
276 |
'result' not in data):
|
|
287 |
KEY_SUCCESS not in data or
|
|
288 |
KEY_RESULT not in data):
|
|
277 | 289 |
raise DecodingError("Invalid response from server: %s" % str(data)) |
278 |
return data |
|
290 |
|
|
291 |
if not data[KEY_SUCCESS]: |
|
292 |
# TODO: decide on a standard exception |
|
293 |
raise RequestError(data[KEY_RESULT]) |
|
294 |
|
|
295 |
return data[KEY_RESULT] |
|
279 | 296 |
|
280 | 297 |
def SubmitJob(self, job): |
281 | 298 |
"""Submit a job""" |
282 |
result = self.SendRequest(REQ_SUBMIT, SerializeJob(job)) |
|
283 |
if not result['success']: |
|
284 |
raise RequestError(result['result']) |
|
285 |
return result['result'] |
|
299 |
return self.CallMethod(REQ_SUBMIT, SerializeJob(job)) |
|
286 | 300 |
|
287 | 301 |
def Query(self, data): |
288 | 302 |
"""Make a query""" |
289 |
result = self.SendRequest(REQ_QUERY, data) |
|
290 |
if not result['success']: |
|
291 |
raise RequestError(result[result]) |
|
292 |
result = result['result'] |
|
303 |
result = self.CallMethod(REQ_QUERY, data) |
|
293 | 304 |
if data["object"] == "jobs": |
294 | 305 |
# custom job processing of query values |
295 | 306 |
for row in result: |
... | ... | |
297 | 308 |
if field == "op_list": |
298 | 309 |
row[idx] = [opcodes.OpCode.LoadOpCode(i) for i in row[idx]] |
299 | 310 |
return result |
311 |
|
|
312 |
# TODO: class Server(object) |
Also available in: Unified diff