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://cinder.example.com/cinder/v1.1/volumes'
Returns: 'http://cinder.example.com/cinder/volumes'

Co-Authored-By: Xinli Guan <xinli@us.ibm.com>

Closes-Bug: #1560947
Change-Id: I8ccc449116ff164aacc0aefca3a0ec2ac8f73aa8
This commit is contained in:
Ryan McNair 2016-03-29 15:34:13 +00:00 committed by Xinli Guan
parent aa7e0f1c8a
commit 41981d4803
2 changed files with 16 additions and 6 deletions

View File

@ -240,11 +240,14 @@ def get_request_url(request):
def remove_version_from_href(href):
"""Removes the first api version from the href.
Given: 'http://www.cinder.com/v1.1/123'
Returns: 'http://www.cinder.com/123'
Given: 'http://cinder.example.com/v1.1/123'
Returns: 'http://cinder.example.com/123'
Given: 'http://www.cinder.com/v1.1'
Returns: 'http://www.cinder.com'
Given: 'http://cinder.example.com/v1.1'
Returns: 'http://cinder.example.com'
Given: 'http://cinder.example.com/volume/drivers/v1.1/flashsystem'
Returns: 'http://cinder.example.com/volume/drivers/flashsystem'
"""
parsed_url = urllib.parse.urlsplit(href)
@ -252,8 +255,10 @@ def remove_version_from_href(href):
# 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

@ -354,6 +354,11 @@ class MiscFunctionsTest(test.TestCase):
actual = common.remove_version_from_href(fixture)
self.assertEqual(expected, actual)
def test_remove_version_from_href_version_not_trailing_domain(self):
fixture = 'http://www.testsite.com/cinder/v2'
expected = 'http://www.testsite.com/cinder'
self.assertEqual(expected, common.remove_version_from_href(fixture))
def test_remove_version_from_href_bad_request(self):
fixture = 'http://www.testsite.com/1.1/images'
self.assertRaises(ValueError,