Skip to content

Commit 5903fff

Browse files
committed
Allow wait_for_delete to work for all clients
Allow the exception and error status strings to be passed in such that other plugins can make use of this function. There is a comment in find_resource: The exception to catch here is dependent on which client library the manager passed in belongs to. Eventually this should be pulled from a common set of client exceptions. Since I think that is a long ways off, this change will work now and also work when a common exception is defined and used. Change-Id: Iab56cd1166028caed4f1e657e0b1ee81af3f48d8
1 parent 636b4de commit 5903fff

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

openstackclient/common/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ def wait_for_status(status_f,
331331
def wait_for_delete(manager,
332332
res_id,
333333
status_field='status',
334+
error_status=['error'],
335+
exception_name=['NotFound'],
334336
sleep_time=5,
335337
timeout=300,
336338
callback=None):
@@ -341,6 +343,8 @@ def wait_for_delete(manager,
341343
:param status_field: the status attribute in the returned resource object,
342344
this is used to check for error states while the resource is being
343345
deleted
346+
:param error_status: a list of status strings for error
347+
:param exception_name: a list of exception strings for deleted case
344348
:param sleep_time: wait this long between checks (seconds)
345349
:param timeout: check until this long (seconds)
346350
:param callback: called per sleep cycle, useful to display progress; this
@@ -357,12 +361,12 @@ def wait_for_delete(manager,
357361
# handle a NotFound exception here without parsing the message
358362
res = manager.get(res_id)
359363
except Exception as ex:
360-
if type(ex).__name__ == 'NotFound':
364+
if type(ex).__name__ in exception_name:
361365
return True
362366
raise
363367

364368
status = getattr(res, status_field, '').lower()
365-
if status == 'error':
369+
if status in error_status:
366370
return False
367371

368372
if callback:

openstackclient/tests/common/test_utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,28 @@ def test_wait_for_delete_error(self, mock_sleep):
212212
self.assertFalse(utils.wait_for_delete(manager, res_id))
213213
self.assertFalse(mock_sleep.called)
214214

215+
@mock.patch.object(time, 'sleep')
216+
def test_wait_for_delete_error_with_overrides(self, mock_sleep):
217+
# Tests that we fail if the resource is my_status=failed
218+
resource = mock.MagicMock(my_status='FAILED')
219+
mock_get = mock.Mock(return_value=resource)
220+
manager = mock.MagicMock(get=mock_get)
221+
res_id = str(uuid.uuid4())
222+
self.assertFalse(utils.wait_for_delete(manager, res_id,
223+
status_field='my_status',
224+
error_status=['failed']))
225+
self.assertFalse(mock_sleep.called)
226+
227+
@mock.patch.object(time, 'sleep')
228+
def test_wait_for_delete_error_with_overrides_exception(self, mock_sleep):
229+
# Tests that we succeed if the resource is specific exception
230+
mock_get = mock.Mock(side_effect=Exception)
231+
manager = mock.MagicMock(get=mock_get)
232+
res_id = str(uuid.uuid4())
233+
self.assertTrue(utils.wait_for_delete(manager, res_id,
234+
exception_name=['Exception']))
235+
self.assertFalse(mock_sleep.called)
236+
215237
def test_build_kwargs_dict_value_set(self):
216238
self.assertEqual({'arg_bla': 'bla'},
217239
utils.build_kwargs_dict('arg_bla', 'bla'))

0 commit comments

Comments
 (0)