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",
}
+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
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."""
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():
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')