Skip to content

Commit 163cb01

Browse files
committed
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 <[email protected]>
1 parent f824e13 commit 163cb01

3 files changed

Lines changed: 108 additions & 10 deletions

File tree

openstackclient/compute/v2/server.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ def take_action(self, parsed_args):
496496
server.add_security_group(security_group['id'])
497497

498498

499-
class AddServerVolume(command.Command):
499+
class AddServerVolume(command.ShowOne):
500500
_description = _(
501501
"Add volume to server. "
502502
"Specify ``--os-compute-api-version 2.20`` or higher to add a volume "
@@ -595,12 +595,30 @@ def take_action(self, parsed_args):
595595

596596
kwargs['delete_on_termination'] = False
597597

598-
compute_client.volumes.create_server_volume(
598+
volume_attachment = compute_client.volumes.create_server_volume(
599599
server.id,
600600
volume.id,
601601
**kwargs
602602
)
603603

604+
columns = ('id', 'serverId', 'volumeId', 'device')
605+
column_headers = ('ID', 'Server ID', 'Volume ID', 'Device')
606+
if compute_client.api_version >= api_versions.APIVersion('2.49'):
607+
columns += ('tag',)
608+
column_headers += ('Tag',)
609+
if compute_client.api_version >= api_versions.APIVersion('2.79'):
610+
columns += ('delete_on_termination',)
611+
column_headers += ('Delete On Termination',)
612+
613+
return (
614+
column_headers,
615+
utils.get_item_properties(
616+
volume_attachment,
617+
columns,
618+
mixed_case_fields=('serverId', 'volumeId'),
619+
)
620+
)
621+
604622

605623
# TODO(stephenfin): Replace with 'MultiKeyValueAction' when we no longer
606624
# support '--nic=auto' and '--nic=none'

openstackclient/tests/unit/compute/v2/test_server.py

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,11 @@ def setUp(self):
670670

671671
def test_server_add_volume(self):
672672
servers = self.setup_servers_mock(count=1)
673+
volume_attachment = \
674+
compute_fakes.FakeVolumeAttachment.create_one_volume_attachment()
675+
self.servers_volumes_mock.create_server_volume.return_value = \
676+
volume_attachment
677+
673678
arglist = [
674679
'--device', '/dev/sdb',
675680
servers[0].id,
@@ -683,18 +688,32 @@ def test_server_add_volume(self):
683688

684689
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
685690

686-
result = self.cmd.take_action(parsed_args)
691+
expected_columns = ('ID', 'Server ID', 'Volume ID', 'Device')
692+
expected_data = (
693+
volume_attachment.id,
694+
volume_attachment.serverId,
695+
volume_attachment.volumeId,
696+
volume_attachment.device,
697+
)
698+
699+
columns, data = self.cmd.take_action(parsed_args)
687700

688701
self.servers_volumes_mock.create_server_volume.assert_called_once_with(
689702
servers[0].id, self.volume.id, device='/dev/sdb')
690-
self.assertIsNone(result)
703+
self.assertEqual(expected_columns, columns)
704+
self.assertEqual(expected_data, data)
691705

692706
def test_server_add_volume_with_tag(self):
693707
# requires API 2.49 or later
694708
self.app.client_manager.compute.api_version = api_versions.APIVersion(
695709
'2.49')
696710

697711
servers = self.setup_servers_mock(count=1)
712+
volume_attachment = \
713+
compute_fakes.FakeVolumeAttachment.create_one_volume_attachment()
714+
self.servers_volumes_mock.create_server_volume.return_value = \
715+
volume_attachment
716+
698717
arglist = [
699718
'--device', '/dev/sdb',
700719
'--tag', 'foo',
@@ -710,11 +729,21 @@ def test_server_add_volume_with_tag(self):
710729

711730
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
712731

713-
result = self.cmd.take_action(parsed_args)
732+
expected_columns = ('ID', 'Server ID', 'Volume ID', 'Device', 'Tag')
733+
expected_data = (
734+
volume_attachment.id,
735+
volume_attachment.serverId,
736+
volume_attachment.volumeId,
737+
volume_attachment.device,
738+
volume_attachment.tag,
739+
)
740+
741+
columns, data = self.cmd.take_action(parsed_args)
714742

715743
self.servers_volumes_mock.create_server_volume.assert_called_once_with(
716744
servers[0].id, self.volume.id, device='/dev/sdb', tag='foo')
717-
self.assertIsNone(result)
745+
self.assertEqual(expected_columns, columns)
746+
self.assertEqual(expected_data, data)
718747

719748
def test_server_add_volume_with_tag_pre_v249(self):
720749
self.app.client_manager.compute.api_version = api_versions.APIVersion(
@@ -746,6 +775,11 @@ def test_server_add_volume_with_enable_delete_on_termination(self):
746775
'2.79')
747776

748777
servers = self.setup_servers_mock(count=1)
778+
volume_attachment = \
779+
compute_fakes.FakeVolumeAttachment.create_one_volume_attachment()
780+
self.servers_volumes_mock.create_server_volume.return_value = \
781+
volume_attachment
782+
749783
arglist = [
750784
'--enable-delete-on-termination',
751785
'--device', '/dev/sdb',
@@ -761,18 +795,41 @@ def test_server_add_volume_with_enable_delete_on_termination(self):
761795
]
762796
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
763797

764-
result = self.cmd.take_action(parsed_args)
798+
expected_columns = (
799+
'ID',
800+
'Server ID',
801+
'Volume ID',
802+
'Device',
803+
'Tag',
804+
'Delete On Termination',
805+
)
806+
expected_data = (
807+
volume_attachment.id,
808+
volume_attachment.serverId,
809+
volume_attachment.volumeId,
810+
volume_attachment.device,
811+
volume_attachment.tag,
812+
volume_attachment.delete_on_termination,
813+
)
814+
815+
columns, data = self.cmd.take_action(parsed_args)
765816

766817
self.servers_volumes_mock.create_server_volume.assert_called_once_with(
767818
servers[0].id, self.volume.id,
768819
device='/dev/sdb', delete_on_termination=True)
769-
self.assertIsNone(result)
820+
self.assertEqual(expected_columns, columns)
821+
self.assertEqual(expected_data, data)
770822

771823
def test_server_add_volume_with_disable_delete_on_termination(self):
772824
self.app.client_manager.compute.api_version = api_versions.APIVersion(
773825
'2.79')
774826

775827
servers = self.setup_servers_mock(count=1)
828+
volume_attachment = \
829+
compute_fakes.FakeVolumeAttachment.create_one_volume_attachment()
830+
self.servers_volumes_mock.create_server_volume.return_value = \
831+
volume_attachment
832+
776833
arglist = [
777834
'--disable-delete-on-termination',
778835
'--device', '/dev/sdb',
@@ -788,12 +845,30 @@ def test_server_add_volume_with_disable_delete_on_termination(self):
788845
]
789846
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
790847

791-
result = self.cmd.take_action(parsed_args)
848+
expected_columns = (
849+
'ID',
850+
'Server ID',
851+
'Volume ID',
852+
'Device',
853+
'Tag',
854+
'Delete On Termination',
855+
)
856+
expected_data = (
857+
volume_attachment.id,
858+
volume_attachment.serverId,
859+
volume_attachment.volumeId,
860+
volume_attachment.device,
861+
volume_attachment.tag,
862+
volume_attachment.delete_on_termination,
863+
)
864+
865+
columns, data = self.cmd.take_action(parsed_args)
792866

793867
self.servers_volumes_mock.create_server_volume.assert_called_once_with(
794868
servers[0].id, self.volume.id,
795869
device='/dev/sdb', delete_on_termination=False)
796-
self.assertIsNone(result)
870+
self.assertEqual(expected_columns, columns)
871+
self.assertEqual(expected_data, data)
797872

798873
def test_server_add_volume_with_enable_delete_on_termination_pre_v279(
799874
self,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- |
4+
The ``server add volume`` command will now return details of the created
5+
volume attachment upon successful attachment.

0 commit comments

Comments
 (0)