Merge "Fix security group renderring in port overview (4)" into stable/2024.2

This commit is contained in:
Zuul
2025-06-16 18:45:54 +00:00
committed by Gerrit Code Review
4 changed files with 24 additions and 29 deletions

View File

@@ -657,6 +657,7 @@ class NetworkPortTests(test.BaseAdminViewTests):
@override_settings(POLICY_CHECK_FUNCTION='openstack_auth.policy.check')
@test.create_mocks({api.neutron: ('port_get',
'network_get',
'security_group_list',
'is_extension_supported')})
def test_add_allowed_address_pair_button_shown(self):
port = self.ports.first()
@@ -680,6 +681,7 @@ class NetworkPortTests(test.BaseAdminViewTests):
@override_settings(POLICY_CHECK_FUNCTION='openstack_auth.policy.check')
@test.create_mocks({api.neutron: ('port_get',
'network_get',
'security_group_list',
'port_update',
'is_extension_supported')})
def test_delete_address_pair_button_shown(self):

View File

@@ -16,6 +16,7 @@ from django.utils.translation import gettext_lazy as _
from horizon import exceptions
from horizon import tabs
from horizon.utils import memoized
from openstack_dashboard.dashboards.project.networks.ports.extensions. \
allowed_address_pairs import tabs as addr_pairs_tabs
@@ -30,8 +31,23 @@ class OverviewTab(tabs.Tab):
template_name = "project/networks/ports/_detail_overview.html"
def get_context_data(self, request):
@memoized.memoized_method
def get_security_groups(sg_ids):
# Avoid extra API calls if no security group is associated.
if not sg_ids:
return []
try:
security_groups = api.neutron.security_group_list(request,
id=sg_ids)
except Exception:
security_groups = []
msg = _("Unable to retrieve security groups for the port.")
exceptions.handle(request, msg)
return security_groups
port = self.tab_group.kwargs['port']
return {'port': port}
security_groups = get_security_groups(port.security_group_ids)
return {'port': port, 'security_groups': security_groups}
class PortDetailTabs(tabs.DetailTabsGroup):

View File

@@ -56,16 +56,13 @@ class NetworkPortTests(test.TestCase):
@test.create_mocks({api.neutron: ('network_get',
'port_get',
'is_extension_supported',
'security_group_list')})
'security_group_list',
'is_extension_supported')})
def _test_port_detail(self, mac_learning=False):
# Use a port associated with security group
port = [p for p in self.ports.list() if p.security_group_ids][0]
sgs = [sg for sg in self.security_groups.list()
if sg.id in port.security_group_ids]
network_id = self.networks.first().id
self.mock_port_get.return_value = port
self.mock_security_group_list.return_value = sgs
self._stub_is_extension_supported({'mac-learning': mac_learning,
'allowed-address-pairs': False})
self.mock_network_get.return_value = self.networks.first()
@@ -77,9 +74,6 @@ class NetworkPortTests(test.TestCase):
self.mock_port_get.assert_called_once_with(test.IsHttpRequest(),
port.id)
self.mock_security_group_list.assert_called_once_with(
test.IsHttpRequest(),
id=tuple(port.security_group_ids))
self._check_is_extension_supported({'mac-learning': 2,
'allowed-address-pairs': 2})
self.mock_network_get.assert_called_once_with(test.IsHttpRequest(),
@@ -297,6 +291,7 @@ class NetworkPortTests(test.TestCase):
@override_settings(POLICY_CHECK_FUNCTION='openstack_auth.policy.check')
@test.create_mocks({api.neutron: ('port_get',
'network_get',
'security_group_list',
'is_extension_supported')})
def test_add_allowed_address_pair_button_shown_to_network_owner(self):
port = self.ports.first()
@@ -414,6 +409,7 @@ class NetworkPortTests(test.TestCase):
@override_settings(POLICY_CHECK_FUNCTION='openstack_auth.policy.check')
@test.create_mocks({api.neutron: ('port_get',
'network_get',
'security_group_list',
'port_update',
'is_extension_supported')})
def test_delete_address_pair_button_shown_to_network_owner(self):

View File

@@ -28,7 +28,6 @@ from openstack_dashboard.dashboards.project.networks.ports \
import tabs as project_tabs
from openstack_dashboard.dashboards.project.networks.ports \
import workflows as project_workflows
from openstack_dashboard.utils import futurist_utils
STATE_DICT = dict(project_tables.DISPLAY_CHOICES)
@@ -106,24 +105,7 @@ class DetailView(tabs.TabbedTableView):
return network
@memoized.memoized_method
def get_security_groups(sg_ids):
# Avoid extra API calls if no security group is associated.
if not sg_ids:
return []
try:
security_groups = api.neutron.security_group_list(self.request,
id=sg_ids)
except Exception:
security_groups = []
msg = _("Unable to retrieve security groups for the port.")
exceptions.handle(self.request, msg)
return security_groups
results = futurist_utils.call_functions_parallel(
(get_network, [port.network_id]),
(get_security_groups, [tuple(port.security_group_ids)]))
network, security_groups = results
network = get_network(port.network_id)
port.network_name = network.get('name')
port.network_url = reverse(network_url, args=[port.network_id])
@@ -138,7 +120,6 @@ class DetailView(tabs.TabbedTableView):
]
context["custom_breadcrumb"] = breadcrumb
context["port"] = port
context["security_groups"] = security_groups
context["url"] = reverse(
'horizon:project:networks:ports_tab', args=[port.network_id])
context["actions"] = table.render_row_actions(port)