-
Notifications
You must be signed in to change notification settings - Fork 0
/
waiters.py
120 lines (113 loc) · 5.1 KB
/
waiters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import time
def get_value(value, key, client):
"""
decide how to handle returned data from client commands
two options: returned dictionary or returned object.
we change the returned data to a dictionay
"""
if client is None:
return value.__dict__[key]
elif "glance" in str(client):
return value[key]
elif "cinder" in str(client):
return value.__dict__[key]
elif "nova" in str(client):
return value.__dict__[key]
class Waiter(object):
RETRY_COUNT = 15
SLEEP_BETWEEN_RETRY = 3
PASS = 1
FAIL = 0
@classmethod
def wait_for_resource_status(cls, function, waiter_id, status, message="",
client=None, retry_count=RETRY_COUNT,
sleep_between_retry=SLEEP_BETWEEN_RETRY,
**function_args):
"""
:param function: caller function
:param waiter_id: the resource id we are waiting for
:param status: the resource expected status
:param function_args: the args the function sends
:param message: print a message
:param client: the resource client
:param retry_count: how many time to call to function
:param sleep_between_retry: sleep time between calls.
:param **function_args -> all params that sent to function
:return pass or fail:
"""
start_count = 0
while start_count < retry_count:
# we assume that we asked for a resource_id -
# returned the resources
# example: glance.images.get("image_id") will return the value
# as a dictionary
resource = function(**function_args)
if not function_args:
# running a list command
for value in resource:
value_id = get_value(value, 'id', client)
if value_id == waiter_id:
print message + " found waiter id: {0}\n"\
.format(waiter_id)
if get_value(value, "status", client) == status:
print message + " status is: {0}\n".format(status)
return cls.PASS
elif get_value(value, "status", client) == "error":
print message + "status is: {0}\n".format("error")
return cls.FAIL
else:
# assume user asked for get_command
if get_value(resource, 'id', client) == waiter_id:
print message + " found waiter id: {0} ".\
format(waiter_id)
if get_value(resource, "status", client) == status:
print message + " status is: {0}".format(status)
return cls.PASS
elif get_value(resource, "status", client) == "error":
print message + "status is: {0}\n".format("error")
return cls.FAIL
time.sleep(sleep_between_retry)
start_count += 1
raise RuntimeError("Timeout expires")
@classmethod
def wait_for_resource_deletion(cls, function, waiter_id, message="",
client=None, retry_count=RETRY_COUNT,
sleep_between_retry=SLEEP_BETWEEN_RETRY,
**function_args):
"""
:param function: caller function
:param waiter_id: the resource id we are waiting for deletion
:param status: the resource expected status
:param function_args: the args the function sends
:param message: print a message
:param client: the resource client
:param retry_count: how many time to call to function
:param sleep_between_retry: sleep time between calls.
:param **function_args -> all params that sent to function
:return pass or fail:
"""
start_count = 0
while start_count < retry_count:
# we assume that we asked for a resource_id -returned the resource
# example: glance.images.get("image_id") will return the value as
# a dictionary
resource = function(**function_args)
if not function_args:
# running a list command
deleted = True
for value in resource:
value_id = get_value(value, 'id', client)
if value_id == waiter_id:
deleted = False
print message + " found waiter id: {0}\n"\
.format(waiter_id)
if deleted:
return cls.PASS
else:
# assume user asked for get_command
if get_value(resource, 'id', client) == waiter_id:
print message + " found waiter id: {0} "\
.format(waiter_id)
time.sleep(sleep_between_retry)
start_count += 1
raise RuntimeError("Timeout expires")