diff options
author | Brian Waldon <bcwaldon@gmail.com> | 2012-11-25 14:26:02 -0500 |
---|---|---|
committer | Brian Waldon <bcwaldon@gmail.com> | 2012-11-28 09:46:32 -0800 |
commit | 4da8b287b40cf0f0de0fd77998e3e66d8bac7999 (patch) | |
tree | a3c2f42395e8f7a0442fac69fe7b1e0a36359503 /glanceclient/v1 | |
parent | c0ec97f310bc56c69c22e49928b4aea4cd8458f1 (diff) | |
download | python-glanceclient-4da8b287b40cf0f0de0fd77998e3e66d8bac7999.tar.gz |
Add --sort-key and --sort-dir to image-list
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
Diffstat (limited to 'glanceclient/v1')
-rw-r--r-- | glanceclient/v1/images.py | 20 | ||||
-rw-r--r-- | glanceclient/v1/shell.py | 14 |
2 files changed, 32 insertions, 2 deletions
diff --git a/glanceclient/v1/images.py b/glanceclient/v1/images.py index 26fe7d6..bb3da3c 100644 --- a/glanceclient/v1/images.py +++ b/glanceclient/v1/images.py @@ -34,6 +34,10 @@ CREATE_PARAMS = UPDATE_PARAMS + ('id',) 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(): diff --git a/glanceclient/v1/shell.py b/glanceclient/v1/shell.py index ff0c2a6..9b888a6 100644 --- a/glanceclient/v1/shell.py +++ b/glanceclient/v1/shell.py @@ -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) |