From 00a620a45b190e4e0a45b0a96fffdaea5fa4d03c Mon Sep 17 00:00:00 2001 From: yshah Date: Thu, 16 Jan 2025 12:06:40 +0000 Subject: [PATCH] Cluster name update check unique name and replace in both collection Change-Id: I9ddc891591ec58e8d3cb51118966fb1ebe6ff0f9 Signed-off-by: yshah --- osm_nbi/acm_topic.py | 27 +++++++++++---------------- osm_nbi/k8s_topics.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/osm_nbi/acm_topic.py b/osm_nbi/acm_topic.py index 87087b7..27273f9 100644 --- a/osm_nbi/acm_topic.py +++ b/osm_nbi/acm_topic.py @@ -262,22 +262,6 @@ class ACMTopic(BaseTopic, ACMOperationTopic): self.db.set_one("k8sclusters", {"_id": item_1["_id"]}, item_1) return - def edit_extra_before(self, session, _id, indata=None, kwargs=None, content=None): - check = self.db.get_one(self.topic, {"_id": _id}) - if self.topic != "clusters": - if check["default"] is True: - raise EngineException( - "Cannot edit default profiles", - HTTPStatus.UNPROCESSABLE_ENTITY, - ) - if "name" in indata: - if check["name"] == indata["name"]: - pass - else: - self.check_unique_name(session, indata["name"]) - - return True - def cluster_unique_name_check(self, session, name): self.check_unique_name(session, name) _filter = {"name": name} @@ -323,6 +307,17 @@ class ProfileTopic(ACMTopic): def __init__(self, db, fs, msg, auth): super().__init__(db, fs, msg, auth) + def edit_extra_before(self, session, _id, indata=None, kwargs=None, content=None): + check = self.db.get_one(self.topic, {"_id": _id}) + if check["default"] is True: + raise EngineException( + "Cannot edit default profiles", + HTTPStatus.UNPROCESSABLE_ENTITY, + ) + if "name" in indata and check["name"] != indata["name"]: + self.check_unique_name(session, indata["name"]) + return True + def delete_extra_before(self, session, _id, db_content, not_send_msg=None): op_id = self.common_delete(_id, db_content) return {"profile_id": _id, "operation_id": op_id} 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 @@ class ClusterTopic(ACMTopic): 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 -- 2.25.1