From ac2858754b0b5feee2be29ffcc78a19ff5e7e70a Mon Sep 17 00:00:00 2001 From: garciadeblas Date: Thu, 5 Dec 2024 12:21:09 +0100 Subject: [PATCH] Delete KSU from DB without sending Kafka message when profile does not exist Change-Id: I534596b9927b4a0a1c1e70f8474dd1bb7b1e875c Signed-off-by: garciadeblas --- osm_nbi/k8s_topics.py | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/osm_nbi/k8s_topics.py b/osm_nbi/k8s_topics.py index 7ee664e..83922e4 100644 --- a/osm_nbi/k8s_topics.py +++ b/osm_nbi/k8s_topics.py @@ -951,15 +951,18 @@ class KsusTopic(BaseTopic): for ksus in indata["ksus"]: content = ksus _id = content["_id"] - _id_list.append(_id) content.pop("_id") - op_id = self.delete(session, _id) + op_id, not_send_msg_ksu = self.delete(session, _id) + if not not_send_msg_ksu: + _id_list.append(_id) else: - _id_list.append(_id) - op_id = self.delete(session, _id) + op_id, not_send_msg_ksu = self.delete(session, _id) + if not not_send_msg_ksu: + _id_list.append(_id) - data = {"ksus_list": _id_list, "operation_id": op_id} - self._send_msg("delete", data) + if _id_list: + data = {"ksus_list": _id_list, "operation_id": op_id} + self._send_msg("delete", data, not_send_msg) return op_id def delete(self, session, _id): @@ -1006,7 +1009,24 @@ class KsusTopic(BaseTopic): {"_id": oka_id}, {"_admin.usageState": "NOT_IN_USE"}, ) - return op_id + # Check if the profile exists. If it doesn't, no message should be sent to Kafka + not_send_msg = None + profile_id = item_content["profile"]["_id"] + profile_type = item_content["profile"]["profile_type"] + profile_collection_map = { + "app_profiles": "k8sapp", + "resource_profiles": "k8sresource", + "infra_controller_profiles": "k8sinfra_controller", + "infra_config_profiles": "k8sinfra_config", + } + profile_collection = profile_collection_map[profile_type] + profile_content = self.db.get_one( + profile_collection, {"_id": profile_id}, fail_on_empty=False + ) + if not profile_content: + self.db.del_one(self.topic, filter_q) + not_send_msg = True + return op_id, not_send_msg class OkaTopic(DescriptorTopic): -- 2.25.1