X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/c2a03789be0213c07e56464222066adccfa5f051..acec9d51f4aea4b4571fcee477bea935487b8b83:/lib/luxi.py diff --git a/lib/luxi.py b/lib/luxi.py index 9c27ad7..df9ed89 100644 --- a/lib/luxi.py +++ b/lib/luxi.py @@ -35,6 +35,7 @@ import simplejson import time from ganeti import opcodes +from ganeti import constants KEY_REQUEST = 'request' @@ -67,6 +68,19 @@ class DecodingError(ProtocolError): """Decoding failure on the receiving side""" +class RequestError(ProtocolError): + """Error on request + + This signifies an error in the request format or request handling, + but not (e.g.) an error in starting up an instance. + + Some common conditions that can trigger this exception: + - job submission failed because the job data was wrong + - query failed because required fields were missing + + """ + + def SerializeJob(job): """Convert a job description to a string format. @@ -216,7 +230,7 @@ class Client(object): implements data serialization/deserialization. """ - def __init__(self, address, timeouts=None, transport=Transport): + def __init__(self, address=None, timeouts=None, transport=Transport): """Constructor for the Client class. Arguments: @@ -229,6 +243,8 @@ class Client(object): class are used. """ + if address is None: + address = constants.MASTER_SOCKET self.transport = transport(address, timeouts=timeouts) def SendRequest(self, request, data): @@ -241,12 +257,29 @@ class Client(object): data = simplejson.loads(result) except Exception, err: raise ProtocolError("Error while deserializing response: %s" % str(err)) + if (not isinstance(data, dict) or + 'success' not in data or + 'result' not in data): + raise DecodingError("Invalid response from server: %s" % str(data)) return data def SubmitJob(self, job): """Submit a job""" - return self.SendRequest(REQ_SUBMIT, SerializeJob(job)) + result = self.SendRequest(REQ_SUBMIT, SerializeJob(job)) + if not result['success']: + raise RequestError(result['result']) + return result['result'] def Query(self, data): """Make a query""" - return self.SendRequest(REQ_QUERY, data) + result = self.SendRequest(REQ_QUERY, data) + if not result['success']: + raise RequestError(result[result]) + result = result['result'] + if data["object"] == "jobs": + # custom job processing of query values + for row in result: + for idx, field in enumerate(data["fields"]): + if field == "op_list": + row[idx] = [opcodes.OpCode.LoadOpCode(i) for i in row[idx]] + return result