Skip to content

Commit c89c978

Browse files
committed
feat: Add BGP Sessions tab to Interface detail pages
- Add InterfaceBGPSessionsView class with tab registration for displaying BGP sessions associated with interfaces via their IP addresses - Fix GenericForeignKey filtering by using ContentType and assigned_object_type/id fields instead of direct assigned_object lookup - Add necessary imports for Interface and ContentType models
1 parent d770996 commit c89c978

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

netbox_bgp/template_content.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
from dcim.models import Device, Site
1+
from dcim.models import Device, Interface, Site
22
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)
310
from netbox.plugins import PluginTemplateExtension
411
from netbox.views.generic import ObjectChildrenView
512
from utilities.views import ViewTab, register_model_view
@@ -164,6 +171,43 @@ def get_children(self, request: HttpRequest, parent: ASN) -> QuerySet[BGPSession
164171
return ASNBGPSessionsView._get_asn_bgp_sessions(parent)
165172

166173

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+
167211
# Register only when device_ext_page is set to 'tab';
168212
class DeviceBGPSessionsView(generic.ObjectChildrenView):
169213
"""View to display BGP sessions associated with a device."""

0 commit comments

Comments
 (0)