From: Brian Waldon Date: Fri, 17 May 2013 22:42:45 +0000 (+0000) Subject: Hide v1/v2 version entities in API when disabled X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=c2ee6e2187e258da07e229e690afe20f0039e019;p=openstack-build%2Fcinder-build.git Hide v1/v2 version entities in API when disabled Fixes bug 1181413 Change-Id: Ia8aaa78a0e0d222c268e9ca780816eb1c26feb3f --- diff --git a/cinder/api/versions.py b/cinder/api/versions.py index d4c0c2dbc..30627bd29 100644 --- a/cinder/api/versions.py +++ b/cinder/api/versions.py @@ -21,9 +21,12 @@ from lxml import etree from cinder.api.openstack import wsgi from cinder.api.views import versions as views_versions from cinder.api import xmlutil +from cinder import flags +FLAGS = flags.FLAGS -VERSIONS = { + +_KNOWN_VERSIONS = { "v2.0": { "id": "v2.0", "status": "CURRENT", @@ -88,6 +91,17 @@ VERSIONS = { } +def get_supported_versions(): + versions = {} + + if FLAGS.enable_v1_api: + versions['v1.0'] = _KNOWN_VERSIONS['v1.0'] + if FLAGS.enable_v2_api: + versions['v2.0'] = _KNOWN_VERSIONS['v2.0'] + + return versions + + class MediaTypesTemplateElement(xmlutil.TemplateElement): def will_render(self, datum): return 'media-types' in datum @@ -236,14 +250,14 @@ class Versions(wsgi.Resource): def index(self, req): """Return all versions.""" builder = views_versions.get_view_builder(req) - return builder.build_versions(VERSIONS) + return builder.build_versions(get_supported_versions()) @wsgi.serializers(xml=ChoicesTemplate) @wsgi.response(300) def multi(self, req): """Return multiple choices.""" builder = views_versions.get_view_builder(req) - return builder.build_choices(VERSIONS, req) + return builder.build_choices(get_supported_versions(), req) def get_action_args(self, request_environment): """Parse dictionary created by routes library.""" @@ -261,7 +275,7 @@ class VolumeVersionV1(object): atom=VersionAtomSerializer) def show(self, req): builder = views_versions.get_view_builder(req) - return builder.build_version(VERSIONS['v1.0']) + return builder.build_version(_KNOWN_VERSIONS['v1.0']) def create_resource(): diff --git a/cinder/tests/api/test_router.py b/cinder/tests/api/test_router.py index 03e294c71..9a3f3da2a 100644 --- a/cinder/tests/api/test_router.py +++ b/cinder/tests/api/test_router.py @@ -64,13 +64,63 @@ class VolumeRouterTestCase(test.TestCase): response = req.get_response(self.app) self.assertEqual(200, response.status_int) - def test_versions_dispatch(self): + def test_versions_multi(self): + req = fakes.HTTPRequest.blank('/') + req.method = 'GET' + req.content_type = 'application/json' + resource = versions.Versions() + result = resource.dispatch(resource.multi, req, {}) + ids = [v['id'] for v in result['choices']] + self.assertEqual(set(ids), set(['v1.0', 'v2.0'])) + + def test_versions_multi_disable_v1(self): + self.flags(enable_v1_api=False) + req = fakes.HTTPRequest.blank('/') + req.method = 'GET' + req.content_type = 'application/json' + resource = versions.Versions() + result = resource.dispatch(resource.multi, req, {}) + ids = [v['id'] for v in result['choices']] + self.assertEqual(set(ids), set(['v2.0'])) + + def test_versions_multi_disable_v2(self): + self.flags(enable_v2_api=False) + req = fakes.HTTPRequest.blank('/') + req.method = 'GET' + req.content_type = 'application/json' + resource = versions.Versions() + result = resource.dispatch(resource.multi, req, {}) + ids = [v['id'] for v in result['choices']] + self.assertEqual(set(ids), set(['v1.0'])) + + def test_versions_index(self): + req = fakes.HTTPRequest.blank('/') + req.method = 'GET' + req.content_type = 'application/json' + resource = versions.Versions() + result = resource.dispatch(resource.index, req, {}) + ids = [v['id'] for v in result['versions']] + self.assertEqual(set(ids), set(['v1.0', 'v2.0'])) + + def test_versions_index_disable_v1(self): + self.flags(enable_v1_api=False) + req = fakes.HTTPRequest.blank('/') + req.method = 'GET' + req.content_type = 'application/json' + resource = versions.Versions() + result = resource.dispatch(resource.index, req, {}) + ids = [v['id'] for v in result['versions']] + self.assertEqual(set(ids), set(['v2.0'])) + + def test_versions_index_disable_v2(self): + self.flags(enable_v2_api=False) req = fakes.HTTPRequest.blank('/') req.method = 'GET' req.content_type = 'application/json' resource = versions.Versions() result = resource.dispatch(resource.index, req, {}) - self.assertTrue(result) + ids = [v['id'] for v in result['versions']] + self.assertEqual(set(ids), set(['v1.0'])) def test_volumes(self): req = fakes.HTTPRequest.blank('/fake/volumes')