Add netloc and version check for version discovery

If the url netloc in the catalog and service's response
are not the same, we should choose the catalog's and
add the version info to it if needed.

Change-Id: If78d368bd505156a5416bb9cbfaf988204925c79
Closes-bug: #1733052
This commit is contained in:
wangxiyuan 2018-07-17 19:43:21 +08:00
parent ebe781a3ea
commit 323f4e4bc4
4 changed files with 61 additions and 2 deletions

View File

@ -417,10 +417,28 @@ def _combine_relative_url(discovery_url, version_url):
parsed_version_url = urllib.parse.urlparse(url)
parsed_discovery_url = urllib.parse.urlparse(discovery_url)
# The services can override the version_url with some config options.(for
# example, In Keystone, Cinder and Glance, the option is "public_endpoint",
# and "compute_link_prefix", "network_link_prefix" in Nova and Neutron.
# In this case, it's hard to distinguish which part in version_url is
# useful for discovery_url , so here we just get the version from
# version_url and then add it into the discovery_url if needed.
path = parsed_version_url.path
if parsed_discovery_url.netloc != parsed_version_url.netloc:
version = version_url.rstrip('/').split('/')[-1]
url_path = parsed_discovery_url.path.rstrip('/')
if not url_path.endswith(version):
path = url_path + '/' + version
if version_url.endswith('/'):
# add '/' back to keep backward compatibility.
path = path + '/'
else:
path = parsed_discovery_url.path
return urllib.parse.ParseResult(
parsed_discovery_url.scheme,
parsed_discovery_url.netloc,
parsed_version_url.path,
path,
parsed_version_url.params,
parsed_version_url.query,
parsed_version_url.fragment).geturl()

View File

@ -329,7 +329,7 @@ class CommonIdentityTests(object):
sb = session.Session()
discovery_cache = {}
expected_url = urllib.parse.urljoin(self.TEST_COMPUTE_ADMIN, '/v2.0')
expected_url = self.TEST_COMPUTE_ADMIN + '/v2.0'
for sess in (sa, sb):
disc = discover.get_discovery(

View File

@ -491,6 +491,40 @@ class VersionDataTests(utils.TestCase):
self.assertTrue(mock.called_once)
def test_version_data_override_version_url(self):
# if the request url is versioned already, just return it.
self.requests_mock.get(
V3_URL, status_code=200,
json={'version': fixture.V3Discovery('http://override/identity/v3')
}
)
disc = discover.Discover(self.session, V3_URL)
version_data = disc.version_data()
for v in version_data:
self.assertEqual(v['version'], (3, 0))
self.assertEqual(v['status'], discover.Status.CURRENT)
self.assertEqual(v['raw_status'], 'stable')
self.assertEqual(v['url'], V3_URL)
# if the request url is not versioned, just add version info to it.(
# do not changed the url's netloc or path)
self.requests_mock.get(
BASE_URL, status_code=200,
json={'version': fixture.V3Discovery('http://override/identity/v3')
}
)
disc = discover.Discover(self.session, BASE_URL)
version_data = disc.version_data()
for v in version_data:
self.assertEqual(v['version'], (3, 0))
self.assertEqual(v['status'], discover.Status.CURRENT)
self.assertEqual(v['raw_status'], 'stable')
self.assertEqual(v['url'], V3_URL)
def test_version_data_unknown(self):
discovery_fixture = fixture.V3Discovery(V3_URL)
discovery_fixture.status = 'hungry'

View File

@ -0,0 +1,7 @@
---
fixes:
- >
[`bug 1733052 <https://bugs.launchpad.net/keystoneauth/+bug/1733052>`_]
Now the version discovery mechanism only fetches the version info from
server side if the versioned url has been overrode. So that the request
url's path won't be changed completely.