From 4511a445d010345c2bbd41bec303fe6df6312f3c Mon Sep 17 00:00:00 2001 From: Brian Rosmaita Date: Tue, 21 Aug 2018 15:08:56 -0400 Subject: [PATCH] Add image-list filter for multihash This was missed when multihash support was added to the glanceclient. The os_hash_value is an indexed field in the API. Includes a release note. Closes-bug: #1788271 Change-Id: Ibfe28b8c644967b7e0295dfd3f55c3ae1b0cbb2d --- glanceclient/tests/unit/v2/test_images.py | 60 +++++++++++++++++++ glanceclient/tests/unit/v2/test_shell_v2.py | 8 +++ glanceclient/v2/shell.py | 5 +- .../multihash-filter-ef2a48dc48fae9dc.yaml | 13 ++++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/multihash-filter-ef2a48dc48fae9dc.yaml diff --git a/glanceclient/tests/unit/v2/test_images.py b/glanceclient/tests/unit/v2/test_images.py index 753f3c47..99926dee 100644 --- a/glanceclient/tests/unit/v2/test_images.py +++ b/glanceclient/tests/unit/v2/test_images.py @@ -26,6 +26,10 @@ from glanceclient.v2 import images _CHKSUM = '93264c3edf5972c9f1cb309543d38a5c' _CHKSUM1 = '54264c3edf5972c9f1cb309453d38a46' +_HASHVAL = '54264c3edf93264c3edf5972c9f1cb309543d38a5c5972c9f1cb309453d38a46' +_HASHVAL1 = 'cb309543d38a5c5972c9f1cb309453d38a4654264c3edf93264c3edf5972c9f1' +_HASHBAD = '93264c3edf597254264c3edf5972c9f1cb309453d38a46c9f1cb309543d38a5c' + _TAG1 = 'power' _TAG2 = '64bit' @@ -457,6 +461,41 @@ data_fixtures = { {'images': []}, ), }, + '/v2/images?limit=%d&os_hash_value=%s' % (images.DEFAULT_PAGE_SIZE, + _HASHVAL): { + 'GET': ( + {}, + {'images': [ + { + 'id': '3a4560a1-e585-443e-9b39-553b46ec92d1', + 'name': 'image-1', + } + ]}, + ), + }, + '/v2/images?limit=%d&os_hash_value=%s' % (images.DEFAULT_PAGE_SIZE, + _HASHVAL1): { + 'GET': ( + {}, + {'images': [ + { + 'id': '2a4560b2-e585-443e-9b39-553b46ec92d1', + 'name': 'image-1', + }, + { + 'id': '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810', + 'name': 'image-2', + }, + ]}, + ), + }, + '/v2/images?limit=%d&os_hash_value=%s' % (images.DEFAULT_PAGE_SIZE, + _HASHBAD): { + 'GET': ( + {}, + {'images': []}, + ), + }, '/v2/images?limit=%d&tag=%s' % (images.DEFAULT_PAGE_SIZE, _TAG1): { 'GET': ( {}, @@ -754,6 +793,27 @@ class TestController(testtools.TestCase): images = self.controller.list(**filters) self.assertEqual(0, len(images)) + def test_list_images_for_hash_single_image(self): + fake_id = '3a4560a1-e585-443e-9b39-553b46ec92d1' + filters = {'filters': {'os_hash_value': _HASHVAL}} + images = self.controller.list(**filters) + self.assertEqual(1, len(images)) + self.assertEqual('%s' % fake_id, images[0].id) + + def test_list_images_for_hash_multiple_images(self): + fake_id1 = '2a4560b2-e585-443e-9b39-553b46ec92d1' + fake_id2 = '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810' + filters = {'filters': {'os_hash_value': _HASHVAL1}} + images = self.controller.list(**filters) + self.assertEqual(2, len(images)) + self.assertEqual('%s' % fake_id1, images[0].id) + self.assertEqual('%s' % fake_id2, images[1].id) + + def test_list_images_for_wrong_hash(self): + filters = {'filters': {'os_hash_value': _HASHBAD}} + images = self.controller.list(**filters) + self.assertEqual(0, len(images)) + def test_list_images_for_bogus_owner(self): filters = {'filters': {'owner': _BOGUS_ID}} images = self.controller.list(**filters) diff --git a/glanceclient/tests/unit/v2/test_shell_v2.py b/glanceclient/tests/unit/v2/test_shell_v2.py index 919e6456..bafa8e53 100644 --- a/glanceclient/tests/unit/v2/test_shell_v2.py +++ b/glanceclient/tests/unit/v2/test_shell_v2.py @@ -265,6 +265,7 @@ class ShellV2Test(testtools.TestCase): 'sort': None, 'verbose': False, 'include_stores': False, + 'os_hash_value': None, 'os_hidden': False } args = self._make_args(input) @@ -302,6 +303,7 @@ class ShellV2Test(testtools.TestCase): 'sort': None, 'verbose': True, 'include_stores': False, + 'os_hash_value': None, 'os_hidden': False } args = self._make_args(input) @@ -328,6 +330,7 @@ class ShellV2Test(testtools.TestCase): 'sort': None, 'verbose': False, 'include_stores': True, + 'os_hash_value': None, 'os_hidden': False } args = self._make_args(input) @@ -353,6 +356,7 @@ class ShellV2Test(testtools.TestCase): 'sort': None, 'verbose': True, 'include_stores': True, + 'os_hash_value': None, 'os_hidden': False } args = self._make_args(input) @@ -379,6 +383,7 @@ class ShellV2Test(testtools.TestCase): 'sort': None, 'verbose': False, 'include_stores': False, + 'os_hash_value': None, 'os_hidden': True } args = self._make_args(input) @@ -416,6 +421,7 @@ class ShellV2Test(testtools.TestCase): 'sort': None, 'verbose': False, 'include_stores': False, + 'os_hash_value': None, 'os_hidden': False } args = self._make_args(input) @@ -453,6 +459,7 @@ class ShellV2Test(testtools.TestCase): 'sort_dir': [], 'verbose': False, 'include_stores': False, + 'os_hash_value': None, 'os_hidden': False } args = self._make_args(input) @@ -490,6 +497,7 @@ class ShellV2Test(testtools.TestCase): 'sort': None, 'verbose': False, 'include_stores': False, + 'os_hash_value': None, 'os_hidden': False } args = self._make_args(input) diff --git a/glanceclient/v2/shell.py b/glanceclient/v2/shell.py index 544416ac..75c3c0d8 100644 --- a/glanceclient/v2/shell.py +++ b/glanceclient/v2/shell.py @@ -327,6 +327,9 @@ def do_image_update(gc, args): action='append', dest='properties', default=[]) @utils.arg('--checksum', metavar='', help=_('Displays images that match the MD5 checksum.')) +@utils.arg('--hash', dest='os_hash_value', default=None, + metavar='', + help=_('Displays images that match the specified hash value.')) @utils.arg('--tag', metavar='', action='append', help=_("Filter images by a user-defined tag.")) @utils.arg('--sort-key', default=[], action='append', @@ -358,7 +361,7 @@ def do_image_update(gc, args): def do_image_list(gc, args): """List images you can access.""" filter_keys = ['visibility', 'member_status', 'owner', 'checksum', 'tag', - 'os_hidden'] + 'os_hidden', 'os_hash_value'] filter_items = [(key, getattr(args, key)) for key in filter_keys] if args.properties: diff --git a/releasenotes/notes/multihash-filter-ef2a48dc48fae9dc.yaml b/releasenotes/notes/multihash-filter-ef2a48dc48fae9dc.yaml new file mode 100644 index 00000000..6bb22aa9 --- /dev/null +++ b/releasenotes/notes/multihash-filter-ef2a48dc48fae9dc.yaml @@ -0,0 +1,13 @@ +--- +features: + - | + For parity with the old ``checksum`` field, this release adds the + ability for CLI users to filter the image list based upon a particular + multihash value using the ``--hash `` option. Issue the + command: + + .. code-block:: none + + glance help image-list + + for more information.