Merge "Add callbacks for networks and subnets in ML2"

This commit is contained in:
Jenkins 2016-07-27 03:42:44 +00:00 committed by Gerrit Code Review
commit 451193c850
2 changed files with 95 additions and 0 deletions

View File

@ -760,6 +760,8 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
def create_network(self, context, network):
result, mech_context = self._create_network_db(context, network)
kwargs = {'context': context, 'network': result}
registry.notify(resources.NETWORK, events.AFTER_CREATE, self, **kwargs)
try:
self.mechanism_manager.create_network_postcommit(mech_context)
except ml2_exc.MechanismDriverError:
@ -807,6 +809,9 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
# by re-calling update_network with the previous attributes. For
# now the error is propagated to the caller, which is expected to
# either undo/retry the operation or delete the resource.
kwargs = {'context': context, 'network': updated_network,
'original_network': original_network}
registry.notify(resources.NETWORK, events.AFTER_UPDATE, self, **kwargs)
self.mechanism_manager.update_network_postcommit(mech_context)
if need_network_update_notify:
self.notifier.network_update(context, updated_network)
@ -937,6 +942,8 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
self._delete_ports(context, port_ids)
self._delete_subnets(context, subnet_ids)
kwargs = {'context': context, 'network': network}
registry.notify(resources.NETWORK, events.AFTER_DELETE, self, **kwargs)
try:
self.mechanism_manager.delete_network_postcommit(mech_context)
except ml2_exc.MechanismDriverError:
@ -962,6 +969,8 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
def create_subnet(self, context, subnet):
result, mech_context = self._create_subnet_db(context, subnet)
kwargs = {'context': context, 'subnet': result}
registry.notify(resources.SUBNET, events.AFTER_CREATE, self, **kwargs)
try:
self.mechanism_manager.create_subnet_postcommit(mech_context)
except ml2_exc.MechanismDriverError:
@ -993,6 +1002,9 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
# by re-calling update_subnet with the previous attributes. For
# now the error is propagated to the caller, which is expected to
# either undo/retry the operation or delete the resource.
kwargs = {'context': context, 'subnet': updated_subnet,
'original_subnet': original_subnet}
registry.notify(resources.SUBNET, events.AFTER_UPDATE, self, **kwargs)
self.mechanism_manager.update_subnet_postcommit(mech_context)
return updated_subnet
@ -1118,6 +1130,8 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
e, _LE("Exception deleting fixed_ip from "
"port %s"), port_id)
kwargs = {'context': context, 'subnet': subnet}
registry.notify(resources.SUBNET, events.AFTER_DELETE, self, **kwargs)
try:
self.mechanism_manager.delete_subnet_postcommit(mech_context)
except ml2_exc.MechanismDriverError:

View File

@ -243,6 +243,48 @@ class TestMl2NetworksV2(test_plugin.TestNetworksV2,
]
self.nets = self.mp_nets + self.pnets
def test_network_after_create_callback(self):
after_create = mock.Mock()
registry.subscribe(after_create, resources.NETWORK,
events.AFTER_CREATE)
with self.network() as n:
after_create.assert_called_once_with(
resources.NETWORK, events.AFTER_CREATE, mock.ANY,
context=mock.ANY, network=mock.ANY)
kwargs = after_create.mock_calls[0][2]
self.assertEqual(n['network']['id'],
kwargs['network']['id'])
def test_network_after_update_callback(self):
after_update = mock.Mock()
registry.subscribe(after_update, resources.NETWORK,
events.AFTER_UPDATE)
with self.network() as n:
data = {'network': {'name': 'updated'}}
req = self.new_update_request('networks', data, n['network']['id'])
self.deserialize(self.fmt, req.get_response(self.api))
after_update.assert_called_once_with(
resources.NETWORK, events.AFTER_UPDATE, mock.ANY,
context=mock.ANY, network=mock.ANY, original_network=mock.ANY)
kwargs = after_update.mock_calls[0][2]
self.assertEqual(n['network']['name'],
kwargs['original_network']['name'])
self.assertEqual('updated', kwargs['network']['name'])
def test_network_after_delete_callback(self):
after_delete = mock.Mock()
registry.subscribe(after_delete, resources.NETWORK,
events.AFTER_DELETE)
with self.network() as n:
req = self.new_delete_request('networks', n['network']['id'])
req.get_response(self.api)
after_delete.assert_called_once_with(
resources.NETWORK, events.AFTER_DELETE, mock.ANY,
context=mock.ANY, network=mock.ANY)
kwargs = after_delete.mock_calls[0][2]
self.assertEqual(n['network']['id'],
kwargs['network']['id'])
def test_port_delete_helper_tolerates_failure(self):
plugin = manager.NeutronManager.get_plugin()
with mock.patch.object(plugin, "delete_port",
@ -421,6 +463,45 @@ class TestMl2NetworksWithAvailabilityZone(TestMl2NetworksV2):
class TestMl2SubnetsV2(test_plugin.TestSubnetsV2,
Ml2PluginV2TestCase):
def test_subnet_after_create_callback(self):
after_create = mock.Mock()
registry.subscribe(after_create, resources.SUBNET, events.AFTER_CREATE)
with self.subnet() as s:
after_create.assert_called_once_with(
resources.SUBNET, events.AFTER_CREATE, mock.ANY,
context=mock.ANY, subnet=mock.ANY)
kwargs = after_create.mock_calls[0][2]
self.assertEqual(s['subnet']['id'], kwargs['subnet']['id'])
def test_subnet_after_update_callback(self):
after_update = mock.Mock()
registry.subscribe(after_update, resources.SUBNET, events.AFTER_UPDATE)
with self.subnet() as s:
data = {'subnet': {'name': 'updated'}}
req = self.new_update_request('subnets', data, s['subnet']['id'])
self.deserialize(self.fmt, req.get_response(self.api))
after_update.assert_called_once_with(
resources.SUBNET, events.AFTER_UPDATE, mock.ANY,
context=mock.ANY, subnet=mock.ANY,
original_subnet=mock.ANY)
kwargs = after_update.mock_calls[0][2]
self.assertEqual(s['subnet']['name'],
kwargs['original_subnet']['name'])
self.assertEqual('updated', kwargs['subnet']['name'])
def test_subnet_after_delete_callback(self):
after_delete = mock.Mock()
registry.subscribe(after_delete, resources.SUBNET, events.AFTER_DELETE)
with self.subnet() as s:
req = self.new_delete_request('subnets', s['subnet']['id'])
req.get_response(self.api)
after_delete.assert_called_once_with(
resources.SUBNET, events.AFTER_DELETE, mock.ANY,
context=mock.ANY, subnet=mock.ANY)
kwargs = after_delete.mock_calls[0][2]
self.assertEqual(s['subnet']['id'], kwargs['subnet']['id'])
def test_delete_subnet_race_with_dhcp_port_creation(self):
with self.network() as network:
with self.subnet(network=network) as subnet: