]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Hide v1/v2 version entities in API when disabled
authorBrian Waldon <bcwaldon@gmail.com>
Fri, 17 May 2013 22:42:45 +0000 (22:42 +0000)
committerBrian Waldon <bcwaldon@gmail.com>
Mon, 20 May 2013 20:03:34 +0000 (13:03 -0700)
Fixes bug 1181413

Change-Id: Ia8aaa78a0e0d222c268e9ca780816eb1c26feb3f

cinder/api/versions.py
cinder/tests/api/test_router.py

index d4c0c2dbc3fae38b84258464495c331e27ed4a22..30627bd29a8adfdc38148e564b9477cbd36ae798 100644 (file)
@@ -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():
index 03e294c71368ee3f7d9149f6bc828400cf02d5d7..9a3f3da2a86aaafd7e34e9cd3ac104196b4b9317 100644 (file)
@@ -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')