]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Add config option to override url for versions
authorwanghao <wanghao749@huawei.com>
Thu, 26 Feb 2015 08:50:31 +0000 (16:50 +0800)
committerwanghao <wanghao749@huawei.com>
Mon, 2 Mar 2015 06:51:22 +0000 (14:51 +0800)
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
cinder/tests/api/test_versions.py [new file with mode: 0644]

index 40de3290711b516b8f6633189155558af207e3c3..d2471925bb80d13892d855c966ed6007f7c183e3 100644 (file)
 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 (file)
index 0000000..7573142
--- /dev/null
@@ -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)