From a7a454403310488122a41b5e75f27d29b9b03159 Mon Sep 17 00:00:00 2001 From: "OTSUKA, Yuanying" Date: Thu, 25 Dec 2014 15:25:39 +0900 Subject: [PATCH] Add hooks to obtain conductor api To obtain conductor api instance easily, this add rpcapi attribute to pecan.request. So now, we can access to conductor api from "pecan.request.rpcapi". Related-Bug: #1406539 Change-Id: I6edbf031d91e65d70637629c3b57d45322eee9fd --- magnum/api/config.py | 3 ++- magnum/api/controllers/v1/bay.py | 14 ++------------ magnum/api/controllers/v1/baymodel.py | 14 ++------------ magnum/api/controllers/v1/container.py | 14 ++------------ magnum/api/controllers/v1/node.py | 14 ++------------ magnum/api/controllers/v1/pod.py | 14 ++------------ magnum/api/controllers/v1/service.py | 14 ++------------ magnum/api/hooks.py | 10 +++++++++- magnum/tests/config.py | 1 + 9 files changed, 24 insertions(+), 74 deletions(-) diff --git a/magnum/api/config.py b/magnum/api/config.py index a01904b96b..9f28ec00cc 100644 --- a/magnum/api/config.py +++ b/magnum/api/config.py @@ -22,7 +22,8 @@ app = { 'modules': ['magnum.api'], 'debug': False, 'hooks': [ - hooks.ContextHook() + hooks.ContextHook(), + hooks.RPCHook(), ], 'acl_public_routes': [ '/' diff --git a/magnum/api/controllers/v1/bay.py b/magnum/api/controllers/v1/bay.py index 6e5081da88..5e2b8286b8 100644 --- a/magnum/api/controllers/v1/bay.py +++ b/magnum/api/controllers/v1/bay.py @@ -307,18 +307,8 @@ class BaysController(rest.RestController): if rpc_bay[field] != patch_val: rpc_bay[field] = patch_val - if hasattr(pecan.request, 'rpcapi'): - rpc_bay = objects.Bay.get_by_id(pecan.request.context, - rpc_bay.bay_id) - topic = pecan.request.rpcapi.get_topic_for(rpc_bay) - - new_bay = pecan.request.rpcapi.update_bay( - pecan.request.context, rpc_bay, topic) - - return Bay.convert_with_links(new_bay) - else: - rpc_bay.save() - return Bay.convert_with_links(rpc_bay) + rpc_bay.save() + return Bay.convert_with_links(rpc_bay) @wsme_pecan.wsexpose(None, types.uuid, status_code=204) def delete(self, bay_uuid): diff --git a/magnum/api/controllers/v1/baymodel.py b/magnum/api/controllers/v1/baymodel.py index 29e04d2040..09332d9764 100644 --- a/magnum/api/controllers/v1/baymodel.py +++ b/magnum/api/controllers/v1/baymodel.py @@ -309,18 +309,8 @@ class BayModelsController(rest.RestController): if rpc_baymodel[field] != patch_val: rpc_baymodel[field] = patch_val - if hasattr(pecan.request, 'rpcapi'): - rpc_baymodel = objects.BayModel.get_by_id(pecan.request.context, - rpc_baymodel.baymodel_id) - topic = pecan.request.rpcapi.get_topic_for(rpc_baymodel) - - new_baymodel = pecan.request.rpcapi.update_baymodel( - pecan.request.context, rpc_baymodel, topic) - - return BayModel.convert_with_links(new_baymodel) - else: - rpc_baymodel.save() - return BayModel.convert_with_links(rpc_baymodel) + rpc_baymodel.save() + return BayModel.convert_with_links(rpc_baymodel) @wsme_pecan.wsexpose(None, types.uuid, status_code=204) def delete(self, baymodel_uuid): diff --git a/magnum/api/controllers/v1/container.py b/magnum/api/controllers/v1/container.py index 7a65ea5cb9..47c50cafe3 100644 --- a/magnum/api/controllers/v1/container.py +++ b/magnum/api/controllers/v1/container.py @@ -379,18 +379,8 @@ class ContainersController(rest.RestController): if rpc_container[field] != patch_val: rpc_container[field] = patch_val - if hasattr(pecan.request, 'rpcapi'): - rpc_container = objects.Container.get_by_id(pecan.request.context, - rpc_container.container_id) - topic = pecan.request.rpcapi.get_topic_for(rpc_container) - - new_container = pecan.request.rpcapi.update_container( - pecan.request.context, rpc_container, topic) - - return Container.convert_with_links(new_container) - else: - rpc_container.save() - return Container.convert_with_links(rpc_container) + rpc_container.save() + return Container.convert_with_links(rpc_container) @wsme_pecan.wsexpose(None, types.uuid, status_code=204) def delete(self, container_uuid): diff --git a/magnum/api/controllers/v1/node.py b/magnum/api/controllers/v1/node.py index 594623b817..7914e7fcde 100644 --- a/magnum/api/controllers/v1/node.py +++ b/magnum/api/controllers/v1/node.py @@ -298,18 +298,8 @@ class NodesController(rest.RestController): if rpc_node[field] != patch_val: rpc_node[field] = patch_val - if hasattr(pecan.request, 'rpcapi'): - rpc_node = objects.Node.get_by_id(pecan.request.context, - rpc_node.node_id) - topic = pecan.request.rpcapi.get_topic_for(rpc_node) - - new_node = pecan.request.rpcapi.update_node( - pecan.request.context, rpc_node, topic) - - return Node.convert_with_links(new_node) - else: - rpc_node.save() - return Node.convert_with_links(rpc_node) + rpc_node.save() + return Node.convert_with_links(rpc_node) @wsme_pecan.wsexpose(None, types.uuid, status_code=204) def delete(self, node_uuid): diff --git a/magnum/api/controllers/v1/pod.py b/magnum/api/controllers/v1/pod.py index ed2290fb84..aa9426575a 100644 --- a/magnum/api/controllers/v1/pod.py +++ b/magnum/api/controllers/v1/pod.py @@ -304,18 +304,8 @@ class PodsController(rest.RestController): if rpc_pod[field] != patch_val: rpc_pod[field] = patch_val - if hasattr(pecan.request, 'rpcapi'): - rpc_pod = objects.Pod.get_by_id(pecan.request.context, - rpc_pod.pod_id) - topic = pecan.request.rpcapi.get_topic_for(rpc_pod) - - new_pod = pecan.request.rpcapi.update_pod( - pecan.request.context, rpc_pod, topic) - - return Pod.convert_with_links(new_pod) - else: - rpc_pod.save() - return Pod.convert_with_links(rpc_pod) + rpc_pod.save() + return Pod.convert_with_links(rpc_pod) @wsme_pecan.wsexpose(None, types.uuid, status_code=204) def delete(self, pod_uuid): diff --git a/magnum/api/controllers/v1/service.py b/magnum/api/controllers/v1/service.py index 3385bdfe86..da6b734eaa 100644 --- a/magnum/api/controllers/v1/service.py +++ b/magnum/api/controllers/v1/service.py @@ -287,18 +287,8 @@ class ServicesController(rest.RestController): if rpc_service[field] != patch_val: rpc_service[field] = patch_val - if hasattr(pecan.request, 'rpcapi'): - rpc_service = objects.Service.get_by_id(pecan.request.context, - rpc_service.service_id) - topic = pecan.request.rpcapi.get_topic_for(rpc_service) - - new_service = pecan.request.rpcapi.update_service( - pecan.request.context, rpc_service, topic) - - return Service.convert_with_links(new_service) - else: - rpc_service.save() - return Service.convert_with_links(rpc_service) + rpc_service.save() + return Service.convert_with_links(rpc_service) @wsme_pecan.wsexpose(None, types.uuid, status_code=204) def delete(self, service_uuid): diff --git a/magnum/api/hooks.py b/magnum/api/hooks.py index 76d60fcae3..fde94368a9 100644 --- a/magnum/api/hooks.py +++ b/magnum/api/hooks.py @@ -22,6 +22,7 @@ from oslo.utils import importutils from pecan import hooks from magnum.common import context +from magnum.conductor import api as conductor_api class ContextHook(hooks.PecanHook): @@ -64,4 +65,11 @@ class ContextHook(hooks.PecanHook): user=user_id, tenant=tenant, domain_id=domain_id, - domain_name=domain_name) \ No newline at end of file + domain_name=domain_name) + + +class RPCHook(hooks.PecanHook): + """Attach the rpcapi object to the request so controllers can get to it.""" + + def before(self, state): + state.request.rpcapi = conductor_api.API(context=state.request.context) \ No newline at end of file diff --git a/magnum/tests/config.py b/magnum/tests/config.py index 0d26b21174..7b4c63f04c 100644 --- a/magnum/tests/config.py +++ b/magnum/tests/config.py @@ -25,6 +25,7 @@ app = { 'debug': True, 'hooks': [ hooks.ContextHook(), + hooks.RPCHook() ], 'acl_public_routes': [ '/'