Merge "Allow image filtering by custom properties"
This commit is contained in:
@@ -98,6 +98,9 @@ class ImageManager(base.Manager):
|
||||
if marker:
|
||||
params['marker'] = marker
|
||||
if filters:
|
||||
properties = filters.pop('properties', {})
|
||||
for key, value in properties.items():
|
||||
params['property-%s' % key] = value
|
||||
params.update(filters)
|
||||
query = '?%s' % urllib.urlencode(params) if params else ''
|
||||
return self._list('/v1/images/detail%s' % query, "images")
|
||||
|
||||
@@ -32,12 +32,20 @@ import glanceclient.v1.images
|
||||
help='Filter images to those with a size greater than this.')
|
||||
@utils.arg('--size-max', metavar='<SIZE>',
|
||||
help='Filter images to those with a size less than this.')
|
||||
@utils.arg('--property-filter', metavar='<KEY=VALUE>',
|
||||
help="Filter images by a user-defined image property.",
|
||||
action='append', dest='properties', default=[])
|
||||
def do_image_list(gc, args):
|
||||
"""List images."""
|
||||
filter_keys = ['name', 'status', 'container_format', 'disk_format',
|
||||
'size_min', 'size_max']
|
||||
filter_items = [(key, getattr(args, key)) for key in filter_keys]
|
||||
filters = dict([item for item in filter_items if item[1] is not None])
|
||||
|
||||
if args.properties:
|
||||
property_filter_items = [p.split('=', 1) for p in args.properties]
|
||||
filters['properties'] = dict(property_filter_items)
|
||||
|
||||
images = gc.images.list(filters=filters)
|
||||
columns = ['ID', 'Name', 'Disk Format', 'Container Format',
|
||||
'Size', 'Status']
|
||||
|
||||
@@ -118,6 +118,11 @@ class ImageManagerTest(unittest.TestCase):
|
||||
expect = [('GET', '/v1/images/detail?name=foo', {}, None)]
|
||||
self.assertEqual(self.api.calls, expect)
|
||||
|
||||
def test_list_with_property_filters(self):
|
||||
self.mgr.list(filters={'properties': {'ping': 'pong'}})
|
||||
expect = [('GET', '/v1/images/detail?property-ping=pong', {}, 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