|  | 
| 1 |  | -from dcim.models import Device, Site | 
|  | 1 | +from dcim.models import Device, Interface, Site | 
| 2 | 2 | from django.conf import settings | 
|  | 3 | +<<<<<<< HEAD | 
|  | 4 | +======= | 
|  | 5 | +from django.contrib.contenttypes.models import ContentType | 
|  | 6 | +from django.db.models import Q, QuerySet | 
|  | 7 | +from django.http import HttpRequest | 
|  | 8 | +from ipam.models import ASN, IPAddress | 
|  | 9 | +>>>>>>> ed30fbc (feat: Add BGP Sessions tab to Interface detail pages) | 
| 3 | 10 | from netbox.plugins import PluginTemplateExtension | 
| 4 | 11 | from netbox.views.generic import ObjectChildrenView | 
| 5 | 12 | from utilities.views import ViewTab, register_model_view | 
| @@ -164,6 +171,43 @@ def get_children(self, request: HttpRequest, parent: ASN) -> QuerySet[BGPSession | 
| 164 | 171 |         return ASNBGPSessionsView._get_asn_bgp_sessions(parent) | 
| 165 | 172 | 
 | 
| 166 | 173 | 
 | 
|  | 174 | +@register_model_view(Interface, name="bgp-sessions", path="bgp-sessions") | 
|  | 175 | +class InterfaceBGPSessionsView(generic.ObjectChildrenView): | 
|  | 176 | +    """View to display BGP sessions associated with an interface.""" | 
|  | 177 | + | 
|  | 178 | +    queryset = Interface.objects.all() | 
|  | 179 | +    child_model = BGPSession | 
|  | 180 | +    filterset = BGPSessionFilterSet | 
|  | 181 | +    table = BGPSessionTable | 
|  | 182 | +    template_name = "generic/object_children.html" | 
|  | 183 | +    hide_if_empty = False | 
|  | 184 | + | 
|  | 185 | +    @staticmethod | 
|  | 186 | +    def _get_interface_bgp_sessions(interface: Interface) -> QuerySet[BGPSession]: | 
|  | 187 | +        """Helper to get BGP sessions related to an interface via its IP addresses.""" | 
|  | 188 | +        ct = ContentType.objects.get_for_model(Interface) | 
|  | 189 | +        ips = IPAddress.objects.filter( | 
|  | 190 | +            assigned_object_type=ct, assigned_object_id=interface.pk | 
|  | 191 | +        ) | 
|  | 192 | +        return BGPSession.objects.filter( | 
|  | 193 | +            Q(local_address__in=ips) | Q(remote_address__in=ips) | 
|  | 194 | +        ).distinct() | 
|  | 195 | + | 
|  | 196 | +    tab = ViewTab( | 
|  | 197 | +        label="BGP Sessions", | 
|  | 198 | +        badge=lambda obj: InterfaceBGPSessionsView._get_interface_bgp_sessions( | 
|  | 199 | +            obj | 
|  | 200 | +        ).count(), | 
|  | 201 | +        permission="netbox_bgp.view_bgpsession", | 
|  | 202 | +    ) | 
|  | 203 | + | 
|  | 204 | +    def get_children( | 
|  | 205 | +        self, request: HttpRequest, parent: Interface | 
|  | 206 | +    ) -> QuerySet[BGPSession]: | 
|  | 207 | +        """Get BGP sessions for the interface.""" | 
|  | 208 | +        return InterfaceBGPSessionsView._get_interface_bgp_sessions(parent) | 
|  | 209 | + | 
|  | 210 | + | 
| 167 | 211 | # Register only when device_ext_page is set to 'tab'; | 
| 168 | 212 | class DeviceBGPSessionsView(generic.ObjectChildrenView): | 
| 169 | 213 |     """View to display BGP sessions associated with a device.""" | 
|  | 
0 commit comments