Skip to content

Commit ddf8442

Browse files
author
Huanxuan Ao
committed
Add "volume backup set" command in volume v2
Add "volume backup set" command in volume v2 (v2 only) to set backup name, description and state Change-Id: If17e8457db9a4704fb5bb9c75921ed82fd0069cf Closes-Bug: #1613261
1 parent 69c4f60 commit ddf8442

5 files changed

Lines changed: 164 additions & 0 deletions

File tree

doc/source/command-objects/volume-backup.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,37 @@ Restore volume backup
115115

116116
Volume to restore to (name or ID)
117117

118+
volume backup set
119+
-----------------
120+
121+
Set volume backup properties
122+
123+
.. program:: volume backup set
124+
.. code:: bash
125+
126+
os volume backup set
127+
[--name <name>]
128+
[--description <description>]
129+
[--state <state>]
130+
<backup>
131+
132+
.. option:: --name <name>
133+
134+
New backup name
135+
136+
.. option:: --description <description>
137+
138+
New backup description
139+
140+
.. option:: --state <state>
141+
142+
New backup state ("available" or "error") (admin only)
143+
144+
.. _backup_set-volume-backup:
145+
.. describe:: <backup>
146+
147+
Backup to modify (name or ID)
148+
118149
volume backup show
119150
------------------
120151

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,75 @@ def test_backup_restore(self):
336336
self.assertIsNone(result)
337337

338338

339+
class TestBackupSet(TestBackup):
340+
341+
backup = volume_fakes.FakeBackup.create_one_backup()
342+
343+
def setUp(self):
344+
super(TestBackupSet, self).setUp()
345+
346+
self.backups_mock.get.return_value = self.backup
347+
348+
# Get the command object to test
349+
self.cmd = backup.SetVolumeBackup(self.app, None)
350+
351+
def test_backup_set_name(self):
352+
arglist = [
353+
'--name', 'new_name',
354+
self.backup.id,
355+
]
356+
verifylist = [
357+
('name', 'new_name'),
358+
('backup', self.backup.id),
359+
]
360+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
361+
362+
# In base command class ShowOne in cliff, abstract method take_action()
363+
# returns nothing
364+
result = self.cmd.take_action(parsed_args)
365+
self.backups_mock.update.assert_called_once_with(
366+
self.backup.id, **{'name': 'new_name'})
367+
self.assertIsNone(result)
368+
369+
def test_backup_set_state(self):
370+
arglist = [
371+
'--state', 'error',
372+
self.backup.id
373+
]
374+
verifylist = [
375+
('state', 'error'),
376+
('backup', self.backup.id)
377+
]
378+
379+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
380+
381+
result = self.cmd.take_action(parsed_args)
382+
self.backups_mock.reset_state.assert_called_once_with(
383+
self.backup.id, 'error')
384+
self.assertIsNone(result)
385+
386+
def test_backup_set_state_failed(self):
387+
self.backups_mock.reset_state.side_effect = exceptions.CommandError()
388+
arglist = [
389+
'--state', 'error',
390+
self.backup.id
391+
]
392+
verifylist = [
393+
('state', 'error'),
394+
('backup', self.backup.id)
395+
]
396+
397+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
398+
try:
399+
self.cmd.take_action(parsed_args)
400+
self.fail('CommandError should be raised.')
401+
except exceptions.CommandError as e:
402+
self.assertEqual('One or more of the set operations failed',
403+
str(e))
404+
self.backups_mock.reset_state.assert_called_with(
405+
self.backup.id, 'error')
406+
407+
339408
class TestBackupShow(TestBackup):
340409

341410
backup = volume_fakes.FakeBackup.create_one_backup()

openstackclient/volume/v2/backup.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,65 @@ def take_action(self, parsed_args):
281281
return super(RestoreBackup, self).take_action(parsed_args)
282282

283283

284+
class SetVolumeBackup(command.Command):
285+
"""Set volume backup properties"""
286+
287+
def get_parser(self, prog_name):
288+
parser = super(SetVolumeBackup, self).get_parser(prog_name)
289+
parser.add_argument(
290+
"backup",
291+
metavar="<backup>",
292+
help=_("Backup to modify (name or ID)")
293+
)
294+
parser.add_argument(
295+
'--name',
296+
metavar='<name>',
297+
help=_('New backup name')
298+
)
299+
parser.add_argument(
300+
'--description',
301+
metavar='<description>',
302+
help=_('New backup description')
303+
)
304+
parser.add_argument(
305+
'--state',
306+
metavar='<state>',
307+
choices=['available', 'error'],
308+
help=_('New backup state ("available" or "error") (admin only)'),
309+
)
310+
return parser
311+
312+
def take_action(self, parsed_args):
313+
volume_client = self.app.client_manager.volume
314+
backup = utils.find_resource(volume_client.backups,
315+
parsed_args.backup)
316+
result = 0
317+
if parsed_args.state:
318+
try:
319+
volume_client.backups.reset_state(
320+
backup.id, parsed_args.state)
321+
except Exception as e:
322+
LOG.error(_("Failed to set backup state: %s"), e)
323+
result += 1
324+
325+
kwargs = {}
326+
if parsed_args.name:
327+
kwargs['name'] = parsed_args.name
328+
if parsed_args.description:
329+
kwargs['description'] = parsed_args.description
330+
if kwargs:
331+
try:
332+
volume_client.backups.update(backup.id, **kwargs)
333+
except Exception as e:
334+
LOG.error(_("Failed to update backup name "
335+
"or description: %s"), e)
336+
result += 1
337+
338+
if result > 0:
339+
raise exceptions.CommandError(_("One or more of the "
340+
"set operations failed"))
341+
342+
284343
class ShowVolumeBackup(command.ShowOne):
285344
"""Display volume backup details"""
286345

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
features:
3+
- Add ``volume backup set`` commands in volume v2.
4+
[Bug `1613261 <https://bugs.launchpad.net/python-openstackclient/+bug/1613261>`_]

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ openstack.volume.v2 =
512512
volume_backup_delete = openstackclient.volume.v2.backup:DeleteVolumeBackup
513513
volume_backup_list = openstackclient.volume.v2.backup:ListVolumeBackup
514514
volume_backup_restore = openstackclient.volume.v2.backup:RestoreVolumeBackup
515+
volume_backup_set = openstackclient.volume.v2.backup:SetVolumeBackup
515516
volume_backup_show = openstackclient.volume.v2.backup:ShowVolumeBackup
516517

517518
volume_type_create = openstackclient.volume.v2.volume_type:CreateVolumeType

0 commit comments

Comments
 (0)