Merge "Add --sort-key and --sort-dir to image-list"
This commit is contained in:
@@ -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):
|
||||
|
@@ -34,6 +34,10 @@ CREATE_PARAMS = UPDATE_PARAMS + ('id', 'store')
|
||||
|
||||
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):
|
||||
@@ -146,6 +150,22 @@ class ImageManager(base.Manager):
|
||||
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():
|
||||
|
@@ -56,6 +56,12 @@ DISK_FORMATS = ('Acceptable formats: ami, ari, aki, vhd, vmdk, raw, '
|
||||
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',
|
||||
@@ -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):
|
||||
@@ -81,6 +89,8 @@ def do_image_list(gc, args):
|
||||
|
||||
images = (convert_size(image) for image in images)
|
||||
|
||||
columns = ['ID', 'Name', 'Disk Format', 'Container Format',
|
||||
'Size', 'Status']
|
||||
utils.print_list(images, columns)
|
||||
|
||||
|
||||
|
@@ -138,6 +138,41 @@ fixtures = {
|
||||
]},
|
||||
),
|
||||
},
|
||||
'/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': (
|
||||
{
|
||||
@@ -249,6 +284,18 @@ class ImageManagerTest(unittest.TestCase):
|
||||
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)]
|
||||
|
Reference in New Issue
Block a user