From 6364df4cbd33a20ea1c729e0031b1aebd3fcd6df Mon Sep 17 00:00:00 2001 From: Huanxuan Ao Date: Thu, 30 Jun 2016 15:06:58 +0800 Subject: [PATCH] Add "--snapshot" option to "backup create" command in volumev2 Add "--snapshot" option to "backup create" command to support backing up a snapshot. Change-Id: Ibecbf1902599875f422d372d529835f73211d3ec Closes-Bug: #1597184 --- doc/source/command-objects/backup.rst | 4 ++++ openstackclient/tests/volume/v2/fakes.py | 1 + openstackclient/tests/volume/v2/test_backup.py | 14 +++++++++++++- openstackclient/volume/v2/backup.py | 10 ++++++++++ .../notes/bug-1597184-f4fb687b3d4d99d9.yaml | 5 +++++ 5 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/bug-1597184-f4fb687b3d4d99d9.yaml diff --git a/doc/source/command-objects/backup.rst b/doc/source/command-objects/backup.rst index 8e657b32e5..9fff393d0c 100644 --- a/doc/source/command-objects/backup.rst +++ b/doc/source/command-objects/backup.rst @@ -31,6 +31,10 @@ Create new backup Description of the backup +.. option:: --snapshot + + Snapshot to backup (name or ID) + .. option:: --force Allow to back up an in-use volume diff --git a/openstackclient/tests/volume/v2/fakes.py b/openstackclient/tests/volume/v2/fakes.py index 1cbbf68a1b..ae4c9f51ea 100644 --- a/openstackclient/tests/volume/v2/fakes.py +++ b/openstackclient/tests/volume/v2/fakes.py @@ -403,6 +403,7 @@ class FakeBackup(object): "id": 'backup-id-' + uuid.uuid4().hex, "name": 'backup-name-' + uuid.uuid4().hex, "volume_id": 'volume-id-' + uuid.uuid4().hex, + "snapshot_id": 'snapshot-id' + uuid.uuid4().hex, "description": 'description-' + uuid.uuid4().hex, "object_count": None, "container": 'container-' + uuid.uuid4().hex, diff --git a/openstackclient/tests/volume/v2/test_backup.py b/openstackclient/tests/volume/v2/test_backup.py index ba0f1c1811..b1e6594cb3 100644 --- a/openstackclient/tests/volume/v2/test_backup.py +++ b/openstackclient/tests/volume/v2/test_backup.py @@ -25,6 +25,8 @@ class TestBackup(volume_fakes.TestVolume): self.backups_mock.reset_mock() self.volumes_mock = self.app.client_manager.volume.volumes self.volumes_mock.reset_mock() + self.snapshots_mock = self.app.client_manager.volume.volume_snapshots + self.snapshots_mock.reset_mock() self.restores_mock = self.app.client_manager.volume.restores self.restores_mock.reset_mock() @@ -32,8 +34,9 @@ class TestBackup(volume_fakes.TestVolume): class TestBackupCreate(TestBackup): volume = volume_fakes.FakeVolume.create_one_volume() + snapshot = volume_fakes.FakeSnapshot.create_one_snapshot() new_backup = volume_fakes.FakeBackup.create_one_backup( - attrs={'volume_id': volume.id}) + attrs={'volume_id': volume.id, 'snapshot_id': snapshot.id}) columns = ( 'availability_zone', @@ -43,6 +46,7 @@ class TestBackupCreate(TestBackup): 'name', 'object_count', 'size', + 'snapshot_id', 'status', 'volume_id', ) @@ -54,6 +58,7 @@ class TestBackupCreate(TestBackup): new_backup.name, new_backup.object_count, new_backup.size, + new_backup.snapshot_id, new_backup.status, new_backup.volume_id, ) @@ -62,6 +67,7 @@ class TestBackupCreate(TestBackup): super(TestBackupCreate, self).setUp() self.volumes_mock.get.return_value = self.volume + self.snapshots_mock.get.return_value = self.snapshot self.backups_mock.create.return_value = self.new_backup # Get the command object to test @@ -73,6 +79,7 @@ class TestBackupCreate(TestBackup): "--description", self.new_backup.description, "--container", self.new_backup.container, "--force", + "--snapshot", self.new_backup.snapshot_id, self.new_backup.volume_id, ] verifylist = [ @@ -80,6 +87,7 @@ class TestBackupCreate(TestBackup): ("description", self.new_backup.description), ("container", self.new_backup.container), ("force", True), + ("snapshot", self.new_backup.snapshot_id), ("volume", self.new_backup.volume_id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -92,6 +100,7 @@ class TestBackupCreate(TestBackup): name=self.new_backup.name, description=self.new_backup.description, force=True, + snapshot_id=self.new_backup.snapshot_id, ) self.assertEqual(self.columns, columns) self.assertEqual(self.data, data) @@ -117,6 +126,7 @@ class TestBackupCreate(TestBackup): name=None, description=self.new_backup.description, force=False, + snapshot_id=None, ) self.assertEqual(self.columns, columns) self.assertEqual(self.data, data) @@ -264,6 +274,7 @@ class TestBackupShow(TestBackup): 'name', 'object_count', 'size', + 'snapshot_id', 'status', 'volume_id', ) @@ -275,6 +286,7 @@ class TestBackupShow(TestBackup): backup.name, backup.object_count, backup.size, + backup.snapshot_id, backup.status, backup.volume_id, ) diff --git a/openstackclient/volume/v2/backup.py b/openstackclient/volume/v2/backup.py index 519913a937..a8da40800d 100644 --- a/openstackclient/volume/v2/backup.py +++ b/openstackclient/volume/v2/backup.py @@ -48,6 +48,11 @@ class CreateBackup(command.ShowOne): metavar="", help=_("Optional backup container name") ) + parser.add_argument( + "--snapshot", + metavar="", + help=_("Snapshot to backup (name or ID)") + ) parser.add_argument( '--force', action='store_true', @@ -60,12 +65,17 @@ class CreateBackup(command.ShowOne): volume_client = self.app.client_manager.volume volume_id = utils.find_resource( volume_client.volumes, parsed_args.volume).id + snapshot_id = None + if parsed_args.snapshot: + snapshot_id = utils.find_resource( + volume_client.volume_snapshots, parsed_args.snapshot).id backup = volume_client.backups.create( volume_id, container=parsed_args.container, name=parsed_args.name, description=parsed_args.description, force=parsed_args.force, + snapshot_id=snapshot_id, ) backup._info.pop("links", None) return zip(*sorted(six.iteritems(backup._info))) diff --git a/releasenotes/notes/bug-1597184-f4fb687b3d4d99d9.yaml b/releasenotes/notes/bug-1597184-f4fb687b3d4d99d9.yaml new file mode 100644 index 0000000000..638afb0ece --- /dev/null +++ b/releasenotes/notes/bug-1597184-f4fb687b3d4d99d9.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Add ``--snapshot`` option to ``backup create`` command. + [Bug `1597184 `_]