From 3e25ee3e28e1832b8eb0474db1e980b75f377df1 Mon Sep 17 00:00:00 2001 From: Hong Hui Xiao Date: Fri, 9 Sep 2016 10:27:47 +0800 Subject: [PATCH] Add sg_id in the AFTER_DELETE event of sg_rule delete In the AFTER_DELETE of sg_rule delete, the sg_rule is actually deleted. This makes it impossible/hard to know which sg is affected. This patch add the sg_id in the event. And this patch will be used in [1]. [1] https://review.openstack.org/#/c/367718 Change-Id: Ibdef6a703913b74504e402d225b1a0dffadb7aff Closes-Bug: #1621698 --- neutron/db/securitygroups_db.py | 5 ++++- neutron/tests/unit/db/test_securitygroups_db.py | 11 +++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/neutron/db/securitygroups_db.py b/neutron/db/securitygroups_db.py index a40184644f6..ea7be357862 100644 --- a/neutron/db/securitygroups_db.py +++ b/neutron/db/securitygroups_db.py @@ -606,12 +606,15 @@ class SecurityGroupDbMixin(ext_sg.SecurityGroupPluginBase): **kwargs) try: + sg_rule = query.one() # As there is a filter on a primary key it is not possible for # MultipleResultsFound to be raised - context.session.delete(query.one()) + context.session.delete(sg_rule) except exc.NoResultFound: raise ext_sg.SecurityGroupRuleNotFound(id=id) + kwargs['security_group_id'] = sg_rule['security_group_id'] + registry.notify( resources.SECURITY_GROUP_RULE, events.AFTER_DELETE, self, **kwargs) diff --git a/neutron/tests/unit/db/test_securitygroups_db.py b/neutron/tests/unit/db/test_securitygroups_db.py index 0f11ae0a981..466a99261d2 100644 --- a/neutron/tests/unit/db/test_securitygroups_db.py +++ b/neutron/tests/unit/db/test_securitygroups_db.py @@ -270,7 +270,7 @@ class SecurityGroupDbMixinTestCase(testlib_api.SqlTestCase): 'precommit_create', mock.ANY, context=mock.ANY, security_group_rule=mock.ANY)]) - def test_security_group_rule_precommit_delete_event(self): + def test_sg_rule_before_precommit_and_after_delete_event(self): sg_dict = self.mixin.create_security_group(self.ctx, FAKE_SECGROUP) fake_rule = FAKE_SECGROUP_RULE fake_rule['security_group_rule']['security_group_id'] = sg_dict['id'] @@ -280,9 +280,16 @@ class SecurityGroupDbMixinTestCase(testlib_api.SqlTestCase): fake_rule) self.mixin.delete_security_group_rule(self.ctx, sg_rule_dict['id']) + mock_notify.assert_has_calls([mock.call('security_group_rule', + 'before_delete', mock.ANY, context=mock.ANY, + security_group_rule_id=sg_rule_dict['id'])]) mock_notify.assert_has_calls([mock.call('security_group_rule', 'precommit_delete', mock.ANY, context=mock.ANY, - security_group_rule_id=mock.ANY)]) + security_group_rule_id=sg_rule_dict['id'])]) + mock_notify.assert_has_calls([mock.call('security_group_rule', + 'after_delete', mock.ANY, context=mock.ANY, + security_group_rule_id=sg_rule_dict['id'], + security_group_id=sg_dict['id'])]) def test_get_ip_proto_name_and_num(self): protocols = [constants.PROTO_NAME_UDP, str(constants.PROTO_NUM_TCP),