compute: Return details of attached volumes

The API behind the 'server add volume' command returns details of the
created volume attachment, however, we were dropping these results
rather than displaying them to the user. Correct this.

Change-Id: I3f7e121220d29422ccf4e6940de2f28bb8496c83
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2021-11-03 11:31:04 +00:00
parent f824e13bc5
commit 163cb01e46
3 changed files with 108 additions and 10 deletions

View File

@ -496,7 +496,7 @@ class AddServerSecurityGroup(command.Command):
server.add_security_group(security_group['id']) server.add_security_group(security_group['id'])
class AddServerVolume(command.Command): class AddServerVolume(command.ShowOne):
_description = _( _description = _(
"Add volume to server. " "Add volume to server. "
"Specify ``--os-compute-api-version 2.20`` or higher to add a volume " "Specify ``--os-compute-api-version 2.20`` or higher to add a volume "
@ -595,12 +595,30 @@ class AddServerVolume(command.Command):
kwargs['delete_on_termination'] = False kwargs['delete_on_termination'] = False
compute_client.volumes.create_server_volume( volume_attachment = compute_client.volumes.create_server_volume(
server.id, server.id,
volume.id, volume.id,
**kwargs **kwargs
) )
columns = ('id', 'serverId', 'volumeId', 'device')
column_headers = ('ID', 'Server ID', 'Volume ID', 'Device')
if compute_client.api_version >= api_versions.APIVersion('2.49'):
columns += ('tag',)
column_headers += ('Tag',)
if compute_client.api_version >= api_versions.APIVersion('2.79'):
columns += ('delete_on_termination',)
column_headers += ('Delete On Termination',)
return (
column_headers,
utils.get_item_properties(
volume_attachment,
columns,
mixed_case_fields=('serverId', 'volumeId'),
)
)
# TODO(stephenfin): Replace with 'MultiKeyValueAction' when we no longer # TODO(stephenfin): Replace with 'MultiKeyValueAction' when we no longer
# support '--nic=auto' and '--nic=none' # support '--nic=auto' and '--nic=none'

View File

@ -670,6 +670,11 @@ class TestServerVolume(TestServer):
def test_server_add_volume(self): def test_server_add_volume(self):
servers = self.setup_servers_mock(count=1) servers = self.setup_servers_mock(count=1)
volume_attachment = \
compute_fakes.FakeVolumeAttachment.create_one_volume_attachment()
self.servers_volumes_mock.create_server_volume.return_value = \
volume_attachment
arglist = [ arglist = [
'--device', '/dev/sdb', '--device', '/dev/sdb',
servers[0].id, servers[0].id,
@ -683,11 +688,20 @@ class TestServerVolume(TestServer):
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args) expected_columns = ('ID', 'Server ID', 'Volume ID', 'Device')
expected_data = (
volume_attachment.id,
volume_attachment.serverId,
volume_attachment.volumeId,
volume_attachment.device,
)
columns, data = self.cmd.take_action(parsed_args)
self.servers_volumes_mock.create_server_volume.assert_called_once_with( self.servers_volumes_mock.create_server_volume.assert_called_once_with(
servers[0].id, self.volume.id, device='/dev/sdb') servers[0].id, self.volume.id, device='/dev/sdb')
self.assertIsNone(result) self.assertEqual(expected_columns, columns)
self.assertEqual(expected_data, data)
def test_server_add_volume_with_tag(self): def test_server_add_volume_with_tag(self):
# requires API 2.49 or later # requires API 2.49 or later
@ -695,6 +709,11 @@ class TestServerVolume(TestServer):
'2.49') '2.49')
servers = self.setup_servers_mock(count=1) servers = self.setup_servers_mock(count=1)
volume_attachment = \
compute_fakes.FakeVolumeAttachment.create_one_volume_attachment()
self.servers_volumes_mock.create_server_volume.return_value = \
volume_attachment
arglist = [ arglist = [
'--device', '/dev/sdb', '--device', '/dev/sdb',
'--tag', 'foo', '--tag', 'foo',
@ -710,11 +729,21 @@ class TestServerVolume(TestServer):
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args) expected_columns = ('ID', 'Server ID', 'Volume ID', 'Device', 'Tag')
expected_data = (
volume_attachment.id,
volume_attachment.serverId,
volume_attachment.volumeId,
volume_attachment.device,
volume_attachment.tag,
)
columns, data = self.cmd.take_action(parsed_args)
self.servers_volumes_mock.create_server_volume.assert_called_once_with( self.servers_volumes_mock.create_server_volume.assert_called_once_with(
servers[0].id, self.volume.id, device='/dev/sdb', tag='foo') servers[0].id, self.volume.id, device='/dev/sdb', tag='foo')
self.assertIsNone(result) self.assertEqual(expected_columns, columns)
self.assertEqual(expected_data, data)
def test_server_add_volume_with_tag_pre_v249(self): def test_server_add_volume_with_tag_pre_v249(self):
self.app.client_manager.compute.api_version = api_versions.APIVersion( self.app.client_manager.compute.api_version = api_versions.APIVersion(
@ -746,6 +775,11 @@ class TestServerVolume(TestServer):
'2.79') '2.79')
servers = self.setup_servers_mock(count=1) servers = self.setup_servers_mock(count=1)
volume_attachment = \
compute_fakes.FakeVolumeAttachment.create_one_volume_attachment()
self.servers_volumes_mock.create_server_volume.return_value = \
volume_attachment
arglist = [ arglist = [
'--enable-delete-on-termination', '--enable-delete-on-termination',
'--device', '/dev/sdb', '--device', '/dev/sdb',
@ -761,18 +795,41 @@ class TestServerVolume(TestServer):
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args) expected_columns = (
'ID',
'Server ID',
'Volume ID',
'Device',
'Tag',
'Delete On Termination',
)
expected_data = (
volume_attachment.id,
volume_attachment.serverId,
volume_attachment.volumeId,
volume_attachment.device,
volume_attachment.tag,
volume_attachment.delete_on_termination,
)
columns, data = self.cmd.take_action(parsed_args)
self.servers_volumes_mock.create_server_volume.assert_called_once_with( self.servers_volumes_mock.create_server_volume.assert_called_once_with(
servers[0].id, self.volume.id, servers[0].id, self.volume.id,
device='/dev/sdb', delete_on_termination=True) device='/dev/sdb', delete_on_termination=True)
self.assertIsNone(result) self.assertEqual(expected_columns, columns)
self.assertEqual(expected_data, data)
def test_server_add_volume_with_disable_delete_on_termination(self): def test_server_add_volume_with_disable_delete_on_termination(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')
servers = self.setup_servers_mock(count=1) servers = self.setup_servers_mock(count=1)
volume_attachment = \
compute_fakes.FakeVolumeAttachment.create_one_volume_attachment()
self.servers_volumes_mock.create_server_volume.return_value = \
volume_attachment
arglist = [ arglist = [
'--disable-delete-on-termination', '--disable-delete-on-termination',
'--device', '/dev/sdb', '--device', '/dev/sdb',
@ -788,12 +845,30 @@ class TestServerVolume(TestServer):
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args) expected_columns = (
'ID',
'Server ID',
'Volume ID',
'Device',
'Tag',
'Delete On Termination',
)
expected_data = (
volume_attachment.id,
volume_attachment.serverId,
volume_attachment.volumeId,
volume_attachment.device,
volume_attachment.tag,
volume_attachment.delete_on_termination,
)
columns, data = self.cmd.take_action(parsed_args)
self.servers_volumes_mock.create_server_volume.assert_called_once_with( self.servers_volumes_mock.create_server_volume.assert_called_once_with(
servers[0].id, self.volume.id, servers[0].id, self.volume.id,
device='/dev/sdb', delete_on_termination=False) device='/dev/sdb', delete_on_termination=False)
self.assertIsNone(result) self.assertEqual(expected_columns, columns)
self.assertEqual(expected_data, data)
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,

View File

@ -0,0 +1,5 @@
---
features:
- |
The ``server add volume`` command will now return details of the created
volume attachment upon successful attachment.