summaryrefslogtreecommitdiff
path: root/glanceclient
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2015-03-16 20:12:42 +0000
committerGerrit Code Review <review@openstack.org>2015-03-16 20:12:42 +0000
commit6db625f287ffcce80df65d25a117ceb0de2ea776 (patch)
tree8429166f39dbfcd22556159decae209797a3a125 /glanceclient
parent82143e26d36431d4600beef833d6b681119c9245 (diff)
parent3f3066be97349677fd489703effb85f45c0cdd76 (diff)
downloadpython-glanceclient-6db625f287ffcce80df65d25a117ceb0de2ea776.tar.gz
Merge "Extend images CLI v2 with new sorting syntax"
Diffstat (limited to 'glanceclient')
-rw-r--r--glanceclient/v2/images.py59
-rw-r--r--glanceclient/v2/shell.py13
2 files changed, 53 insertions, 19 deletions
diff --git a/glanceclient/v2/images.py b/glanceclient/v2/images.py
index 5f51539..c9fb0a7 100644
--- a/glanceclient/v2/images.py
+++ b/glanceclient/v2/images.py
@@ -47,6 +47,29 @@ class Controller(object):
return [value]
return value
+ @staticmethod
+ def _validate_sort_param(sort):
+ """Validates sorting argument for invalid keys and directions values.
+
+ :param sort: comma-separated list of sort keys with optional <:dir>
+ after each key
+ """
+ for sort_param in sort.strip().split(','):
+ key, _sep, dir = sort_param.partition(':')
+ if dir and dir not in SORT_DIR_VALUES:
+ msg = ('Invalid sort direction: %(sort_dir)s.'
+ ' It must be one of the following: %(available)s.'
+ ) % {'sort_dir': dir,
+ 'available': ', '.join(SORT_DIR_VALUES)}
+ raise exc.HTTPBadRequest(msg)
+ if key not in SORT_KEY_VALUES:
+ msg = ('Invalid sort key: %(sort_key)s.'
+ ' It must be one of the following: %(available)s.'
+ ) % {'sort_key': key,
+ 'available': ', '.join(SORT_KEY_VALUES)}
+ raise exc.HTTPBadRequest(msg)
+ return sort
+
def list(self, **kwargs):
"""Retrieve a listing of Image objects
@@ -104,16 +127,6 @@ class Controller(object):
# the page_size as Glance's limit.
filters['limit'] = page_size
- sort_dir = self._wrap(kwargs.get('sort_dir', []))
- sort_key = self._wrap(kwargs.get('sort_key', []))
-
- if len(sort_key) != len(sort_dir) and len(sort_dir) > 1:
- raise exc.HTTPBadRequest("Unexpected number of sort directions: "
- "provide only one default sorting "
- "direction for each key or make sure "
- "that sorting keys number matches with a "
- "number of sorting directions.")
-
tags = filters.pop('tag', [])
tags_url_params = []
@@ -130,11 +143,27 @@ class Controller(object):
for param in tags_url_params:
url = '%s&%s' % (url, parse.urlencode(param))
- for key in sort_key:
- url = '%s&sort_key=%s' % (url, key)
-
- for dir in sort_dir:
- url = '%s&sort_dir=%s' % (url, dir)
+ if 'sort' in kwargs:
+ if 'sort_key' in kwargs or 'sort_dir' in kwargs:
+ raise exc.HTTPBadRequest("The 'sort' argument is not supported"
+ " with 'sort_key' or 'sort_dir'.")
+ url = '%s&sort=%s' % (url,
+ self._validate_sort_param(
+ kwargs['sort']))
+ else:
+ sort_dir = self._wrap(kwargs.get('sort_dir', []))
+ sort_key = self._wrap(kwargs.get('sort_key', []))
+
+ if len(sort_key) != len(sort_dir) and len(sort_dir) > 1:
+ raise exc.HTTPBadRequest(
+ "Unexpected number of sort directions: "
+ "either provide a single sort direction or an equal "
+ "number of sort keys and sort directions.")
+ for key in sort_key:
+ url = '%s&sort_key=%s' % (url, key)
+
+ for dir in sort_dir:
+ url = '%s&sort_dir=%s' % (url, dir)
for image in paginate(url, page_size, limit):
yield image
diff --git a/glanceclient/v2/shell.py b/glanceclient/v2/shell.py
index d45f3c7..2628919 100644
--- a/glanceclient/v2/shell.py
+++ b/glanceclient/v2/shell.py
@@ -131,6 +131,10 @@ def do_image_update(gc, args):
@utils.arg('--sort-dir', default=[], action='append',
choices=images.SORT_DIR_VALUES,
help='Sort image list in specified directions.')
+@utils.arg('--sort', metavar='<key>[:<direction>]', default=None,
+ help=(("Comma-separated list of sort keys and directions in the "
+ "form of <key>[:<asc|desc>]. Valid keys: %s. OPTIONAL: "
+ "Default='name:asc'.") % ', '.join(images.SORT_KEY_VALUES)))
def do_image_list(gc, args):
"""List images you can access."""
filter_keys = ['visibility', 'member_status', 'owner', 'checksum', 'tag']
@@ -148,14 +152,15 @@ def do_image_list(gc, args):
kwargs['limit'] = args.limit
if args.page_size is not None:
kwargs['page_size'] = args.page_size
+
if args.sort_key:
kwargs['sort_key'] = args.sort_key
- else:
- kwargs['sort_key'] = ['name']
if args.sort_dir:
kwargs['sort_dir'] = args.sort_dir
- else:
- kwargs['sort_dir'] = ['asc']
+ if args.sort is not None:
+ kwargs['sort'] = args.sort
+ elif not args.sort_dir and not args.sort_key:
+ kwargs['sort'] = 'name:asc'
images = gc.images.list(**kwargs)
columns = ['ID', 'Name']