Moved build_links and req into the view, cut out some redundant exceptions
This commit is contained in:
@@ -22,7 +22,6 @@ import logging
|
||||
from reddwarf import db
|
||||
|
||||
from novaclient import exceptions as nova_exceptions
|
||||
from novaclient.v1_1.client import Client
|
||||
from reddwarf.common import config
|
||||
from reddwarf.common import exception as rd_exceptions
|
||||
from reddwarf.common import utils
|
||||
@@ -35,7 +34,7 @@ LOG = logging.getLogger('reddwarf.database.models')
|
||||
|
||||
class Flavor(object):
|
||||
|
||||
_data_fields = ['id', 'links', 'name', 'ram']
|
||||
_data_fields = ['id', 'links', 'name', 'ram', 'vcpus']
|
||||
|
||||
def __init__(self, flavor=None, context=None, flavor_id=None):
|
||||
if flavor:
|
||||
@@ -72,39 +71,7 @@ class Flavor(object):
|
||||
|
||||
@property
|
||||
def links(self):
|
||||
return self._build_links()
|
||||
|
||||
def _build_links(self):
|
||||
if self.req is None:
|
||||
# If there's no request available, don't modify the links.
|
||||
return self.flavor.links
|
||||
result = []
|
||||
scheme = self.req.scheme
|
||||
endpoint = self.req.host
|
||||
splitpath = self.req.path.split('/')
|
||||
detailed = ''
|
||||
if splitpath[-1] == 'detail':
|
||||
detailed = '/detail'
|
||||
splitpath.pop(-1)
|
||||
flavorid = self.flavor.id
|
||||
if splitpath[-1] == 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 self.flavor.links
|
||||
|
||||
|
||||
class Flavors(NovaRemoteModelBase):
|
||||
|
||||
@@ -15,19 +15,14 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import logging
|
||||
import routes
|
||||
import webob.exc
|
||||
|
||||
from reddwarf.common import config
|
||||
from reddwarf.common import exception
|
||||
from reddwarf.common import wsgi
|
||||
from reddwarf.flavor import models
|
||||
from reddwarf.flavor import views
|
||||
|
||||
CONFIG = config.Config
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class BaseController(wsgi.Controller):
|
||||
"""Base controller class."""
|
||||
@@ -62,29 +57,20 @@ class FlavorController(BaseController):
|
||||
def show(self, req, tenant_id, id):
|
||||
"""Return a single flavor."""
|
||||
context = req.environ[wsgi.CONTEXT_KEY]
|
||||
try:
|
||||
flavor = models.Flavor(context=context, flavor_id=id)
|
||||
except exception.ReddwarfError, e:
|
||||
return wsgi.Result(str(e), 404)
|
||||
flavor = models.Flavor(context=context, flavor_id=id)
|
||||
# Pass in the request to build accurate links.
|
||||
return wsgi.Result(views.FlavorDetailView(flavor, req).data(), 200)
|
||||
|
||||
def detail(self, req, tenant_id):
|
||||
"""Return a list of flavors, with additional data about each flavor."""
|
||||
context = req.environ[wsgi.CONTEXT_KEY]
|
||||
try:
|
||||
flavors = models.Flavors(context=context)
|
||||
except exception.ReddwarfError, e:
|
||||
return wsgi.Result(str(e), 404)
|
||||
return wsgi.Result(views.FlavorsView(flavors, req).data(detailed=True), 200)
|
||||
flavors = models.Flavors(context=context)
|
||||
return wsgi.Result(views.FlavorsDetailView(flavors, req).data(), 200)
|
||||
|
||||
def index(self, req, tenant_id):
|
||||
"""Return all flavors."""
|
||||
context = req.environ[wsgi.CONTEXT_KEY]
|
||||
try:
|
||||
flavors = models.Flavors(context=context)
|
||||
except exception.ReddwarfError, e:
|
||||
return wsgi.Result(str(e), 404)
|
||||
flavors = models.Flavors(context=context)
|
||||
return wsgi.Result(views.FlavorsView(flavors, req).data(), 200)
|
||||
|
||||
class API(wsgi.Router):
|
||||
|
||||
@@ -19,15 +19,44 @@ class FlavorView(object):
|
||||
|
||||
def __init__(self, flavor, req=None):
|
||||
self.flavor = flavor
|
||||
self.flavor.req = req
|
||||
self.req = req
|
||||
|
||||
def data(self):
|
||||
return {"flavor": {
|
||||
'id': self.flavor.id,
|
||||
'links': self.flavor.links,
|
||||
'links': self._build_links(),
|
||||
'name': self.flavor.name,
|
||||
}}
|
||||
|
||||
def _build_links(self):
|
||||
result = []
|
||||
scheme = self.req.scheme
|
||||
endpoint = self.req.host
|
||||
splitpath = self.req.path.split('/')
|
||||
detailed = ''
|
||||
if splitpath[-1] == 'detail':
|
||||
detailed = '/detail'
|
||||
splitpath.pop(-1)
|
||||
flavorid = self.flavor.id
|
||||
if splitpath[-1] == 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
|
||||
|
||||
|
||||
class FlavorDetailView(FlavorView):
|
||||
|
||||
@@ -42,6 +71,7 @@ class FlavorDetailView(FlavorView):
|
||||
|
||||
|
||||
class FlavorsView(object):
|
||||
view = FlavorView
|
||||
|
||||
def __init__(self, flavors, req=None):
|
||||
self.flavors = flavors
|
||||
@@ -50,9 +80,9 @@ class FlavorsView(object):
|
||||
def data(self, detailed=False):
|
||||
data = []
|
||||
for flavor in self.flavors:
|
||||
if detailed:
|
||||
data.append(FlavorDetailView(flavor, req=self.req).data()['flavor'])
|
||||
else:
|
||||
data.append(FlavorView(flavor, req=self.req).data()['flavor'])
|
||||
|
||||
data.append(self.view(flavor, req=self.req).data()['flavor'])
|
||||
return {"flavors": data}
|
||||
|
||||
|
||||
class FlavorsDetailView(FlavorsView):
|
||||
view = FlavorDetailView
|
||||
|
||||
Reference in New Issue
Block a user