Cluster name update check unique name and replace in both collection

Change-Id: I9ddc891591ec58e8d3cb51118966fb1ebe6ff0f9
Signed-off-by: yshah <shahithya.y@tataelxsi.co.in>
diff --git a/osm_nbi/k8s_topics.py b/osm_nbi/k8s_topics.py
index d4ff80b..c930472 100644
--- a/osm_nbi/k8s_topics.py
+++ b/osm_nbi/k8s_topics.py
@@ -365,6 +365,34 @@
                 error_msg = "Add / remove operation is only applicable"
                 raise EngineException(error_msg, HTTPStatus.UNPROCESSABLE_ENTITY)
 
+    def edit_extra_before(self, session, _id, indata=None, kwargs=None, content=None):
+        check = self.db.get_one(self.topic, {"_id": _id})
+        if "name" in indata and check["name"] != indata["name"]:
+            self.check_unique_name(session, indata["name"])
+            _filter = {"name": indata["name"]}
+            topic_list = [
+                "k8sclusters",
+                "k8sinfra_controller",
+                "k8sinfra_config",
+                "k8sapp",
+                "k8sresource",
+            ]
+            # Check unique name for k8scluster and profiles
+            for topic in topic_list:
+                if self.db.get_one(
+                    topic, _filter, fail_on_empty=False, fail_on_more=False
+                ):
+                    raise EngineException(
+                        "name '{}' already exists for {}".format(indata["name"], topic),
+                        HTTPStatus.CONFLICT,
+                    )
+            # Replace name in k8scluster and profiles
+            for topic in topic_list:
+                data = self.db.get_one(topic, {"name": check["name"]})
+                data["name"] = indata["name"]
+                self.db.replace(topic, data["_id"], data)
+        return True
+
     def add_profile(self, session, _id, item, indata=None):
         indata = self._remove_envelop(indata)
         operation_params = indata