neutron/neutron/services/logapi/common/sg_callback.py
Rodolfo Alonso Hernandez 41f78c678b Delete log entries when SG or port is deleted
NOTE: this patch is an ammend of [1].

When a SG or a port is deleted, the related log entry should be
too. A log entry has the following fields:
- log.resource_id = SG ID
- log.target_id = port ID

[1] was deleting all log entries, related or not with the SG ID
deleted. This is because "get_logs_bound_sg" returned all log entries,
including those ones without any "resource_id" or "target_id".

Now this method can return only the log entries related to a port or
a SG, excluding those ones without those two parameters populated.

Closes-Bug: #1939558

[1]https://review.opendev.org/c/openstack/neutron/+/804237

Change-Id: Icb92327a06486e168ce064532d819347e6031cc1
2021-10-05 16:27:13 +00:00

37 lines
1.3 KiB
Python

# Copyright (c) 2018 Fujitsu Limited
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from neutron_lib.services.logapi import constants as log_const
from neutron.services.logapi.common import db_api
from neutron.services.logapi.drivers import manager
class SecurityGroupRuleCallBack(manager.ResourceCallBackBase):
def handle_event(self, resource, event, trigger, payload):
context = payload.context
sg_rule = payload.latest_state
if sg_rule:
sg_id = sg_rule.get('security_group_id')
else:
sg_id = payload.resource_id
log_resources = db_api.get_logs_bound_sg(
context, sg_id=sg_id, project_id=context.project_id)
if log_resources:
self.resource_push_api(
log_const.RESOURCE_UPDATE, context, log_resources)