diff options
author | Nicolas Bock <nicolas.bock@canonical.com> | 2021-09-15 09:39:31 -0600 |
---|---|---|
committer | Akihiro Motoki <amotoki@gmail.com> | 2022-02-24 16:23:38 +0900 |
commit | 9f5d659d160d24d359992dda22fecb0db6b6f805 (patch) | |
tree | 06161ce956c9ecef836c72f281d6f11c3659e1ee | |
parent | 047b81e979e3ec96bd52a9e306eea8c985bcad7c (diff) | |
download | horizon-9f5d659d160d24d359992dda22fecb0db6b6f805.tar.gz |
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>
-rw-r--r-- | openstack_dashboard/dashboards/project/instances/tests.py | 12 | ||||
-rw-r--r-- | openstack_dashboard/dashboards/project/instances/utils.py | 20 |
2 files changed, 21 insertions, 11 deletions
diff --git a/openstack_dashboard/dashboards/project/instances/tests.py b/openstack_dashboard/dashboards/project/instances/tests.py index 075beea34..d2fa8e5b9 100644 --- a/openstack_dashboard/dashboards/project/instances/tests.py +++ b/openstack_dashboard/dashboards/project/instances/tests.py @@ -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) diff --git a/openstack_dashboard/dashboards/project/instances/utils.py b/openstack_dashboard/dashboards/project/instances/utils.py index 11bd5dc68..5f759cd69 100644 --- a/openstack_dashboard/dashboards/project/instances/utils.py +++ b/openstack_dashboard/dashboards/project/instances/utils.py @@ -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 |