Merge "Added AddNetwork command to server"

This commit is contained in:
Zuul 2017-10-30 04:21:41 +00:00 committed by Gerrit Code Review
commit a51ac0a889
4 changed files with 87 additions and 0 deletions

View File

@ -300,6 +300,39 @@ class AddPort(command.Command):
server.interface_attach(port_id=port_id, net_id=None, fixed_ip=None)
class AddNetwork(command.Command):
_description = _("Add network to server")
def get_parser(self, prog_name):
parser = super(AddNetwork, self).get_parser(prog_name)
parser.add_argument(
"server",
metavar="<server>",
help=_("Server to add the network to (name or ID)"),
)
parser.add_argument(
"network",
metavar="<network>",
help=_("Network to add to 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
server.interface_attach(port_id=None, net_id=net_id, fixed_ip=None)
class AddServerSecurityGroup(command.Command):
_description = _("Add security group to server")

View File

@ -232,6 +232,53 @@ class TestServerAddPort(TestServer):
self.find_port.assert_not_called()
class TestServerAddNetwork(TestServer):
def setUp(self):
super(TestServerAddNetwork, self).setUp()
# Get the command object to test
self.cmd = server.AddNetwork(self.app, None)
# Set add_fixed_ip method to be tested.
self.methods = {
'interface_attach': None,
}
self.find_network = mock.Mock()
self.app.client_manager.network.find_network = self.find_network
def _test_server_add_network(self, net_id):
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_attach.assert_called_once_with(
port_id=None, net_id=net_id, fixed_ip=None)
self.assertIsNone(result)
def test_server_add_network(self):
self._test_server_add_network(self.find_network.return_value.id)
self.find_network.assert_called_once_with(
'fake-network', ignore_missing=False)
def test_server_add_network_no_neutron(self):
self.app.client_manager.network_endpoint_enabled = False
self._test_server_add_network('fake-network')
self.find_network.assert_not_called()
@mock.patch(
'openstackclient.api.compute_v2.APIv2.security_group_find'
)

View File

@ -0,0 +1,6 @@
---
features:
- |
Add ``server add network`` command. This command will create a neutron
port from the specified neutron network and attach the port to the
specified instance.

View File

@ -104,6 +104,7 @@ openstack.compute.v2 =
server_add_fixed_ip = openstackclient.compute.v2.server:AddFixedIP
server_add_floating_ip = openstackclient.compute.v2.server:AddFloatingIP
server_add_port = openstackclient.compute.v2.server:AddPort
server_add_network = openstackclient.compute.v2.server:AddNetwork
server_add_security_group = openstackclient.compute.v2.server:AddServerSecurityGroup
server_add_volume = openstackclient.compute.v2.server:AddServerVolume
server_create = openstackclient.compute.v2.server:CreateServer