From d90f51dd08e0bbd64b012547c4f929b85e6cafb8 Mon Sep 17 00:00:00 2001 From: skanthed Date: Mon, 7 Oct 2024 00:29:52 -0400 Subject: [PATCH] Added port creation and floating IP logic --- esi/lib/networks.py | 33 ++++++++++++++++ esi/tests/unit/lib/test_networks.py | 61 +++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/esi/lib/networks.py b/esi/lib/networks.py index 92412fb..9531087 100644 --- a/esi/lib/networks.py +++ b/esi/lib/networks.py @@ -112,3 +112,36 @@ def get_networks_from_port(connection, port, networks_dict={}, floating_ips_dict floating_network = connection.network.get_network(floating_network_id) return parent_network, trunk_networks, trunk_ports, floating_network + + +def create_port(connection, network_name): + """ + Creates a port on the specified network using the network name provided. + + :param connection: An OpenStack connection object + :param network_name: The name of the network where the port should be created + + :return: The created port object + """ + network = connection.network.find_network(network_name) + + port = connection.network.create_port(network_id=network.id) + return port + + +def attach_floating_ip(connection, floating_ip_address, port_id): + """ + Attaches a floating IP to a port. + + :param connection: An OpenStack connection object + :param floating_ip_address: The floating IP address to attach (selected by the user in the UI) + :param port_id: The ID of the port to which the floating IP will be attached + + :return: The updated floating IP object + """ + floating_ip = next( + (ip for ip in connection.network.ips() if ip.floating_ip_address == floating_ip_address), None + ) + + updated_floating_ip = connection.network.update_ip(floating_ip, port_id=port_id) + return updated_floating_ip diff --git a/esi/tests/unit/lib/test_networks.py b/esi/tests/unit/lib/test_networks.py index 621288f..d8532ab 100644 --- a/esi/tests/unit/lib/test_networks.py +++ b/esi/tests/unit/lib/test_networks.py @@ -389,3 +389,64 @@ def test_get_networks_from_port_trunk(self): ) self.assertEqual(actual, expected) + + +class TestCreatePort(TestCase): + + def setUp(self): + super(TestCreatePort, self).setUp() + self.connection = mock.Mock() + + self.network_name = "test_network" + self.network = test_utils.create_mock_object({ + "id": "network_uuid", + "name": self.network_name + }) + + self.port = test_utils.create_mock_object({ + "id": "port_uuid", + "network_id": "network_uuid", + "name": "test_port", + }) + + def test_create_port_with_defaults(self): + self.connection.network.find_network.return_value = self.network + self.connection.network.create_port.return_value = self.port + + actual_port = networks.create_port(self.connection, self.network_name) + + self.connection.network.find_network.assert_called_once_with(self.network_name) + self.connection.network.create_port.assert_called_once_with( + network_id="network_uuid" + ) + self.assertEqual(actual_port, self.port) + + +class TestAttachFloatingIP(TestCase): + + def setUp(self): + super(TestAttachFloatingIP, self).setUp() + self.connection = mock.Mock() + self.floating_ip_address = "8.8.8.8" + self.port_id = "neutron_port_uuid_2" + + self.floating_ip = test_utils.create_mock_object({ + "id": "floating_ip_uuid", + "floating_ip_address": self.floating_ip_address, + "floating_network_id": "floating_network_id", + "port_id": self.port_id + }) + + def test_attach_floating_ip(self): + self.connection.network.ips.return_value = [self.floating_ip] + self.connection.network.update_ip.return_value = self.floating_ip + + actual_floating_ip = networks.attach_floating_ip( + self.connection, self.floating_ip_address, self.port_id + ) + + self.connection.network.ips.assert_called_once() + self.connection.network.update_ip.assert_called_once_with( + self.floating_ip, port_id=self.port_id + ) + self.assertEqual(actual_floating_ip, self.floating_ip)