X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osmclient%2Fcommon%2Fwait.py;fp=osmclient%2Fcommon%2Fwait.py;h=d3f673c64a285d2bb51d3c92e3e27c9ee92161ee;hb=95686bbc69ded243c346f94dceb0bee567572fb7;hp=bb9a82a0d8c1480e738ff58a425293de88934b7b;hpb=52424a2ae26db69c5a97d01e84454ffdd4d31228;p=osm%2Fosmclient.git diff --git a/osmclient/common/wait.py b/osmclient/common/wait.py index bb9a82a..d3f673c 100644 --- a/osmclient/common/wait.py +++ b/osmclient/common/wait.py @@ -56,10 +56,10 @@ def _get_finished_states(entity): :param entity: can be NS, NSI, or other :return: two tuples with status completed strings, status failed string """ - if entity == 'NS' or entity == 'NSI': - return ('COMPLETED', 'PARTIALLY_COMPLETED'), ('FAILED_TEMP', 'FAILED') + if entity == "NS" or entity == "NSI": + return ("COMPLETED", "PARTIALLY_COMPLETED"), ("FAILED_TEMP", "FAILED") else: - return ('ENABLED', ), ('ERROR', ) + return ("ENABLED",), ("ERROR",) def _get_operational_state(resp, entity): @@ -72,10 +72,10 @@ def _get_operational_state(resp, entity): :param entity: can be NS, NSI, or other :return: status of the operation """ - if entity == 'NS' or entity == 'NSI': - return resp.get('operationState') + if entity == "NS" or entity == "NSI": + return resp.get("operationState") else: - return resp.get('_admin', {}).get('operationalState') + return resp.get("_admin", {}).get("operationalState") def _op_has_finished(resp, entity): @@ -95,9 +95,11 @@ def _op_has_finished(resp, entity): if op_state in finished_states_ok: return True elif op_state in finished_states_error: - raise ClientException("Operation failed with status '{}'".format(op_state)) + raise ClientException( + "Operation failed with status '{}'".format(op_state) + ) return False - raise ClientException('Unexpected response from server: {} '.format(resp)) + raise ClientException("Unexpected response from server: {} ".format(resp)) def _get_detailed_status(resp, entity): @@ -109,28 +111,39 @@ def _get_detailed_status(resp, entity): :param entity: can be NS, NSI, or other :return: """ - if entity in ('NS', 'NSI'): + if entity in ("NS", "NSI"): # For NS and NSI, 'detailed-status' is a JSON "root" member: - return resp.get('detailed-status') + return resp.get("detailed-status") else: - ops = resp.get('_admin', {}).get('operations') - current_op = resp.get('_admin', {}).get('current_operation') + ops = resp.get("_admin", {}).get("operations") + current_op = resp.get("_admin", {}).get("current_operation") if ops and current_op is not None: # Operations are supported, verify operation index if isinstance(ops, dict) and current_op in ops: return ops[current_op].get("detailed-status") - elif isinstance(ops, list) and isinstance(current_op, int) or current_op.isdigit(): + elif ( + isinstance(ops, list) + and isinstance(current_op, int) + or current_op.isdigit() + ): current_op = int(current_op) - if current_op >= 0 and current_op < len(ops) and ops[current_op] and ops[current_op]["detailed-status"]: + if ( + current_op >= 0 + and current_op < len(ops) + and ops[current_op] + and ops[current_op]["detailed-status"] + ): return ops[current_op]["detailed-status"] # operation index is either non-numeric or out-of-range - return 'Unexpected error when getting detailed-status!' + return "Unexpected error when getting detailed-status!" else: # Operations are NOT supported - return resp.get('_admin', {}).get('detailed-status') + return resp.get("_admin", {}).get("detailed-status") -def wait_for_status(entity_label, entity_id, timeout, apiUrlStatus, http_cmd, deleteFlag=False): +def wait_for_status( + entity_label, entity_id, timeout, apiUrlStatus, http_cmd, deleteFlag=False +): """ Wait until operation ends, making polling every 5s. Prints detailed status when it changes :param entity_label: String describing the entities using '--wait': 'NS', 'NSI', 'SDNC', 'VIM', 'WIM' @@ -149,11 +162,11 @@ def wait_for_status(entity_label, entity_id, timeout, apiUrlStatus, http_cmd, de max_retries = 1 while True: try: - http_code, resp_unicode = http_cmd('{}/{}'.format(apiUrlStatus, entity_id)) + http_code, resp_unicode = http_cmd("{}/{}".format(apiUrlStatus, entity_id)) retries = 0 except NotFound: if deleteFlag: - _show_detailed_status(detailed_status, 'Deleted') + _show_detailed_status(detailed_status, "Deleted") return raise except ClientException: @@ -163,14 +176,14 @@ def wait_for_status(entity_label, entity_id, timeout, apiUrlStatus, http_cmd, de sleep(POLLING_TIME_INTERVAL) continue - resp = '' + resp = "" if resp_unicode: resp = json.loads(resp_unicode) new_detailed_status = _get_detailed_status(resp, entity_label) # print('DETAILED-STATUS: {}'.format(new_detailed_status)) if not new_detailed_status: - new_detailed_status = 'In progress' + new_detailed_status = "In progress" detailed_status = _show_detailed_status(detailed_status, new_detailed_status) # Get operation status @@ -179,5 +192,5 @@ def wait_for_status(entity_label, entity_id, timeout, apiUrlStatus, http_cmd, de if time() >= time_to_finish: # There was a timeout, so raise an exception - raise ClientException('operation timeout after {} seconds'.format(timeout)) + raise ClientException("operation timeout after {} seconds".format(timeout)) sleep(POLLING_TIME_INTERVAL)