diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index 844ac737d3..61fffe2d3b 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -345,10 +345,6 @@ class AddFixedIP(command.ShowOne): else: net_id = parsed_args.network - if not sdk_utils.supports_microversion(compute_client, '2.44'): - compute_client.add_fixed_ip_to_server(server.id, net_id) - return ((), ()) - kwargs = {'net_id': net_id} if parsed_args.fixed_ip_address: kwargs['fixed_ips'] = [ @@ -375,9 +371,11 @@ class AddFixedIP(command.ShowOne): 'Port State', 'Fixed IPs', ) - if sdk_utils.supports_microversion(compute_client, '2.49'): - columns += ('tag',) - column_headers += ('Tag',) + + if parsed_args.tag: + if sdk_utils.supports_microversion(compute_client, '2.49'): + columns += ('tag',) + column_headers += ('Tag',) return ( column_headers, diff --git a/openstackclient/tests/unit/compute/v2/test_server.py b/openstackclient/tests/unit/compute/v2/test_server.py index 6abe775923..f7f0972e3f 100644 --- a/openstackclient/tests/unit/compute/v2/test_server.py +++ b/openstackclient/tests/unit/compute/v2/test_server.py @@ -205,105 +205,6 @@ class TestServerAddFixedIP(TestServer): self.find_network = mock.Mock() self.app.client_manager.network.find_network = self.find_network - @mock.patch.object(sdk_utils, 'supports_microversion') - def test_server_add_fixed_ip_pre_v244(self, sm_mock): - sm_mock.return_value = False - - servers = self.setup_sdk_servers_mock(count=1) - network = compute_fakes.create_one_network() - - with mock.patch.object( - self.app.client_manager, - 'is_network_endpoint_enabled', - return_value=False, - ): - arglist = [ - servers[0].id, - network['id'], - ] - verifylist = [ - ('server', servers[0].id), - ('network', network['id']), - ('fixed_ip_address', None), - ] - parsed_args = self.check_parser(self.cmd, arglist, verifylist) - - result = self.cmd.take_action(parsed_args) - - self.sdk_client.add_fixed_ip_to_server.assert_called_once_with( - servers[0].id, network['id'] - ) - # the legacy API operates asynchronously - self.assertEqual(((), ()), result) - - @mock.patch.object(sdk_utils, 'supports_microversion') - def test_server_add_fixed_ip_pre_v244_with_fixed_ip(self, sm_mock): - sm_mock.return_value = False - - servers = self.setup_sdk_servers_mock(count=1) - network = compute_fakes.create_one_network() - - with mock.patch.object( - self.app.client_manager, - 'is_network_endpoint_enabled', - return_value=False, - ): - arglist = [ - servers[0].id, - network['id'], - '--fixed-ip-address', - '5.6.7.8', - ] - verifylist = [ - ('server', servers[0].id), - ('network', network['id']), - ('fixed_ip_address', '5.6.7.8'), - ] - parsed_args = self.check_parser(self.cmd, arglist, verifylist) - - result = self.cmd.take_action(parsed_args) - - self.sdk_client.add_fixed_ip_to_server.assert_called_once_with( - servers[0].id, network['id'] - ) - # the legacy API operates asynchronously - self.assertEqual(((), ()), result) - - @mock.patch.object(sdk_utils, 'supports_microversion') - def test_server_add_fixed_ip_pre_v244_with_tag(self, sm_mock): - sm_mock.return_value = False - - servers = self.setup_sdk_servers_mock(count=1) - network = compute_fakes.create_one_network() - - with mock.patch.object( - self.app.client_manager, - 'is_network_endpoint_enabled', - return_value=False, - ): - 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.object(sdk_utils, 'supports_microversion') def test_server_add_fixed_ip_pre_v249_with_tag(self, sm_mock): sm_mock.side_effect = [False, True] @@ -412,6 +313,63 @@ class TestServerAddFixedIP(TestServer): ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) + expected_columns = ( + 'Port ID', + 'Server ID', + 'Network ID', + 'MAC Address', + 'Port State', + 'Fixed IPs', + ) + expected_data = ( + interface.port_id, + interface.server_id, + interface.net_id, + interface.mac_addr, + interface.port_state, + format_columns.ListDictColumn(interface.fixed_ips), + ) + + columns, data = self.cmd.take_action(parsed_args) + + self.assertEqual(expected_columns, columns) + self.assertEqual(expected_data, tuple(data)) + self.sdk_client.create_server_interface.assert_called_once_with( + servers[0].id, + net_id=network['id'], + fixed_ips=[{'ip_address': '5.6.7.8'}], + ) + + @mock.patch.object(sdk_utils, 'supports_microversion') + def test_server_add_fixed_ip_with_tag(self, sm_mock): + sm_mock.side_effect = [True, True, True] + + servers = self.setup_sdk_servers_mock(count=1) + network = compute_fakes.create_one_network() + interface = compute_fakes.create_one_server_interface() + self.sdk_client.create_server_interface.return_value = interface + + with mock.patch.object( + self.app.client_manager, + 'is_network_endpoint_enabled', + return_value=False, + ): + 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) + expected_columns = ( 'Port ID', 'Server ID', @@ -439,11 +397,12 @@ class TestServerAddFixedIP(TestServer): servers[0].id, net_id=network['id'], fixed_ips=[{'ip_address': '5.6.7.8'}], + tag='tag1', ) @mock.patch.object(sdk_utils, 'supports_microversion') - def test_server_add_fixed_ip_with_tag(self, sm_mock): - sm_mock.side_effect = [True, True, True] + def test_server_add_fixed_ip_with_fixed_ip_with_tag(self, sm_mock): + sm_mock.side_effect = [True, True] servers = self.setup_sdk_servers_mock(count=1) network = compute_fakes.create_one_network()