Use BEFORE_CREATE events for _ensure_default_security_group

By using an event we can keep the default security group
logic isolated to the securiy groups DB code. This will
allow us to eliminate create_network/create_port overrides
in ML2.

Change-Id: I37a84889f8584c43f53686b7a2282bf2eb795227
This commit is contained in:
Kevin Benton 2017-03-02 02:33:01 -08:00 committed by Kevin Benton
parent 2bf91b2f10
commit 8bba50cdf5
2 changed files with 12 additions and 7 deletions

View File

@ -35,6 +35,7 @@ from neutron.db.models import securitygroup as sg_models
from neutron.extensions import securitygroup as ext_sg
@registry.has_registry_receivers
class SecurityGroupDbMixin(ext_sg.SecurityGroupPluginBase):
"""Mixin class to add security group to db_base_plugin_v2."""
@ -685,6 +686,13 @@ class SecurityGroupDbMixin(ext_sg.SecurityGroupPluginBase):
except exc.NoResultFound:
pass
@registry.receives(resources.PORT, [events.BEFORE_CREATE])
@registry.receives(resources.NETWORK, [events.BEFORE_CREATE])
def _ensure_default_security_group_handler(self, resource, event, trigger,
context, **kwargs):
tenant_id = kwargs[resource]['tenant_id']
self._ensure_default_security_group(context, tenant_id)
def _ensure_default_security_group(self, context, tenant_id):
"""Create a default security group if one doesn't exist.

View File

@ -734,6 +734,8 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
net_data = network[attributes.NETWORK]
tenant_id = net_data['tenant_id']
session = context.session
registry.notify(resources.NETWORK, events.BEFORE_CREATE, self,
context=context, network=net_data)
with session.begin(subtransactions=True):
net_db = self.create_network_db(context, network)
result = self._make_network_dict(net_db, process_extensions=False,
@ -770,8 +772,6 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
@utils.transaction_guard
@db_api.retry_if_session_inactive()
def create_network(self, context, network):
self._ensure_default_security_group(context,
network['network']['tenant_id'])
result, mech_context = self._create_network_db(context, network)
kwargs = {'context': context, 'network': result}
registry.notify(resources.NETWORK, events.AFTER_CREATE, self, **kwargs)
@ -788,8 +788,6 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
@utils.transaction_guard
@db_api.retry_if_session_inactive()
def create_network_bulk(self, context, networks):
tenants = {n['network']['tenant_id'] for n in networks['networks']}
map(lambda t: self._ensure_default_security_group(context, t), tenants)
objects = self._create_bulk_ml2(attributes.NETWORK, context, networks)
return [obj['result'] for obj in objects]
@ -1242,9 +1240,8 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
if not attrs.get('status'):
attrs['status'] = const.PORT_STATUS_DOWN
# NOTE(kevinbenton): triggered outside of transaction since it
# emits 'AFTER' events if it creates.
self._ensure_default_security_group(context, attrs['tenant_id'])
registry.notify(resources.PORT, events.BEFORE_CREATE, self,
context=context, port=attrs)
session = context.session
with session.begin(subtransactions=True):
dhcp_opts = attrs.get(edo_ext.EXTRADHCPOPTS, [])