Skip to content

Commit 40ec7a9

Browse files
author
zhiyong.dai
committed
Support --no-property in "volume set" command
Add "--no-property" option to "volume set" command in v1 and v2 and update the test cases. Change-Id: Id5660f23b3b2d9aa72f4c16b19ce83f3f7ed2fa4
1 parent 3b562ff commit 40ec7a9

8 files changed

Lines changed: 95 additions & 11 deletions

File tree

doc/source/command-objects/volume.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ Set volume properties
269269
[--name <name>]
270270
[--size <size>]
271271
[--description <description>]
272+
[--no-property]
272273
[--property <key=value> [...] ]
273274
[--image-property <key=value> [...] ]
274275
[--state <state>]
@@ -290,6 +291,12 @@ Set volume properties
290291
291292
New volume description
292293
294+
.. option:: --no-property
295+
296+
Remove all properties from :ref:`\<volume\> <volume_set-volume>`
297+
(specify both :option:`--no-property` and :option:`--property` to
298+
remove the current properties before setting new properties.)
299+
293300
.. option:: --property <key=value>
294301
295302
Set a property on this volume (repeat option to set multiple properties)
@@ -304,7 +311,7 @@ Set volume properties
304311
305312
Migration policy while re-typing volume
306313
("never" or "on-demand", default is "never" )
307-
(available only when "--type" option is specified)
314+
(available only when :option:`--type` option is specified)
308315
309316
*Volume version 2 only*
310317

openstackclient/tests/functional/volume/v1/test_volume.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ def test_volume_set_and_unset(self):
141141
'--name ' + new_name +
142142
' --size 2 ' +
143143
'--description bbbb ' +
144-
'--property Alpha=c ' +
144+
'--no-property ' +
145145
'--property Beta=b ' +
146+
'--property Gamma=c ' +
146147
'--bootable ' +
147148
name,
148149
)
@@ -165,7 +166,7 @@ def test_volume_set_and_unset(self):
165166
cmd_output["display_description"],
166167
)
167168
self.assertEqual(
168-
"Alpha='c', Beta='b'",
169+
"Beta='b', Gamma='c'",
169170
cmd_output["properties"],
170171
)
171172
self.assertEqual(
@@ -176,7 +177,7 @@ def test_volume_set_and_unset(self):
176177
# Test volume unset
177178
raw_output = self.openstack(
178179
'volume unset ' +
179-
'--property Alpha ' +
180+
'--property Beta ' +
180181
new_name,
181182
)
182183
self.assertOutput('', raw_output)
@@ -186,7 +187,7 @@ def test_volume_set_and_unset(self):
186187
new_name
187188
))
188189
self.assertEqual(
189-
"Beta='b'",
190+
"Gamma='c'",
190191
cmd_output["properties"],
191192
)
192193

openstackclient/tests/functional/volume/v2/test_volume.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def test_volume_list(self):
104104
# TODO(qiangjiahui): Add project option to filter tests when we can
105105
# specify volume with project
106106

107-
def test_volume_set(self):
107+
def test_volume_set_and_unset(self):
108108
"""Tests create volume, set, unset, show, delete"""
109109
name = uuid.uuid4().hex
110110
new_name = name + "_"
@@ -144,8 +144,11 @@ def test_volume_set(self):
144144
'--name ' + new_name +
145145
' --size 2 ' +
146146
'--description bbbb ' +
147-
'--property Alpha=c ' +
147+
'--no-property ' +
148148
'--property Beta=b ' +
149+
'--property Gamma=c ' +
150+
'--image-property a=b ' +
151+
'--image-property c=d ' +
149152
'--bootable ' +
150153
name,
151154
)
@@ -168,9 +171,13 @@ def test_volume_set(self):
168171
cmd_output["description"],
169172
)
170173
self.assertEqual(
171-
"Alpha='c', Beta='b'",
174+
"Beta='b', Gamma='c'",
172175
cmd_output["properties"],
173176
)
177+
self.assertEqual(
178+
{'a': 'b', 'c': 'd'},
179+
cmd_output["volume_image_metadata"],
180+
)
174181
self.assertEqual(
175182
'true',
176183
cmd_output["bootable"],
@@ -179,7 +186,8 @@ def test_volume_set(self):
179186
# Test volume unset
180187
raw_output = self.openstack(
181188
'volume unset ' +
182-
'--property Alpha ' +
189+
'--property Beta ' +
190+
'--image-property a ' +
183191
new_name,
184192
)
185193
self.assertOutput('', raw_output)
@@ -189,9 +197,13 @@ def test_volume_set(self):
189197
new_name
190198
))
191199
self.assertEqual(
192-
"Beta='b'",
200+
"Gamma='c'",
193201
cmd_output["properties"],
194202
)
203+
self.assertEqual(
204+
{'c': 'd'},
205+
cmd_output["volume_image_metadata"],
206+
)
195207

196208
def test_volume_snapshot(self):
197209
"""Tests volume create from snapshot"""

openstackclient/tests/unit/volume/v1/test_volume.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,7 @@ def test_volume_set_size_not_available(self):
10711071

10721072
def test_volume_set_property(self):
10731073
arglist = [
1074+
'--no-property',
10741075
'--property', 'myprop=myvalue',
10751076
self._volume.display_name,
10761077
]
@@ -1080,6 +1081,7 @@ def test_volume_set_property(self):
10801081
('name', None),
10811082
('description', None),
10821083
('size', None),
1084+
('no_property', True),
10831085
('property', {'myprop': 'myvalue'}),
10841086
('volume', self._volume.display_name),
10851087
('bootable', False),
@@ -1097,6 +1099,10 @@ def test_volume_set_property(self):
10971099
self._volume.id,
10981100
metadata
10991101
)
1102+
self.volumes_mock.delete_metadata.assert_called_with(
1103+
self._volume.id,
1104+
self._volume.metadata.keys()
1105+
)
11001106
self.volumes_mock.update_readonly_flag.assert_not_called()
11011107
self.assertIsNone(result)
11021108

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,24 @@ def setUp(self):
13681368
# Get the command object to test
13691369
self.cmd = volume.SetVolume(self.app, None)
13701370

1371+
def test_volume_set_property(self):
1372+
arglist = [
1373+
'--property', 'a=b',
1374+
'--property', 'c=d',
1375+
self.new_volume.id,
1376+
]
1377+
verifylist = [
1378+
('property', {'a': 'b', 'c': 'd'}),
1379+
('volume', self.new_volume.id),
1380+
('bootable', False),
1381+
('non_bootable', False)
1382+
]
1383+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
1384+
1385+
self.cmd.take_action(parsed_args)
1386+
self.volumes_mock.set_metadata.assert_called_with(
1387+
self.new_volume.id, parsed_args.property)
1388+
13711389
def test_volume_set_image_property(self):
13721390
arglist = [
13731391
'--image-property', 'Alpha=a',

openstackclient/volume/v1/volume.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,15 @@ def get_parser(self, prog_name):
439439
type=int,
440440
help=_('Extend volume size in GB'),
441441
)
442+
parser.add_argument(
443+
"--no-property",
444+
dest="no_property",
445+
action="store_true",
446+
help=_("Remove all properties from <volume> "
447+
"(specify both --no-property and --property to "
448+
"remove the current properties before setting "
449+
"new properties.)"),
450+
)
442451
parser.add_argument(
443452
'--property',
444453
metavar='<key=value>',
@@ -489,6 +498,15 @@ def take_action(self, parsed_args):
489498
except Exception as e:
490499
LOG.error(_("Failed to set volume size: %s"), e)
491500
result += 1
501+
502+
if parsed_args.no_property:
503+
try:
504+
volume_client.volumes.delete_metadata(
505+
volume.id, volume.metadata.keys())
506+
except Exception as e:
507+
LOG.error(_("Failed to clean volume properties: %s"), e)
508+
result += 1
509+
492510
if parsed_args.property:
493511
try:
494512
volume_client.volumes.set_metadata(

openstackclient/volume/v2/volume.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,15 @@ def get_parser(self, prog_name):
523523
metavar='<description>',
524524
help=_('New volume description'),
525525
)
526+
parser.add_argument(
527+
"--no-property",
528+
dest="no_property",
529+
action="store_true",
530+
help=_("Remove all properties from <volume> "
531+
"(specify both --no-property and --property to "
532+
"remove the current properties before setting "
533+
"new properties.)"),
534+
)
526535
parser.add_argument(
527536
'--property',
528537
metavar='<key=value>',
@@ -561,7 +570,7 @@ def get_parser(self, prog_name):
561570
choices=['never', 'on-demand'],
562571
help=_('Migration policy while re-typing volume '
563572
'("never" or "on-demand", default is "never" ) '
564-
'(available only when "--type" option is specified)'),
573+
'(available only when --type option is specified)'),
565574
)
566575
bootable_group = parser.add_mutually_exclusive_group()
567576
bootable_group.add_argument(
@@ -607,6 +616,14 @@ def take_action(self, parsed_args):
607616
LOG.error(_("Failed to set volume size: %s"), e)
608617
result += 1
609618

619+
if parsed_args.no_property:
620+
try:
621+
volume_client.volumes.delete_metadata(
622+
volume.id, volume.metadata.keys())
623+
except Exception as e:
624+
LOG.error(_("Failed to clean volume properties: %s"), e)
625+
result += 1
626+
610627
if parsed_args.property:
611628
try:
612629
volume_client.volumes.set_metadata(
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- |
4+
Add ``--no-property`` option in ``volume set``, this removes all properties from a volume.
5+
[Blueprint `allow-overwrite-set-options <https://blueprints.launchpad.net/python-openstackclient/+spec/allow-overwrite-set-options>`_]

0 commit comments

Comments
 (0)