Skip to content

Commit c75b11d

Browse files
grudzienyadvr
authored andcommitted
CLOUDSTACK-1875: add JSON output to cloudmonkey
Added 1. display = [default|json|tabularize] has been added in the config to replace tabularize = [true|false] 2. tabularize is deprecated but we will still set it as "false" once the user removes it out of their config to avoid throwing an error. This will be removed in the next major version. 3. display = "default" is added to the [ui] section of the config if it is not present. 4. You can now output JSON formatted text by setting the config display = json 5. You can now filter text in JSON output mode. (i.e. list users account=grudzien filter=account,id,email). Filtered output returns a properly formatted JSON document. Removed 1. Removed the printing of attr keys in read_config(). Deprecated 1. tabularize = [true|false] is now messaged as deprecated. Signed-off-by: Justin Grudzien <grudzien@gmail.com>
1 parent 63fa086 commit c75b11d

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

tools/cli/cloudmonkey/cloudmonkey.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import shlex
2828
import sys
2929
import types
30+
import copy
3031

3132
from cachemaker import loadcache, savecache, monkeycache, splitverbsubject
3233
from config import __version__, __description__, __projecturl__
@@ -162,6 +163,44 @@ def printer_helper(printer, toprow):
162163
self.monkeyprint(printer)
163164
return PrettyTable(toprow)
164165

166+
# method: print_result_json( result, result_filter )
167+
# parameters: result - raw results from the API call
168+
# result_filter - filterset
169+
# description: prints result as a json object
170+
def print_result_json(result, result_filter=None):
171+
tfilter = {} # temp var to hold a dict of the filters
172+
tresult = copy.deepcopy(result) # dupe the result to filter
173+
if result_filter != None:
174+
for res in result_filter:
175+
tfilter[ res ] = 1
176+
myresults = {}
177+
for okey, oval in result.iteritems():
178+
if isinstance( oval, dict ):
179+
for tkey in x:
180+
if tkey not in tfilter:
181+
try:
182+
del( tresult[okey][x][tkey] )
183+
except:
184+
pass
185+
elif isinstance( oval, list ):
186+
for x in range( len( oval ) ):
187+
if isinstance( oval[x], dict ):
188+
for tkey in oval[x]:
189+
if tkey not in tfilter:
190+
try:
191+
del( tresult[okey][x][tkey] )
192+
except:
193+
pass
194+
else:
195+
try:
196+
del( tresult[ okey ][ x ] )
197+
except:
198+
pass
199+
print json.dumps(tresult,
200+
sort_keys=True,
201+
indent=2,
202+
separators=(',', ': '))
203+
165204
def print_result_tabular(result, result_filter=None):
166205
toprow = None
167206
printer = None
@@ -183,6 +222,12 @@ def print_result_tabular(result, result_filter=None):
183222
self.monkeyprint(printer)
184223

185224
def print_result_as_dict(result, result_filter=None):
225+
226+
# tabularize overrides self.display
227+
if self.display == "json" and not self.tabularize == "true":
228+
print_result_json(result, result_filter)
229+
return
230+
186231
for key in sorted(result.keys(), key=lambda x:
187232
x not in ['id', 'count', 'name'] and x):
188233
if not (isinstance(result[key], list) or
@@ -195,7 +240,7 @@ def print_result_as_dict(result, result_filter=None):
195240
def print_result_as_list(result, result_filter=None):
196241
for node in result:
197242
# Tabular print if it's a list of dict and tabularize is true
198-
if isinstance(node, dict) and self.tabularize == 'true':
243+
if isinstance(node, dict) and (self.display == 'tabularize' or self.tabularize == 'true'):
199244
print_result_tabular(result, result_filter)
200245
break
201246
self.print_result(node)
@@ -318,7 +363,7 @@ def completedefault(self, text, line, begidx, endidx):
318363
autocompletions = uuids
319364
search_string = value
320365

321-
if self.tabularize == "true" and subject != "":
366+
if (self.display == "tabularize" or self.display == "json" or self.tabularize == "true") and subject != "":
322367
autocompletions.append("filter=")
323368
return [s for s in autocompletions if s.startswith(search_string)]
324369

@@ -459,7 +504,6 @@ def do_quit(self, args):
459504
self.monkeyprint("Bye!")
460505
return self.do_EOF(args)
461506

462-
463507
class MonkeyParser(OptionParser):
464508
def format_help(self, formatter=None):
465509
if formatter is None:
@@ -473,7 +517,6 @@ def format_help(self, formatter=None):
473517
result.append("\nTry cloudmonkey [help|?]\n")
474518
return "".join(result)
475519

476-
477520
def main():
478521
parser = MonkeyParser()
479522
parser.add_option("-c", "--config-file",

tools/cli/cloudmonkey/config.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
# ui
5757
config_fields['ui']['color'] = 'true'
5858
config_fields['ui']['prompt'] = '> '
59-
config_fields['ui']['tabularize'] = 'false'
59+
config_fields['ui']['tabularize'] = 'false' # deprecate - REMOVE
60+
config_fields['ui']['display'] = 'default' # default display mechanism
6061

6162
# server
6263
config_fields['server']['host'] = 'localhost'
@@ -111,9 +112,19 @@ def read_config(get_attr, set_attr, config_file):
111112
for section in config_fields.keys():
112113
for key in config_fields[section].keys():
113114
try:
115+
if( key == "tabularize" ): # this key is deprecated
116+
print "\ntabularize config parameter is deprecated:",
117+
print "please switch to display =",
118+
print "[default,json,tabularize]\n"
114119
set_attr(key, config.get(section, key))
115120
except Exception:
116-
missing_keys.append(key)
121+
if( key == "tabularize" ): # this key is deprecated
122+
set_attr( key, "false" ) # set default
123+
elif( key == "display" ): # this key is deprecated
124+
config = write_config(get_attr, config_file, True)
125+
set_attr( key, "default" ) # set default
126+
else:
127+
missing_keys.append(key)
117128

118129
if len(missing_keys) > 0:
119130
print "Please fix `%s` in %s" % (', '.join(missing_keys), config_file)

0 commit comments

Comments
 (0)