diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index d4edf8c535..034199822d 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -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="", + help=_("Server to add the network to (name or ID)"), + ) + parser.add_argument( + "network", + metavar="", + 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") diff --git a/openstackclient/tests/unit/compute/v2/test_server.py b/openstackclient/tests/unit/compute/v2/test_server.py index 660f4f9790..ad52e23225 100644 --- a/openstackclient/tests/unit/compute/v2/test_server.py +++ b/openstackclient/tests/unit/compute/v2/test_server.py @@ -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' ) diff --git a/releasenotes/notes/add-server-add-network-98ede8ff6079eb23.yaml b/releasenotes/notes/add-server-add-network-98ede8ff6079eb23.yaml new file mode 100644 index 0000000000..cf6dada673 --- /dev/null +++ b/releasenotes/notes/add-server-add-network-98ede8ff6079eb23.yaml @@ -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. diff --git a/setup.cfg b/setup.cfg index 2412b6fe02..f2a4436bbf 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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