Skip to content
Merged
Show file tree
Hide file tree
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
28 changes: 26 additions & 2 deletions cloudmonkey/cloudmonkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import sys
import time
import types
import jmespath

from cachemaker import loadcache, savecache, monkeycache, splitverbsubject
from config import __version__, __description__, __projecturl__
Expand Down Expand Up @@ -234,7 +235,7 @@ def monkeyprint(self, *args):
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

Expand Down Expand Up @@ -277,6 +278,21 @@ def print_result_json(result):
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)
Expand Down Expand Up @@ -369,6 +385,10 @@ def print_result_as_list(result):
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
Expand Down Expand Up @@ -457,6 +477,10 @@ def default(self, args):
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() != '',
Expand Down Expand Up @@ -489,7 +513,7 @@ def default(self, args):
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:
Expand Down
2 changes: 1 addition & 1 deletion cloudmonkey/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
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')
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from cloudmonkey import __project__, __projecturl__, __projectemail__

requires = [
'jmespath',
'Pygments>=1.5',
'argcomplete',
'dicttoxml',
Expand Down