From 3ae2bd6728f9f9cca30ff39582ca731e4b347d7f Mon Sep 17 00:00:00 2001 From: imacdonn Date: Wed, 26 Sep 2018 23:28:32 +0000 Subject: [PATCH] Exclude disabled API versions from listing The code for listing supported API versions would return a static list, whether those versions were enabled or not. This changes the logic to drop versions that are not enabled so we don't report back support for something that then would cause a failure if the user tried to use it. Closes-bug: #1788039 Change-Id: I52c1264b3b67a5c1ea00de1c0de5bea04c3096ee Co-Authored-By: iain MacDonnell Signed-off-by: Sean McGinnis --- cinder/api/versions.py | 12 +++++++++++- cinder/tests/unit/api/test_versions.py | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/cinder/api/versions.py b/cinder/api/versions.py index dd01ad9167d..93f988bf1c8 100644 --- a/cinder/api/versions.py +++ b/cinder/api/versions.py @@ -17,6 +17,7 @@ import copy +from oslo_config import cfg from six.moves import http_client from cinder.api import extensions @@ -26,6 +27,9 @@ from cinder.api.openstack import wsgi from cinder.api.views import versions as views_versions +CONF = cfg.CONF + + _LINKS = [{ "rel": "describedby", "type": "text/html", @@ -111,9 +115,15 @@ class VersionsController(wsgi.Controller): # available versions. @wsgi.response(http_client.MULTIPLE_CHOICES) def all(self, req): - """Return all known versions.""" + """Return all known and enabled versions.""" builder = views_versions.get_view_builder(req) known_versions = copy.deepcopy(_KNOWN_VERSIONS) + + if not CONF.enable_v2_api: + known_versions.pop('v2.0') + if not CONF.enable_v3_api: + known_versions.pop('v3.0') + return builder.build_versions(known_versions) diff --git a/cinder/tests/unit/api/test_versions.py b/cinder/tests/unit/api/test_versions.py index 480de3339c4..090e879d337 100644 --- a/cinder/tests/unit/api/test_versions.py +++ b/cinder/tests/unit/api/test_versions.py @@ -14,6 +14,8 @@ # under the License. import ddt +from oslo_config import cfg +from oslo_config import fixture as config_fixture from oslo_serialization import jsonutils from oslo_utils import encodeutils import six @@ -29,6 +31,7 @@ from cinder import test from cinder.tests.unit.api import fakes +CONF = cfg.CONF VERSION_HEADER_NAME = 'OpenStack-API-Version' VOLUME_SERVICE = 'volume ' @@ -81,6 +84,21 @@ class VersionsControllerTestCase(test.TestCase): self.assertEqual(api_version_request._MIN_API_VERSION, v3.get('min_version')) + @ddt.data('2.0', '3.0') + def test_all_versions_excludes_disabled(self, version): + self.fixture = self.useFixture(config_fixture.Config(CONF)) + if version == '2.0': + self.fixture.config(enable_v2_api=False) + elif version == '3.0': + self.fixture.config(enable_v3_api=False) + else: + return + vc = versions.VersionsController() + req = self.build_request(base_url='http://localhost') + resp = vc.all(req) + all_versions = [x['id'] for x in resp['versions']] + self.assertNotIn('v' + version, all_versions) + def test_versions_v2_no_header(self): req = self.build_request(base_url='http://localhost/v2')