From e5217d901d2b819d68a41e938ce43cf13111d044 Mon Sep 17 00:00:00 2001 From: Sean McGinnis Date: Mon, 6 Aug 2018 10:26:33 -0500 Subject: [PATCH] Fix endpoint identification for api-version query The api-version query is a GET against the root endpoint of cinder. Determining this root endpoint had a flaw with the newer wsgi deployment URLs that would cause it to find the root endpoint of the web server and not cinder. This updates the logic to work with legacy, wsgi, and custom URLs for the Cinder endpoint. Change-Id: Iaeba1f8d50ee8cc9410cc9f638770a56796871fb Closes-bug: #1785594 Signed-off-by: Sean McGinnis --- cinderclient/client.py | 8 +++++++- cinderclient/tests/unit/test_client.py | 17 ++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/cinderclient/client.py b/cinderclient/client.py index c4626a922..e0d928f05 100644 --- a/cinderclient/client.py +++ b/cinderclient/client.py @@ -210,7 +210,13 @@ class SessionClient(adapter.LegacyJsonAdapter): def _get_base_url(self): endpoint = self.get_endpoint() - base_url = '/'.join(endpoint.split('/')[:3]) + '/' + m = re.search('(.+)/v[1-3].*', endpoint) + if m: + # Get everything up until the version identifier + base_url = '%s/' % m.group(1) + else: + # Fall back to the root of the URL + base_url = '/'.join(endpoint.split('/')[:3]) + '/' return base_url def get_volume_api_version_from_endpoint(self): diff --git a/cinderclient/tests/unit/test_client.py b/cinderclient/tests/unit/test_client.py index 63735c836..7fc6643c8 100644 --- a/cinderclient/tests/unit/test_client.py +++ b/cinderclient/tests/unit/test_client.py @@ -32,6 +32,7 @@ from cinderclient.tests.unit import utils from cinderclient.tests.unit.v3 import fakes +@ddt.ddt class ClientTest(utils.TestCase): def test_get_client_class_v1(self): @@ -99,11 +100,21 @@ class ClientTest(utils.TestCase): unknown_url) @mock.patch('cinderclient.client.SessionClient.get_endpoint') - def test_get_base_url(self, mock_get_endpoint): - url = 'http://192.168.122.104:8776/v3/de50d1f33a38415fadfd3e1dea28f4d3' + @ddt.data( + ('http://192.168.1.1:8776/v2', 'http://192.168.1.1:8776/'), + ('http://192.168.1.1:8776/v3/e5526285ebd741b1819393f772f11fc3', + 'http://192.168.1.1:8776/'), + ('https://192.168.1.1:8080/volumes/v3/' + 'e5526285ebd741b1819393f772f11fc3', + 'https://192.168.1.1:8080/volumes/'), + ('http://192.168.1.1/volumes/v3/e5526285ebd741b1819393f772f11fc3', + 'http://192.168.1.1/volumes/'), + ('https://volume.example.com/', 'https://volume.example.com/')) + @ddt.unpack + def test_get_base_url(self, url, expected_base, mock_get_endpoint): mock_get_endpoint.return_value = url cs = cinderclient.client.SessionClient(self, api_version='3.0') - self.assertEqual('http://192.168.122.104:8776/', cs._get_base_url()) + self.assertEqual(expected_base, cs._get_base_url()) @mock.patch.object(adapter.Adapter, 'request') @mock.patch.object(exceptions, 'from_response')