Skip to content

Commit

Permalink
Add --sort-key and --sort-dir to image-list
Browse files Browse the repository at this point in the history
The --sort-key and --sort-dir CLI options allow users to control the
field and direction by which their images are sorted in an image-list
operation. The previous default sort behavior of sorting by ID asc has
been preserved.

Fixes bug 1082957.

Change-Id: I1d3664219c275b0379fe176f8288d6ffae0dffbe
  • Loading branch information
bcwaldon committed Nov 28, 2012
1 parent c0ec97f commit 4da8b28
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
2 changes: 1 addition & 1 deletion glanceclient/common/utils.py
Expand Up @@ -54,7 +54,7 @@ def print_list(objs, fields, formatters={}):
row.append(data)
pt.add_row(row)

print pt.get_string(sortby=fields[0])
print pt.get_string()


def print_dict(d):
Expand Down
20 changes: 20 additions & 0 deletions glanceclient/v1/images.py
Expand Up @@ -34,6 +34,10 @@

DEFAULT_PAGE_SIZE = 20

SORT_DIR_VALUES = ('asc', 'desc')
SORT_KEY_VALUES = ('name', 'status', 'container_format', 'disk_format',
'size', 'id', 'created_at', 'updated_at')


class Image(base.Resource):
def __repr__(self):
Expand Down Expand Up @@ -146,6 +150,22 @@ def paginate(qp, seen=0):
if 'marker' in kwargs:
params['marker'] = kwargs['marker']

sort_key = kwargs.get('sort_key')
if sort_key is not None:
if sort_key in SORT_KEY_VALUES:
params['sort_key'] = sort_key
else:
raise ValueError('sort_key must be one of the following: %s.'
% ', '.join(SORT_KEY_VALUES))

sort_dir = kwargs.get('sort_dir')
if sort_dir is not None:
if sort_dir in SORT_DIR_VALUES:
params['sort_dir'] = sort_dir
else:
raise ValueError('sort_dir must be one of the following: %s.'
% ', '.join(SORT_DIR_VALUES))

filters = kwargs.get('filters', {})
properties = filters.pop('properties', {})
for key, value in properties.items():
Expand Down
14 changes: 12 additions & 2 deletions glanceclient/v1/shell.py
Expand Up @@ -56,6 +56,12 @@
help='Number of images to request in each paginated request.')
@utils.arg('--human-readable', action='store_true', default=False,
help='Print image size in a human-friendly format.')
@utils.arg('--sort-key', default='id',
choices=glanceclient.v1.images.SORT_KEY_VALUES,
help='Sort image list by specified field.')
@utils.arg('--sort-dir', default='asc',
choices=glanceclient.v1.images.SORT_DIR_VALUES,
help='Sort image list in specified direction.')
def do_image_list(gc, args):
"""List images you can access."""
filter_keys = ['name', 'status', 'container_format', 'disk_format',
Expand All @@ -70,9 +76,11 @@ def do_image_list(gc, args):
kwargs = {'filters': filters}
if args.page_size is not None:
kwargs['page_size'] = args.page_size

kwargs['sort_key'] = args.sort_key
kwargs['sort_dir'] = args.sort_dir

images = gc.images.list(**kwargs)
columns = ['ID', 'Name', 'Disk Format', 'Container Format',
'Size', 'Status']

if args.human_readable:
def convert_size(image):
Expand All @@ -81,6 +89,8 @@ def convert_size(image):

images = (convert_size(image) for image in images)

columns = ['ID', 'Name', 'Disk Format', 'Container Format',
'Size', 'Status']
utils.print_list(images, columns)


Expand Down
47 changes: 47 additions & 0 deletions tests/v1/test_images.py
Expand Up @@ -138,6 +138,41 @@
]},
),
},
'/v1/images/detail?sort_dir=desc&limit=20': {
'GET': (
{},
{'images': [
{
'id': 'a',
'name': 'image-1',
'properties': {'arch': 'x86_64'},
},
{
'id': 'b',
'name': 'image-2',
'properties': {'arch': 'x86_64'},
},
]},
),
},
'/v1/images/detail?sort_key=name&limit=20': {
'GET': (
{},
{'images': [
{
'id': 'a',
'name': 'image-1',
'properties': {'arch': 'x86_64'},
},
{
'id': 'b',
'name': 'image-2',
'properties': {'arch': 'x86_64'},
},
]},
),
},

'/v1/images/1': {
'HEAD': (
{
Expand Down Expand Up @@ -249,6 +284,18 @@ def test_list_with_property_filters(self):
expect = [('GET', url, {}, None)]
self.assertEqual(self.api.calls, expect)

def test_list_with_sort_dir(self):
list(self.mgr.list(sort_dir='desc'))
url = '/v1/images/detail?sort_dir=desc&limit=20'
expect = [('GET', url, {}, None)]
self.assertEqual(self.api.calls, expect)

def test_list_with_sort_key(self):
list(self.mgr.list(sort_key='name'))
url = '/v1/images/detail?sort_key=name&limit=20'
expect = [('GET', url, {}, None)]
self.assertEqual(self.api.calls, expect)

def test_get(self):
image = self.mgr.get('1')
expect = [('HEAD', '/v1/images/1', {}, None)]
Expand Down

0 comments on commit 4da8b28

Please sign in to comment.