Fix cluster-get-credentials to check in a loop the completion of the operation
Change-Id: Ib427fad148cd492fec243ab85694fd299dab6a5e
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
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 @@
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 os
import shutil
import tempfile
+from math import ceil
+from time import sleep
class GenericOSMAPIObject(ABC):
@@ -349,3 +351,28 @@
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