From a8f7de2afde8496256727d2ae08ee520e50b4066 Mon Sep 17 00:00:00 2001 From: John Bresnahan Date: Tue, 12 Mar 2013 14:24:58 -1000 Subject: [PATCH] Filter images list by public=True|False When running the image-list command the user should have the option to list images according to whether or not the image is was set to public. A new test is included to verify this behavior. Change-Id: If645e7390fcf850648cda780a04ea37a26d855a2 Fixes bug: 1118799 --- glanceclient/v1/shell.py | 5 +++- tests/v1/test_images.py | 49 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/glanceclient/v1/shell.py b/glanceclient/v1/shell.py index 079db11..5ef6cfa 100644 --- a/glanceclient/v1/shell.py +++ b/glanceclient/v1/shell.py @@ -62,10 +62,13 @@ DISK_FORMATS = ('Acceptable formats: ami, ari, aki, vhd, vmdk, raw, ' @utils.arg('--sort-dir', default='asc', choices=glanceclient.v1.images.SORT_DIR_VALUES, help='Sort image list in specified direction.') +@utils.arg('--is-public', type=utils.string_to_bool, metavar='{True|False}', + help=('Allows the user to select a listing of public or non ' + 'public images.')) def do_image_list(gc, args): """List images you can access.""" filter_keys = ['name', 'status', 'container_format', 'disk_format', - 'size_min', 'size_max'] + 'size_min', 'size_max', 'is_public'] 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]) diff --git a/tests/v1/test_images.py b/tests/v1/test_images.py index a9d4eeb..a0f4a4e 100644 --- a/tests/v1/test_images.py +++ b/tests/v1/test_images.py @@ -17,8 +17,11 @@ import errno import json import StringIO import testtools +import urlparse +import glanceclient.v1.client as client import glanceclient.v1.images +import glanceclient.v1.shell as shell from tests import utils @@ -523,3 +526,49 @@ class ImageTest(testtools.TestCase): expect += [('GET', '/v1/images/3', {}, None)] self.assertEqual(self.api.calls, expect) self.assertEqual(data, 'ZZZ') + + +class ParameterFakeAPI(utils.FakeAPI): + image_list = {'images': [ + { + 'id': 'a', + 'name': 'image-1', + 'properties': {'arch': 'x86_64'}, + }, + { + 'id': 'b', + 'name': 'image-2', + 'properties': {'arch': 'x86_64'}, + }, + ]} + + def json_request(self, method, url, **kwargs): + self.url = url + return utils.FakeResponse({}), ParameterFakeAPI.image_list + + +class FakeArg(object): + def __init__(self, arg_dict): + self.arg_dict = arg_dict + + def __getattr__(self, name): + if name in self.arg_dict: + return self.arg_dict[name] + else: + return None + + +class UrlParameterTest(testtools.TestCase): + + def setUp(self): + super(UrlParameterTest, self).setUp() + self.api = ParameterFakeAPI({}) + self.gc = client.Client("http://fakeaddress.com") + self.gc.images = glanceclient.v1.images.ImageManager(self.api) + + def test_is_public_list(self): + shell.do_image_list(self.gc, FakeArg({"is_public": "True"})) + parts = urlparse.urlparse(self.api.url) + qs_dict = urlparse.parse_qs(parts.query) + self.assertTrue('is_public' in qs_dict) + self.assertTrue(qs_dict['is_public'][0].lower() == "true")