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 10, 2024
1 parent 2384f4f commit a2a1161
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
41 changes: 41 additions & 0 deletions esi/lib/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,44 @@ 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=None):
"""
Creates a port on the specified network using the network object.
:param connection: An OpenStack connection object
:param network: The network object where the port should be created
:param name: Optional name for the port. If not provided, a default name is generated.
:return: The created port object
"""
if not name:
name = 'esi-port-{0}'.format(network.name)

existing_ports = list(connection.network.ports(name=name, status='DOWN'))
if existing_ports:
network_port = existing_ports[0]
else:
network_port = connection.network.create_port(name=name, network_id=network.id)

return network_port


def attach_floating_ip(connection, floating_ip_address, port_id):
"""
Attaches a floating IP to a port using the floating IP address.
:param connection: An OpenStack connection object
:param floating_ip_address: The floating IP address to attach
:param port_id: The ID of the port to which the floating IP will be attached
:return: The updated floating IP object
"""

floating_ips = connection.network.ips()

floating_ip = next(ip for ip in floating_ips if ip.floating_ip_address == floating_ip_address)

updated_floating_ip = connection.network.update_ip(floating_ip, port_id=port_id)

return updated_floating_ip
67 changes: 67 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,70 @@ 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 = test_utils.create_mock_object({
"id": "network_uuid",
"name": "test_network"
})
self.port = test_utils.create_mock_object({
"id": "port_uuid",
"network_id": "network_uuid",
"name": "esi-port-test_network",
})

def test_create_port_with_defaults(self):
self.connection.network.ports.return_value = []
self.connection.network.create_port.return_value = self.port
actual_port = networks.create_port(self.connection, self.network)
self.connection.network.ports.assert_called_once_with(
name='esi-port-test_network', status='DOWN'
)
self.connection.network.create_port.assert_called_once_with(
name='esi-port-test_network', network_id="network_uuid"
)
self.assertEqual(actual_port, self.port)

def test_create_port_with_existing_port(self):
self.connection.network.ports.return_value = [self.port]
actual_port = networks.create_port(self.connection, self.network)
self.connection.network.ports.assert_called_once_with(
name='esi-port-test_network', status='DOWN'
)
self.connection.network.create_port.assert_not_called()
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 a2a1161

Please sign in to comment.