From 2eb25ab8803214cb3beb5d8fe3efbf70a462c414 Mon Sep 17 00:00:00 2001 From: wanghao Date: Thu, 26 Feb 2015 16:50:31 +0800 Subject: [PATCH] Add config option to override url for versions The versions url returns the wrong data when cinder api is behind a proxy. This adds a new config option so it can be set properly. DocImpact Change-Id: I46a90120b21e43bf8dca9e5f0efdf339f0d3e8e6 Closes-Bug: #1384379 --- cinder/api/views/versions.py | 17 +++++++- cinder/tests/api/test_versions.py | 71 +++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 cinder/tests/api/test_versions.py diff --git a/cinder/api/views/versions.py b/cinder/api/views/versions.py index 40de32907..d2471925b 100644 --- a/cinder/api/views/versions.py +++ b/cinder/api/views/versions.py @@ -16,9 +16,24 @@ import copy import os +from oslo_config import cfg + + +versions_opts = [ + cfg.StrOpt('public_endpoint', default=None, + help="Public url to use for versions endpoint. The default " + "is None, which will use the request's host_url " + "attribute to populate the URL base. If Cinder is " + "operating behind a proxy, you will want to change " + "this to represent the proxy's URL."), +] + +CONF = cfg.CONF +CONF.register_opts(versions_opts) + def get_view_builder(req): - base_url = req.application_url + base_url = CONF.public_endpoint or req.application_url return ViewBuilder(base_url) diff --git a/cinder/tests/api/test_versions.py b/cinder/tests/api/test_versions.py new file mode 100644 index 000000000..7573142fc --- /dev/null +++ b/cinder/tests/api/test_versions.py @@ -0,0 +1,71 @@ +# Copyright (c) 2015 - 2016 Huawei Technologies Co., Ltd. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import webob + +from cinder.api import versions +from cinder import test + + +class VersionsTest(test.TestCase): + + """Test the version information returned from the API service.""" + + def test_get_version_list_public_endpoint(self): + req = webob.Request.blank('/', base_url='http://127.0.0.1:8776/') + req.accept = 'application/json' + self.override_config('public_endpoint', 'https://example.com:8776') + res = versions.Versions().index(req) + results = res['versions'] + expected = [ + { + 'id': 'v1.0', + 'status': 'SUPPORTED', + 'updated': '2014-06-28T12:20:21Z', + 'links': [{'rel': 'self', + 'href': 'https://example.com:8776/v1/'}], + }, + { + 'id': 'v2.0', + 'status': 'CURRENT', + 'updated': '2012-11-21T11:33:21Z', + 'links': [{'rel': 'self', + 'href': 'https://example.com:8776/v2/'}], + }, + ] + self.assertEqual(expected, results) + + def test_get_version_list(self): + req = webob.Request.blank('/', base_url='http://127.0.0.1:8776/') + req.accept = 'application/json' + res = versions.Versions().index(req) + results = res['versions'] + expected = [ + { + 'id': 'v1.0', + 'status': 'SUPPORTED', + 'updated': '2014-06-28T12:20:21Z', + 'links': [{'rel': 'self', + 'href': 'http://127.0.0.1:8776/v1/'}], + }, + { + 'id': 'v2.0', + 'status': 'CURRENT', + 'updated': '2012-11-21T11:33:21Z', + 'links': [{'rel': 'self', + 'href': 'http://127.0.0.1:8776/v2/'}], + }, + ] + self.assertEqual(expected, results) -- 2.45.2