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:
import os
import shutil
import tempfile
+from math import ceil
+from time import sleep
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