From 0578023d881ab77fb6708ca39a99f772763ef1a6 Mon Sep 17 00:00:00 2001 From: Justin Pomeroy Date: Tue, 17 Nov 2015 10:19:59 -0600 Subject: [PATCH] Add version check for listing namespaces Listing metadata definitions namespaces requires glance API version 2. This adds a check that makes sure an empty array is returned if any version less than 2 is being used. Closes-Bug: #1516711 Partially-Implements: blueprint horizon-glance-v2 Change-Id: Ifc0a091318296da06168f9701a280eb7357632c7 --- openstack_dashboard/api/glance.py | 19 +++++++++++ .../test/api_tests/glance_tests.py | 33 +++++++++++++++++++ openstack_dashboard/test/helpers.py | 10 +++++- 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/openstack_dashboard/api/glance.py b/openstack_dashboard/api/glance.py index 3f04b7204e..35ab7ab70c 100644 --- a/openstack_dashboard/api/glance.py +++ b/openstack_dashboard/api/glance.py @@ -42,6 +42,20 @@ from openstack_dashboard.api import base LOG = logging.getLogger(__name__) VERSIONS = base.APIVersionManager("image", preferred_version=2) +try: + from glanceclient.v2 import client as glance_client_v2 + VERSIONS.load_supported_version(2, {"client": glance_client_v2, + "version": 2}) +except ImportError: + pass + +try: + from glanceclient.v1 import client as glance_client_v1 + VERSIONS.load_supported_version(1, {"client": glance_client_v1, + "version": 1}) +except ImportError: + pass + @memoized def glanceclient(request, version='1'): @@ -256,6 +270,11 @@ def metadefs_namespace_list(request, 3) A boolean of whether or not there are more page(s). """ + # Listing namespaces requires the v2 API. If not supported we return an + # empty array so callers don't need to worry about version checking. + if get_version() < 2: + return [], False, False + limit = getattr(settings, 'API_RESULT_LIMIT', 1000) page_size = utils.get_page_size(request) diff --git a/openstack_dashboard/test/api_tests/glance_tests.py b/openstack_dashboard/test/api_tests/glance_tests.py index 0a198d5f5b..35a743d990 100644 --- a/openstack_dashboard/test/api_tests/glance_tests.py +++ b/openstack_dashboard/test/api_tests/glance_tests.py @@ -247,3 +247,36 @@ class GlanceApiTests(test.APITestCase): self.mox.ReplayAll() image = api.glance.image_get(self.request, 'empty') self.assertIsNone(image.name) + + def test_metadefs_namespace_list(self): + metadata_defs = self.metadata_defs.list() + limit = getattr(settings, 'API_RESULT_LIMIT', 1000) + + glanceclient = self.stub_glanceclient() + glanceclient.metadefs_namespace = self.mox.CreateMockAnything() + glanceclient.metadefs_namespace.list(page_size=limit, + limit=limit, + filters={}, + sort_dir='asc', + sort_key='namespace',) \ + .AndReturn(metadata_defs) + + self.mox.ReplayAll() + + defs, more, prev = api.glance.metadefs_namespace_list(self.request) + self.assertEqual(len(metadata_defs), len(defs)) + for i in range(len(metadata_defs)): + self.assertEqual(metadata_defs[i].namespace, defs[i].namespace) + self.assertEqual(more, False) + self.assertEqual(prev, False) + + def test_metadefs_namespace_list_v1(self): + api.glance.get_version = self.mox.CreateMockAnything() + api.glance.get_version().AndReturn(1) + + self.mox.ReplayAll() + + defs, more, prev = api.glance.metadefs_namespace_list(self.request) + self.assertItemsEqual(defs, []) + self.assertEqual(more, False) + self.assertEqual(prev, False) diff --git a/openstack_dashboard/test/helpers.py b/openstack_dashboard/test/helpers.py index a1f4875123..b108d6be0e 100644 --- a/openstack_dashboard/test/helpers.py +++ b/openstack_dashboard/test/helpers.py @@ -332,6 +332,14 @@ class APITestCase(TestCase): """ return self.stub_keystoneclient() + def fake_glanceclient(request, version='1'): + """Returns the stub glanceclient. + + Only necessary because the function takes too many arguments to + conveniently be a lambda. + """ + return self.stub_glanceclient() + # Store the original clients self._original_glanceclient = api.glance.glanceclient self._original_keystoneclient = api.keystone.keystoneclient @@ -342,7 +350,7 @@ class APITestCase(TestCase): self._original_ceilometerclient = api.ceilometer.ceilometerclient # Replace the clients with our stubs. - api.glance.glanceclient = lambda request: self.stub_glanceclient() + api.glance.glanceclient = fake_glanceclient api.keystone.keystoneclient = fake_keystoneclient api.nova.novaclient = lambda request: self.stub_novaclient() api.neutron.neutronclient = lambda request: self.stub_neutronclient()