diff --git a/reddwarf/common/views.py b/reddwarf/common/views.py new file mode 100644 index 0000000000..e133323da6 --- /dev/null +++ b/reddwarf/common/views.py @@ -0,0 +1,42 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2010-2011 OpenStack LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http: //www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +from reddwarf.common import wsgi + + +def create_links(resource_path, request, id): + """Creates the links dictionary in the format typical of most resources.""" + context = request.environ[wsgi.CONTEXT_KEY] + link_info = { + 'host': request.host, + 'version': request.url_version, + 'tenant_id': context.tenant, + 'resource_path': resource_path, + 'id': id, + } + return [ + { + "href": "https://%(host)s/v%(version)s/%(tenant_id)s" + "/%(resource_path)s/%(id)s" % link_info, + "rel": "self" + }, + { + "href": "https://%(host)s/%(resource_path)s/%(id)s" % link_info, + "rel": "bookmark" + } + ] diff --git a/reddwarf/flavor/views.py b/reddwarf/flavor/views.py index 5d2c530eee..5518360bda 100644 --- a/reddwarf/flavor/views.py +++ b/reddwarf/flavor/views.py @@ -16,6 +16,9 @@ # under the License. +from reddwarf.common.views import create_links + + class FlavorView(object): def __init__(self, flavor, req=None): @@ -30,34 +33,7 @@ class FlavorView(object): }} def _build_links(self): - result = [] - #scheme = self.req.scheme - scheme = 'https' # Forcing https - endpoint = self.req.host - splitpath = self.req.path.split('/') - detailed = '' - if splitpath[-1] == 'detail': - detailed = '/detail' - splitpath.pop(-1) - flavorid = self.flavor.id - if str(splitpath[-1]) == str(flavorid): - splitpath.pop(-1) - href_template = "%(scheme)s://%(endpoint)s%(path)s/%(flavorid)s" - for link in self.flavor.links: - rlink = link - href = rlink['href'] - if rlink['rel'] == 'self': - path = '/'.join(splitpath) - href = href_template % locals() - elif rlink['rel'] == 'bookmark': - splitpath.pop(2) # Remove the version. - splitpath.pop(1) # Remove the tenant id. - path = '/'.join(splitpath) - href = href_template % locals() - - rlink['href'] = href - result.append(rlink) - return result + return create_links("flavors", self.req, self.flavor.id) class FlavorDetailView(FlavorView): diff --git a/reddwarf/instance/views.py b/reddwarf/instance/views.py index 396f96cad6..b038c150ea 100644 --- a/reddwarf/instance/views.py +++ b/reddwarf/instance/views.py @@ -19,6 +19,8 @@ import logging from reddwarf.common import config from reddwarf.common import utils from reddwarf.common import wsgi +from reddwarf.common.views import create_links + LOG = logging.getLogger(__name__) @@ -58,24 +60,7 @@ class InstanceView(object): return {"instance": instance_dict} def _build_links(self): - context = self.req.environ[wsgi.CONTEXT_KEY] - link_info = { - 'host':self.req.host, - 'version':self.req.url_version, - 'tenant_id':context.tenant, - 'id':self.instance.id, - } - return [ - { - "href": "https://%(host)s/v%(version)s/%(tenant_id)s" - "/instances/%(id)s" % link_info, - "rel": "self" - }, - { - "href": "https://%(host)s/instances/%(id)s" % link_info, - "rel": "bookmark" - } - ] + return create_links("instances", self.req, self.instance.id) class InstanceDetailView(InstanceView): @@ -90,25 +75,10 @@ class InstanceDetailView(InstanceView): self.add_volumes = add_volumes def _build_flavor_info(self): - context = self.req.environ[wsgi.CONTEXT_KEY] - link_info = { - 'host':self.req.host, - 'version':self.req.url_version, - 'tenant_id':context.tenant, - 'flavor_id':self.instance.flavor_id, - } return { "id": self.instance.flavor_id, - "links": [{ - "href": "https://%(host)s/v%(version)s/%(tenant_id)s" - "/flavors/%(flavor_id)s" % link_info, - "rel": "self" - }, - { - "href": "https://%(host)s/flavors/%(flavor_id)s" % link_info, - "rel": "bookmark" - }] - } + "links": self._build_flavor_links() + } def data(self): result = super(InstanceDetailView, self).data() @@ -125,46 +95,9 @@ class InstanceDetailView(InstanceView): result['instance']['ip'] = ip return result - def _build_flavor(self): - try: - return self.instance.flavor - except: - return { - 'id': self.instance.db_info.flavor_id, - 'links': self._build_flavor_links(), - } - def _build_flavor_links(self): - result = [] - #scheme = self.req.scheme - scheme = 'https' # Forcing https - endpoint = self.req.host - splitpath = self.req.path.split('/') - detailed = '' - if splitpath[-1] == 'detail': - detailed = '/detail' - splitpath.pop(-1) - flavorid = self.instance.db_info.flavor_id - if str(splitpath[-1]) == str(flavorid): - splitpath.pop(-1) - href_template = "%(scheme)s://%(endpoint)s%(path)s/%(flavorid)s" - for link in self.instance.flavor_links: - rlink = link - href = rlink['href'] - if rlink['rel'] == 'self': - path = '/'.join(splitpath) - href = href_template % locals() - elif rlink['rel'] == 'bookmark': - splitpath.pop(2) # Remove the version. - splitpath.pop(1) # Remove the tenant id. - path = '/'.join(splitpath) - href = href_template % locals() - - rlink['href'] = href - result.append(rlink) - for link in result: - link['href'] = link['href'].replace('instances', 'flavors') - return result + return create_links("flavors", self.req, + self.instance.flavor_id) class InstancesView(object):