summaryrefslogtreecommitdiff
path: root/openstackclient/common/utils.py
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2015-06-01 20:07:27 +0000
committerGerrit Code Review <review@openstack.org>2015-06-01 20:07:27 +0000
commit4d57e9f62a6d49fb7114ea718dda166f349d9341 (patch)
tree1d89091f385113f1771806a8e436f65a26bbfed6 /openstackclient/common/utils.py
parent40634c3f2cec4c23e2123b3ff5cdc447f26cf977 (diff)
parent224d375ef4120998dc51fbf55f1778d1ccf118a0 (diff)
downloadpython-openstackclient-4d57e9f62a6d49fb7114ea718dda166f349d9341.tar.gz
Merge "Add --wait to server delete"
Diffstat (limited to 'openstackclient/common/utils.py')
-rw-r--r--openstackclient/common/utils.py46
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