Merge pull request #129 from rnirmal/farvel_flavor_details

Farvel /flavors/detail. Also consolidating the API() into a common class
This commit is contained in:
Nirmal Ranganathan 2012-06-14 09:11:37 -07:00
commit 3380bbb738
6 changed files with 57 additions and 76 deletions

View File

@ -101,7 +101,7 @@ paste.filter_factory = reddwarf.common.auth:AuthorizationMiddleware.factory
paste.filter_factory = reddwarf.common.wsgi:ContextMiddleware.factory
[app:reddwarfapp]
paste.app_factory = reddwarf.instance.service:app_factory
paste.app_factory = reddwarf.common.api:app_factory
#Add this filter to log request and response for debugging
[filter:debug]

View File

@ -107,7 +107,7 @@ paste.filter_factory = reddwarf.common.auth:AuthorizationMiddleware.factory
paste.filter_factory = reddwarf.common.wsgi:ContextMiddleware.factory
[app:reddwarfapp]
paste.app_factory = reddwarf.instance.service:app_factory
paste.app_factory = reddwarf.common.api:app_factory
#Add this filter to log request and response for debugging
[filter:debug]

43
reddwarf/common/api.py Normal file
View File

@ -0,0 +1,43 @@
# Copyright 2012 OpenStack LLC
#
# 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.
import routes
from reddwarf.common import wsgi
from reddwarf.flavor.service import FlavorController
from reddwarf.instance.service import InstanceController
class API(wsgi.Router):
"""API"""
def __init__(self):
mapper = routes.Mapper()
super(API, self).__init__(mapper)
self._instance_router(mapper)
self._flavor_router(mapper)
def _instance_router(self, mapper):
instance_resource = InstanceController().create_resource()
path = "/{tenant_id}/instances"
mapper.resource("instance", path, controller=instance_resource,
member={'action': 'POST'})
def _flavor_router(self, mapper):
flavor_resource = FlavorController().create_resource()
path = "/{tenant_id}/flavors"
mapper.resource("flavor", path, controller=flavor_resource)
def app_factory(global_conf, **local_conf):
return API()

View File

@ -58,15 +58,10 @@ class FlavorController(BaseController):
def show(self, req, tenant_id, id):
"""Return a single flavor."""
context = req.environ[wsgi.CONTEXT_KEY]
flavor = models.Flavor(context=context, flavor_id=id)
self._validate_flavor_id(id)
flavor = models.Flavor(context=context, flavor_id=int(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]
flavors = models.Flavors(context=context)
return wsgi.Result(views.FlavorsDetailView(flavors, req).data(), 200)
return wsgi.Result(views.FlavorView(flavor, req).data(), 200)
def index(self, req, tenant_id):
"""Return all flavors."""
@ -74,20 +69,9 @@ class FlavorController(BaseController):
flavors = models.Flavors(context=context)
return wsgi.Result(views.FlavorsView(flavors, req).data(), 200)
class API(wsgi.Router):
"""API"""
def __init__(self):
mapper = routes.Mapper()
super(API, self).__init__(mapper)
self._flavor_router(mapper)
def _flavor_router(self, mapper):
flavor_resource = FlavorController().create_resource()
path = "/{tenant_id}/flavors"
mapper.resource("flavor", path, controller=flavor_resource,
collection={'detail': 'GET', '': 'GET'})
def app_factory(global_conf, **local_conf):
return API()
def _validate_flavor_id(self, id):
try:
if int(id) != float(id):
raise exception.NotFound(uuid=id)
except ValueError:
raise exception.NotFound(uuid=id)

View File

@ -27,26 +27,16 @@ class FlavorView(object):
def data(self):
return {"flavor": {
'id': self.flavor.id,
'id': int(self.flavor.id),
'links': self._build_links(),
'name': self.flavor.name,
'ram': self.flavor.ram,
}}
def _build_links(self):
return create_links("flavors", self.req, self.flavor.id)
class FlavorDetailView(FlavorView):
def data(self):
result = super(FlavorDetailView, self).data()
details = {
'ram': self.flavor.ram
}
result["flavor"].update(details)
return result
class FlavorsView(object):
view = FlavorView
@ -54,12 +44,8 @@ class FlavorsView(object):
self.flavors = flavors
self.req = req
def data(self, detailed=False):
def data(self):
data = []
for flavor in self.flavors:
data.append(self.view(flavor, req=self.req).data()['flavor'])
return {"flavors": data}
class FlavorsDetailView(FlavorsView):
view = FlavorDetailView

View File

@ -26,10 +26,6 @@ from reddwarf.common import utils
from reddwarf.common import wsgi
from reddwarf.instance import models, views
#TODO(ed-): Import these properly after this is restructured
from reddwarf.flavor import models as flavormodels
from reddwarf.flavor import views as flavorviews
from reddwarf.flavor import service as flavorservice
CONFIG = config.Config
LOG = logging.getLogger(__name__)
@ -322,31 +318,3 @@ class InstanceController(BaseController):
LOG.error(_("Create Instance Required field(s) - %s") % e)
raise exception.ReddwarfError("Required element/key - %s "
"was not specified" % e)
class API(wsgi.Router):
"""API"""
def __init__(self):
mapper = routes.Mapper()
super(API, self).__init__(mapper)
self._instance_router(mapper)
# TODO(ed-): Remove after restructure
self._flavor_router(mapper)
def _instance_router(self, mapper):
instance_resource = InstanceController().create_resource()
path = "/{tenant_id}/instances"
mapper.resource("instance", path, controller=instance_resource,
member={'action': 'POST'})
# TODO(ed-): remove this when all mention of flavorservice
# et cetera are moved away
def _flavor_router(self, mapper):
flavor_resource = flavorservice.FlavorController().create_resource()
path = "/{tenant_id}/flavors"
mapper.resource("flavor", path, controller=flavor_resource,
collection={'detail': 'GET'})
def app_factory(global_conf, **local_conf):
return API()