Skip to content

Commit 10e665a

Browse files
author
Huanxuan Ao
committed
Error handling of multi REST API calls for "snapshot set" command
Support multi REST API calls error handling for "snapshot set" command follow the rule in doc/source/command-errors.rst. Also add a unit test for testing the error handling Change-Id: I0c6214271bc54a25b051c0a62438c3344c8b51d7
1 parent 676a0e9 commit 10e665a

3 files changed

Lines changed: 100 additions & 11 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: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@
1616
"""Volume v1 Snapshot action implementations"""
1717

1818
import copy
19+
import logging
1920

2021
from osc_lib.cli import parseractions
2122
from osc_lib.command import command
23+
from osc_lib import exceptions
2224
from osc_lib import utils
2325
import six
2426

2527
from openstackclient.i18n import _
2628

2729

30+
LOG = logging.getLogger(__name__)
31+
32+
2833
class CreateSnapshot(command.ShowOne):
2934
"""Create new snapshot"""
3035

@@ -199,17 +204,31 @@ def take_action(self, parsed_args):
199204
snapshot = utils.find_resource(volume_client.volume_snapshots,
200205
parsed_args.snapshot)
201206

207+
result = 0
202208
if parsed_args.property:
203-
volume_client.volume_snapshots.set_metadata(snapshot.id,
204-
parsed_args.property)
209+
try:
210+
volume_client.volume_snapshots.set_metadata(
211+
snapshot.id, parsed_args.property)
212+
except Exception as e:
213+
LOG.error(_("Failed to set snapshot property: %s"), e)
214+
result += 1
205215

206216
kwargs = {}
207217
if parsed_args.name:
208218
kwargs['display_name'] = parsed_args.name
209219
if parsed_args.description:
210220
kwargs['display_description'] = parsed_args.description
211-
212-
snapshot.update(**kwargs)
221+
if kwargs:
222+
try:
223+
snapshot.update(**kwargs)
224+
except Exception as e:
225+
LOG.error(_("Failed to update snapshot display name "
226+
"or display description: %s"), e)
227+
result += 1
228+
229+
if result > 0:
230+
raise exceptions.CommandError(_("One or more of the "
231+
"set operations failed"))
213232

214233

215234
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)