Skip to content

Commit 7357b24

Browse files
Huanxuan Aoranasheel2000
andcommitted
Add "--remote-source" option to "volume snapshot create" command
Add "--remote-source" option to "volume snapshot create" command to support creating snapshot from an existing remote snapshot in volume v2 (v2 only), also add the doc, unit tests and release note. Change-Id: I9e5fad4f0db5b44d528eb6b930edbc816e392c3a Implements: bp cinder-command-support Closes-Bug: #1618676 Co-Authored-By: Sheel Rana <ranasheel2000@gmail.com>
1 parent 29587ea commit 7357b24

4 files changed

Lines changed: 74 additions & 7 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Create new volume snapshot
1717
[--description <description>]
1818
[--force]
1919
[--property <key=value> [...] ]
20+
[--remote-source <key=value> [...]]
2021
<snapshot-name>
2122
2223
.. option:: --volume <volume>
@@ -37,6 +38,14 @@ Create new volume snapshot
3738
3839
*Volume version 2 only*
3940
41+
.. option:: --remote-source <key=value>
42+
43+
The attribute(s) of the exsiting remote volume snapshot
44+
(admin required) (repeat option to specify multiple attributes)
45+
e.g.: '--remote-source source-name=test_name --remote-source source-id=test_id'
46+
47+
*Volume version 2 only*
48+
4049
.. _volume_snapshot_create-snapshot-name:
4150
.. describe:: <snapshot-name>
4251

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def setUp(self):
6767

6868
self.volumes_mock.get.return_value = self.volume
6969
self.snapshots_mock.create.return_value = self.new_snapshot
70+
self.snapshots_mock.manage.return_value = self.new_snapshot
7071
# Get the command object to test
7172
self.cmd = volume_snapshot.CreateVolumeSnapshot(self.app, None)
7273

@@ -152,6 +153,33 @@ def test_snapshot_create_without_volume(self):
152153
self.assertEqual(self.columns, columns)
153154
self.assertEqual(self.data, data)
154155

156+
def test_snapshot_create_without_remote_source(self):
157+
arglist = [
158+
'--remote-source', 'source-name=test_source_name',
159+
'--remote-source', 'source-id=test_source_id',
160+
'--volume', self.new_snapshot.volume_id,
161+
]
162+
ref_dict = {'source-name': 'test_source_name',
163+
'source-id': 'test_source_id'}
164+
verifylist = [
165+
('remote_source', ref_dict),
166+
('volume', self.new_snapshot.volume_id),
167+
]
168+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
169+
170+
columns, data = self.cmd.take_action(parsed_args)
171+
172+
self.snapshots_mock.manage.assert_called_with(
173+
volume_id=self.new_snapshot.volume_id,
174+
ref=ref_dict,
175+
name=None,
176+
description=None,
177+
metadata=None,
178+
)
179+
self.snapshots_mock.create.assert_not_called()
180+
self.assertEqual(self.columns, columns)
181+
self.assertEqual(self.data, data)
182+
155183

156184
class TestSnapshotDelete(TestSnapshot):
157185

openstackclient/volume/v2/volume_snapshot.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ def get_parser(self, prog_name):
6565
help=_("Set a property to this snapshot "
6666
"(repeat option to set multiple properties)"),
6767
)
68+
parser.add_argument(
69+
"--remote-source",
70+
metavar="<key=value>",
71+
action=parseractions.KeyValueAction,
72+
help=_("The attribute(s) of the exsiting remote volume snapshot "
73+
"(admin required) (repeat option to specify multiple "
74+
"attributes) e.g.: '--remote-source source-name=test_name "
75+
"--remote-source source-id=test_id'"),
76+
)
6877
return parser
6978

7079
def take_action(self, parsed_args):
@@ -74,13 +83,29 @@ def take_action(self, parsed_args):
7483
volume = parsed_args.snapshot_name
7584
volume_id = utils.find_resource(
7685
volume_client.volumes, volume).id
77-
snapshot = volume_client.volume_snapshots.create(
78-
volume_id,
79-
force=parsed_args.force,
80-
name=parsed_args.snapshot_name,
81-
description=parsed_args.description,
82-
metadata=parsed_args.property,
83-
)
86+
if parsed_args.remote_source:
87+
# Create a new snapshot from an existing remote snapshot source
88+
if parsed_args.force:
89+
msg = (_("'--force' option will not work when you create "
90+
"new volume snapshot from an existing remote "
91+
"volume snapshot"))
92+
LOG.warning(msg)
93+
snapshot = volume_client.volume_snapshots.manage(
94+
volume_id=volume_id,
95+
ref=parsed_args.remote_source,
96+
name=parsed_args.snapshot_name,
97+
description=parsed_args.description,
98+
metadata=parsed_args.property,
99+
)
100+
else:
101+
# create a new snapshot from scratch
102+
snapshot = volume_client.volume_snapshots.create(
103+
volume_id,
104+
force=parsed_args.force,
105+
name=parsed_args.snapshot_name,
106+
description=parsed_args.description,
107+
metadata=parsed_args.property,
108+
)
84109
snapshot._info.update(
85110
{'properties': utils.format_dict(snapshot._info.pop('metadata'))}
86111
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- Add ``--remote-source`` option to ``volume snapshot create`` command to support
4+
creating volume snapshot from an existing remote volume snapshot in volume v2.
5+
[Bug `1618676 <https://bugs.launchpad.net/python-openstackclient/+bug/1618676>`_]

0 commit comments

Comments
 (0)