From 90da2c677976b3e9e96bd68703a8b9d5ba347ce3 Mon Sep 17 00:00:00 2001 From: "Mauro S. M. Rodrigues" Date: Tue, 4 Jun 2013 00:49:08 -0400 Subject: Change resource links when url has no project id This patch allow response body to be look like the request url. So if a project_id is passed the resource links on the response will also have a project_id, and in the case of api-v3 where project_id are no longer present in the url the response will not have it either. Complements bp v3-api-remove-project-id. Also fix two minor issues on the unit tests, which were breaking with this change: - When doing ServersViewBuilderTest there was no api version specified. - ServersControllerTest was duplicating api version on the application url. Change-Id: I872332ef56b4b6e99fedb43833ad45e8e654f44f --- nova/api/openstack/common.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'nova/api') diff --git a/nova/api/openstack/common.py b/nova/api/openstack/common.py index bec919f4b..7daf7aadb 100644 --- a/nova/api/openstack/common.py +++ b/nova/api/openstack/common.py @@ -454,6 +454,16 @@ def check_snapshots_enabled(f): class ViewBuilder(object): """Model API responses as dictionaries.""" + def _get_project_id(self, request): + """ + Get project id from request url if present or empty string + otherwise + """ + project_id = request.environ["nova.context"].project_id + if project_id in request.url: + return project_id + return '' + def _get_links(self, request, identifier, collection_name): return [{ "rel": "self", @@ -472,7 +482,7 @@ class ViewBuilder(object): params["marker"] = identifier prefix = self._update_compute_link_prefix(request.application_url) url = os.path.join(prefix, - request.environ["nova.context"].project_id, + self._get_project_id(request), collection_name) return "%s?%s" % (url, dict_to_query_str(params)) @@ -480,7 +490,7 @@ class ViewBuilder(object): """Return an href string pointing to this object.""" prefix = self._update_compute_link_prefix(request.application_url) return os.path.join(prefix, - request.environ["nova.context"].project_id, + self._get_project_id(request), collection_name, str(identifier)) @@ -489,7 +499,7 @@ class ViewBuilder(object): base_url = remove_version_from_href(request.application_url) base_url = self._update_compute_link_prefix(base_url) return os.path.join(base_url, - request.environ["nova.context"].project_id, + self._get_project_id(request), collection_name, str(identifier)) -- cgit