Extend remove_version_from_href support

Improve remove_version_from_href so that it supports URLs for which
the version does not directly trail the hostname

Given: 'http://manila.example.com/share/v1.1/123'
Returns: 'http://manila.example.com/share/123'

Based on I8ccc449116ff164aacc0aefca3a0ec2ac8f73aa8

Change-Id: I13010d3ee0a83a67204145837503abe7126263cd
Closes-Bug: #1815038
(cherry picked from commit 6d970b7375)
This commit is contained in:
Victoria Martinez de la Cruz 2019-02-07 12:56:42 +00:00 committed by Tom Barron
parent c9f581caeb
commit 670449f3e1
3 changed files with 43 additions and 46 deletions

View File

@ -139,20 +139,25 @@ def limited(items, request, max_limit=CONF.osapi_max_limit):
def remove_version_from_href(href):
"""Removes the first api version from the href.
Given: 'http://www.manila.com/v1.1/123'
Returns: 'http://www.manila.com/123'
Given: 'http://manila.example.com/v1.1/123'
Returns: 'http://manila.example.com/123'
Given: 'http://www.manila.com/v1.1'
Returns: 'http://www.manila.com'
Given: 'http://manila.example.com/share/v1.1/123'
Returns: 'http://manila.example.com/share/123'
"""
parsed_url = parse.urlsplit(href)
url_parts = parsed_url.path.split('/', 2)
url_parts = parsed_url.path.split('/')
# NOTE: this should match vX.X or vX
expression = re.compile(r'^v([0-9]+|[0-9]+\.[0-9]+)(/.*|$)')
if expression.match(url_parts[1]):
del url_parts[1]
for x in range(len(url_parts)):
if expression.match(url_parts[x]):
del url_parts[x]
break
new_path = '/'.join(url_parts)

View File

@ -193,50 +193,36 @@ class PaginationParamsTest(test.TestCase):
@ddt.ddt
class MiscFunctionsTest(test.TestCase):
def test_remove_major_version_from_href(self):
fixture = 'http://www.testsite.com/v1/images'
expected = 'http://www.testsite.com/images'
@ddt.data(
('http://manila.example.com/v2/b2d18606-2673-4965-885a-4f5a8b955b9b/',
'http://manila.example.com/b2d18606-2673-4965-885a-4f5a8b955b9b/'),
('http://manila.example.com/v1/',
'http://manila.example.com/'),
('http://manila.example.com/share/v2.22/',
'http://manila.example.com/share/'),
('http://manila.example.com/share/v1/'
'b2d18606-2673-4965-885a-4f5a8b955b9b/',
'http://manila.example.com/share/'
'b2d18606-2673-4965-885a-4f5a8b955b9b/'),
('http://10.10.10.10:3366/v1/',
'http://10.10.10.10:3366/'),
('http://10.10.10.10:3366/v2/b2d18606-2673-4965-885a-4f5a8b955b9b/',
'http://10.10.10.10:3366/b2d18606-2673-4965-885a-4f5a8b955b9b/'),
('http://manila.example.com:3366/v1.1/',
'http://manila.example.com:3366/'),
('http://manila.example.com:3366/v2/'
'b2d18606-2673-4965-885a-4f5a8b955b9b/',
'http://manila.example.com:3366/'
'b2d18606-2673-4965-885a-4f5a8b955b9b/'))
@ddt.unpack
def test_remove_version_from_href(self, fixture, expected):
actual = common.remove_version_from_href(fixture)
self.assertEqual(expected, actual)
def test_remove_version_from_href(self):
fixture = 'http://www.testsite.com/v1.1/images'
expected = 'http://www.testsite.com/images'
actual = common.remove_version_from_href(fixture)
self.assertEqual(expected, actual)
def test_remove_version_from_href_2(self):
fixture = 'http://www.testsite.com/v1.1/'
expected = 'http://www.testsite.com/'
actual = common.remove_version_from_href(fixture)
self.assertEqual(expected, actual)
def test_remove_version_from_href_3(self):
fixture = 'http://www.testsite.com/v10.10'
expected = 'http://www.testsite.com'
actual = common.remove_version_from_href(fixture)
self.assertEqual(expected, actual)
def test_remove_version_from_href_4(self):
fixture = 'http://www.testsite.com/v1.1/images/v10.5'
expected = 'http://www.testsite.com/images/v10.5'
actual = common.remove_version_from_href(fixture)
self.assertEqual(expected, actual)
def test_remove_version_from_href_bad_request(self):
fixture = 'http://www.testsite.com/1.1/images'
self.assertRaises(ValueError,
common.remove_version_from_href,
fixture)
def test_remove_version_from_href_bad_request_2(self):
fixture = 'http://www.testsite.com/v/images'
self.assertRaises(ValueError,
common.remove_version_from_href,
fixture)
def test_remove_version_from_href_bad_request_3(self):
fixture = 'http://www.testsite.com/v1.1images'
@ddt.data('http://manila.example.com/1.1/shares',
'http://manila.example.com/v/shares',
'http://manila.example.com/v1.1shares')
def test_remove_version_from_href_bad_request(self, fixture):
self.assertRaises(ValueError,
common.remove_version_from_href,
fixture)

View File

@ -0,0 +1,6 @@
---
fixes:
- |
`Launchpad bug 1815038 <https://bugs.launchpad.net/manila/+bug/1815038>`_
has been fixed and now we correctly parse the base URL from manila's
endpoint url, accounting for proxy URLs.