Skip to content

Commit

Permalink
Merge pull request #64 from tzumainn/port-forwarding
Browse files Browse the repository at this point in the history
Update `node network list` to include floating IP port forwarding
  • Loading branch information
tzumainn committed May 10, 2024
2 parents 8b19eb9 + e811ea4 commit 6453c86
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
40 changes: 28 additions & 12 deletions esiclient/tests/unit/v1/test_node_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,27 @@ def setUp(self):
"id": "floating_network_id",
"name": "floating_network"
})
self.floating_ip1 = utils.create_mock_object({
"id": "floating_ip_uuid_1",
"floating_ip_address": "9.9.9.9",
"floating_network_id": "floating_network_id",
"port_id": "neutron_port_uuid_1"
})
self.floating_ip2 = utils.create_mock_object({
self.floating_ip = utils.create_mock_object({
"id": "floating_ip_uuid_2",
"floating_ip_address": "8.8.8.8",
"floating_network_id": "floating_network_id",
"port_id": "neutron_port_uuid_2"

})
self.floating_ip_pfw = utils.create_mock_object({
"id": "floating_ip_uuid_1",
"floating_ip_address": "9.9.9.9",
"floating_network_id": "floating_network_id",
"port_id": None
})
self.pfw1 = utils.create_mock_object({
"internal_port": 22,
"external_port": 22,
"internal_port_id": "neutron_port_uuid_1"
})
self.pfw2 = utils.create_mock_object({
"internal_port": 23,
"external_port": 23,
"internal_port_id": "neutron_port_uuid_1"
})

def mock_node_get(node_uuid):
Expand All @@ -108,9 +117,16 @@ def mock_neutron_port_get(port_uuid):
elif port_uuid == "neutron_port_uuid_2":
return self.neutron_port2
return None

self.app.client_manager.network.get_port.\
side_effect = mock_neutron_port_get

def mock_neutron_port_forwardings(fip):
if fip.id == "floating_ip_uuid_1":
return [self.pfw1, self.pfw2]
return []
self.app.client_manager.network.port_forwardings.\
side_effect = mock_neutron_port_forwardings

self.app.client_manager.network.find_network.\
return_value = self.network
self.app.client_manager.network.get_network.\
Expand All @@ -122,7 +138,7 @@ def mock_neutron_port_get(port_uuid):
self.app.client_manager.baremetal.node.list.\
return_value = [self.node1, self.node2]
self.app.client_manager.network.ips.\
return_value = [self.floating_ip1, self.floating_ip2]
return_value = [self.floating_ip, self.floating_ip_pfw]

def test_take_action(self):
self.app.client_manager.baremetal.port.list.\
Expand All @@ -138,7 +154,7 @@ def test_take_action(self):
["Node", "MAC Address", "Port", "Network", "Fixed IP",
"Floating Network", "Floating IP"],
[['node1', 'aa:aa:aa:aa:aa:aa', 'neutron_port_1', 'test_network',
'1.1.1.1', 'floating_network', '9.9.9.9'],
'1.1.1.1', 'floating_network', '9.9.9.9 (22:22,23:23)'],
['node2', 'bb:bb:bb:bb:bb:bb', None, None, None, None, None],
['node2', 'cc:cc:cc:cc:cc:cc', 'neutron_port_2', 'test_network',
'2.2.2.2', 'floating_network', '8.8.8.8']]
Expand Down Expand Up @@ -204,7 +220,7 @@ def test_take_action_network_filter(self):
"Floating Network", "Floating IP"],
[["node1", "aa:aa:aa:aa:aa:aa",
"neutron_port_1", "test_network", "1.1.1.1",
"floating_network", "9.9.9.9"],
"floating_network", "9.9.9.9 (22:22,23:23)"],
["node2", "cc:cc:cc:cc:cc:cc",
"neutron_port_2", "test_network", "2.2.2.2",
"floating_network", "8.8.8.8"]]
Expand Down
42 changes: 33 additions & 9 deletions esiclient/v1/node_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ def get_parser(self, prog_name):
)
return parser

def _get_ports(self, neutron_client, network=None):
if network:
filter_network = neutron_client.find_network(network)
neutron_ports = list(neutron_client.ports(
network_id=filter_network.id))
else:
neutron_ports = list(neutron_client.ports())
return neutron_ports

def take_action(self, parsed_args):
self.log.debug("take_action(%s)", parsed_args)

Expand All @@ -67,18 +76,33 @@ def take_action(self, parsed_args):
filter_network = None
if parsed_args.network:
filter_network = neutron_client.find_network(parsed_args.network)
neutron_ports = list(neutron_client.ports(
network_id=filter_network.id))
else:
neutron_ports = list(neutron_client.ports())

floating_ips = list(neutron_client.ips())

networks = list(neutron_client.networks())
networks_dict = {n.id: n for n in networks}
# base network information
with concurrent.futures.ThreadPoolExecutor() as executor:
f1 = executor.submit(neutron_client.ips)
f2 = executor.submit(neutron_client.networks)
f3 = executor.submit(
self._get_ports, neutron_client, filter_network)
floating_ips = list(f1.result())
networks = list(f2.result())
networks_dict = {n.id: n for n in networks}
neutron_ports = f3.result()

# update floating IP list to include port forwarding information
for fip in floating_ips:
# no need to do this for floating IPs associated with a port,
# as port forwarding is irrelevant in such a case
if not fip.port_id:
pfws = list(neutron_client.port_forwardings(fip))
if len(pfws):
fip.port_id = pfws[0].internal_port_id
pfw_ports = ["%s:%s" % (pfw.internal_port,
pfw.external_port)
for pfw in pfws]
fip.floating_ip_address = "%s (%s)" % (
fip.floating_ip_address, ','.join(pfw_ports))

data = []

for port in ports:
if not parsed_args.node:
node_name = next((node for node in nodes
Expand Down

0 comments on commit 6453c86

Please sign in to comment.