Skip to content

Commit

Permalink
Added port creation and floating IP logic
Browse files Browse the repository at this point in the history
  • Loading branch information
skanthed committed Oct 7, 2024
1 parent 2384f4f commit d90f51d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
33 changes: 33 additions & 0 deletions esi/lib/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
61 changes: 61 additions & 0 deletions esi/tests/unit/lib/test_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit d90f51d

Please sign in to comment.