Delete KSU from DB without sending Kafka message when profile does not exist 12/14812/3
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Thu, 5 Dec 2024 11:21:09 +0000 (12:21 +0100)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Mon, 9 Dec 2024 10:10:04 +0000 (11:10 +0100)
Change-Id: I534596b9927b4a0a1c1e70f8474dd1bb7b1e875c
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
osm_nbi/k8s_topics.py

index 7ee664e..83922e4 100644 (file)
@@ -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):