Add support for volume unmanage

This patch adds support for unmanaging a volume with the
``openstack volume delete --remote`` command.

Change-Id: Id71681e817f6e56b4ef553079f0bcfac8252d3cf
This commit is contained in:
Rajat Dhasmana 2024-05-15 08:02:12 +05:30
parent 00af14b3f2
commit fdc2763ac2
3 changed files with 122 additions and 1 deletions

View File

@ -427,3 +427,104 @@ class TestVolumeCreate(BaseVolumeTest):
'--host and --cluster options are only supported ',
str(exc),
)
class TestVolumeDelete(BaseVolumeTest):
def setUp(self):
super().setUp()
self.volumes_mock = self.volume_client.volumes
self.volumes_mock.reset_mock()
self.volume_sdk_client.unmanage_volume.return_value = None
# Get the command object to mock
self.cmd = volume.DeleteVolume(self.app, None)
def test_volume_delete_remote(self):
vol = sdk_fakes.generate_fake_resource(_volume.Volume, **{'size': 1})
self.volumes_mock.get.return_value = vol
arglist = ['--remote', vol.id]
verifylist = [
("remote", True),
("force", False),
("purge", False),
("volumes", [vol.id]),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args)
self.volume_sdk_client.unmanage_volume.assert_called_once_with(vol.id)
self.assertIsNone(result)
def test_volume_delete_multi_volumes_remote(self):
volumes = sdk_fakes.generate_fake_resources(
_volume.Volume, count=3, attrs={'size': 1}
)
arglist = ['--remote']
arglist += [v.id for v in volumes]
verifylist = [
('remote', True),
('force', False),
('purge', False),
('volumes', arglist[1:]),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args)
calls = [mock.call(v.id) for v in volumes]
self.volume_sdk_client.unmanage_volume.assert_has_calls(calls)
self.assertIsNone(result)
def test_volume_delete_remote_with_purge(self):
vol = sdk_fakes.generate_fake_resource(_volume.Volume, **{'size': 1})
arglist = [
'--remote',
'--purge',
vol.id,
]
verifylist = [
('remote', True),
('force', False),
('purge', True),
('volumes', [vol.id]),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
exc = self.assertRaises(
exceptions.CommandError, self.cmd.take_action, parsed_args
)
self.assertIn(
"The --force and --purge options are not supported with the "
"--remote parameter.",
str(exc),
)
def test_volume_delete_remote_with_force(self):
vol = sdk_fakes.generate_fake_resource(_volume.Volume, **{'size': 1})
arglist = [
'--remote',
'--force',
vol.id,
]
verifylist = [
('remote', True),
('force', True),
('purge', False),
('volumes', [vol.id]),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
exc = self.assertRaises(
exceptions.CommandError, self.cmd.take_action, parsed_args
)
self.assertIn(
"The --force and --purge options are not supported with the "
"--remote parameter.",
str(exc),
)

View File

@ -244,16 +244,31 @@ class DeleteVolume(volume_v2.DeleteVolume):
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument(
'--remote',
action='store_true',
help=_("Specify this parameter to unmanage a volume."),
)
return parser
def take_action(self, parsed_args):
volume_client = self.app.client_manager.volume
volume_client_sdk = self.app.client_manager.sdk_connection.volume
result = 0
if parsed_args.remote and (parsed_args.force or parsed_args.purge):
msg = _(
"The --force and --purge options are not "
"supported with the --remote parameter."
)
raise exceptions.CommandError(msg)
for i in parsed_args.volumes:
try:
volume_obj = utils.find_resource(volume_client.volumes, i)
if parsed_args.force:
if parsed_args.remote:
volume_client_sdk.unmanage_volume(volume_obj.id)
elif parsed_args.force:
volume_client.volumes.force_delete(volume_obj.id)
else:
volume_client.volumes.delete(

View File

@ -0,0 +1,5 @@
---
features:
- |
Add support for unmanaging volumes with
``openstack volume delete --remote <volume>`` command.