use payloads for SECURITY_GROUP BEFORE_CREATE events

This patch switches the code over to the payload style of callbacks [1]
for SECURITY_GROUP BEFORE_CREATE events.

NeutronLibImpact

[1] https://docs.openstack.org/neutron-lib/latest/contributor/callbacks.html

Change-Id: Id48d1d0ec429011310571a7b43ffbb4a6d9f1610
This commit is contained in:
Boden R 2019-01-03 14:41:28 -07:00
parent f3810d0da3
commit 1687aaee3c
2 changed files with 21 additions and 13 deletions

View File

@ -80,9 +80,12 @@ class SecurityGroupDbMixin(ext_sg.SecurityGroupPluginBase):
'security_group': s,
'is_default': default_sg,
}
self._registry_notify(resources.SECURITY_GROUP, events.BEFORE_CREATE,
exc_cls=ext_sg.SecurityGroupConflict, **kwargs)
exc_cls=ext_sg.SecurityGroupConflict,
payload=events.DBEventPayload(
context, metadata={'is_default': default_sg},
request_body=security_group,
desired_state=s))
tenant_id = s['tenant_id']

View File

@ -75,8 +75,8 @@ class SecurityGroupDbMixinTestCase(testlib_api.SqlTestCase):
self.mixin = SecurityGroupDbMixinImpl()
def test_create_security_group_conflict(self):
with mock.patch.object(registry, "notify") as mock_notify:
mock_notify.side_effect = exceptions.CallbackFailure(Exception())
with mock.patch.object(registry, "publish") as mock_publish:
mock_publish.side_effect = exceptions.CallbackFailure(Exception())
secgroup = {'security_group': mock.ANY}
with testtools.ExpectedException(
securitygroup.SecurityGroupConflict):
@ -279,29 +279,34 @@ class SecurityGroupDbMixinTestCase(testlib_api.SqlTestCase):
DEFAULT_SECGROUP_DICT.update({
'revision_number': mock.ANY,
})
with mock.patch.object(registry, "notify") as mock_notify:
with mock.patch.object(registry, 'publish') as publish, \
mock.patch.object(registry, "notify") as mock_notify:
sg_dict = self.mixin.create_security_group(self.ctx, FAKE_SECGROUP)
mock_notify.assert_has_calls([
mock.call('security_group', 'before_create', mock.ANY,
context=mock.ANY, is_default=False,
security_group=FAKE_SECGROUP['security_group']),
mock.call('security_group', 'before_create', mock.ANY,
context=mock.ANY, is_default=True,
security_group=DEFAULT_SECGROUP),
mock.call('security_group', 'precommit_create', mock.ANY,
context=mock.ANY, is_default=True,
security_group=DEFAULT_SECGROUP_DICT),
mock.call('security_group', 'after_create', mock.ANY,
context=mock.ANY, is_default=True,
security_group=DEFAULT_SECGROUP_DICT),
mock.call('security_group', 'precommit_create', mock.ANY,
context=mock.ANY, is_default=False,
security_group=sg_dict),
mock.call('security_group', 'after_create', mock.ANY,
context=mock.ANY, is_default=False,
security_group=sg_dict)])
publish.assert_has_calls([
mock.call('security_group', 'before_create', mock.ANY,
payload=mock.ANY),
mock.call('security_group', 'before_create', mock.ANY,
payload=mock.ANY)])
payload = publish.mock_calls[0][2]['payload']
self.assertDictEqual(payload.desired_state,
FAKE_SECGROUP['security_group'])
payload = publish.mock_calls[1][2]['payload']
self.assertDictEqual(payload.desired_state, DEFAULT_SECGROUP)
# Ensure that the result of create is same as get.
# Especially we want to check the revision number here.
sg_dict_got = self.mixin.get_security_group(