diff options
| author | Matt Riedemann <mriedem@us.ibm.com> | 2015-05-29 11:55:09 -0700 |
|---|---|---|
| committer | Matt Riedemann <mriedem@us.ibm.com> | 2015-05-29 14:32:51 -0700 |
| commit | 224d375ef4120998dc51fbf55f1778d1ccf118a0 (patch) | |
| tree | 0347f83242d39d28c8164c860e4d9c834fff4337 /openstackclient/common/utils.py | |
| parent | 211c14c638b9bf393932be42d4f04a4dd12a84bc (diff) | |
| download | python-openstackclient-224d375ef4120998dc51fbf55f1778d1ccf118a0.tar.gz | |
Add --wait to server delete
This allows the server delete command to wait for the server to be
deleted (obviously).
The wait method is the same model that Tempest uses, i.e. wait for a 404
on server GET (successful deletion), fail if the server went to ERROR
status, or fail if a timeout is reached. The default timeout of 300
seconds is also what Tempest uses.
Closes-Bug: #1460112
Change-Id: I0e66c400903e82832944d1cad61e7eb30177c3e8
Diffstat (limited to 'openstackclient/common/utils.py')
| -rw-r--r-- | openstackclient/common/utils.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/openstackclient/common/utils.py b/openstackclient/common/utils.py index 4139770c..aad0519c 100644 --- a/openstackclient/common/utils.py +++ b/openstackclient/common/utils.py @@ -283,6 +283,52 @@ def wait_for_status(status_f, return retval +def wait_for_delete(manager, + res_id, + status_field='status', + sleep_time=5, + timeout=300, + callback=None): + """Wait for resource deletion + + :param res_id: the resource id to watch + :param status_field: the status attribute in the returned resource object, + this is used to check for error states while the resource is being + deleted + :param sleep_time: wait this long between checks (seconds) + :param timeout: check until this long (seconds) + :param callback: called per sleep cycle, useful to display progress; this + function is passed a progress value during each iteration of the wait + loop + :rtype: True on success, False if the resource has gone to error state or + the timeout has been reached + """ + total_time = 0 + while total_time < timeout: + try: + # might not be a bad idea to re-use find_resource here if it was + # a bit more friendly in the exceptions it raised so we could just + # handle a NotFound exception here without parsing the message + res = manager.get(res_id) + except Exception as ex: + if type(ex).__name__ == 'NotFound': + return True + raise + + status = getattr(res, status_field, '').lower() + if status == 'error': + return False + + if callback: + progress = getattr(res, 'progress', None) or 0 + callback(progress) + time.sleep(sleep_time) + total_time += sleep_time + + # if we got this far we've timed out + return False + + def get_effective_log_level(): """Returns the lowest logging level considered by logging handlers |
