From: garciadeblas Date: Thu, 31 Oct 2024 00:13:46 +0000 (+0100) Subject: Fix cluster-get-credentials to check in a loop the completion of the operation X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F92%2F14692%2F1;p=osm%2Fosmclient.git Fix cluster-get-credentials to check in a loop the completion of the operation Change-Id: Ib427fad148cd492fec243ab85694fd299dab6a5e Signed-off-by: garciadeblas --- diff --git a/osmclient/sol005/cluster.py b/osmclient/sol005/cluster.py index 2517456..8062ce4 100644 --- a/osmclient/sol005/cluster.py +++ b/osmclient/sol005/cluster.py @@ -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: diff --git a/osmclient/sol005/osm_api_object.py b/osmclient/sol005/osm_api_object.py index 0e4b8cb..780bb04 100644 --- a/osmclient/sol005/osm_api_object.py +++ b/osmclient/sol005/osm_api_object.py @@ -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