diff options
| author | Brian Waldon <bcwaldon@gmail.com> | 2012-07-04 15:46:29 -0700 |
|---|---|---|
| committer | Brian Waldon <bcwaldon@gmail.com> | 2012-07-04 15:49:41 -0700 |
| commit | a1691ddc10b7fa4df0ff8a434d060ee1762773c0 (patch) | |
| tree | 877d93fb6b75bcf603eb1bb3a4b503c0a1843b3f | |
| parent | e7db533bc0222f912ec0405e101e3dafbd49ece3 (diff) | |
| download | python-glanceclient-a1691ddc10b7fa4df0ff8a434d060ee1762773c0.tar.gz | |
Allow image filtering by custom properties
A user can filter a list of images by passing in a 'properties'
sub-dictionary inside of the 'filters' keyword argumen to
ImageManager.list(). The same functionality can be used through
the CLI through the use of one or more'--property-filter' options.
Related to bp glance-client-parity.
Change-Id: I7d119174d83faa894dde557e1944289de296a02c
| -rw-r--r-- | glanceclient/v1/images.py | 3 | ||||
| -rw-r--r-- | glanceclient/v1/shell.py | 8 | ||||
| -rw-r--r-- | tests/v1/test_images.py | 5 |
3 files changed, 16 insertions, 0 deletions
diff --git a/glanceclient/v1/images.py b/glanceclient/v1/images.py index 866fb92..ad8b43a 100644 --- a/glanceclient/v1/images.py +++ b/glanceclient/v1/images.py @@ -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") diff --git a/glanceclient/v1/shell.py b/glanceclient/v1/shell.py index 4052b45..d1e456e 100644 --- a/glanceclient/v1/shell.py +++ b/glanceclient/v1/shell.py @@ -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'] diff --git a/tests/v1/test_images.py b/tests/v1/test_images.py index db50768..fe90fdd 100644 --- a/tests/v1/test_images.py +++ b/tests/v1/test_images.py @@ -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)] |
