Skip to content
Snippets Groups Projects
Commit 1b0850a7 authored by garciadeblas's avatar garciadeblas
Browse files

Fix cluster-get-credentials to check in a loop the completion of the operation


Change-Id: Ib427fad148cd492fec243ab85694fd299dab6a5e
Signed-off-by: default avatargarciadeblas <gerardo.garciadeblas@telefonica.com>
parent a905f09c
No related branches found
No related tags found
No related merge requests found
......@@ -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:
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment