diff options
author | Ivan Kolodyazhny <e0ne@e0ne.info> | 2017-08-02 17:22:43 +0300 |
---|---|---|
committer | Ivan Kolodyazhny <e0ne@e0ne.info> | 2017-08-07 15:26:46 +0000 |
commit | b321dd798575a4e39c01f3b6db29d875cd142fe2 (patch) | |
tree | fd09fa35abb4f8b8d90e95758da232b6937061c7 | |
parent | 99bb6f3ecb1f3c883c50bf715618e64b145257b2 (diff) | |
download | python-cinderclient-b321dd798575a4e39c01f3b6db29d875cd142fe2.tar.gz |
Fix get_highest_client_server_version with Cinder API + uWSGI
get_highest_client_server_version should work with any endpoint type:
cinder-api with eventlet, uWSGI, etc.
This patch is based on python-novaclient patch [1]. In a future, we can
deprecate get_highest_client_server_version in flavor of keystoneauth
descovery feature.
[1] Icba858b496855e2ffd71b35168e8057b28236119
Closes-Bug: #1708188
Change-Id: I9acf6dc84c32b25bfe3254eb0f97248736498d32
(cherry picked from commit 19bb7cdf4fcba4a8db540d584d1e03c9276885bb)
-rw-r--r-- | cinderclient/client.py | 26 | ||||
-rw-r--r-- | cinderclient/tests/unit/test_client.py | 16 |
2 files changed, 30 insertions, 12 deletions
diff --git a/cinderclient/client.py b/cinderclient/client.py index dabfcd3..6537cfe 100644 --- a/cinderclient/client.py +++ b/cinderclient/client.py @@ -39,6 +39,7 @@ from oslo_utils import importutils from oslo_utils import strutils osprofiler_web = importutils.try_import("osprofiler.web") # noqa import requests +from six.moves import urllib import six.moves.urllib.parse as urlparse from cinderclient import api_versions @@ -83,8 +84,29 @@ def get_server_version(url): logger = logging.getLogger(__name__) try: - scheme, netloc, path, query, frag = urlparse.urlsplit(url) - response = requests.get(scheme + '://' + netloc) + u = urllib.parse.urlparse(url) + version_url = None + + # NOTE(andreykurilin): endpoint URL has at least 2 formats: + # 1. The classic (legacy) endpoint: + # http://{host}:{optional_port}/v{1 or 2 or 3}/{project-id} + # http://{host}:{optional_port}/v{1 or 2 or 3} + # 3. Under wsgi: + # http://{host}:{optional_port}/volume/v{1 or 2 or 3} + for ver in ['v1', 'v2', 'v3']: + if u.path.endswith(ver) or "/{0}/".format(ver) in u.path: + path = u.path[:u.path.rfind(ver)] + version_url = '%s://%s%s' % (u.scheme, u.netloc, path) + break + + if not version_url: + # NOTE(andreykurilin): probably, it is one of the next cases: + # * https://volume.example.com/ + # * https://example.com/volume + # leave as is without cropping. + version_url = url + + response = requests.get(version_url) data = json.loads(response.text) versions = data['versions'] for version in versions: diff --git a/cinderclient/tests/unit/test_client.py b/cinderclient/tests/unit/test_client.py index bd3b3f6..5697039 100644 --- a/cinderclient/tests/unit/test_client.py +++ b/cinderclient/tests/unit/test_client.py @@ -332,8 +332,12 @@ class GetAPIVersionTestCase(utils.TestCase): self.assertEqual(api_versions.APIVersion('2.0'), max_version) @mock.patch('cinderclient.client.requests.get') - def test_get_server_version(self, mock_request): - + @ddt.data( + 'http://192.168.122.127:8776/v3/e5526285ebd741b1819393f772f11fc3', + 'https://192.168.122.127:8776/v3/e55285ebd741b1819393f772f11fc3', + 'http://192.168.122.127/volumesv3/e5526285ebd741b1819393f772f11fc3' + ) + def test_get_server_version(self, url, mock_request): mock_response = utils.TestResponse({ "status_code": 200, "text": json.dumps(fakes.fake_request_get()) @@ -341,14 +345,6 @@ class GetAPIVersionTestCase(utils.TestCase): mock_request.return_value = mock_response - url = "http://192.168.122.127:8776/v3/e5526285ebd741b1819393f772f11fc3" - - min_version, max_version = cinderclient.client.get_server_version(url) - self.assertEqual(min_version, api_versions.APIVersion('3.0')) - self.assertEqual(max_version, api_versions.APIVersion('3.16')) - - url = "https://192.168.122.127:8776/v3/e55285ebd741b1819393f772f11fc3" - min_version, max_version = cinderclient.client.get_server_version(url) self.assertEqual(min_version, api_versions.APIVersion('3.0')) self.assertEqual(max_version, api_versions.APIVersion('3.16')) |