Skip to content
This repository has been archived by the owner on Aug 22, 2020. It is now read-only.

Added logic to return Cloudstack API error information #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions CloudStack/BaseClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,37 @@ def request(self, command, args):
args['apikey'] = self.apikey
args['command'] = command
args['response'] = 'json'

params=[]

keys = sorted(args.keys())

for k in keys:
params.append(k + '=' + urllib.quote_plus(args[k]).replace("+", "%20"))

query = '&'.join(params)

signature = base64.b64encode(hmac.new(
self.secret,
msg=query.lower(),
self.secret,
msg=query.lower(),
digestmod=hashlib.sha1
).digest())

query += '&signature=' + urllib.quote_plus(signature)

response = urllib2.urlopen(self.api + '?' + query)
try:
response = urllib2.urlopen(self.api + '?' + query)
except urllib2.HTTPError as error:
error_msg = ''
error_data = json.loads(error.read())
if len(error_data) == 1:
error_msg = 'ERROR: %s - %s' % (error_data.keys()[0],error_data[error_data.keys()[0]]['errortext'])
else:
error_msg = 'ERROR: Recieved muliaple errors.'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

muliaple == multiple?

raise RuntimeError(error_msg)

decoded = json.loads(response.read())

propertyResponse = command.lower() + 'response'
if not propertyResponse in decoded:
if 'errorresponse' in decoded:
Expand Down