diff --git a/manila/api/common.py b/manila/api/common.py index af9a88ca9e..015a7f242c 100644 --- a/manila/api/common.py +++ b/manila/api/common.py @@ -142,20 +142,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) diff --git a/manila/tests/api/test_common.py b/manila/tests/api/test_common.py index e6c70d4d6c..5bb595b2ff 100644 --- a/manila/tests/api/test_common.py +++ b/manila/tests/api/test_common.py @@ -196,50 +196,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) diff --git a/releasenotes/notes/bug-1815038-extend-remove_version_from_href-support-ea479daaaf5c5700.yaml b/releasenotes/notes/bug-1815038-extend-remove_version_from_href-support-ea479daaaf5c5700.yaml new file mode 100644 index 0000000000..18c7f7261b --- /dev/null +++ b/releasenotes/notes/bug-1815038-extend-remove_version_from_href-support-ea479daaaf5c5700.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + `Launchpad bug 1815038 `_ + has been fixed and now we correctly parse the base URL from manila's + endpoint url, accounting for proxy URLs.