From 70783b01579c772db26e6719bdb8e5fbe5c802bb Mon Sep 17 00:00:00 2001 From: hejiawei Date: Thu, 1 Dec 2016 10:25:43 +0800 Subject: [PATCH] Fix the bug 1578882 1.What is the problem? Any functions should not be copied and pasted but imported from other components.The follow functions are copied from Nova: 42 def url_join(*parts): [snip] 395 def url_join(*parts): [snip] 43 def remove_trailing_version_from_href(href): [snip] 278 def remove_trailing_version_from_href(href): [snip] 2.What is the solution to the problem? We can't import it from nova,so we move it to common module. 3.What the features need to be implemented to the Tricircle to realize the solution? No new features. Change-Id: I747f1a43fa20bf66ae56b6e2927410224d0e8acd Closes-Bug: #1578882 --- trio2o/common/utils.py | 41 ++++++++++++++++++++++ trio2o/nova_apigw/controllers/image.py | 48 +++----------------------- 2 files changed, 45 insertions(+), 44 deletions(-) diff --git a/trio2o/common/utils.py b/trio2o/common/utils.py index 0300260..eb40675 100644 --- a/trio2o/common/utils.py +++ b/trio2o/common/utils.py @@ -16,6 +16,8 @@ import six import pecan +import re +import urlparse from oslo_log import log as logging @@ -165,3 +167,42 @@ def get_pod_by_top_id(context, _id): return None return mappings[0][0] + + +def url_join(*parts): + """Convenience method for joining parts of a URL + + Any leading and trailing '/' characters are removed, and the parts joined + together with '/' as a separator. If last element of 'parts' is an empty + string, the returned URL will have a trailing slash. + """ + parts = parts or [''] + clean_parts = [part.strip('/') for part in parts if part] + if not parts[-1]: + # Empty last element should add a trailing slash + clean_parts.append('') + return '/'.join(clean_parts) + + +def remove_trailing_version_from_href(href): + """Removes the api version from the href. + + Given: 'http://www.nova.com/compute/v1.1' + Returns: 'http://www.nova.com/compute' + + Given: 'http://www.nova.com/v1.1' + Returns: 'http://www.nova.com' + + """ + parsed_url = urlparse.urlsplit(href) + url_parts = parsed_url.path.rsplit('/', 1) + + # NOTE: this should match vX.X or vX + expression = re.compile(r'^v([0-9]+|[0-9]+\.[0-9]+)(/.*|$)') + if not expression.match(url_parts.pop()): + raise ValueError('URL %s does not contain version' % href) + + new_path = url_join(*url_parts) + parsed_url = list(parsed_url) + parsed_url[2] = new_path + return urlparse.urlunsplit(parsed_url) diff --git a/trio2o/nova_apigw/controllers/image.py b/trio2o/nova_apigw/controllers/image.py index 3c045cf..bc8e6e3 100644 --- a/trio2o/nova_apigw/controllers/image.py +++ b/trio2o/nova_apigw/controllers/image.py @@ -15,8 +15,6 @@ from pecan import expose from pecan import rest -import re -import urlparse import trio2o.common.client as t_client from trio2o.common import constants @@ -26,45 +24,6 @@ from trio2o.common import utils import trio2o.db.api as db_api -def url_join(*parts): - """Convenience method for joining parts of a URL - - Any leading and trailing '/' characters are removed, and the parts joined - together with '/' as a separator. If last element of 'parts' is an empty - string, the returned URL will have a trailing slash. - """ - parts = parts or [''] - clean_parts = [part.strip('/') for part in parts if part] - if not parts[-1]: - # Empty last element should add a trailing slash - clean_parts.append('') - return '/'.join(clean_parts) - - -def remove_trailing_version_from_href(href): - """Removes the api version from the href. - - Given: 'http://www.nova.com/compute/v1.1' - Returns: 'http://www.nova.com/compute' - - Given: 'http://www.nova.com/v1.1' - Returns: 'http://www.nova.com' - - """ - parsed_url = urlparse.urlsplit(href) - url_parts = parsed_url.path.rsplit('/', 1) - - # NOTE: this should match vX.X or vX - expression = re.compile(r'^v([0-9]+|[0-9]+\.[0-9]+)(/.*|$)') - if not expression.match(url_parts.pop()): - raise ValueError('URL %s does not contain version' % href) - - new_path = url_join(*url_parts) - parsed_url = list(parsed_url) - parsed_url[2] = new_path - return urlparse.urlunsplit(parsed_url) - - class ImageController(rest.RestController): def __init__(self, project_id): @@ -76,9 +35,10 @@ class ImageController(rest.RestController): context, db_api.get_top_pod(context)['pod_id'], constants.ST_NOVA) nova_url = nova_url.replace('/$(tenant_id)s', '') - self_link = url_join(nova_url, self.project_id, 'images', image['id']) - bookmark_link = url_join( - remove_trailing_version_from_href(nova_url), + self_link = utils.url_join(nova_url, self.project_id, + 'images', image['id']) + bookmark_link = utils.url_join( + utils.remove_trailing_version_from_href(nova_url), self.project_id, 'images', image['id']) glance_url = self.client.get_endpoint( context, db_api.get_top_pod(context)['pod_id'],