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
This commit is contained in:
Justin Pomeroy 2015-11-17 10:19:59 -06:00
parent 88c4013c7c
commit 0578023d88
3 changed files with 61 additions and 1 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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()