Merge "Extend images CLI v2 with new sorting syntax"

This commit is contained in:
Jenkins
2015-03-16 20:12:42 +00:00
committed by Gerrit Code Review
4 changed files with 139 additions and 21 deletions
+43 -14
View File
@@ -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)
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', []))
for dir in sort_dir:
url = '%s&sort_dir=%s' % (url, dir)
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
+9 -4
View File
@@ -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']