diff --git a/etc/reddwarf/reddwarf.conf.sample b/etc/reddwarf/reddwarf.conf.sample index 301f1b5147..43cdd0bed9 100644 --- a/etc/reddwarf/reddwarf.conf.sample +++ b/etc/reddwarf/reddwarf.conf.sample @@ -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] diff --git a/etc/reddwarf/reddwarf.conf.test b/etc/reddwarf/reddwarf.conf.test index 6124dab53c..d821593441 100644 --- a/etc/reddwarf/reddwarf.conf.test +++ b/etc/reddwarf/reddwarf.conf.test @@ -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] diff --git a/reddwarf/common/api.py b/reddwarf/common/api.py new file mode 100644 index 0000000000..1ba4e0f855 --- /dev/null +++ b/reddwarf/common/api.py @@ -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() diff --git a/reddwarf/flavor/service.py b/reddwarf/flavor/service.py index 1fb0e6d177..3f69ce4afe 100644 --- a/reddwarf/flavor/service.py +++ b/reddwarf/flavor/service.py @@ -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) diff --git a/reddwarf/flavor/views.py b/reddwarf/flavor/views.py index 5ed5a5064b..6a38b23f2a 100644 --- a/reddwarf/flavor/views.py +++ b/reddwarf/flavor/views.py @@ -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 diff --git a/reddwarf/instance/service.py b/reddwarf/instance/service.py index c32b2fbe1d..9ab1a7bc68 100644 --- a/reddwarf/instance/service.py +++ b/reddwarf/instance/service.py @@ -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()