From 61a40343fdbb89a1c6404ab03fcfd84daee31c9e Mon Sep 17 00:00:00 2001 From: Dean Troyer Date: Wed, 21 Jan 2015 15:02:58 -0600 Subject: Add filter to image list * Hides previously broken --page-size option * Adds --property to image list for filtering on properties * Adds Visibility, Protected, Owner, Properties/Tags to --long output * Adds api.utils.simple_filter() for selecting matches out of a list of objects * Adds tests for all of the above * Updates image docs There are additional filtering options to be added in later reviews. Change-Id: I32feff0ad61aae749b33621c817658d7dc90c3aa Closes-bug: 1401902 --- openstackclient/image/v2/image.py | 81 +++++++++++++++++++++++++++++++++------ 1 file changed, 70 insertions(+), 11 deletions(-) (limited to 'openstackclient/image/v2/image.py') diff --git a/openstackclient/image/v2/image.py b/openstackclient/image/v2/image.py index 2e0fd393..4eda506c 100644 --- a/openstackclient/image/v2/image.py +++ b/openstackclient/image/v2/image.py @@ -15,6 +15,7 @@ """Image V2 Action Implementations""" +import argparse import logging import six @@ -23,6 +24,8 @@ from cliff import lister from cliff import show from glanceclient.common import utils as gc_utils +from openstackclient.api import utils as api_utils +from openstackclient.common import parseractions from openstackclient.common import utils @@ -60,11 +63,6 @@ class ListImage(lister.Lister): def get_parser(self, prog_name): parser = super(ListImage, self).get_parser(prog_name) - parser.add_argument( - "--page-size", - metavar="", - help="Number of images to request in each paginated request", - ) public_group = parser.add_mutually_exclusive_group() public_group.add_argument( "--public", @@ -80,12 +78,33 @@ class ListImage(lister.Lister): default=False, help="List only private images", ) + public_group.add_argument( + "--shared", + dest="shared", + action="store_true", + default=False, + help="List only shared images", + ) + parser.add_argument( + '--property', + metavar='', + action=parseractions.KeyValueAction, + help='Filter output based on property', + ) parser.add_argument( '--long', action='store_true', default=False, help='List additional fields in output', ) + + # --page-size has never worked, leave here for silent compatability + # We'll implement limit/marker differently later + parser.add_argument( + "--page-size", + metavar="", + help=argparse.SUPPRESS, + ) return parser def take_action(self, parsed_args): @@ -94,23 +113,63 @@ class ListImage(lister.Lister): image_client = self.app.client_manager.image kwargs = {} - if parsed_args.page_size is not None: - kwargs["page_size"] = parsed_args.page_size if parsed_args.public: kwargs['public'] = True if parsed_args.private: kwargs['private'] = True - kwargs['detailed'] = parsed_args.long + if parsed_args.shared: + kwargs['shared'] = True if parsed_args.long: - columns = ('ID', 'Name', 'Disk Format', 'Container Format', - 'Size', 'Status') + columns = ( + 'ID', + 'Name', + 'Disk Format', + 'Container Format', + 'Size', + 'Status', + 'visibility', + 'protected', + 'owner', + 'tags', + ) + column_headers = ( + 'ID', + 'Name', + 'Disk Format', + 'Container Format', + 'Size', + 'Status', + 'Visibility', + 'Protected', + 'Owner', + 'Tags', + ) else: columns = ("ID", "Name") + column_headers = columns data = image_client.api.image_list(**kwargs) - return (columns, (utils.get_dict_properties(s, columns) for s in data)) + if parsed_args.property: + # NOTE(dtroyer): coerce to a list to subscript it in py3 + attr, value = list(parsed_args.property.items())[0] + api_utils.simple_filter( + data, + attr=attr, + value=value, + property_field='properties', + ) + return ( + column_headers, + (utils.get_dict_properties( + s, + columns, + formatters={ + 'tags': utils.format_dict, + }, + ) for s in data) + ) class SaveImage(command.Command): -- cgit v1.2.1