Skip to content

Commit 20ae540

Browse files
Xi YangHuanxuan Ao
andcommitted
Add support of setting volume's state
OSC does not support to set volume's state, this patch is going to add this functionality. Closes-Bug:#1535213 Change-Id: I5bc1c7e81b8ba61c37f4bfd209fc86c5857fb050 Co-Authored-By: Huanxuan Ao <huanxuan.ao@easystack.cn>
1 parent eaee74b commit 20ae540

5 files changed

Lines changed: 81 additions & 30 deletions

File tree

doc/source/command-objects/volume.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ Set volume properties
180180
[--description <description>]
181181
[--property <key=value> [...] ]
182182
[--image-property <key=value> [...] ]
183+
[--state <state>]
183184
<volume>
184185
185186
.. option:: --name <name>
@@ -209,6 +210,14 @@ Set volume properties
209210
210211
*Volume version 2 only*
211212
213+
.. option:: --state <state>
214+
215+
New volume state
216+
("available", "error", "creating", "deleting", "in-use",
217+
"attaching", "detaching", "error_deleting" or "maintenance")
218+
219+
*Volume version 2 only*
220+
212221
.. _volume_set-volume:
213222
.. describe:: <volume>
214223

functional/tests/volume/v2/test_volume.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,16 @@ def tearDownClass(cls):
4343
'volume set --name ' + cls.OTHER_NAME + ' ' + cls.NAME)
4444
cls.assertOutput('', raw_output)
4545

46+
# Set volume state
47+
cls.openstack('volume set --state error ' + cls.OTHER_NAME)
48+
opts = cls.get_opts(["status"])
49+
raw_output_status = cls.openstack(
50+
'volume show ' + cls.OTHER_NAME + opts)
51+
4652
# Delete test volume
4753
raw_output = cls.openstack('volume delete ' + cls.OTHER_NAME)
4854
cls.assertOutput('', raw_output)
55+
cls.assertOutput('error\n', raw_output_status)
4956

5057
def test_volume_list(self):
5158
opts = self.get_opts(self.HEADERS)

openstackclient/tests/volume/v2/test_volume.py

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,53 @@ def test_volume_list_long(self):
814814
self.assertEqual(datalist, tuple(data))
815815

816816

817+
class TestVolumeSet(TestVolume):
818+
819+
def setUp(self):
820+
super(TestVolumeSet, self).setUp()
821+
822+
self.new_volume = volume_fakes.FakeVolume.create_one_volume()
823+
self.volumes_mock.get.return_value = self.new_volume
824+
825+
# Get the command object to test
826+
self.cmd = volume.SetVolume(self.app, None)
827+
828+
def test_volume_set_image_property(self):
829+
arglist = [
830+
'--image-property', 'Alpha=a',
831+
'--image-property', 'Beta=b',
832+
self.new_volume.id,
833+
]
834+
verifylist = [
835+
('image_property', {'Alpha': 'a', 'Beta': 'b'}),
836+
('volume', self.new_volume.id),
837+
]
838+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
839+
840+
# In base command class ShowOne in cliff, abstract method take_action()
841+
# returns nothing
842+
self.cmd.take_action(parsed_args)
843+
self.volumes_mock.set_image_metadata.assert_called_with(
844+
self.volumes_mock.get().id, parsed_args.image_property)
845+
846+
def test_volume_set_state(self):
847+
arglist = [
848+
'--state', 'error',
849+
self.new_volume.id
850+
]
851+
verifylist = [
852+
('state', 'error'),
853+
('volume', self.new_volume.id)
854+
]
855+
856+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
857+
858+
result = self.cmd.take_action(parsed_args)
859+
self.volumes_mock.reset_state.assert_called_with(
860+
self.new_volume.id, 'error')
861+
self.assertIsNone(result)
862+
863+
817864
class TestVolumeShow(TestVolume):
818865

819866
def setUp(self):
@@ -845,36 +892,6 @@ def test_volume_show(self):
845892
data)
846893

847894

848-
class TestVolumeSet(TestVolume):
849-
850-
def setUp(self):
851-
super(TestVolumeSet, self).setUp()
852-
853-
self.new_volume = volume_fakes.FakeVolume.create_one_volume()
854-
self.volumes_mock.create.return_value = self.new_volume
855-
856-
# Get the command object to test
857-
self.cmd = volume.SetVolume(self.app, None)
858-
859-
def test_volume_set_image_property(self):
860-
arglist = [
861-
'--image-property', 'Alpha=a',
862-
'--image-property', 'Beta=b',
863-
self.new_volume.id,
864-
]
865-
verifylist = [
866-
('image_property', {'Alpha': 'a', 'Beta': 'b'}),
867-
('volume', self.new_volume.id),
868-
]
869-
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
870-
871-
# In base command class ShowOne in cliff, abstract method take_action()
872-
# returns nothing
873-
self.cmd.take_action(parsed_args)
874-
self.volumes_mock.set_image_metadata.assert_called_with(
875-
self.volumes_mock.get().id, parsed_args.image_property)
876-
877-
878895
class TestVolumeUnset(TestVolume):
879896

880897
def setUp(self):

openstackclient/volume/v2/volume.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,16 @@ def get_parser(self, prog_name):
378378
help=_('Set an image property on this volume '
379379
'(repeat option to set multiple image properties)'),
380380
)
381+
parser.add_argument(
382+
"--state",
383+
metavar="<state>",
384+
choices=['available', 'error', 'creating', 'deleting',
385+
'in-use', 'attaching', 'detaching', 'error_deleting',
386+
'maintenance'],
387+
help=_('New volume state ("available", "error", "creating", '
388+
'"deleting", "in-use", "attaching", "detaching", '
389+
'"error_deleting" or "maintenance")'),
390+
)
381391
return parser
382392

383393
def take_action(self, parsed_args):
@@ -400,6 +410,8 @@ def take_action(self, parsed_args):
400410
if parsed_args.image_property:
401411
volume_client.volumes.set_image_metadata(
402412
volume.id, parsed_args.image_property)
413+
if parsed_args.state:
414+
volume_client.volumes.reset_state(volume.id, parsed_args.state)
403415

404416
kwargs = {}
405417
if parsed_args.name:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
Support a new ``--state`` option for ``volume set`` command that
5+
changes the state of a volume.
6+
[Bug `1535213 <https://bugs.launchpad.net/bugs/1535213>`_]

0 commit comments

Comments
 (0)