Merge "Add version check for listing namespaces"

This commit is contained in:
Jenkins 2015-11-20 01:23:49 +00:00 committed by Gerrit Code Review
commit 2ced9d67af
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()