Add RemoveNetwork command to server

This command will detach a server from a network. All server's
neutron ports that belongs to the specified networks will be removed.

Change-Id: I83a064ed62ab00c6f1016900b9cf30f1c15b8382
This commit is contained in:
Hongbin Lu 2017-10-26 15:35:47 +00:00 committed by Hongbin Lu
parent 4742d4df70
commit 809355894f
4 changed files with 92 additions and 0 deletions

View File

@ -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="<server>",
help=_("Server to remove the port from (name or ID)"),
)
parser.add_argument(
"network",
metavar="<network>",
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")

View File

@ -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'
)

View File

@ -0,0 +1,5 @@
---
features:
- |
Add ``server remove network`` command. This command will remove all
network ports from the specified network and instance.

View File

@ -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