Compute: Add tag support for server add volume

Change-Id: Id9f2e09426f6824e9ca672bf7808b5165c650a69
Story: 2002195
Task: 21675
This commit is contained in:
tianhui 2018-07-06 08:23:06 +00:00 committed by Stephen Finucane
parent 960004dcc7
commit 1c7fe3b6bd
3 changed files with 104 additions and 25 deletions

View File

@ -460,20 +460,32 @@ class AddServerVolume(command.Command):
metavar='<device>', metavar='<device>',
help=_('Server internal device name for volume'), 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 = parser.add_mutually_exclusive_group()
termination_group.add_argument( termination_group.add_argument(
'--enable-delete-on-termination', '--enable-delete-on-termination',
action='store_true', action='store_true',
help=_("Specify if the attached volume should be deleted when " help=_(
"the server is destroyed. (Supported with " "Specify if the attached volume should be deleted when the "
"``--os-compute-api-version`` 2.79 or greater.)"), "server is destroyed. "
"(Supported by API versions '2.79' - '2.latest')"
),
) )
termination_group.add_argument( termination_group.add_argument(
'--disable-delete-on-termination', '--disable-delete-on-termination',
action='store_true', action='store_true',
help=_("Specify if the attached volume should not be deleted " help=_(
"when the server is destroyed. (Supported with " "Specify if the attached volume should not be deleted when "
"``--os-compute-api-version`` 2.79 or greater.)"), "the server is destroyed. "
"(Supported by API versions '2.79' - '2.latest')"
),
) )
return parser return parser
@ -490,28 +502,38 @@ class AddServerVolume(command.Command):
parsed_args.volume, 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 = { kwargs = {
"device": parsed_args.device "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 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 kwargs['delete_on_termination'] = True
if parsed_args.disable_delete_on_termination: 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 kwargs['delete_on_termination'] = False
compute_client.volumes.create_server_volume( compute_client.volumes.create_server_volume(

View File

@ -503,8 +503,57 @@ class TestServerVolume(TestServer):
servers[0].id, self.volume.id, device='/dev/sdb') servers[0].id, self.volume.id, device='/dev/sdb')
self.assertIsNone(result) 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): def test_server_add_volume_with_enable_delete_on_termination(self):
self.app.client_manager.compute.api_version = api_versions.APIVersion( self.app.client_manager.compute.api_version = api_versions.APIVersion(
@ -561,7 +610,8 @@ class TestServerVolumeV279(TestServerVolume):
self.assertIsNone(result) self.assertIsNone(result)
def test_server_add_volume_with_enable_delete_on_termination_pre_v279( def test_server_add_volume_with_enable_delete_on_termination_pre_v279(
self): self,
):
self.app.client_manager.compute.api_version = api_versions.APIVersion( self.app.client_manager.compute.api_version = api_versions.APIVersion(
'2.78') '2.78')
@ -585,7 +635,8 @@ class TestServerVolumeV279(TestServerVolume):
str(ex)) str(ex))
def test_server_add_volume_with_disable_delete_on_termination_pre_v279( def test_server_add_volume_with_disable_delete_on_termination_pre_v279(
self): self,
):
self.app.client_manager.compute.api_version = api_versions.APIVersion( self.app.client_manager.compute.api_version = api_versions.APIVersion(
'2.78') '2.78')
@ -609,7 +660,8 @@ class TestServerVolumeV279(TestServerVolume):
str(ex)) str(ex))
def test_server_add_volume_with_disable_and_enable_delete_on_termination( def test_server_add_volume_with_disable_and_enable_delete_on_termination(
self): self,
):
self.app.client_manager.compute.api_version = api_versions.APIVersion( self.app.client_manager.compute.api_version = api_versions.APIVersion(
'2.79') '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``.