You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ro...@apache.org on 2019/03/05 09:59:35 UTC

[cloudstack-cloudmonkey] branch 5.3 updated: display: New display mode based on jmespath for 5.3 version. (#44)

This is an automated email from the ASF dual-hosted git repository.

rohit pushed a commit to branch 5.3
in repository https://gitbox.apache.org/repos/asf/cloudstack-cloudmonkey.git


The following commit(s) were added to refs/heads/5.3 by this push:
     new db74764  display: New display mode based on jmespath for 5.3 version. (#44)
db74764 is described below

commit db74764aacb46f83be3eb1fd851f82c42be135f2
Author: Danilo Figueiredo Rocha <sn...@gmail.com>
AuthorDate: Tue Mar 5 06:59:31 2019 -0300

    display: New display mode based on jmespath for 5.3 version. (#44)
    
    This add to cloudmonkey CLI the abilities to make a JMESPATH filter with following changes in version 5.3:
    
    Add new argument 'query'.
    Add new display type 'jmespath'.
    Import jmespath module.
---
 cloudmonkey/cloudmonkey.py | 28 ++++++++++++++++++++++++++--
 cloudmonkey/config.py      |  2 +-
 setup.py                   |  1 +
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/cloudmonkey/cloudmonkey.py b/cloudmonkey/cloudmonkey.py
index b106362..e8da457 100644
--- a/cloudmonkey/cloudmonkey.py
+++ b/cloudmonkey/cloudmonkey.py
@@ -31,6 +31,7 @@ try:
     import sys
     import time
     import types
+    import jmespath
 
     from cachemaker import loadcache, savecache, monkeycache, splitverbsubject
     from config import __version__, __description__, __projecturl__
@@ -234,7 +235,7 @@ class CloudMonkeyShell(cmd.Cmd, object):
             else:
                 print output
 
-    def print_result(self, result, result_filter=[]):
+    def print_result(self, result, result_filter=[], result_query=''):
         if not result or len(result) == 0:
             return
 
@@ -277,6 +278,21 @@ class CloudMonkeyShell(cmd.Cmd, object):
                                         ensure_ascii=False,
                                         separators=(',', ': ')))
 
+        def print_result_jmespath(result, result_query):
+            try:
+                expression = jmespath.compile(result_query)
+            except Exception as e:
+                raise ValueError("Bad value for --query: %s \n\n %s" % (result_query, str(e)))
+            
+            try:
+                self.monkeyprint(json.dumps(expression.search(result),
+                                                    sort_keys=True,
+                                                    indent=2,
+                                                    ensure_ascii=False,
+                                                    separators=(',', ': ')))
+            except Exception as e:
+                raise ValueError("Bad formatted JSON for JMESPATH: %s \n\n %s" % (result, str(e)))
+
         def print_result_xml(result):
             custom_root = "CloudStack-%s" % self.profile.replace(" ", "_")
             xml = dicttoxml(result, attr_type=False, custom_root=custom_root)
@@ -369,6 +385,10 @@ class CloudMonkeyShell(cmd.Cmd, object):
             print_result_json(filtered_result)
             return
 
+        if self.display == "jmespath":
+            print_result_jmespath(filtered_result, result_query)
+            return
+
         if self.display == "xml":
             print_result_xml(filtered_result)
             return
@@ -457,6 +477,10 @@ class CloudMonkeyShell(cmd.Cmd, object):
                                         x.partition("=")[2]],
                              args[1:])[x] for x in range(len(args) - 1))
 
+        field_query = []
+        if 'query' in args_dict:
+            field_query = args_dict.pop('query')
+        
         field_filter = []
         if 'filter' in args_dict:
             field_filter = filter(lambda x: x.strip() != '',
@@ -489,7 +513,7 @@ class CloudMonkeyShell(cmd.Cmd, object):
         try:
             responsekeys = filter(lambda x: 'response' in x, result.keys())
             for responsekey in responsekeys:
-                self.print_result(result[responsekey], field_filter)
+                self.print_result(result[responsekey], field_filter, field_query)
             if apiname.startswith("list") and "id" not in args_dict:
                 self.update_param_cache(apiname, result)
         except Exception as e:
diff --git a/cloudmonkey/config.py b/cloudmonkey/config.py
index 47235ef..ac6daa1 100644
--- a/cloudmonkey/config.py
+++ b/cloudmonkey/config.py
@@ -39,7 +39,7 @@ param_type = ['boolean', 'date', 'float', 'integer', 'short', 'list',
 iterable_type = ['set', 'list', 'object']
 
 # cloudmonkey display types
-display_types = ["json", "xml", "csv", "table", "default"]
+display_types = ["jmespath","json", "xml", "csv", "table", "default"]
 
 config_dir = expanduser('~/.cloudmonkey')
 config_file = expanduser(config_dir + '/config')
diff --git a/setup.py b/setup.py
index 4422328..f3912fb 100644
--- a/setup.py
+++ b/setup.py
@@ -29,6 +29,7 @@ from cloudmonkey import __maintainer__, __maintaineremail__
 from cloudmonkey import __project__, __projecturl__, __projectemail__
 
 requires = [
+              'jmespath',
               'Pygments>=1.5',
               'argcomplete',
               'dicttoxml',