Merge "Compute: Add tag support for server add volume"

This commit is contained in:
Zuul 2020-10-14 03:46:06 +00:00 committed by Gerrit Code Review
commit 6216025e9d
3 changed files with 104 additions and 25 deletions

View File

@ -510,20 +510,32 @@ class AddServerVolume(command.Command):
metavar='<device>',
help=_('Server internal device name for volume'),
)
parser.add_argument(
'--tag',
metavar='<tag>',
help=_(
"Tag for the attached volume. "
"(Supported by API versions '2.49' - '2.latest')"
),
)
termination_group = parser.add_mutually_exclusive_group()
termination_group.add_argument(
'--enable-delete-on-termination',
action='store_true',
help=_("Specify if the attached volume should be deleted when "
"the server is destroyed. (Supported with "
"``--os-compute-api-version`` 2.79 or greater.)"),
help=_(
"Specify if the attached volume should be deleted when the "
"server is destroyed. "
"(Supported by API versions '2.79' - '2.latest')"
),
)
termination_group.add_argument(
'--disable-delete-on-termination',
action='store_true',
help=_("Specify if the attached volume should not be deleted "
"when the server is destroyed. (Supported with "
"``--os-compute-api-version`` 2.79 or greater.)"),
help=_(
"Specify if the attached volume should not be deleted when "
"the server is destroyed. "
"(Supported by API versions '2.79' - '2.latest')"
),
)
return parser
@ -540,28 +552,38 @@ class AddServerVolume(command.Command):
parsed_args.volume,
)
support_set_delete_on_termination = (compute_client.api_version >=
api_versions.APIVersion('2.79'))
if not support_set_delete_on_termination:
if parsed_args.enable_delete_on_termination:
msg = _('--os-compute-api-version 2.79 or greater '
'is required to support the '
'--enable-delete-on-termination option.')
raise exceptions.CommandError(msg)
if parsed_args.disable_delete_on_termination:
msg = _('--os-compute-api-version 2.79 or greater '
'is required to support the '
'--disable-delete-on-termination option.')
raise exceptions.CommandError(msg)
kwargs = {
"device": parsed_args.device
}
if parsed_args.tag:
if compute_client.api_version < api_versions.APIVersion('2.49'):
msg = _(
'--os-compute-api-version 2.49 or greater is required to '
'support the --tag option'
)
raise exceptions.CommandError(msg)
kwargs['tag'] = parsed_args.tag
if parsed_args.enable_delete_on_termination:
if compute_client.api_version < api_versions.APIVersion('2.79'):
msg = _(
'--os-compute-api-version 2.79 or greater is required to '
'support the --enable-delete-on-termination option.'
)
raise exceptions.CommandError(msg)
kwargs['delete_on_termination'] = True
if parsed_args.disable_delete_on_termination:
if compute_client.api_version < api_versions.APIVersion('2.79'):
msg = _(
'--os-compute-api-version 2.79 or greater is required to '
'support the --disable-delete-on-termination option.'
)
raise exceptions.CommandError(msg)
kwargs['delete_on_termination'] = False
compute_client.volumes.create_server_volume(

View File

@ -592,8 +592,57 @@ class TestServerVolume(TestServer):
servers[0].id, self.volume.id, device='/dev/sdb')
self.assertIsNone(result)
def test_server_add_volume_with_tag(self):
# requires API 2.49 or later
self.app.client_manager.compute.api_version = api_versions.APIVersion(
'2.49')
class TestServerVolumeV279(TestServerVolume):
servers = self.setup_servers_mock(count=1)
arglist = [
'--device', '/dev/sdb',
'--tag', 'foo',
servers[0].id,
self.volume.id,
]
verifylist = [
('server', servers[0].id),
('volume', self.volume.id),
('device', '/dev/sdb'),
('tag', 'foo'),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args)
self.servers_volumes_mock.create_server_volume.assert_called_once_with(
servers[0].id, self.volume.id, device='/dev/sdb', tag='foo')
self.assertIsNone(result)
def test_server_add_volume_with_tag_pre_v249(self):
self.app.client_manager.compute.api_version = api_versions.APIVersion(
'2.48')
servers = self.setup_servers_mock(count=1)
arglist = [
servers[0].id,
self.volume.id,
'--tag', 'foo',
]
verifylist = [
('server', servers[0].id),
('volume', self.volume.id),
('tag', 'foo'),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
ex = self.assertRaises(
exceptions.CommandError,
self.cmd.take_action,
parsed_args)
self.assertIn(
'--os-compute-api-version 2.49 or greater is required',
str(ex))
def test_server_add_volume_with_enable_delete_on_termination(self):
self.app.client_manager.compute.api_version = api_versions.APIVersion(
@ -650,7 +699,8 @@ class TestServerVolumeV279(TestServerVolume):
self.assertIsNone(result)
def test_server_add_volume_with_enable_delete_on_termination_pre_v279(
self):
self,
):
self.app.client_manager.compute.api_version = api_versions.APIVersion(
'2.78')
@ -674,7 +724,8 @@ class TestServerVolumeV279(TestServerVolume):
str(ex))
def test_server_add_volume_with_disable_delete_on_termination_pre_v279(
self):
self,
):
self.app.client_manager.compute.api_version = api_versions.APIVersion(
'2.78')
@ -698,7 +749,8 @@ class TestServerVolumeV279(TestServerVolume):
str(ex))
def test_server_add_volume_with_disable_and_enable_delete_on_termination(
self):
self,
):
self.app.client_manager.compute.api_version = api_versions.APIVersion(
'2.79')

View File

@ -0,0 +1,5 @@
---
features:
- Add ``--tag`` option to ``server add volume`` command when
add a volume to server. Only available starting with
``--os-compute-api-version 2.49``.