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 c32b4b4
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions esi/lib/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,43 @@ 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_id, name=None, security_group_ids=None, fixed_ips=None):
"""
Creates a port on the specified network.
:param connection: An OpenStack connection object
:param network_id: The ID of the network where the port should be created
:param name: Optional name for the port
:param security_group_ids: Optional list of security group IDs to assign to the port
:param fixed_ips: Optional list of fixed IPs to assign to the port
:return: The created port object
"""
port_body = {
'network_id': network_id,
}

if name:
port_body['name'] = name
if security_group_ids:
port_body['security_group_ids'] = security_group_ids
if fixed_ips:
port_body['fixed_ips'] = fixed_ips

port = connection.network.create_port(**port_body)
return port

def attach_floating_ip(connection, floating_ip_id, port_id):
"""
Attaches a floating IP to a port.
:param connection: An OpenStack connection object
:param floating_ip_id: The ID of the floating IP 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_ip = connection.network.get_ip(floating_ip_id)
updated_floating_ip = connection.network.update_ip(floating_ip, port_id=port_id)
return updated_floating_ip

0 comments on commit c32b4b4

Please sign in to comment.