Pass and call the application from the controllers

Our idea is to convert an incoming OCCI request into an OpenStack
request, so that we can pass it downstream so as to be processed by
nova. However, we should call the final application in each of the
controlers instead of just modifying the request, in order to be able to
process the response from nova and "re-occi-fy" it.  In order to do so,
the controllers should receive the application.

 - This change modifies the BaseController so that in receives the app.
   If the controller returns a response (because it calls the
   application, gets the response and process it or by any other
   reason), that response will be used. If it returns None, the
   application will be called.
 - Also modifies the compute controller so that it implements the
   create_resources() function.
This commit is contained in:
Alvaro Lopez Garcia
2015-03-05 10:10:07 +01:00
parent 4d4e7eada2
commit cab5f1374a
5 changed files with 39 additions and 20 deletions

View File

@@ -16,4 +16,5 @@
class BaseController(object):
pass
def __init__(self, app):
self.app = app

View File

@@ -15,9 +15,15 @@
# under the License.
import ooi.api
import ooi.wsgi
class ComputeController(ooi.api.BaseController):
def index(self, req):
tenant_id = req.environ["keystone.token_auth"].user.project_id
req.path_info = "/%s/servers" % tenant_id
return req.get_response(self.app)
def create_resource(app):
return ooi.wsgi.Resource(ComputeController(app))

View File

@@ -14,16 +14,17 @@
# License for the specific language governing permissions and limitations
# under the License.
import ooi.api
from ooi.occi.infrastructure import compute
import ooi.wsgi
class Controller(object):
def index(self, *args, **kwargs):
class Controller(ooi.api.BaseController):
def index(self, req):
l = []
l.extend(compute.ComputeResource.actions)
return l
def create_resource():
return ooi.wsgi.Resource(Controller())
def create_resource(app):
return ooi.wsgi.Resource(Controller(app))

View File

@@ -14,6 +14,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import json
import mock
import webob
import webob.dec
@@ -23,27 +25,36 @@ from ooi.tests import base
from ooi import wsgi
@webob.dec.wsgify
def fake_app(req):
resp = webob.Response("Hi")
return resp
def fake_app(resp):
@webob.dec.wsgify
def app(req):
return resp
return app
def create_fake_json_resp(data):
r = webob.Response()
r.headers["Content-Type"] = "application/json"
r.charset = "utf8"
r.body = json.dumps(data).encode("utf8")
return r
class TestComputeMiddleware(base.TestCase):
def setUp(self):
super(TestComputeMiddleware, self).setUp()
self.app = wsgi.OCCIMiddleware(fake_app)
def test_list_vms_all(self):
req = webob.Request.blank("/compute",
method="GET")
d = {"servers": []}
fake_resp = create_fake_json_resp(d)
app = wsgi.OCCIMiddleware(fake_app(fake_resp))
req = webob.Request.blank("/compute", method="GET")
m = mock.MagicMock()
m.user.project_id = "3dd7b3f6-c19d-11e4-8dfc-aa07a5b093db"
req.environ["keystone.token_auth"] = m
req.get_response(self.app)
resp = req.get_response(app)
self.assertEqual("/3dd7b3f6-c19d-11e4-8dfc-aa07a5b093db/servers",
req.environ["PATH_INFO"])
self.assertEqual(d, resp.json_body)

View File

@@ -110,13 +110,13 @@ class OCCIMiddleware(object):
"""
self.mapper.redirect("", "/")
self.resources["query"] = query.create_resource()
self.resources["query"] = query.create_resource(self.application)
self.mapper.connect("query", "/-/",
controller=self.resources["query"],
action="index")
self.resources["compute"] = Resource(
ooi.api.compute.ComputeController())
self.resources["compute"] = ooi.api.compute.create_resource(
self.application)
self.mapper.resource("server", "compute",
controller=self.resources["compute"])