From 635581e9c7aee75ab64dd51c5d4eb5c759c1d2d4 Mon Sep 17 00:00:00 2001 From: Russell Bryant Date: Tue, 18 Nov 2014 14:46:06 +0000 Subject: [PATCH] Drop RpcProxy usage from FWaaS code This patch removes the usage of the RpcProxy compatibility class from the FWaaS code. The equivalent direct usage of oslo.messaging APIs are now used instead. Part of blueprint drop-rpc-compat. Change-Id: I1bab4dcd376d4e9684fee1edc97fd29a61ba38de --- .../firewall/agents/firewall_agent_api.py | 22 ++++--- neutron/services/firewall/fwaas_plugin.py | 31 ++++------ .../agents/test_firewall_agent_api.py | 60 +++++++------------ .../services/firewall/test_fwaas_plugin.py | 31 +++++----- 4 files changed, 57 insertions(+), 87 deletions(-) diff --git a/neutron/services/firewall/agents/firewall_agent_api.py b/neutron/services/firewall/agents/firewall_agent_api.py index 71caca8c6..85be6e6a4 100644 --- a/neutron/services/firewall/agents/firewall_agent_api.py +++ b/neutron/services/firewall/agents/firewall_agent_api.py @@ -14,6 +14,7 @@ # under the License. from oslo.config import cfg +from oslo import messaging from neutron.common import rpc as n_rpc from neutron.openstack.common import log as logging @@ -33,28 +34,25 @@ FWaaSOpts = [ cfg.CONF.register_opts(FWaaSOpts, 'fwaas') -class FWaaSPluginApiMixin(n_rpc.RpcProxy): +class FWaaSPluginApiMixin(object): """Agent side of the FWaaS agent to FWaaS Plugin RPC API.""" - RPC_API_VERSION = '1.0' - def __init__(self, topic, host): - super(FWaaSPluginApiMixin, - self).__init__(topic=topic, - default_version=self.RPC_API_VERSION) self.host = host + target = messaging.Target(topic=topic, version='1.0') + self.client = n_rpc.get_client(target) def set_firewall_status(self, context, firewall_id, status): """Make a RPC to set the status of a firewall.""" - return self.call(context, - self.make_msg('set_firewall_status', host=self.host, - firewall_id=firewall_id, status=status)) + cctxt = self.client.prepare() + return cctxt.call(context, 'set_firewall_status', host=self.host, + firewall_id=firewall_id, status=status) def firewall_deleted(self, context, firewall_id): """Make a RPC to indicate that the firewall resources are deleted.""" - return self.call(context, - self.make_msg('firewall_deleted', host=self.host, - firewall_id=firewall_id)) + cctxt = self.client.prepare() + return cctxt.call(context, 'firewall_deleted', host=self.host, + firewall_id=firewall_id) class FWaaSAgentRpcCallbackMixin(object): diff --git a/neutron/services/firewall/fwaas_plugin.py b/neutron/services/firewall/fwaas_plugin.py index 15179f4df..e67d75aa5 100644 --- a/neutron/services/firewall/fwaas_plugin.py +++ b/neutron/services/firewall/fwaas_plugin.py @@ -97,35 +97,28 @@ class FirewallCallbacks(object): return fw_tenant_list -class FirewallAgentApi(n_rpc.RpcProxy): +class FirewallAgentApi(object): """Plugin side of plugin to agent RPC API.""" - API_VERSION = '1.0' - def __init__(self, topic, host): - super(FirewallAgentApi, self).__init__(topic, self.API_VERSION) self.host = host + target = messaging.Target(topic=topic, version='1.0') + self.client = n_rpc.get_client(target) def create_firewall(self, context, firewall): - return self.fanout_cast( - context, - self.make_msg('create_firewall', firewall=firewall, - host=self.host) - ) + cctxt = self.client.prepare(fanout=True) + cctxt.cast(context, 'create_firewall', firewall=firewall, + host=self.host) def update_firewall(self, context, firewall): - return self.fanout_cast( - context, - self.make_msg('update_firewall', firewall=firewall, - host=self.host) - ) + cctxt = self.client.prepare(fanout=True) + cctxt.cast(context, 'update_firewall', firewall=firewall, + host=self.host) def delete_firewall(self, context, firewall): - return self.fanout_cast( - context, - self.make_msg('delete_firewall', firewall=firewall, - host=self.host) - ) + cctxt = self.client.prepare(fanout=True) + cctxt.cast(context, 'delete_firewall', firewall=firewall, + host=self.host) class FirewallCountExceeded(n_exception.Conflict): diff --git a/neutron/tests/unit/services/firewall/agents/test_firewall_agent_api.py b/neutron/tests/unit/services/firewall/agents/test_firewall_agent_api.py index e2891d328..63266d9b0 100644 --- a/neutron/tests/unit/services/firewall/agents/test_firewall_agent_api.py +++ b/neutron/tests/unit/services/firewall/agents/test_firewall_agent_api.py @@ -52,46 +52,26 @@ class TestFWaaSAgentApi(base.BaseTestCase): def test_init(self): self.assertEqual(self.api.host, 'host') - def test_set_firewall_status(self): + def _test_firewall_method(self, method_name, **kwargs): with contextlib.nested( - mock.patch.object(self.api, 'make_msg'), - mock.patch.object(self.api, 'call') - ) as (mock_make_msg, mock_call): - - self.assertEqual( - self.api.set_firewall_status( - mock.sentinel.context, - 'firewall_id', - 'status'), - mock_call.return_value) - - mock_make_msg.assert_called_once_with( - 'set_firewall_status', - host='host', - firewall_id='firewall_id', - status='status') - - mock_call.assert_called_once_with( - mock.sentinel.context, - mock_make_msg.return_value) + mock.patch.object(self.api.client, 'call'), + mock.patch.object(self.api.client, 'prepare'), + ) as ( + rpc_mock, prepare_mock + ): + prepare_mock.return_value = self.api.client + getattr(self.api, method_name)(mock.sentinel.context, 'test', + **kwargs) + + prepare_args = {} + prepare_mock.assert_called_once_with(**prepare_args) + + rpc_mock.assert_called_once_with(mock.sentinel.context, method_name, + firewall_id='test', host='host', + **kwargs) + + def test_set_firewall_status(self): + self._test_firewall_method('set_firewall_status', status='fake_status') def test_firewall_deleted(self): - with contextlib.nested( - mock.patch.object(self.api, 'make_msg'), - mock.patch.object(self.api, 'call') - ) as (mock_make_msg, mock_call): - - self.assertEqual( - self.api.firewall_deleted( - mock.sentinel.context, - 'firewall_id'), - mock_call.return_value) - - mock_make_msg.assert_called_once_with( - 'firewall_deleted', - host='host', - firewall_id='firewall_id') - - mock_call.assert_called_once_with( - mock.sentinel.context, - mock_make_msg.return_value) + self._test_firewall_method('firewall_deleted') diff --git a/neutron/tests/unit/services/firewall/test_fwaas_plugin.py b/neutron/tests/unit/services/firewall/test_fwaas_plugin.py index a9f8e6729..dd162f044 100644 --- a/neutron/tests/unit/services/firewall/test_fwaas_plugin.py +++ b/neutron/tests/unit/services/firewall/test_fwaas_plugin.py @@ -171,27 +171,26 @@ class TestFirewallAgentApi(base.BaseTestCase): super(TestFirewallAgentApi, self).setUp() self.api = fwaas_plugin.FirewallAgentApi('topic', 'host') - self.mock_fanoutcast = mock.patch.object(self.api, - 'fanout_cast').start() - self.mock_msg = mock.patch.object(self.api, 'make_msg').start() def test_init(self): - self.assertEqual(self.api.topic, 'topic') + self.assertEqual(self.api.client.target.topic, 'topic') self.assertEqual(self.api.host, 'host') def _call_test_helper(self, method_name): - rv = getattr(self.api, method_name)(mock.sentinel.context, 'test') - self.assertEqual(rv, self.mock_fanoutcast.return_value) - self.mock_fanoutcast.assert_called_once_with( - mock.sentinel.context, - self.mock_msg.return_value - ) - - self.mock_msg.assert_called_once_with( - method_name, - firewall='test', - host='host' - ) + with contextlib.nested( + mock.patch.object(self.api.client, 'cast'), + mock.patch.object(self.api.client, 'prepare'), + ) as ( + rpc_mock, prepare_mock + ): + prepare_mock.return_value = self.api.client + getattr(self.api, method_name)(mock.sentinel.context, 'test') + + prepare_args = {'fanout': True} + prepare_mock.assert_called_once_with(**prepare_args) + + rpc_mock.assert_called_once_with(mock.sentinel.context, method_name, + firewall='test', host='host') def test_create_firewall(self): self._call_test_helper('create_firewall') -- 2.45.2