Fix cluster-get-credentials to check in a loop the completion of the operation 92/14692/1
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Thu, 31 Oct 2024 00:13:46 +0000 (01:13 +0100)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Thu, 31 Oct 2024 00:13:46 +0000 (01:13 +0100)
Change-Id: Ib427fad148cd492fec243ab85694fd299dab6a5e
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
osmclient/sol005/cluster.py
osmclient/sol005/osm_api_object.py

index 2517456..8062ce4 100644 (file)
@@ -72,14 +72,18 @@ class Cluster(GenericOSMAPIObject):
         try:
             _, resp = self._http.get2_cmd(f"{self._apiBase}/{item['_id']}/get_creds")
             if resp:
-                resp = json.loads(resp)
-                # TODO: loop to check if the credentials were populated in the db
-                item = self.get(name)
-                print(
-                    yaml.safe_dump(
-                        item["credentials"], indent=4, default_flow_style=False
+                op_id = json.loads(resp)
+                # Wait loop to check if the operation completed
+                result = self.wait_for_operation_status(item["_id"], 10, op_id)
+                if result:
+                    item = self.get(name)
+                    print(
+                        yaml.safe_dump(
+                            item["credentials"], indent=4, default_flow_style=False
+                        )
                     )
-                )
+                else:
+                    print("No credentials were found")
         except NotFound:
             raise NotFound(f"{self._logObjectName} '{name}' not found")
         except Exception as e:
index 0e4b8cb..780bb04 100644 (file)
@@ -30,6 +30,8 @@ import magic
 import os
 import shutil
 import tempfile
+from math import ceil
+from time import sleep
 
 
 class GenericOSMAPIObject(ABC):
@@ -349,3 +351,28 @@ class GenericOSMAPIObject(ABC):
             print("Updated")
         if tempdir:
             tempdir.cleanup()
+
+    def wait_for_operation_status(self, id, timeout, operation_id):
+        """
+        Wait until operation ends, making polling every 5s. Prints detailed status when it changes
+        :param id: ID of the entity to be checked.
+        :param timeout: Timeout in seconds
+        :param operation_id: ID of the operation to be checked
+        :return: True if operation completes successfully, False otherwise
+        """
+        # Loop here until the operation finishes, or a timeout occurs.
+        self._logger.debug("")
+        POLLING_TIME_INTERVAL = 5
+        tries = 0
+        max_tries = ceil(timeout / POLLING_TIME_INTERVAL)
+        while tries < max_tries:
+            self._logger.info(f"Wait for operation to complete. Try {tries+1}")
+            item = self.get(id)
+            for op in item.get("operationHistory", []):
+                if op["op_id"] == operation_id:
+                    if op["result"]:
+                        return True
+                    break
+            sleep(POLLING_TIME_INTERVAL)
+            tries += 1
+        return False