Skip to content

Commit 9a66beb

Browse files
committed
CLOUDSTACK-717: Fix response json handling in cloudmonkey
- Fixes response handling - Sorts alphabetically, count and id are on top if available - Fix colors Signed-off-by: Rohit Yadav <bhaisaab@apache.org>
1 parent 569ca6d commit 9a66beb

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

‎tools/cli/cloudmonkey/cloudmonkey.py‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ def print_shell(self, *args):
160160
elif 'type' in arg:
161161
print colored.green(arg),
162162
elif 'state' in arg or 'count' in arg:
163-
print colored.yellow(arg),
163+
print colored.magenta(arg),
164164
elif 'id =' in arg:
165-
print colored.cyan(arg),
165+
print colored.yellow(arg),
166166
elif 'name =' in arg:
167-
print colored.magenta(arg),
167+
print colored.cyan(arg),
168168
else:
169169
print arg,
170170
else:
@@ -203,7 +203,8 @@ def print_result_tabular(result, result_filter=None):
203203
print printer
204204

205205
def print_result_as_dict(result, result_filter=None):
206-
for key in result.keys():
206+
for key in sorted(result.keys(),
207+
key=lambda x: x!='id' and x!='count' and x):
207208
if not (isinstance(result[key], list) or
208209
isinstance(result[key], dict)):
209210
self.print_shell("%s = %s" % (key, result[key]))
@@ -268,7 +269,11 @@ def process_json(response):
268269
while timeout > 0:
269270
response = process_json(conn.make_request_with_auth(command,
270271
requests))
271-
result = response[response.keys()[0]]
272+
responsekeys = filter(lambda x: 'response' in x,
273+
response.keys())
274+
if len(responsekeys) < 1:
275+
continue
276+
result = response[responsekeys[0]]
272277
jobstatus = result['jobstatus']
273278
if jobstatus == 2:
274279
jobresult = result["jobresult"]
@@ -346,9 +351,9 @@ def default(self, args):
346351
if result is None:
347352
return
348353
try:
349-
# Response is in the key "apiname+response" (lowercase)
350-
self.print_result(result[api_name.lower() + 'response'],
351-
field_filter)
354+
responsekeys = filter(lambda x: 'response' in x, result.keys())
355+
for responsekey in responsekeys:
356+
self.print_result(result[responsekey], field_filter)
352357
print
353358
except Exception as e:
354359
self.print_shell("🙈 Error on parsing and printing", e)

0 commit comments

Comments
 (0)