You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by pr...@apache.org on 2013/04/10 00:49:02 UTC

[22/26] git commit: updated refs/heads/affinity_groups to 90172c3

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 <gr...@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/c75b11d1
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/c75b11d1
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/c75b11d1

Branch: refs/heads/affinity_groups
Commit: c75b11d13a49065554d492d5ed2fafc6bff9493e
Parents: 63fa086
Author: Justin Grudzien <gr...@gmail.com>
Authored: Wed Apr 3 15:00:00 2013 -0500
Committer: Rohit Yadav <bh...@apache.org>
Committed: Fri Apr 5 22:48:15 2013 +0530

----------------------------------------------------------------------
 tools/cli/cloudmonkey/cloudmonkey.py |   51 ++++++++++++++++++++++++++--
 tools/cli/cloudmonkey/config.py      |   15 +++++++-
 2 files changed, 60 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c75b11d1/tools/cli/cloudmonkey/cloudmonkey.py
----------------------------------------------------------------------
diff --git a/tools/cli/cloudmonkey/cloudmonkey.py b/tools/cli/cloudmonkey/cloudmonkey.py
index 94006c9..842af26 100644
--- a/tools/cli/cloudmonkey/cloudmonkey.py
+++ b/tools/cli/cloudmonkey/cloudmonkey.py
@@ -27,6 +27,7 @@ try:
     import shlex
     import sys
     import types
+    import copy
 
     from cachemaker import loadcache, savecache, monkeycache, splitverbsubject
     from config import __version__, __description__, __projecturl__
@@ -162,6 +163,44 @@ class CloudMonkeyShell(cmd.Cmd, object):
                 self.monkeyprint(printer)
             return PrettyTable(toprow)
 
+        # method: print_result_json( result, result_filter )
+        # parameters: result - raw results from the API call
+        #             result_filter - filterset
+        # description: prints result as a json object
+        def print_result_json(result, result_filter=None):
+            tfilter = {} # temp var to hold a dict of the filters
+            tresult = copy.deepcopy(result) # dupe the result to filter
+            if result_filter != None:
+                for res in result_filter:
+                    tfilter[ res ] = 1
+                myresults = {}
+                for okey, oval in result.iteritems():
+                    if isinstance( oval, dict ):
+                        for tkey in x:
+                            if tkey not in tfilter:
+                                try:
+                                    del( tresult[okey][x][tkey] )
+                                except:
+                                    pass
+                    elif isinstance( oval, list ):
+                        for x in range( len( oval ) ):
+                            if isinstance( oval[x], dict ):
+                                for tkey in oval[x]:
+                                    if tkey not in tfilter:
+                                        try:
+                                            del( tresult[okey][x][tkey] )
+                                        except:
+                                            pass
+                            else:
+                                try:
+                                    del( tresult[ okey ][ x ] )
+                                except:
+                                    pass
+            print json.dumps(tresult,
+                             sort_keys=True,
+                             indent=2,
+                             separators=(',', ': '))
+
         def print_result_tabular(result, result_filter=None):
             toprow = None
             printer = None
@@ -183,6 +222,12 @@ class CloudMonkeyShell(cmd.Cmd, object):
                 self.monkeyprint(printer)
 
         def print_result_as_dict(result, result_filter=None):
+
+            # tabularize overrides self.display
+            if self.display == "json" and not self.tabularize == "true":
+                print_result_json(result, result_filter)
+                return
+
             for key in sorted(result.keys(), key=lambda x:
                               x not in ['id', 'count', 'name'] and x):
                 if not (isinstance(result[key], list) or
@@ -195,7 +240,7 @@ class CloudMonkeyShell(cmd.Cmd, object):
         def print_result_as_list(result, result_filter=None):
             for node in result:
                 # Tabular print if it's a list of dict and tabularize is true
-                if isinstance(node, dict) and self.tabularize == 'true':
+                if isinstance(node, dict) and (self.display == 'tabularize' or self.tabularize == 'true'):
                     print_result_tabular(result, result_filter)
                     break
                 self.print_result(node)
@@ -318,7 +363,7 @@ class CloudMonkeyShell(cmd.Cmd, object):
                     autocompletions = uuids
                     search_string = value
 
-        if self.tabularize == "true" and subject != "":
+        if (self.display == "tabularize" or self.display == "json" or self.tabularize == "true") and subject != "":
             autocompletions.append("filter=")
         return [s for s in autocompletions if s.startswith(search_string)]
 
@@ -459,7 +504,6 @@ class CloudMonkeyShell(cmd.Cmd, object):
         self.monkeyprint("Bye!")
         return self.do_EOF(args)
 
-
 class MonkeyParser(OptionParser):
     def format_help(self, formatter=None):
         if formatter is None:
@@ -473,7 +517,6 @@ class MonkeyParser(OptionParser):
         result.append("\nTry cloudmonkey [help|?]\n")
         return "".join(result)
 
-
 def main():
     parser = MonkeyParser()
     parser.add_option("-c", "--config-file",

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c75b11d1/tools/cli/cloudmonkey/config.py
----------------------------------------------------------------------
diff --git a/tools/cli/cloudmonkey/config.py b/tools/cli/cloudmonkey/config.py
index 75605df..b21ae4c 100644
--- a/tools/cli/cloudmonkey/config.py
+++ b/tools/cli/cloudmonkey/config.py
@@ -56,7 +56,8 @@ config_fields['core']['log_file'] = expanduser(config_dir + '/log')
 # ui
 config_fields['ui']['color'] = 'true'
 config_fields['ui']['prompt'] = '> '
-config_fields['ui']['tabularize'] = 'false'
+config_fields['ui']['tabularize'] = 'false' # deprecate - REMOVE
+config_fields['ui']['display'] = 'default' # default display mechanism
 
 # server
 config_fields['server']['host'] = 'localhost'
@@ -111,9 +112,19 @@ def read_config(get_attr, set_attr, config_file):
     for section in config_fields.keys():
         for key in config_fields[section].keys():
             try:
+                if( key == "tabularize" ): # this key is deprecated
+                    print "\ntabularize config parameter is deprecated:",
+                    print "please switch to display =",
+                    print "[default,json,tabularize]\n"
                 set_attr(key, config.get(section, key))
             except Exception:
-                missing_keys.append(key)
+                if( key == "tabularize" ): # this key is deprecated
+                    set_attr( key, "false" ) # set default
+                elif( key == "display" ): # this key is deprecated
+                    config = write_config(get_attr, config_file, True)
+                    set_attr( key, "default" ) # set default
+                else:
+                    missing_keys.append(key)
 
     if len(missing_keys) > 0:
         print "Please fix `%s` in %s" % (', '.join(missing_keys), config_file)