R&D/클라우드

CloudStack python openapi

sunshout 2012. 11. 21. 14:52

Python 코딩에 대한 상당한 이해도를 가지고 있는 것 같다.

참조


    def make_request_with_auth(self, command, requests={}):

        requests["command"] = command

        requests["apiKey"] = self.apiKey

        requests["response"] = "json"

        request = zip(requests.keys(), requests.values())

        request.sort(key=lambda x: str.lower(x[0]))


        requestUrl = "&".join(["=".join([r[0], urllib.quote_plus(str(r[1]))]) for r in request])

        hashStr = "&".join(["=".join([str.lower(r[0]), str.lower(urllib.quote_plus(str(r[1]))).replace("+", "%20")]) for r in request])


        sig = urllib.quote_plus(base64.encodestring(hmac.new(self.securityKey, hashStr, hashlib.sha1).digest()).strip())

        requestUrl += "&signature=%s"%sig


        try:

            self.connection = urllib2.urlopen("http://%s:%d/client/api?%s"%(self.mgtSvr, self.port, requestUrl))

            if self.logging is not None:

                self.logging.debug("sending GET request: %s"%requestUrl)

            response = self.connection.read()

            if self.logging is not None:

                self.logging.info("got response: %s"%response)

        except IOError, e:

            if hasattr(e, 'reason'):

                if self.logging is not None:

                    self.logging.critical("failed to reach %s because of %s"%(self.mgtSvr, e.reason))

            elif hasattr(e, 'code'):

                if self.logging is not None:

                    self.logging.critical("server returned %d error code"%e.code)

        except httplib.HTTPException, h:

            if self.logging is not None:

                self.logging.debug("encountered http Exception %s"%h.args)

            if self.retries > 0:

                self.retries = self.retries - 1

                self.make_request_with_auth(command, requests)

            else:

                self.retries = 5

                raise h

        else:

            return response