summaryrefslogtreecommitdiff
path: root/openstackclient
diff options
context:
space:
mode:
authorHuanxuan Ao <huanxuan.ao@easystack.cn>2016-08-16 10:52:26 +0800
committerHuanxuan Ao <huanxuan.ao@easystack.cn>2016-09-22 13:55:12 +0800
commitddf84429f297b34ce7067250d834ea897e37f37c (patch)
tree2a10b2f1b65b9f8b4ae595553039871064916c18 /openstackclient
parent69c4f605ecce597af3a574d2f7c27c1dbaa17989 (diff)
downloadpython-openstackclient-ddf84429f297b34ce7067250d834ea897e37f37c.tar.gz
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
Diffstat (limited to 'openstackclient')
-rw-r--r--openstackclient/tests/unit/volume/v2/test_backup.py69
-rw-r--r--openstackclient/volume/v2/backup.py59
2 files changed, 128 insertions, 0 deletions
diff --git a/openstackclient/tests/unit/volume/v2/test_backup.py b/openstackclient/tests/unit/volume/v2/test_backup.py
index 45633870..306c9eb3 100644
--- a/openstackclient/tests/unit/volume/v2/test_backup.py
+++ b/openstackclient/tests/unit/volume/v2/test_backup.py
@@ -336,6 +336,75 @@ class TestBackupRestore(TestBackup):
self.assertIsNone(result)
+class TestBackupSet(TestBackup):
+
+ backup = volume_fakes.FakeBackup.create_one_backup()
+
+ def setUp(self):
+ super(TestBackupSet, self).setUp()
+
+ self.backups_mock.get.return_value = self.backup
+
+ # Get the command object to test
+ self.cmd = backup.SetVolumeBackup(self.app, None)
+
+ def test_backup_set_name(self):
+ arglist = [
+ '--name', 'new_name',
+ self.backup.id,
+ ]
+ verifylist = [
+ ('name', 'new_name'),
+ ('backup', self.backup.id),
+ ]
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ # In base command class ShowOne in cliff, abstract method take_action()
+ # returns nothing
+ result = self.cmd.take_action(parsed_args)
+ self.backups_mock.update.assert_called_once_with(
+ self.backup.id, **{'name': 'new_name'})
+ self.assertIsNone(result)
+
+ def test_backup_set_state(self):
+ arglist = [
+ '--state', 'error',
+ self.backup.id
+ ]
+ verifylist = [
+ ('state', 'error'),
+ ('backup', self.backup.id)
+ ]
+
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ result = self.cmd.take_action(parsed_args)
+ self.backups_mock.reset_state.assert_called_once_with(
+ self.backup.id, 'error')
+ self.assertIsNone(result)
+
+ def test_backup_set_state_failed(self):
+ self.backups_mock.reset_state.side_effect = exceptions.CommandError()
+ arglist = [
+ '--state', 'error',
+ self.backup.id
+ ]
+ verifylist = [
+ ('state', 'error'),
+ ('backup', self.backup.id)
+ ]
+
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+ try:
+ self.cmd.take_action(parsed_args)
+ self.fail('CommandError should be raised.')
+ except exceptions.CommandError as e:
+ self.assertEqual('One or more of the set operations failed',
+ str(e))
+ self.backups_mock.reset_state.assert_called_with(
+ self.backup.id, 'error')
+
+
class TestBackupShow(TestBackup):
backup = volume_fakes.FakeBackup.create_one_backup()
diff --git a/openstackclient/volume/v2/backup.py b/openstackclient/volume/v2/backup.py
index 07c1c94f..4a133d36 100644
--- a/openstackclient/volume/v2/backup.py
+++ b/openstackclient/volume/v2/backup.py
@@ -281,6 +281,65 @@ class RestoreBackup(RestoreVolumeBackup):
return super(RestoreBackup, self).take_action(parsed_args)
+class SetVolumeBackup(command.Command):
+ """Set volume backup properties"""
+
+ def get_parser(self, prog_name):
+ parser = super(SetVolumeBackup, self).get_parser(prog_name)
+ parser.add_argument(
+ "backup",
+ metavar="<backup>",
+ help=_("Backup to modify (name or ID)")
+ )
+ parser.add_argument(
+ '--name',
+ metavar='<name>',
+ help=_('New backup name')
+ )
+ parser.add_argument(
+ '--description',
+ metavar='<description>',
+ help=_('New backup description')
+ )
+ parser.add_argument(
+ '--state',
+ metavar='<state>',
+ choices=['available', 'error'],
+ help=_('New backup state ("available" or "error") (admin only)'),
+ )
+ return parser
+
+ def take_action(self, parsed_args):
+ volume_client = self.app.client_manager.volume
+ backup = utils.find_resource(volume_client.backups,
+ parsed_args.backup)
+ result = 0
+ if parsed_args.state:
+ try:
+ volume_client.backups.reset_state(
+ backup.id, parsed_args.state)
+ except Exception as e:
+ LOG.error(_("Failed to set backup state: %s"), e)
+ result += 1
+
+ kwargs = {}
+ if parsed_args.name:
+ kwargs['name'] = parsed_args.name
+ if parsed_args.description:
+ kwargs['description'] = parsed_args.description
+ if kwargs:
+ try:
+ volume_client.backups.update(backup.id, **kwargs)
+ except Exception as e:
+ LOG.error(_("Failed to update backup name "
+ "or description: %s"), e)
+ result += 1
+
+ if result > 0:
+ raise exceptions.CommandError(_("One or more of the "
+ "set operations failed"))
+
+
class ShowVolumeBackup(command.ShowOne):
"""Display volume backup details"""