From 1c7fe3b6bd60aa22ae525d0418729590f6429e96 Mon Sep 17 00:00:00 2001 From: tianhui Date: Fri, 6 Jul 2018 08:23:06 +0000 Subject: [PATCH] Compute: Add tag support for server add volume Change-Id: Id9f2e09426f6824e9ca672bf7808b5165c650a69 Story: 2002195 Task: 21675 --- openstackclient/compute/v2/server.py | 64 +++++++++++++------ .../tests/unit/compute/v2/test_server.py | 60 +++++++++++++++-- ...rt-server-add-volume-278e79a22dd482f4.yaml | 5 ++ 3 files changed, 104 insertions(+), 25 deletions(-) create mode 100644 releasenotes/notes/add-tag-support-server-add-volume-278e79a22dd482f4.yaml diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index 06bbb7ee70..0b1209f496 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -460,20 +460,32 @@ class AddServerVolume(command.Command): metavar='', help=_('Server internal device name for volume'), ) + parser.add_argument( + '--tag', + metavar='', + 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 @@ -490,28 +502,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( diff --git a/openstackclient/tests/unit/compute/v2/test_server.py b/openstackclient/tests/unit/compute/v2/test_server.py index dffedc6dab..d3e159159f 100644 --- a/openstackclient/tests/unit/compute/v2/test_server.py +++ b/openstackclient/tests/unit/compute/v2/test_server.py @@ -503,8 +503,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( @@ -561,7 +610,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') @@ -585,7 +635,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') @@ -609,7 +660,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') diff --git a/releasenotes/notes/add-tag-support-server-add-volume-278e79a22dd482f4.yaml b/releasenotes/notes/add-tag-support-server-add-volume-278e79a22dd482f4.yaml new file mode 100644 index 0000000000..510218b2f3 --- /dev/null +++ b/releasenotes/notes/add-tag-support-server-add-volume-278e79a22dd482f4.yaml @@ -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``.