Permalink
Browse files

Pass the RequestContext to the handlers

Make sure that one instance of a handler is used for each request
as we are passing in the context in the constructor.

Change-Id: I066090968db733aed93f7b8729e0fec8c83876d0
  • Loading branch information...
asalkeld committed Mar 7, 2014
1 parent 272f7b4 commit bc2607ad65b242d1a75289ab52002e0f0a889700
@@ -20,7 +20,8 @@
sample_data = {"name": "test_plan",
"description": "A test to create plan",
"project_id": "project_id",
- "user_id": "user_id"}
+ "user_id": "user_id",
+ "type": "plan"}
class TestPlanController(base.TestCase):
@@ -35,10 +36,9 @@ def _delete_all(self):
[self._delete_plan(pl['uuid']) for pl in data]
def _assert_output_expected(self, body_data, data):
- self.assertEqual(body_data['user_id'], data['user_id'])
- self.assertEqual(body_data['project_id'], data['project_id'])
self.assertEqual(body_data['description'], data['description'])
self.assertEqual(body_data['name'], data['name'])
+ self.assertEqual(body_data['type'], 'plan')
self.assertIsNotNone(body_data['uuid'])
def _delete_plan(self, uuid):
@@ -82,8 +82,7 @@ def test_plans_put(self):
uuid = self._create_plan()
updated_data = {"name": "test_plan updated",
"description": "A test to create plan updated",
- "project_id": "project_id updated",
- "user_id": "user_id updated"}
+ "type": "plan"}
updated_json = json.dumps(updated_data)
resp, body = self.client.put('v1/plans/%s' % uuid, updated_json)
self.assertEqual(resp.status, 200)
@@ -23,7 +23,8 @@
"project_id": "project_id",
"user_id": "user_id",
"service_type": "mysql",
- "read_only": True}
+ "read_only": True,
+ "type": "service"}
class TestServiceController(base.TestCase):
@@ -38,12 +39,11 @@ def _delete_all(self):
[self._delete_service(ser['uuid']) for ser in data]
def _assert_output_expected(self, body_data, data):
- self.assertEqual(body_data['user_id'], data['user_id'])
- self.assertEqual(body_data['project_id'], data['project_id'])
self.assertEqual(body_data['description'], data['description'])
self.assertEqual(body_data['name'], data['name'])
self.assertEqual(body_data['service_type'], data['service_type'])
self.assertEqual(body_data['read_only'], data['read_only'])
+ self.assertEqual(body_data['type'], 'service')
self.assertIsNotNone(body_data['uuid'])
def _delete_service(self, uuid):
@@ -29,38 +29,38 @@ class AssemblyController(rest.RestController):
def __init__(self, assembly_id):
super(AssemblyController, self).__init__()
self._id = assembly_id
- self._handler = assembly_handler.AssemblyHandler()
@exception.wrap_controller_exception
@wsme_pecan.wsexpose(assembly.Assembly)
def get(self):
"""Return this assembly."""
- return assembly.Assembly.from_db_model(self._handler.get(self._id),
+ handler = assembly_handler.AssemblyHandler(
+ pecan.request.security_context)
+ return assembly.Assembly.from_db_model(handler.get(self._id),
pecan.request.host_url)
@exception.wrap_controller_exception
@wsme_pecan.wsexpose(assembly.Assembly, body=assembly.Assembly)
def put(self, data):
"""Modify this assembly."""
-
- res = self._handler.update(self._id,
- data.as_dict(objects.registry.Assembly))
+ handler = assembly_handler.AssemblyHandler(
+ pecan.request.security_context)
+ res = handler.update(self._id,
+ data.as_dict(objects.registry.Assembly))
return assembly.Assembly.from_db_model(res, pecan.request.host_url)
@exception.wrap_controller_exception
@wsme_pecan.wsexpose(status_code=204)
def delete(self):
"""Delete this assembly."""
- return self._handler.delete(self._id)
+ handler = assembly_handler.AssemblyHandler(
+ pecan.request.security_context)
+ return handler.delete(self._id)
class AssembliesController(rest.RestController):
"""Manages operations on the assemblies collection."""
- def __init__(self):
- super(AssembliesController, self).__init__()
- self._handler = assembly_handler.AssemblyHandler()
-
@pecan.expose()
def _lookup(self, assembly_id, *remainder):
if remainder and not remainder[-1]:
@@ -72,7 +72,6 @@ def _lookup(self, assembly_id, *remainder):
status_code=201)
def post(self, data):
"""Create a new assembly."""
-
js_data = data.as_dict(objects.registry.Assembly)
if data.plan_uri is not wsme.Unset:
plan_uri = data.plan_uri
@@ -85,12 +84,16 @@ def post(self, data):
# download the plan and insert it into our db.
pass
+ handler = assembly_handler.AssemblyHandler(
+ pecan.request.security_context)
return assembly.Assembly.from_db_model(
- self._handler.create(js_data), pecan.request.host_url)
+ handler.create(js_data), pecan.request.host_url)
@exception.wrap_controller_exception
@wsme_pecan.wsexpose([assembly.Assembly])
def get_all(self):
"""Return all assemblies, based on the query provided."""
+ handler = assembly_handler.AssemblyHandler(
+ pecan.request.security_context)
return [assembly.Assembly.from_db_model(assm, pecan.request.host_url)
- for assm in self._handler.get_all()]
+ for assm in handler.get_all()]
@@ -26,38 +26,38 @@ class ComponentController(rest.RestController):
def __init__(self, component_id):
super(ComponentController, self).__init__()
self._id = component_id
- self._handler = component_handler.ComponentHandler()
@exception.wrap_controller_exception
@wsme_pecan.wsexpose(component.Component)
def get(self):
"""Return this component."""
- return component.Component.from_db_model(self._handler.get(self._id),
+ handler = component_handler.ComponentHandler(
+ pecan.request.security_context)
+ return component.Component.from_db_model(handler.get(self._id),
pecan.request.host_url)
@exception.wrap_controller_exception
@wsme_pecan.wsexpose(component.Component, body=component.Component)
def put(self, data):
"""Modify this component."""
- res = self._handler.update(self._id,
- data.as_dict(objects.registry.Plan))
+ handler = component_handler.ComponentHandler(
+ pecan.request.security_context)
+ res = handler.update(self._id,
+ data.as_dict(objects.registry.Plan))
return component.Component.from_db_model(res, pecan.request.host_url)
@exception.wrap_controller_exception
@wsme_pecan.wsexpose(None, status_code=204)
def delete(self):
"""Delete this component."""
- return self._handler.delete(self._id)
+ handler = component_handler.ComponentHandler(
+ pecan.request.security_context)
+ return handler.delete(self._id)
class ComponentsController(rest.RestController):
"""Manages operations on the components collection."""
- def __init__(self):
- super(ComponentsController, self).__init__()
- self._handler = component_handler.ComponentHandler()
- objects.load()
-
@pecan.expose()
def _lookup(self, component_id, *remainder):
if remainder and not remainder[-1]:
@@ -69,12 +69,16 @@ def _lookup(self, component_id, *remainder):
status_code=201)
def post(self, data):
"""Create a new component."""
+ handler = component_handler.ComponentHandler(
+ pecan.request.security_context)
return component.Component.from_db_model(
- self._handler.create(data.as_dict()), pecan.request.host_url)
+ handler.create(data.as_dict()), pecan.request.host_url)
@exception.wrap_controller_exception
@wsme_pecan.wsexpose([component.Component])
def get_all(self):
"""Return all components, based on the query provided."""
+ handler = component_handler.ComponentHandler(
+ pecan.request.security_context)
return [component.Component.from_db_model(ser, pecan.request.host_url)
- for ser in self._handler.get_all()]
+ for ser in handler.get_all()]
@@ -26,38 +26,39 @@ class ExtensionController(rest.RestController):
def __init__(self, extension_id):
self._id = extension_id
- self._handler = extension_handler.ExtensionHandler()
@exception.wrap_controller_exception
@wsme_pecan.wsexpose(extension.Extension, wtypes.text)
def get(self):
"""Return this extension."""
- return extension.Extension.from_db_model(self._handler.get(self._id),
+ handler = extension_handler.ExtensionHandler(
+ pecan.request.security_context)
+ return extension.Extension.from_db_model(handler.get(self._id),
pecan.request.host_url)
@exception.wrap_controller_exception
@wsme_pecan.wsexpose(extension.Extension, wtypes.text,
body=extension.Extension)
def put(self, data):
"""Modify this extension."""
- obj = self._handler.update(self._id,
- data.as_dict(objects.registry.Extension))
+ handler = extension_handler.ExtensionHandler(
+ pecan.request.security_context)
+ obj = handler.update(self._id,
+ data.as_dict(objects.registry.Extension))
return extension.Extension.from_db_model(obj, pecan.request.host_url)
@exception.wrap_controller_exception
@wsme_pecan.wsexpose(None, wtypes.text, status_code=204)
def delete(self):
"""Delete this extension."""
- self._handler.delete(self._id)
+ handler = extension_handler.ExtensionHandler(
+ pecan.request.security_context)
+ handler.delete(self._id)
class ExtensionsController(rest.RestController):
"""Manages operations on the extensions collection."""
- def __init__(self):
- super(ExtensionsController, self).__init__()
- self._handler = extension_handler.ExtensionHandler()
-
@pecan.expose()
def _lookup(self, extension_id, *remainder):
if remainder and not remainder[-1]:
@@ -70,11 +71,15 @@ def _lookup(self, extension_id, *remainder):
status_code=201)
def post(self, data):
"""Create a new extension."""
- obj = self._handler.create(data.as_dict(objects.registry.Extension))
+ handler = extension_handler.ExtensionHandler(
+ pecan.request.security_context)
+ obj = handler.create(data.as_dict(objects.registry.Extension))
return extension.Extension.from_db_model(obj, pecan.request.host_url)
@wsme_pecan.wsexpose([extension.Extension])
def get_all(self):
"""Return all extensions, based on the query provided."""
+ handler = extension_handler.ExtensionHandler(
+ pecan.request.security_context)
return [extension.Extension.from_db_model(obj, pecan.request.host_url)
- for obj in self._handler.get_all()]
+ for obj in handler.get_all()]
@@ -27,22 +27,19 @@ class LanguagePackController(rest.RestController):
def __init__(self, language_pack_id):
super(LanguagePackController, self).__init__()
self._id = language_pack_id
- self._handler = lp_handler.LanguagePackHandler()
@exception.wrap_controller_exception
@wsme_pecan.wsexpose(lp.LanguagePack)
def get(self):
"""Return a language_pack."""
+ handler = lp_handler.LanguagePackHandler(
+ pecan.request.security_context)
return lp.LanguagePack.from_db_model(
- self._handler.get(self._id), pecan.request.host_url)
+ handler.get(self._id), pecan.request.host_url)
class LanguagePacksController(rest.RestController):
- """Manages operations on the language_packs collection."""
-
- def __init__(self):
- super(LanguagePacksController, self).__init__()
- self._handler = lp_handler.LanguagePackHandler()
+ """Manages operations on the language packs collection."""
@pecan.expose()
def _lookup(self, language_pack_id, *remainder):
@@ -54,6 +51,8 @@ def _lookup(self, language_pack_id, *remainder):
@wsme_pecan.wsexpose([lp.LanguagePack])
def get_all(self):
"""Return all language_packs, based on the query provided."""
+ handler = lp_handler.LanguagePackHandler(
+ pecan.request.security_context)
return [lp.LanguagePack.from_db_model(langpack,
pecan.request.host_url)
- for langpack in self._handler.get_all()]
+ for langpack in handler.get_all()]
@@ -27,13 +27,15 @@ class OperationController(rest.RestController):
"""Manages operations on a single operation."""
def __init__(self, operation_id):
+ super(OperationController, self).__init__()
self._id = operation_id
@exception.wrap_controller_exception
@wsme_pecan.wsexpose(operation.Operation, wtypes.text)
def get(self):
"""Return this operation."""
- handler = operation_handler.OperationHandler()
+ handler = operation_handler.OperationHandler(
+ pecan.request.security_context)
return handler.get(self._id)
@wsme_pecan.wsexpose(operation.Operation, wtypes.text,
@@ -73,5 +75,6 @@ def post(self, data):
@wsme_pecan.wsexpose([operation.Operation])
def get_all(self):
"""Return all operations, based on the query provided."""
- handler = operation_handler.OperationHandler()
+ handler = operation_handler.OperationHandler(
+ pecan.request.security_context)
return handler.get_all()
@@ -28,37 +28,38 @@ class PlanController(rest.RestController):
def __init__(self, plan_id):
super(PlanController, self).__init__()
self._id = plan_id
- self._handler = plan_handler.PlanHandler()
@exception.wrap_controller_exception
@wsme_pecan.wsexpose(plan.Plan)
def get(self):
"""Return this plan."""
- return plan.Plan.from_db_model(self._handler.get(self._id),
+ handler = plan_handler.PlanHandler(
+ pecan.request.security_context)
+ return plan.Plan.from_db_model(handler.get(self._id),
pecan.request.host_url)
@exception.wrap_controller_exception
@wsme_pecan.wsexpose(plan.Plan, body=plan.Plan)
def put(self, data):
"""Modify this plan."""
- res = self._handler.update(self._id,
- data.as_dict(objects.registry.Plan))
+ handler = plan_handler.PlanHandler(
+ pecan.request.security_context)
+ res = handler.update(self._id,
+ data.as_dict(objects.registry.Plan))
return plan.Plan.from_db_model(res, pecan.request.host_url)
@exception.wrap_controller_exception
@wsme_pecan.wsexpose(status_code=204)
def delete(self):
"""Delete this plan."""
- return self._handler.delete(self._id)
+ handler = plan_handler.PlanHandler(
+ pecan.request.security_context)
+ return handler.delete(self._id)
class PlansController(rest.RestController):
"""Manages operations on the plans collection."""
- def __init__(self):
- super(PlansController, self).__init__()
- self._handler = plan_handler.PlanHandler()
-
@pecan.expose()
def _lookup(self, plan_id, *remainder):
if remainder and not remainder[-1]:
@@ -69,13 +70,17 @@ def _lookup(self, plan_id, *remainder):
@wsme_pecan.wsexpose(plan.Plan, body=plan.Plan, status_code=201)
def post(self, data):
"""Create a new plan."""
+ handler = plan_handler.PlanHandler(
+ pecan.request.security_context)
return plan.Plan.from_db_model(
- self._handler.create(data.as_dict(objects.registry.Plan)),
+ handler.create(data.as_dict(objects.registry.Plan)),
pecan.request.host_url)
@exception.wrap_controller_exception
@wsme_pecan.wsexpose([plan.Plan])
def get_all(self):
"""Return all plans, based on the query provided."""
+ handler = plan_handler.PlanHandler(
+ pecan.request.security_context)
return [plan.Plan.from_db_model(obj, pecan.request.host_url)
- for obj in self._handler.get_all()]
+ for obj in handler.get_all()]
Oops, something went wrong.

0 comments on commit bc2607a

Please sign in to comment.