Get ports directly instead of via loop

In order to get a list of available ports, the current implementation
of the form code in `project/instances/attach_interface` loops through
the available networks in the project and requests a list of ports for
each via the Neutron API. This implementation is O(N) in time with a
large prefactor (the time for a Neutron API call) where N is the
number of networks.

Instead of calling the Neutron API for each network this change uses
one call to the Neutron API to get the list of ports on all networks
and filters this list via a list comprehension. While this
implementation is still O(N) the prefactor is significantly smaller.

Closes-Bug: #1943639
Change-Id: I8fd32c3aad22d8ef7f57201f5144f6b2e357ef10
Signed-off-by: Nicolas Bock <nicolas.bock@canonical.com>
This commit is contained in:
Nicolas Bock 2021-09-15 09:39:31 -06:00 committed by Akihiro Motoki
parent 047b81e979
commit 9f5d659d16
2 changed files with 21 additions and 11 deletions

View File

@ -3005,13 +3005,15 @@ class ConsoleManagerTests(helpers.ResetImageAPIVersionMixin, helpers.TestCase):
self.assertRaises(exceptions.NotAvailable,
console.get_console, None, 'FAKE', None)
@helpers.create_mocks({api.neutron: ('network_list_for_tenant',)})
@helpers.create_mocks({api.neutron: ('network_list_for_tenant',
'port_list_with_trunk_types')})
def test_interface_attach_get(self):
server = self.servers.first()
self.mock_network_list_for_tenant.side_effect = [
self.networks.list()[:1],
[],
]
self.mock_port_list_with_trunk_types.return_value = self.ports.list()
url = reverse('horizon:project:instances:attach_interface',
args=[server.id])
@ -3024,8 +3026,11 @@ class ConsoleManagerTests(helpers.ResetImageAPIVersionMixin, helpers.TestCase):
mock.call(helpers.IsHttpRequest(), self.tenant.id),
])
self.assertEqual(2, self.mock_network_list_for_tenant.call_count)
self.mock_port_list_with_trunk_types.assert_called_once_with(
helpers.IsHttpRequest(), tenant_id=self.tenant.id)
@helpers.create_mocks({api.neutron: ('network_list_for_tenant',),
@helpers.create_mocks({api.neutron: ('network_list_for_tenant',
'port_list_with_trunk_types'),
api.nova: ('interface_attach',)})
def test_interface_attach_post(self):
fixed_ip = '10.0.0.10'
@ -3035,6 +3040,7 @@ class ConsoleManagerTests(helpers.ResetImageAPIVersionMixin, helpers.TestCase):
[network],
[],
]
self.mock_port_list_with_trunk_types.return_value = self.ports.list()
self.mock_interface_attach.return_value = None
form_data = {'instance_id': server.id,
@ -3054,6 +3060,8 @@ class ConsoleManagerTests(helpers.ResetImageAPIVersionMixin, helpers.TestCase):
mock.call(helpers.IsHttpRequest(), self.tenant.id),
])
self.assertEqual(2, self.mock_network_list_for_tenant.call_count)
self.mock_port_list_with_trunk_types.assert_called_once_with(
helpers.IsHttpRequest(), tenant_id=self.tenant.id)
self.mock_interface_attach.assert_called_once_with(
helpers.IsHttpRequest(), server.id,
net_id=network.id, fixed_ip=fixed_ip, port_id=None)

View File

@ -196,7 +196,7 @@ def port_field_data(request, with_network=False):
port_name = "{} ({})".format(
port.name_or_id, ",".join(
[ip['ip_address'] for ip in port['fixed_ips']]))
if with_network and network:
if with_network:
port_name += " - {}".format(network.name_or_id)
return port_name
@ -204,14 +204,16 @@ def port_field_data(request, with_network=False):
if api.base.is_service_enabled(request, 'network'):
network_list = api.neutron.network_list_for_tenant(
request, request.user.tenant_id)
for network in network_list:
ports.extend(
[(port.id, add_more_info_port_name(port, network))
for port in api.neutron.port_list_with_trunk_types(
request, network_id=network.id,
tenant_id=request.user.tenant_id)
if (not port.device_owner and
not isinstance(port, api.neutron.PortTrunkSubport))])
network_dict = dict((n.id, n) for n in network_list)
ports = [
(port.id,
add_more_info_port_name(port, network_dict[port.network_id]))
for port
in api.neutron.port_list_with_trunk_types(
request, tenant_id=request.user.tenant_id)
if (not port.device_owner and
not isinstance(port, api.neutron.PortTrunkSubport))
]
ports.sort(key=lambda obj: obj[1])
return ports