use payloads for PORT BEFORE_UPDATE events

This patch switches the code over to the payload style of callbacks
for PORT BEFORE_UPDATE events

Change-Id: Ie55a04deac6c2f54f7f5d475c350f0fbf7b1fe77
This commit is contained in:
Nurmatov Mamatisa 2021-06-11 11:38:05 +03:00
parent 1e2088abbe
commit 324a35a3d0
5 changed files with 71 additions and 47 deletions

View File

@ -888,20 +888,15 @@ class SecurityGroupDbMixin(ext_sg.SecurityGroupPluginBase,
if default_group:
return default_group.security_group_id
@registry.receives(resources.PORT, [events.BEFORE_UPDATE])
def _ensure_default_security_group_handler_port(
self, resource, event, trigger, context, **kwargs):
project_id = kwargs['original_' + resource]['tenant_id']
if project_id:
self._ensure_default_security_group(context, project_id)
@registry.receives(resources.PORT, [events.BEFORE_CREATE])
@registry.receives(resources.PORT, [events.BEFORE_CREATE,
events.BEFORE_UPDATE])
@registry.receives(resources.NETWORK, [events.BEFORE_CREATE])
def _ensure_default_security_group_handler_before_create(
self, resource, event, trigger, payload=None):
# TODO(boden): refactor into single callback method
project_id = payload.latest_state['tenant_id']
def _ensure_default_security_group_handler(self, resource, event, trigger,
payload):
if event == events.BEFORE_UPDATE:
project_id = payload.states[0]['tenant_id']
else:
project_id = payload.latest_state['tenant_id']
if project_id:
self._ensure_default_security_group(payload.context, project_id)

View File

@ -627,16 +627,18 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
need_notify, update_binding_levels=True):
port_id = orig_context.current['id']
plugin_context = orig_context._plugin_context
port = orig_context.current
original_port = orig_context.current
orig_binding = orig_context._binding
new_binding = bind_context._binding
# TODO(yamahata): revise what to be passed or new resource
# like PORTBINDING should be introduced?
# It would be addressed during EventPayload conversion.
registry.notify(resources.PORT, events.BEFORE_UPDATE, self,
context=plugin_context, port=orig_context.current,
original_port=orig_context.current,
orig_binding=orig_binding, new_binding=new_binding)
registry.publish(resources.PORT, events.BEFORE_UPDATE, self,
payload=events.DBEventPayload(
plugin_context,
resource_id=port_id,
metadata={'orig_binding': orig_binding,
'new_binding': new_binding},
states=(original_port, port)))
# After we've attempted to bind the port, we begin a
# transaction, get the current port state, and decide whether
@ -1697,9 +1699,11 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
need_port_update_notify = False
bound_mech_contexts = []
original_port = self.get_port(context, id)
registry.notify(resources.PORT, events.BEFORE_UPDATE, self,
context=context, port=attrs,
original_port=original_port)
registry.publish(resources.PORT, events.BEFORE_UPDATE, self,
payload=events.DBEventPayload(
context,
resource_id=id,
states=(original_port, attrs)))
with db_api.CONTEXT_WRITER.using(context):
port_db = self._get_port(context, id)
binding = p_utils.get_port_binding_by_status_and_host(
@ -2161,9 +2165,11 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
portbindings.HOST_ID: host,
'status': status
}
registry.notify(resources.PORT, events.BEFORE_UPDATE, self,
original_port=port,
context=context, port=attr)
registry.publish(resources.PORT, events.BEFORE_UPDATE, self,
payload=events.DBEventPayload(
context,
resource_id=port_id,
states=(port, attr,)))
with db_api.CONTEXT_WRITER.using(context):
context.session.add(port) # bring port into writer session
if (port.status != status and

View File

@ -86,9 +86,6 @@ class QoSPlugin(qos.QoSPluginBase):
self._validate_create_port_callback,
callbacks_resources.PORT,
callbacks_events.PRECOMMIT_CREATE)
# TODO(lajoskatona): PORT BEFORE_UPDATE is a notify, so
# "old style" kwargs instead of payload object, let's change it
# to notify and payload.
callbacks_registry.subscribe(
self._check_port_for_placement_allocation_change,
callbacks_resources.PORT,
@ -254,13 +251,14 @@ class QoSPlugin(qos.QoSPluginBase):
self.validate_policy_for_port(context, policy, port)
def _check_port_for_placement_allocation_change(self, resource, event,
trigger, **kwargs):
context = kwargs['context']
orig_port = kwargs['original_port']
trigger, payload):
context = payload.context
orig_port = payload.states[0]
port = payload.latest_state
original_policy_id = orig_port.get(qos_consts.QOS_POLICY_ID)
if qos_consts.QOS_POLICY_ID not in kwargs['port']:
if qos_consts.QOS_POLICY_ID not in port:
return
policy_id = kwargs['port'].get(qos_consts.QOS_POLICY_ID)
policy_id = port.get(qos_consts.QOS_POLICY_ID)
if policy_id == original_policy_id:
return

View File

@ -1471,7 +1471,7 @@ class TestMl2PortsV2(test_plugin.TestPortsV2, Ml2PluginV2TestCase):
ctx = context.get_admin_context()
b_update_events = []
a_update_events = []
b_receiver = lambda *a, **k: b_update_events.append(k)
b_receiver = lambda r, e, t, payload: b_update_events.append(payload)
a_receiver = lambda *a, **k: a_update_events.append(k['port'])
registry.subscribe(b_receiver, resources.PORT,
events.BEFORE_UPDATE)
@ -1483,20 +1483,24 @@ class TestMl2PortsV2(test_plugin.TestPortsV2, Ml2PluginV2TestCase):
# updating in the host should result in two AFTER_UPDATE events.
# one to change the host_id, the second to commit a binding
self.assertEqual(2, len(b_update_events))
self.assertEqual({'context': ctx,
'port': {'binding:host_id': 'newhost'},
'original_port': mock.ANY},
b_update_events[0])
self.assertIn('orig_binding', b_update_events[1])
self.assertIn('new_binding', b_update_events[1])
self.assertDictContainsSubset({'context': ctx}, b_update_events[1])
# use dict for assertEqual because payload is object
expected_dict = {'context': ctx,
'port': {'binding:host_id': 'newhost'},
'original_port': mock.ANY}
actual_dict = {'context': b_update_events[0].context,
'port': b_update_events[0].latest_state,
'original_port': b_update_events[0].states[0]}
self.assertEqual(expected_dict, actual_dict)
self.assertIn('orig_binding', b_update_events[1].metadata)
self.assertIn('new_binding', b_update_events[1].metadata)
self.assertDictContainsSubset({'context': ctx}, actual_dict)
self.assertDictContainsSubset({
'admin_state_up': True,
'binding:host_id': 'newhost',
'binding:vif_type': 'unbound',
'binding:vnic_type': u'normal',
'status': 'DOWN'},
b_update_events[1]['port'])
b_update_events[1].latest_state)
self.assertEqual('newhost', a_update_events[0]['binding:host_id'])
self.assertEqual('unbound', a_update_events[0]['binding:vif_type'])
self.assertEqual('newhost', a_update_events[1]['binding:host_id'])

View File

@ -1355,11 +1355,17 @@ class TestQosPluginDB(base.BaseQosTestCase):
qos1_obj = self._make_qos_policy()
kwargs = self._prepare_for_port_placement_allocation_change(
qos1=qos1_obj, qos2=qos1_obj)
context = kwargs['context']
original_port = kwargs['original_port']
port = kwargs['port']
with mock.patch.object(
self.qos_plugin,
'_change_placement_allocation') as mock_alloc_change:
self.qos_plugin._check_port_for_placement_allocation_change(
'PORT', 'before_update', 'test_plugin', **kwargs)
'PORT', 'before_update', 'test_plugin',
payload=events.DBEventPayload(
context, states=(original_port, port)))
mock_alloc_change.assert_not_called()
def test_check_port_for_placement_allocation_change(self):
@ -1367,12 +1373,17 @@ class TestQosPluginDB(base.BaseQosTestCase):
qos2_obj = self._make_qos_policy()
kwargs = self._prepare_for_port_placement_allocation_change(
qos1=qos1_obj, qos2=qos2_obj)
context = kwargs['context']
original_port = kwargs['original_port']
port = kwargs['port']
with mock.patch.object(
self.qos_plugin,
'_change_placement_allocation') as mock_alloc_change:
self.qos_plugin._check_port_for_placement_allocation_change(
'PORT', 'before_update', 'test_plugin', **kwargs)
'PORT', 'before_update', 'test_plugin',
payload=events.DBEventPayload(
context, states=(original_port, port)))
mock_alloc_change.assert_called_once_with(
qos1_obj, qos2_obj, kwargs['original_port'])
@ -1380,12 +1391,17 @@ class TestQosPluginDB(base.BaseQosTestCase):
qos1_obj = self._make_qos_policy()
kwargs = self._prepare_for_port_placement_allocation_change(
qos1=qos1_obj, qos2=None)
context = kwargs['context']
original_port = kwargs['original_port']
port = kwargs['port']
with mock.patch.object(
self.qos_plugin,
'_change_placement_allocation') as mock_alloc_change:
self.qos_plugin._check_port_for_placement_allocation_change(
'PORT', 'before_update', 'test_plugin', **kwargs)
'PORT', 'before_update', 'test_plugin',
payload=events.DBEventPayload(
context, states=(original_port, port)))
mock_alloc_change.assert_called_once_with(
qos1_obj, None, kwargs['original_port'])
@ -1394,12 +1410,17 @@ class TestQosPluginDB(base.BaseQosTestCase):
kwargs = self._prepare_for_port_placement_allocation_change(
qos1=qos1_obj, qos2=None)
kwargs['port'].pop('qos_policy_id')
context = kwargs['context']
original_port = kwargs['original_port']
port = kwargs['port']
with mock.patch.object(
self.qos_plugin,
'_change_placement_allocation') as mock_alloc_change:
self.qos_plugin._check_port_for_placement_allocation_change(
'PORT', 'before_update', 'test_plugin', **kwargs)
'PORT', 'before_update', 'test_plugin',
payload=events.DBEventPayload(
context, states=(original_port, port)))
mock_alloc_change.assert_not_called()
def _prepare_port_for_placement_allocation(self, qos1, qos2=None,