def marvin_request(self, cmd, response_type=None, method='GET', data=''):
        """
        Requester for marvin command objects
        @param cmd: marvin's command from cloudstackAPI
        @param response_type: response type of the command in cmd
        @param method: HTTP GET/POST, defaults to GET
        @return:
        """
        cmdname, isAsync, payload = self.sanitize_command(cmd)
        self.logging.debug("sending %s request: %s %s" % (method, cmdname,
                                                          str(payload)))
        response = self.request(
            cmdname, self.auth, payload=payload, method=method)
        self.logging.debug("Request: %s Response: %s" %
                           (response.url, response.text))
        try:
            response = jsonHelper.getResultObj(response.json(), response_type)
        except TypeError:
            response = jsonHelper.getResultObj(response.json, response_type)

        if isAsync == "false":
            return response
        else:
            asyncJobId = response.jobid
            response = self.poll(asyncJobId, response_type)
            return response.jobresult
Ejemplo n.º 2
0
    def __parseAndGetResponse(self, cmd_response, response_cls, is_async):
        '''
        @Name : __parseAndGetResponse
        @Desc : Verifies the  Response(from CS) and returns an
                appropriate json parsed Response
        @Input: cmd_response: Command Response from cs
                response_cls : Mapping class for this Response
                is_async: Whether the cmd is async or not.
        @Output:Response output from CS
        '''
        try:
            try:
                ret = jsonHelper.getResultObj(
                    cmd_response.json(),
                    response_cls)
            except TypeError:
                ret = jsonHelper.getResultObj(cmd_response.json, response_cls)

            '''
            If the response is asynchronous, poll and return response
            else return response as it is
            '''
            if is_async == "false":
                self.logger.debug("Response : %s" % str(ret))
                return ret
            else:
                response = self.__poll(ret.jobid, response_cls)
                self.logger.debug("Response : %s" % str(response))
                return response.jobresult if response != FAILED else FAILED
        except Exception as e:
            self.__lastError = e
            self.logger.\
                exception("Exception:%s" % GetDetailExceptionInfo(e))
            return FAILED
Ejemplo n.º 3
0
    def marvin_request(self, cmd, response_type=None, method='GET'):
        """
        Requester for marvin command objects
        @param cmd: marvin's command from cloudstackAPI
        @param response_type: response type of the command in cmd
        @param method: HTTP GET/POST, defaults to GET
        @return:
        """
        cmdname, isAsync, payload = self.sanitize_command(cmd)
        self.logging.debug("sending %s request: %s %s" %
                           (method, cmdname, str(payload)))
        response = self.request(cmdname,
                                self.auth,
                                payload=payload,
                                method=method)
        self.logging.debug("Request: %s Response: %s" %
                           (response.url, response.text))
        try:
            response = jsonHelper.getResultObj(response.json(), response_type)
        except TypeError:
            response = jsonHelper.getResultObj(response.json, response_type)

        if isAsync == "false":
            return response
        else:
            asyncJobId = response.jobid
            response = self.poll(asyncJobId, response_type)
            return response.jobresult
Ejemplo n.º 4
0
 def __parseAndGetResponse(self, cmd_response, response_cls, is_async):
     '''
     @Name : __parseAndGetResponse
     @Desc : Verifies the  Response(from CS) and returns an
             appropriate json parsed Response
     @Input: cmd_response: Command Response from cs
             response_cls : Mapping class for this Response
             is_async: Whether the cmd is async or not.
     @Output:Response output from CS
     '''
     try:
         try:
             ret = jsonHelper.getResultObj(cmd_response.json(),
                                           response_cls)
         except TypeError:
             ret = jsonHelper.getResultObj(cmd_response.json, response_cls)
         '''
         If the response is asynchronous, poll and return response
         else return response as it is
         '''
         if is_async == "false":
             self.logger.debug("Response: %s" %
                               self.pretty_printer.pformat(ret))
             return ret
         else:
             response = self.__poll(ret.jobid, response_cls)
             if response == FAILED:
                 self.logger.debug("Response: %s" %
                                   self.pretty_printer.pformat(response))
                 return FAILED
             else:
                 self.logger.debug("Job command is %s and result is %s" %
                                   (response.cmd, response.jobresult))
                 return response.jobresult
     except CloudstackAPIException as e:
         """ CloudstackAPIException might be intentional. We don't log them on a debug level. """
         self.__lastError = e
         self.logger.debug("Exception: %s" % e)
         return FAILED
     except Exception as e:
         self.__lastError = e
         self.logger.exception("Exception: %s" % e)
         return FAILED
Ejemplo n.º 5
0
 def make_request(self, cmd, response = None, raw=False):
     commandName = cmd.__class__.__name__.replace("Cmd", "")
     isAsync = "false"
     requests = {}
     required = []
     for attribute in dir(cmd):
         if attribute != "__doc__" and attribute != "__init__" and attribute != "__module__":
             if attribute == "isAsync":
                 isAsync = getattr(cmd, attribute)
             elif attribute == "required":
                 required = getattr(cmd, attribute)
             else:
                 requests[attribute] = getattr(cmd, attribute)
     
     for requiredPara in required:
         if requests[requiredPara] is None:
             raise cloudstackException.cloudstackAPIException(commandName, "%s is required"%requiredPara)
     '''remove none value'''
     for param, value in requests.items():
         if value is None:
             requests.pop(param)
         elif isinstance(value, list):
             if len(value) == 0:
                 requests.pop(param)
             else:
                 if not isinstance(value[0], dict):
                     requests[param] = ",".join(value)
                 else:
                     requests.pop(param)
                     i = 0
                     for v in value:
                         for key, val in v.iteritems():
                             requests["%s[%d].%s"%(param,i,key)] = val
                         i = i + 1
     
     if self.logging is not None:
         self.logging.debug("sending command: %s %s"%(commandName, str(requests)))
     result = None
     if self.auth:
         result = self.make_request_with_auth(commandName, requests)
     else:
         result = self.make_request_without_auth(commandName, requests)
     
     if self.logging is not None:
         self.logging.debug("got result: "  + result)
     if result is None:
         return None
     
     result = jsonHelper.getResultObj(result, response)
     if raw or isAsync == "false":
         return result
     else:
         asynJobId = result.jobid
         result = self.pollAsyncJob(asynJobId, response)
         return result.jobresult
    def make_request(self, cmd, response=None, raw=False):
        commandName = cmd.__class__.__name__.replace("Cmd", "")
        isAsync = "false"
        requests = {}
        required = []
        for attribute in dir(cmd):
            if attribute != "__doc__" and attribute != "__init__" and attribute != "__module__":
                if attribute == "isAsync":
                    isAsync = getattr(cmd, attribute)
                elif attribute == "required":
                    required = getattr(cmd, attribute)
                else:
                    requests[attribute] = getattr(cmd, attribute)

        for requiredPara in required:
            if requests[requiredPara] is None:
                raise cloudstackException.cloudstackAPIException(
                    commandName, "%s is required" % requiredPara)
        '''remove none value'''
        for param, value in requests.items():
            if value is None:
                requests.pop(param)
            elif isinstance(value, list):
                if len(value) == 0:
                    requests.pop(param)
                else:
                    if not isinstance(value[0], dict):
                        requests[param] = ",".join(value)
                    else:
                        requests.pop(param)
                        i = 0
                        for v in value:
                            for key, val in v.iteritems():
                                requests["%s[%d].%s" % (param, i, key)] = val
                            i = i + 1

        if self.logging is not None:
            self.logging.info("sending command: %s %s" %
                              (commandName, str(requests)))
        result = None
        if self.auth:
            result = self.make_request_with_auth(commandName, requests)
        else:
            result = self.make_request_without_auth(commandName, requests)

        if result is None:
            return None

        result = jsonHelper.getResultObj(result, response)
        if raw or isAsync == "false":
            return result
        else:
            asynJobId = result.jobid
            result = self.pollAsyncJob(asynJobId, response)
            return result.jobresult
    def __parseAndGetResponse(self, cmd_response, response_cls, is_async):
        '''
        @Name : __parseAndGetResponse
        @Desc : Verifies the  Response(from CS) and returns an
                appropriate json parsed Response
        @Input: cmd_response: Command Response from cs
                response_cls : Mapping class for this Response
                is_async: Whether the cmd is async or not.
        @Output:Response output from CS
        '''
        try:
            try:
                ret = jsonHelper.getResultObj(cmd_response.json(), response_cls)
            except TypeError:
                ret = jsonHelper.getResultObj(cmd_response.json, response_cls)

            '''
            If the response is asynchronous, poll and return response
            else return response as it is
            '''
            if is_async == "false":
                self.logger.debug("Response: %s" % self.pretty_printer.pformat(ret))
                return ret
            else:
                response = self.__poll(ret.jobid, response_cls)
                if response == FAILED:
                    self.logger.debug("Response: %s" % self.pretty_printer.pformat(response))
                    return FAILED
                else:
                    self.logger.debug("Job command is %s and result is %s" % (response.cmd, response.jobresult))
                    return response.jobresult
        except CloudstackAPIException as e:
            """ CloudstackAPIException might be intentional. We don't log them on a debug level. """
            self.__lastError = e
            self.logger.debug("Exception: %s" % e)
            return FAILED
        except Exception as e:
            self.__lastError = e
            self.logger.exception("Exception: %s" % e)
            return FAILED