commit 742c80a82574e5a6928c305842047b8b6b2b3807 Author: tianhui Date: Fri Jul 27 06:20:56 2018 +0000 Compute: Add tag support for server add fixed ip Change-Id: I62ed4729dead9f91630d1f568c834c9642965558 Story: 2002195 Task: 21679 diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index e5a7a32..3e69d93 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -236,6 +236,14 @@ class AddFixedIP(command.Command): metavar="", help=_("Requested fixed IP address"), ) + parser.add_argument( + '--tag', + metavar='', + help=_( + 'Tag for the attached interface. ' + '(supported by --os-compute-api-version 2.52 or above)' + ) + ) return parser def take_action(self, parsed_args): @@ -246,11 +254,23 @@ class AddFixedIP(command.Command): network = compute_client.api.network_find(parsed_args.network) - server.interface_attach( - port_id=None, - net_id=network['id'], - fixed_ip=parsed_args.fixed_ip_address, - ) + kwargs = { + 'port_id': None, + 'net_id': network['id'], + 'fixed_ip': parsed_args.fixed_ip_address, + } + + 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 + + server.interface_attach(**kwargs) class AddFloatingIP(network_common.NetworkAndComputeCommand): diff --git a/openstackclient/tests/unit/compute/v2/test_server.py b/openstackclient/tests/unit/compute/v2/test_server.py index 59282b4..c91d6e4 100644 --- a/openstackclient/tests/unit/compute/v2/test_server.py +++ b/openstackclient/tests/unit/compute/v2/test_server.py @@ -177,6 +177,72 @@ class TestServerAddFixedIP(TestServer): extralist = ['--fixed-ip-address', '5.6.7.8'] self._test_server_add_fixed_ip(extralist, '5.6.7.8') + def test_server_add_fixed_ip_with_tag(self): + self.app.client_manager.compute.api_version = api_versions.APIVersion( + '2.49') + + servers = self.setup_servers_mock(count=1) + network = compute_fakes.FakeNetwork.create_one_network() + with mock.patch( + 'openstackclient.api.compute_v2.APIv2.network_find' + ) as net_mock: + net_mock.return_value = network + + arglist = [ + servers[0].id, + network['id'], + '--fixed-ip-address', '5.6.7.8', + '--tag', 'tag1', + ] + verifylist = [ + ('server', servers[0].id), + ('network', network['id']), + ('fixed_ip_address', '5.6.7.8'), + ('tag', 'tag1'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + + servers[0].interface_attach.assert_called_once_with( + port_id=None, + net_id=network['id'], + fixed_ip='5.6.7.8', + tag='tag1' + ) + self.assertIsNone(result) + + def test_server_add_fixed_ip_with_tag_pre_v249(self): + self.app.client_manager.compute.api_version = api_versions.APIVersion( + '2.48') + + servers = self.setup_servers_mock(count=1) + network = compute_fakes.FakeNetwork.create_one_network() + with mock.patch( + 'openstackclient.api.compute_v2.APIv2.network_find' + ) as net_mock: + net_mock.return_value = network + + arglist = [ + servers[0].id, + network['id'], + '--fixed-ip-address', '5.6.7.8', + '--tag', 'tag1', + ] + verifylist = [ + ('server', servers[0].id), + ('network', network['id']), + ('fixed_ip_address', '5.6.7.8'), + ('tag', 'tag1'), + ] + 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)) + @mock.patch( 'openstackclient.api.compute_v2.APIv2.floating_ip_add' diff --git a/releasenotes/notes/add-tag-support-server-add-fixed-ip-8de2db58f2a80e85.yaml b/releasenotes/notes/add-tag-support-server-add-fixed-ip-8de2db58f2a80e85.yaml new file mode 100644 index 0000000..731236e --- /dev/null +++ b/releasenotes/notes/add-tag-support-server-add-fixed-ip-8de2db58f2a80e85.yaml @@ -0,0 +1,5 @@ +--- +features: + - Add ``--tag`` option to ``server add fixed ip`` command + when adding a fixed IP to server. Only available starting + with ``--os-compute-api-version 2.49``.