New API for get_creds_file and adding operationHistory for get_creds 43/14643/6
authorshahithya <shahithya.y@tataelxsi.co.in>
Thu, 17 Oct 2024 05:51:39 +0000 (05:51 +0000)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Mon, 11 Nov 2024 09:49:53 +0000 (10:49 +0100)
Change-Id: Ibe163ac0302b4456f26550719e6717969b15fe10
Signed-off-by: shahithya <shahithya.y@tataelxsi.co.in>
osm_nbi/base_topic.py
osm_nbi/engine.py
osm_nbi/k8s_topics.py
osm_nbi/nbi.py

index 35514a5..a327260 100644 (file)
@@ -411,7 +411,9 @@ class BaseTopic:
         return None
 
     @staticmethod
-    def format_on_operation(content, operation_type, operation_params):
+    def format_on_operation(
+        content, operation_type, operation_params=None, launch_workflow=True
+    ):
         if content["current_operation"] is None:
             op_id = str(uuid4())
             content["current_operation"] = op_id
@@ -423,14 +425,15 @@ class BaseTopic:
 
         operation = {}
         operation["operationType"] = operation_type
-        operation["git_operation_info"] = None
         operation["op_id"] = op_id
         operation["result"] = None
-        operation["workflowState"] = "PROCESSING"
-        operation["resourceState"] = "NOT_READY"
         operation["creationDate"] = now
         operation["endDate"] = None
-        operation["operationParams"] = operation_params
+        if launch_workflow:
+            operation["workflowState"] = "PROCESSING"
+            operation["resourceState"] = "NOT_READY"
+            operation["git_operation_info"] = None
+            operation["operationParams"] = operation_params
 
         content["operationHistory"].append(operation)
         return op_id
index bac0802..02b3e14 100644 (file)
@@ -344,12 +344,19 @@ class Engine(object):
         with self.write_lock:
             return self.map_topic[topic].move_ksu(session, _id, indata, kwargs)
 
-    def get_cluster_info(self, session, topic, _id, item):
+    def get_cluster_creds_file(self, session, topic, _id, item, op_id):
         if topic not in self.map_topic:
             raise EngineException(
                 "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR
             )
-        return self.map_topic[topic].get_cluster_info(session, _id, item)
+        return self.map_topic[topic].get_cluster_creds_file(session, _id, item, op_id)
+
+    def get_cluster_creds(self, session, topic, _id, item):
+        if topic not in self.map_topic:
+            raise EngineException(
+                "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR
+            )
+        return self.map_topic[topic].get_cluster_creds(session, _id, item)
 
     def update_cluster(self, session, topic, _id, item, indata):
         if topic not in self.map_topic:
index ea18a58..22eceba 100644 (file)
@@ -458,44 +458,67 @@ class K8sTopic(BaseTopic):
                 f"{item} {profile_id} does'nt exists", HTTPStatus.UNPROCESSABLE_ENTITY
             )
 
-    def get_cluster_info(self, session, _id, item):
+    def get_cluster_creds(self, session, _id, item):
         if not self.multiproject:
             filter_db = {}
         else:
             filter_db = self._get_project_filter(session)
-        # To allow project&user addressing by name AS WELL AS _id
         filter_db[BaseTopic.id_field(self.topic, _id)] = _id
-        self._send_msg(item, {"_id": _id})
-
+        op_id = str(uuid4())
+        operation_params = {}
         data = self.db.get_one(self.topic, filter_db)
-        credentials = data["credentials"]
+        data["current_operation"] = op_id
+        self.format_on_operation(data, item, operation_params, launch_workflow=False)
+        self.db.set_one(self.topic, {"_id": data["_id"]}, data)
+        self._send_msg("get_creds", {"cluster_id": _id, "operation_id": op_id})
+        return op_id
 
-        file_pkg = None
-        current_path = _id
+    def get_cluster_creds_file(self, session, _id, item, op_id):
+        if not self.multiproject:
+            filter_db = {}
+        else:
+            filter_db = self._get_project_filter(session)
+        filter_db[BaseTopic.id_field(self.topic, _id)] = _id
 
-        self.fs.file_delete(current_path, ignore_non_exist=True)
-        self.fs.mkdir(current_path)
-        filename = "credentials.yaml"
+        data = self.db.get_one(self.topic, filter_db)
+        creds_flag = None
+        for operations in data["operationHistory"]:
+            if operations["op_id"] == op_id:
+                creds_flag = operations["result"]
+        self.logger.info("Creds Flag: {}".format(creds_flag))
 
-        file_path = (current_path, filename)
-        self.logger.info("File path: {}".format(file_path))
-        file_pkg = self.fs.file_open(file_path, "a+b")
+        if creds_flag is True:
+            credentials = data["credentials"]
 
-        credentials_yaml = yaml.safe_dump(
-            credentials, indent=4, default_flow_style=False
-        )
-        file_pkg.write(credentials_yaml.encode(encoding="utf-8"))
+            file_pkg = None
+            current_path = _id
 
-        if file_pkg:
-            file_pkg.close()
-        file_pkg = None
+            self.fs.file_delete(current_path, ignore_non_exist=True)
+            self.fs.mkdir(current_path)
+            filename = "credentials.yaml"
+            file_path = (current_path, filename)
+            self.logger.info("File path: {}".format(file_path))
+            file_pkg = self.fs.file_open(file_path, "a+b")
 
-        self.fs.sync(from_path=current_path)
+            credentials_yaml = yaml.safe_dump(
+                credentials, indent=4, default_flow_style=False
+            )
+            file_pkg.write(credentials_yaml.encode(encoding="utf-8"))
 
-        return (
-            self.fs.file_open((current_path, filename), "rb"),
-            "text/plain",
-        )
+            if file_pkg:
+                file_pkg.close()
+            file_pkg = None
+            self.fs.sync(from_path=current_path)
+
+            return (
+                self.fs.file_open((current_path, filename), "rb"),
+                "text/plain",
+            )
+        else:
+            raise EngineException(
+                "Not possible to get the credentials of the cluster",
+                HTTPStatus.UNPROCESSABLE_ENTITY,
+            )
 
     def update_cluster(self, session, _id, item, indata):
         if not self.multiproject:
index 3fd3745..e11f4ba 100644 (file)
@@ -711,6 +711,14 @@ valid_url_methods = {
                         "METHODS": ("GET",),
                         "ROLE_PERMISSION": "k8scluster:id:get_creds:",
                     },
+                    "get_creds_file": {
+                        "METHODS": ("GET",),
+                        "ROLE_PERMISSION": "k8scluster:id:get_creds_file:",
+                        "<ID>": {
+                            "METHODS": ("GET",),
+                            "ROLE_PERMISSION": "k8scluster:id:get_creds_file:id",
+                        },
+                    },
                     "scale": {
                         "METHODS": ("POST",),
                         "ROLE_PERMISSION": "k8scluster:id:scale:",
@@ -1779,11 +1787,22 @@ class Server(object):
                         filter_q,
                         api_req=True,
                     )
-                elif topic == "clusters" and item == "get_creds":
-                    file, _format = self.engine.get_cluster_info(
-                        engine_session, engine_topic, _id, item
-                    )
-                    outdata = file
+                elif (
+                    topic == "clusters"
+                    and item == "get_creds_file"
+                    or item == "get_creds"
+                ):
+                    if item == "get_creds_file":
+                        op_id = args[0]
+                        file, _format = self.engine.get_cluster_creds_file(
+                            engine_session, engine_topic, _id, item, op_id
+                        )
+                        outdata = file
+                    if item == "get_creds":
+                        op_id = self.engine.get_cluster_creds(
+                            engine_session, engine_topic, _id, item
+                        )
+                        outdata = {"op_id": op_id}
                 else:
                     if item == "reports":
                         # TODO check that project_id (_id in this context) has permissions
@@ -1873,9 +1892,11 @@ class Server(object):
                             nslcmop_desc = {
                                 "lcmOperationType": "terminate",
                                 "nsInstanceId": ns_id,
-                                "autoremove": indata.get("autoremove")
-                                if "autoremove" in indata
-                                else True,
+                                "autoremove": (
+                                    indata.get("autoremove")
+                                    if "autoremove" in indata
+                                    else True
+                                ),
                             }
                             op_id, _, _ = self.engine.new_item(
                                 rollback,