Payloads for PORT: BEFORE_CREATE and PRECOMMIT_CREATE

This patch switches the code over to the payload style of callbacks
for PORT BEFORE_CREATE and PRECOMMIT_CREATE events

Change-Id: Ia6903be1af4de575ab9d82c9cb0c88290d07abb3
This commit is contained in:
Nurmatov Mamatisa 2021-06-03 15:34:37 +03:00 committed by Mamatisa Nurmatov
parent 0bdf3b56e0
commit 07c64d8384
4 changed files with 28 additions and 21 deletions

View File

@ -888,19 +888,16 @@ class SecurityGroupDbMixin(ext_sg.SecurityGroupPluginBase,
if default_group:
return default_group.security_group_id
@registry.receives(resources.PORT, [events.BEFORE_CREATE,
events.BEFORE_UPDATE])
@registry.receives(resources.PORT, [events.BEFORE_UPDATE])
def _ensure_default_security_group_handler_port(
self, resource, event, trigger, context, **kwargs):
if event == events.BEFORE_UPDATE:
tenant_id = kwargs['original_' + resource]['tenant_id']
else:
tenant_id = kwargs[resource]['tenant_id']
if tenant_id:
self._ensure_default_security_group(context, tenant_id)
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.NETWORK, [events.BEFORE_CREATE])
def _ensure_default_security_group_handler_net(
def _ensure_default_security_group_handler_before_create(
self, resource, event, trigger, payload=None):
# TODO(boden): refactor into single callback method

View File

@ -1414,8 +1414,10 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
if not attrs.get('status'):
attrs['status'] = const.PORT_STATUS_DOWN
registry.notify(resources.PORT, events.BEFORE_CREATE, self,
context=context, port=attrs)
registry.publish(resources.PORT, events.BEFORE_CREATE, self,
payload=events.DBEventPayload(
context,
states=(attrs,)))
def _create_port_db(self, context, port):
attrs = port[port_def.RESOURCE_NAME]
@ -1441,9 +1443,11 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
attrs.get(addr_apidef.ADDRESS_PAIRS)))
self._process_port_create_extra_dhcp_opts(context, result,
dhcp_opts)
kwargs = {'context': context, 'port': result}
registry.notify(
resources.PORT, events.PRECOMMIT_CREATE, self, **kwargs)
registry.publish(resources.PORT, events.PRECOMMIT_CREATE, self,
payload=events.DBEventPayload(
context,
resource_id=result['id'],
states=(result,)))
self.mechanism_manager.create_port_precommit(mech_context)
self._setup_dhcp_agent_provisioning_component(context, result)
@ -1610,9 +1614,12 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
self._process_port_create_extra_dhcp_opts(context, port_dict,
dhcp_opts)
# send PRECOMMIT_CREATE notification
kwargs = {'context': context, 'port': db_port_obj}
registry.notify(
resources.PORT, events.PRECOMMIT_CREATE, self, **kwargs)
registry.publish(resources.PORT, events.PRECOMMIT_CREATE, self,
payload=events.DBEventPayload(
context,
resource_id=db_port_obj['id'],
states=(db_port_obj,)))
self.mechanism_manager.create_port_precommit(mech_context)
# handle DHCP agent provisioning

View File

@ -241,9 +241,9 @@ class QoSPlugin(qos.QoSPluginBase):
return list(set(ports_with_policy + ports_with_net_policy))
def _validate_create_port_callback(self, resource, event, trigger,
**kwargs):
context = kwargs['context']
port_id = kwargs['port']['id']
payload=None):
context = payload.context
port_id = payload.resource_id
port = ports_object.Port.get_object(context, id=port_id)
policy_id = port.qos_policy_id or port.qos_network_policy_id

View File

@ -1307,7 +1307,10 @@ class TestQosPluginDB(base.BaseQosTestCase):
with mock.patch.object(self.qos_plugin, 'validate_policy_for_port') \
as mock_validate_policy:
self.qos_plugin._validate_create_port_callback(
'PORT', 'precommit_create', 'test_plugin', **kwargs)
"PORT", "precommit_create", "test_plugin",
payload=events.DBEventPayload(
self.context,
resource_id=kwargs['port']['id'],))
qos_policy = None
if port_qos: