summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Gundlach <michael.gundlach@rackspace.com>2010-08-16 13:22:41 -0400
committerMichael Gundlach <michael.gundlach@rackspace.com>2010-08-16 13:22:41 -0400
commitf78a8936b1a401f07fc0a09d4bd150d2793e436e (patch)
tree337d18ca9938db5a08ee9006d8650fe64df7bced
parent5c4a806c852a1c7180bc1c7e2ea8f065198e36d2 (diff)
downloadnova-f78a8936b1a401f07fc0a09d4bd150d2793e436e.tar.gz
nova-f78a8936b1a401f07fc0a09d4bd150d2793e436e.tar.xz
nova-f78a8936b1a401f07fc0a09d4bd150d2793e436e.zip
All controller actions receive a 'req' parameter containing the webob Request.
-rw-r--r--nova/endpoint/__init__.py10
-rw-r--r--nova/endpoint/aws/__init__.py6
-rw-r--r--nova/endpoint/rackspace/__init__.py23
-rw-r--r--nova/endpoint/rackspace/controllers/servers.py2
-rw-r--r--nova/wsgi.py48
5 files changed, 41 insertions, 48 deletions
diff --git a/nova/endpoint/__init__.py b/nova/endpoint/__init__.py
index 065f45848..9aae933af 100644
--- a/nova/endpoint/__init__.py
+++ b/nova/endpoint/__init__.py
@@ -36,16 +36,16 @@ import routes
from nova.endpoint import rackspace
from nova.endpoint import aws
-class ApiVersionRouter(wsgi.Router):
+class APIVersionRouter(wsgi.Router):
"""Routes top-level requests to the appropriate API."""
def __init__(self):
mapper = routes.Mapper()
- mapper.connect(None, "/v1.0/{path_info:.*}", controller="rs")
- mapper.connect(None, "/ec2/{path_info:.*}", controller="ec2")
+ rsapi = rackspace.API()
+ mapper.connect(None, "/v1.0/{path_info:.*}", controller=rsapi)
- targets = {"rs": rackspace.Api(), "ec2": aws.Api()}
+ mapper.connect(None, "/ec2/{path_info:.*}", controller=aws.API())
- super(ApiVersionRouter, self).__init__(mapper, targets)
+ super(APIVersionRouter, self).__init__(mapper)
diff --git a/nova/endpoint/aws/__init__.py b/nova/endpoint/aws/__init__.py
index 4507cae62..55cbb8fd3 100644
--- a/nova/endpoint/aws/__init__.py
+++ b/nova/endpoint/aws/__init__.py
@@ -10,11 +10,9 @@ class API(wsgi.Router):
def __init__(self):
mapper = routes.Mapper()
- mapper.connect(None, "{all:.*}", controller="dummy")
+ mapper.connect(None, "{all:.*}", controller=self.dummy)
- targets = {"dummy": self.dummy }
-
- super(API, self).__init__(mapper, targets)
+ super(API, self).__init__(mapper)
@webob.dec.wsgify
def dummy(self, req):
diff --git a/nova/endpoint/rackspace/__init__.py b/nova/endpoint/rackspace/__init__.py
index 162b35caa..78b9c9429 100644
--- a/nova/endpoint/rackspace/__init__.py
+++ b/nova/endpoint/rackspace/__init__.py
@@ -75,16 +75,13 @@ class APIRouter(wsgi.Router):
def __init__(self):
mapper = routes.Mapper()
- mapper.resource("server", "servers")
- mapper.resource("image", "images")
- mapper.resource("flavor", "flavors")
- mapper.resource("sharedipgroup", "sharedipgroups")
-
- targets = {
- 'servers': controllers.ServersController(),
- 'images': controllers.ImagesController(),
- 'flavors': controllers.FlavorsController(),
- 'sharedipgroups': controllers.SharedIpGroupsController()
- }
-
- super(APIRouter, self).__init__(mapper, targets)
+ mapper.resource("server", "servers",
+ controller=controllers.ServersController())
+ mapper.resource("image", "images",
+ controller=controllers.ImagesController())
+ mapper.resource("flavor", "flavors",
+ controller=controllers.FlavorsController())
+ mapper.resource("sharedipgroup", "sharedipgroups",
+ controller=controllers.SharedIpGroupsController())
+
+ super(APIRouter, self).__init__(mapper)
diff --git a/nova/endpoint/rackspace/controllers/servers.py b/nova/endpoint/rackspace/controllers/servers.py
index db02e058d..2f8e662d6 100644
--- a/nova/endpoint/rackspace/controllers/servers.py
+++ b/nova/endpoint/rackspace/controllers/servers.py
@@ -5,7 +5,7 @@ from nova.endpoint.rackspace.controllers.base import BaseController
class ServersController(BaseController):
entity_name = 'servers'
- def index(cls):
+ def index(self, **kwargs):
return [instance_details(inst) for inst in compute.InstanceDirectory().all]
def show(self, **kwargs):
diff --git a/nova/wsgi.py b/nova/wsgi.py
index 52e155101..a0a175dc7 100644
--- a/nova/wsgi.py
+++ b/nova/wsgi.py
@@ -140,34 +140,31 @@ class Router(object):
WSGI middleware that maps incoming requests to WSGI apps.
"""
- def __init__(self, mapper, targets):
+ def __init__(self, mapper):
"""
Create a router for the given routes.Mapper.
- Each route in `mapper` must specify a 'controller' string, which is
- a key into the 'targets' dictionary whose value is a WSGI app to
- run. If routing to a wsgi.Controller, you'll want to specify
- 'action' as well so the controller knows what method to call on
- itself.
+ Each route in `mapper` must specify a 'controller', which is a
+ WSGI app to call. You'll probably want to specify an 'action' as
+ well and have your controller be a wsgi.Controller, who will route
+ the request to the action method.
Examples:
mapper = routes.Mapper()
- targets = { "servers": ServerController(), "blog": BlogWsgiApp() }
+ sc = ServerController()
# Explicit mapping of one route to a controller+action
- mapper.connect(None, "/svrlist", controller="servers", action="list")
+ mapper.connect(None, "/svrlist", controller=sc, action="list")
- # Controller string is implicitly equal to 2nd param here, and
- # actions are all implicitly defined
- mapper.resource("server", "servers")
+ # Actions are all implicitly defined
+ mapper.resource("server", "servers", controller=sc)
# Pointing to an arbitrary WSGI app. You can specify the
# {path_info:.*} parameter so the target app can be handed just that
# section of the URL.
- mapper.connect(None, "/v1.0/{path_info:.*}", controller="blog")
+ mapper.connect(None, "/v1.0/{path_info:.*}", controller=BlogApp())
"""
self.map = mapper
- self.targets = targets
self._router = routes.middleware.RoutesMiddleware(self._dispatch,
self.map)
@@ -186,31 +183,32 @@ class Router(object):
and putting the information into req.environ. Either returns 404
or the routed WSGI app's response.
"""
- if req.environ['routes.route'] is None:
- return webob.exc.HTTPNotFound()
match = req.environ['wsgiorg.routing_args'][1]
- app_name = match['controller']
-
- app = self.targets[app_name]
+ if not match:
+ return webob.exc.HTTPNotFound()
+ app = match['controller']
return app
class Controller(object):
"""
WSGI app that reads routing information supplied by RoutesMiddleware
- and calls the requested action method on itself.
+ and calls the requested action method upon itself. All action methods
+ must, in addition to their normal parameters, accept a 'req' argument
+ which is the incoming webob.Request.
"""
@webob.dec.wsgify
def __call__(self, req):
"""
- Call the method on self specified in req.environ by RoutesMiddleware.
+ Call the method specified in req.environ by RoutesMiddleware.
"""
- routes_dict = req.environ['wsgiorg.routing_args'][1]
- action = routes_dict['action']
+ arg_dict = req.environ['wsgiorg.routing_args'][1]
+ action = arg_dict['action']
method = getattr(self, action)
- del routes_dict['controller']
- del routes_dict['action']
- return method(**routes_dict)
+ del arg_dict['controller']
+ del arg_dict['action']
+ arg_dict['req'] = req
+ return method(**arg_dict)
class Serializer(object):