Updated Branches: refs/heads/master e03176c4d -> b12905b7a
pep8 compliance for cloudstackConnection Signed-off-by: Prasanna Santhanam <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/b12905b7 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/b12905b7 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/b12905b7 Branch: refs/heads/master Commit: b12905b7a828a0766a521ca9859158d4356405ed Parents: e03176c Author: Prasanna Santhanam <[email protected]> Authored: Thu Apr 18 07:44:25 2013 +0530 Committer: Prasanna Santhanam <[email protected]> Committed: Thu Apr 18 07:44:25 2013 +0530 ---------------------------------------------------------------------- test/integration/smoke/test_public_ip_range.py | 2 +- tools/marvin/marvin/cloudstackConnection.py | 92 ++++++++++++------- 2 files changed, 58 insertions(+), 36 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b12905b7/test/integration/smoke/test_public_ip_range.py ---------------------------------------------------------------------- diff --git a/test/integration/smoke/test_public_ip_range.py b/test/integration/smoke/test_public_ip_range.py index a7aad6b..7c965ea 100644 --- a/test/integration/smoke/test_public_ip_range.py +++ b/test/integration/smoke/test_public_ip_range.py @@ -49,7 +49,7 @@ class Services: "endip": "10.102.197.73", "zoneid": "1", "podid": "", - "vlan": "101", + "vlan": "4444", } class TesDedicatePublicIPRange(cloudstackTestCase): http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b12905b7/tools/marvin/marvin/cloudstackConnection.py ---------------------------------------------------------------------- diff --git a/tools/marvin/marvin/cloudstackConnection.py b/tools/marvin/marvin/cloudstackConnection.py index 5fd3e12..14b12e7 100644 --- a/tools/marvin/marvin/cloudstackConnection.py +++ b/tools/marvin/marvin/cloudstackConnection.py @@ -5,9 +5,9 @@ # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -22,16 +22,20 @@ import hmac import hashlib import time import cloudstackException -from cloudstackAPI import * +from cloudstackAPI import * import jsonHelper from requests import ConnectionError from requests import HTTPError from requests import Timeout from requests import RequestException + class cloudConnection(object): - def __init__(self, mgtSvr, port=8096, apiKey=None, securityKey=None, asyncTimeout=3600, logging=None, - scheme='http', path='client/api'): + """ Connections to make API calls to the cloudstack management server + """ + def __init__(self, mgtSvr, port=8096, apiKey=None, securityKey=None, + asyncTimeout=3600, logging=None, scheme='http', + path='client/api'): self.apiKey = apiKey self.securityKey = securityKey self.mgtSvr = mgtSvr @@ -42,16 +46,18 @@ class cloudConnection(object): self.asyncTimeout = asyncTimeout self.auth = True if port == 8096 or \ - (self.apiKey == None and self.securityKey == None): + (self.apiKey is None and self.securityKey is None): self.auth = False if scheme not in ['http', 'https']: raise RequestException("Protocol must be HTTP") self.protocol = scheme - self.baseurl = "%s://%s:%d/%s"%(self.protocol, self.mgtSvr, self.port, self.path) + self.baseurl = "%s://%s:%d/%s"\ + % (self.protocol, self.mgtSvr, self.port, self.path) def __copy__(self): - return cloudConnection(self.mgtSvr, self.port, self.apiKey, self.securityKey, self.asyncTimeout, - self.logging, self.protocol, self.path) + return cloudConnection(self.mgtSvr, self.port, self.apiKey, + self.securityKey, self.asyncTimeout, + self.logging, self.protocol, self.path) def poll(self, jobid, response): """ @@ -68,16 +74,19 @@ class cloudConnection(object): asyncResonse = self.marvin_request(cmd, response_type=response) if asyncResonse.jobstatus == 2: - raise cloudstackException.cloudstackAPIException("asyncquery", asyncResonse.jobresult) + raise cloudstackException.cloudstackAPIException( + "asyncquery", asyncResonse.jobresult) elif asyncResonse.jobstatus == 1: return asyncResonse time.sleep(5) if self.logging is not None: - self.logging.debug("job: %s still processing, will timeout in %ds"%(jobid, timeout)) + self.logging.debug("job: %s still processing," + " will timeout in %ds" % (jobid, timeout)) timeout = timeout - 5 - raise cloudstackException.cloudstackAPIException("asyncquery", "Async job timeout %s"%jobid) + raise cloudstackException.cloudstackAPIException( + "asyncquery", "Async job timeout %s" % jobid) def sign(self, payload): """ @@ -90,19 +99,26 @@ class cloudConnection(object): params.sort(key=lambda k: str.lower(k[0])) hashStr = "&".join( ["=".join( - [str.lower(r[0]), str.lower(urllib.quote_plus(str(r[1]))).replace("+", "%20")] + [str.lower(r[0]), + str.lower( + urllib.quote_plus(str(r[1])) + ).replace("+", "%20")] ) for r in params] ) - signature = base64.encodestring(hmac.new(self.securityKey, hashStr, hashlib.sha1).digest()).strip() - self.logging.info("Computed Signature by Marvin: %s"%signature) + signature = base64.encodestring(hmac.new( + self.securityKey, hashStr, hashlib.sha1).digest()).strip() + self.logging.info("Computed Signature by Marvin: %s" % signature) return signature def request(self, command, auth=True, payload={}, data={}): """ - Makes requests on the `integration.api.port` - @param command: cloudstack API command name eg: deployVirtualMachineCommand - @param auth: Authentication (apikey,secretKey) => True, else False - @param payload: GET param data composed as a dictionary of key,value pairs + Makes requests using auth or over integration port + @param command: cloudstack API command name + eg: deployVirtualMachineCommand + @param auth: Authentication (apikey,secretKey) => True + else False for integration.api.port + @param payload: GET param data composed as a dictionary + of key,value pairs @param data: POST data as a dictionary @return: """ @@ -114,23 +130,24 @@ class cloudConnection(object): signature = self.sign(payload) payload["signature"] = signature - try: if data: - response = requests.get(self.baseurl, params=payload, data=data) + response = requests.get(self.baseurl, params=payload, + data=data) else: response = requests.get(self.baseurl, params=payload) except ConnectionError, c: - self.logging.debug("Connection refused. Reason: %s"%(self.baseurl, c)) + self.logging.debug("Connection refused. Reason: %s" % + (self.baseurl, c)) raise c except HTTPError, h: - self.logging.debug("Server returned error code: %s"%h) + self.logging.debug("Server returned error code: %s" % h) raise h except Timeout, t: - self.logging.debug("Connection timed out with %s"%t) + self.logging.debug("Connection timed out with %s" % t) raise t - except RequestException,r: - self.logging.debug("Error returned by server %s"%r) + except RequestException, r: + self.logging.debug("Error returned by server %s" % r) raise r else: return response @@ -144,7 +161,8 @@ class cloudConnection(object): requests = {} required = [] for attribute in dir(cmd): - if attribute != "__doc__" and attribute != "__init__" and attribute != "__module__": + if attribute != "__doc__" and attribute != "__init__" and \ + attribute != "__module__": if attribute == "isAsync": isAsync = getattr(cmd, attribute) elif attribute == "required": @@ -155,7 +173,8 @@ class cloudConnection(object): cmdname = cmd.__class__.__name__.replace("Cmd", "") for requiredPara in required: if requests[requiredPara] is None: - raise cloudstackException.cloudstackAPIException(cmdname, "%s is required"%requiredPara) + raise cloudstackException.cloudstackAPIException( + cmdname, "%s is required" % requiredPara) for param, value in requests.items(): if value is None: requests.pop(param) @@ -169,8 +188,8 @@ class cloudConnection(object): requests.pop(param) i = 0 for val in value: - for k,v in val.iteritems(): - requests["%s[%d].%s"%(param,i,k)] = v + for k, v in val.iteritems(): + requests["%s[%d].%s" % (param, i, k)] = v i = i + 1 return cmdname, isAsync, requests @@ -184,14 +203,17 @@ class cloudConnection(object): @return: """ cmdname, isAsync, payload = self.sanitize_command(cmd) - self.logging.info("sending command: %s %s"%(cmdname, str(payload))) + self.logging.info("sending command: %s %s" % (cmdname, str(payload))) if self.auth: - response = self.request(cmdname, auth=True, payload=payload, data=data) + response = self.request( + cmdname, auth=True, payload=payload, data=data) else: - response = self.request(cmdname, auth=False, payload=payload, data=data) + response = self.request( + cmdname, auth=False, payload=payload, data=data) - self.logging.info("Request: %s Response: %s"%(response.url, response.text)) - response = jsonHelper.getResultObj(response.json, response_type) + self.logging.info("Request: %s Response: %s" % + (response.url, response.text)) + response = jsonHelper.getResultObj(response.json(), response_type) if isAsync == "false": return response
