diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index 034199822d..78c567889f 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -1542,6 +1542,41 @@ class RemovePort(command.Command): server.interface_detach(port_id) +class RemoveNetwork(command.Command): + _description = _("Remove all ports of a network from server") + + def get_parser(self, prog_name): + parser = super(RemoveNetwork, self).get_parser(prog_name) + parser.add_argument( + "server", + metavar="", + help=_("Server to remove the port from (name or ID)"), + ) + parser.add_argument( + "network", + metavar="", + help=_("Network to remove from the server (name or ID)"), + ) + return parser + + def take_action(self, parsed_args): + compute_client = self.app.client_manager.compute + + server = utils.find_resource( + compute_client.servers, parsed_args.server) + + if self.app.client_manager.is_network_endpoint_enabled(): + network_client = self.app.client_manager.network + net_id = network_client.find_network( + parsed_args.network, ignore_missing=False).id + else: + net_id = parsed_args.network + + for inf in server.interface_list(): + if inf.net_id == net_id: + server.interface_detach(inf.port_id) + + class RemoveServerSecurityGroup(command.Command): _description = _("Remove security group from server") diff --git a/openstackclient/tests/unit/compute/v2/test_server.py b/openstackclient/tests/unit/compute/v2/test_server.py index ad52e23225..20b9532aad 100644 --- a/openstackclient/tests/unit/compute/v2/test_server.py +++ b/openstackclient/tests/unit/compute/v2/test_server.py @@ -2380,6 +2380,57 @@ class TestServerRemovePort(TestServer): self.find_port.assert_not_called() +class TestServerRemoveNetwork(TestServer): + + def setUp(self): + super(TestServerRemoveNetwork, self).setUp() + + # Get the command object to test + self.cmd = server.RemoveNetwork(self.app, None) + + # Set method to be tested. + self.fake_inf = mock.Mock() + self.methods = { + 'interface_list': [self.fake_inf], + 'interface_detach': None, + } + + self.find_network = mock.Mock() + self.app.client_manager.network.find_network = self.find_network + + def _test_server_remove_network(self, network_id): + self.fake_inf.net_id = network_id + self.fake_inf.port_id = 'fake-port' + servers = self.setup_servers_mock(count=1) + network = 'fake-network' + + arglist = [ + servers[0].id, + network, + ] + verifylist = [ + ('server', servers[0].id), + ('network', network), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + result = self.cmd.take_action(parsed_args) + + servers[0].interface_list.assert_called_once_with() + servers[0].interface_detach.assert_called_once_with('fake-port') + self.assertIsNone(result) + + def test_server_remove_network(self): + self._test_server_remove_network(self.find_network.return_value.id) + self.find_network.assert_called_once_with( + 'fake-network', ignore_missing=False) + + def test_server_remove_network_no_neutron(self): + self.app.client_manager.network_endpoint_enabled = False + self._test_server_remove_network('fake-network') + self.find_network.assert_not_called() + + @mock.patch( 'openstackclient.api.compute_v2.APIv2.security_group_find' ) diff --git a/releasenotes/notes/add-server-remove-network-fb09c53d5b0c0068.yaml b/releasenotes/notes/add-server-remove-network-fb09c53d5b0c0068.yaml new file mode 100644 index 0000000000..6aaf905dea --- /dev/null +++ b/releasenotes/notes/add-server-remove-network-fb09c53d5b0c0068.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Add ``server remove network`` command. This command will remove all + network ports from the specified network and instance. diff --git a/setup.cfg b/setup.cfg index 97333f61d1..e7b9cb6a2f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -118,6 +118,7 @@ openstack.compute.v2 = server_remove_fixed_ip = openstackclient.compute.v2.server:RemoveFixedIP server_remove_floating_ip = openstackclient.compute.v2.server:RemoveFloatingIP server_remove_port = openstackclient.compute.v2.server:RemovePort + server_remove_network = openstackclient.compute.v2.server:RemoveNetwork server_remove_security_group = openstackclient.compute.v2.server:RemoveServerSecurityGroup server_remove_volume = openstackclient.compute.v2.server:RemoveServerVolume server_rescue = openstackclient.compute.v2.server:RescueServer