Skip to content

Commit 30afdb9

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Error handling of multi REST API calls for "snapshot set" command"
2 parents cc5379b + 10e665a commit 30afdb9

3 files changed

Lines changed: 94 additions & 10 deletions

File tree

openstackclient/tests/unit/volume/v2/test_snapshot.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,55 @@ def test_snapshot_set_state_to_error(self):
376376
self.snapshot.id, "error")
377377
self.assertIsNone(result)
378378

379+
def test_volume_set_state_failed(self):
380+
self.snapshots_mock.reset_state.side_effect = exceptions.CommandError()
381+
arglist = [
382+
'--state', 'error',
383+
self.snapshot.id
384+
]
385+
verifylist = [
386+
('state', 'error'),
387+
('snapshot', self.snapshot.id)
388+
]
389+
390+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
391+
try:
392+
self.cmd.take_action(parsed_args)
393+
self.fail('CommandError should be raised.')
394+
except exceptions.CommandError as e:
395+
self.assertEqual('One or more of the set operations failed',
396+
str(e))
397+
self.snapshots_mock.reset_state.assert_called_once_with(
398+
self.snapshot.id, 'error')
399+
400+
def test_volume_set_name_and_state_failed(self):
401+
self.snapshots_mock.reset_state.side_effect = exceptions.CommandError()
402+
arglist = [
403+
'--state', 'error',
404+
"--name", "new_snapshot",
405+
self.snapshot.id
406+
]
407+
verifylist = [
408+
('state', 'error'),
409+
("name", "new_snapshot"),
410+
('snapshot', self.snapshot.id)
411+
]
412+
413+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
414+
try:
415+
self.cmd.take_action(parsed_args)
416+
self.fail('CommandError should be raised.')
417+
except exceptions.CommandError as e:
418+
self.assertEqual('One or more of the set operations failed',
419+
str(e))
420+
kwargs = {
421+
"name": "new_snapshot",
422+
}
423+
self.snapshots_mock.update.assert_called_once_with(
424+
self.snapshot.id, **kwargs)
425+
self.snapshots_mock.reset_state.assert_called_once_with(
426+
self.snapshot.id, 'error')
427+
379428

380429
class TestSnapshotShow(TestSnapshot):
381430

openstackclient/volume/v1/snapshot.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,31 @@ def take_action(self, parsed_args):
218218
snapshot = utils.find_resource(volume_client.volume_snapshots,
219219
parsed_args.snapshot)
220220

221+
result = 0
221222
if parsed_args.property:
222-
volume_client.volume_snapshots.set_metadata(snapshot.id,
223-
parsed_args.property)
223+
try:
224+
volume_client.volume_snapshots.set_metadata(
225+
snapshot.id, parsed_args.property)
226+
except Exception as e:
227+
LOG.error(_("Failed to set snapshot property: %s"), e)
228+
result += 1
224229

225230
kwargs = {}
226231
if parsed_args.name:
227232
kwargs['display_name'] = parsed_args.name
228233
if parsed_args.description:
229234
kwargs['display_description'] = parsed_args.description
235+
if kwargs:
236+
try:
237+
snapshot.update(**kwargs)
238+
except Exception as e:
239+
LOG.error(_("Failed to update snapshot display name "
240+
"or display description: %s"), e)
241+
result += 1
230242

231-
snapshot.update(**kwargs)
243+
if result > 0:
244+
raise exceptions.CommandError(_("One or more of the "
245+
"set operations failed"))
232246

233247

234248
class ShowSnapshot(command.ShowOne):

openstackclient/volume/v2/snapshot.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,40 @@ def take_action(self, parsed_args):
240240
snapshot = utils.find_resource(volume_client.volume_snapshots,
241241
parsed_args.snapshot)
242242

243+
result = 0
244+
if parsed_args.property:
245+
try:
246+
volume_client.volume_snapshots.set_metadata(
247+
snapshot.id, parsed_args.property)
248+
except Exception as e:
249+
LOG.error(_("Failed to set snapshot property: %s"), e)
250+
result += 1
251+
252+
if parsed_args.state:
253+
try:
254+
volume_client.volume_snapshots.reset_state(
255+
snapshot.id, parsed_args.state)
256+
except Exception as e:
257+
LOG.error(_("Failed to set snapshot state: %s"), e)
258+
result += 1
259+
243260
kwargs = {}
244261
if parsed_args.name:
245262
kwargs['name'] = parsed_args.name
246263
if parsed_args.description:
247264
kwargs['description'] = parsed_args.description
265+
if kwargs:
266+
try:
267+
volume_client.volume_snapshots.update(
268+
snapshot.id, **kwargs)
269+
except Exception as e:
270+
LOG.error(_("Failed to update snapshot name "
271+
"or description: %s"), e)
272+
result += 1
248273

249-
if parsed_args.property:
250-
volume_client.volume_snapshots.set_metadata(snapshot.id,
251-
parsed_args.property)
252-
if parsed_args.state:
253-
volume_client.volume_snapshots.reset_state(snapshot.id,
254-
parsed_args.state)
255-
volume_client.volume_snapshots.update(snapshot.id, **kwargs)
274+
if result > 0:
275+
raise exceptions.CommandError(_("One or more of the "
276+
"set operations failed"))
256277

257278

258279
class ShowSnapshot(command.ShowOne):

0 commit comments

Comments
 (0)