use payloads for AGENT BEFORE_DELETE callbacks

This patch switches BEFORE_DELETE callback events for AGENT resources
over to the payload style args use a DBEventPayload object. In addition,
the _delete_mac_associated_with_agent is now refactored into it's own
method to allow the context parameter to be parsed by
retry_if_session_inactive.

NeutronLibImpact

Change-Id: I0af951f4346f9290c2eba6aad17f772fbf27bb2f
This commit is contained in:
Boden R 2018-08-23 11:59:43 -06:00 committed by Slawek Kaplonski
parent 00de8f9a9e
commit 6a24efcb70
3 changed files with 18 additions and 9 deletions

View File

@ -266,8 +266,9 @@ class AgentDbMixin(ext_agent.AgentPluginBase, AgentAvailabilityZoneMixin):
@db_api.retry_if_session_inactive()
def delete_agent(self, context, id):
agent = self._get_agent(context, id)
registry.notify(resources.AGENT, events.BEFORE_DELETE, self,
context=context, agent=agent)
registry.publish(resources.AGENT, events.BEFORE_DELETE, self,
payload=events.DBEventPayload(
context, states=(agent,), resource_id=id))
agent.delete()
@db_api.retry_if_session_inactive()

View File

@ -59,10 +59,8 @@ class DVRDbMixin(ext_dvr.DVRMacAddressPluginBase):
return self._plugin
@staticmethod
@registry.receives(resources.AGENT, [events.BEFORE_DELETE])
@db_api.retry_if_session_inactive()
def _delete_mac_associated_with_agent(resource, event, trigger, context,
agent, **kwargs):
def _db_delete_mac_associated_with_agent(context, agent):
host = agent['host']
plugin = directory.get_plugin()
if [a for a in plugin.get_agents(context, filters={'host': [host]})
@ -78,6 +76,14 @@ class DVRDbMixin(ext_dvr.DVRMacAddressPluginBase):
dvr_macs = plugin.get_dvr_mac_address_list(context)
plugin.notifier.dvr_mac_address_update(context, dvr_macs)
@staticmethod
@registry.receives(resources.AGENT, [events.BEFORE_DELETE])
def _delete_mac_associated_with_agent(resource, event,
trigger, payload=None):
DVRDbMixin._db_delete_mac_associated_with_agent(
payload.context, payload.latest_state)
@db_api.context_manager.reader
def _get_dvr_mac_address_by_host(self, context, host):
dvr_obj = router.DVRMacAddress.get_object(context, host=host)

View File

@ -94,8 +94,9 @@ class DvrDbMixinTestCase(test_plugin.Ml2PluginV2TestCase):
agent2 = {'host': 'host_1', 'id': 'a2'}
with mock.patch.object(plugin, 'get_agents', return_value=[agent2]):
with mock.patch.object(plugin, 'notifier') as notifier:
registry.notify(resources.AGENT, events.BEFORE_DELETE, self,
context=self.ctx, agent=agent1)
registry.publish(resources.AGENT, events.BEFORE_DELETE, self,
payload=events.DBEventPayload(
self.ctx, states=(agent1,)))
mac_list = self.mixin.get_dvr_mac_address_list(self.ctx)
for mac in mac_list:
self.assertIsInstance(mac, dict)
@ -110,8 +111,9 @@ class DvrDbMixinTestCase(test_plugin.Ml2PluginV2TestCase):
self._create_dvr_mac_entry('host_2', mac_2)
agent = {'host': 'host_1', 'id': 'a1'}
with mock.patch.object(plugin, 'notifier') as notifier:
registry.notify(resources.AGENT, events.BEFORE_DELETE, self,
context=self.ctx, agent=agent)
registry.publish(resources.AGENT, events.BEFORE_DELETE, self,
payload=events.DBEventPayload(
self.ctx, states=(agent,)))
mac_list = self.mixin.get_dvr_mac_address_list(self.ctx)
self.assertEqual(1, len(mac_list))
for mac in mac_list: