Skip to content

Commit d770996

Browse files
committed
feat: Add BGP sessions tab to ASN view
- Implement ASNBGPSessionsView in [netbox_bgp/template_content.py]template_content.py ) as an ObjectChildrenView for displaying BGP sessions associated with an ASN - Include tab with badge showing count of related BGP sessions (where ASN is local or remote AS) and permission check for viewing
1 parent 6c18a28 commit d770996

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

netbox_bgp/template_content.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,33 @@ def get_children(
137137
return BGPSession.objects.filter(virtualmachine=parent)
138138

139139

140+
@register_model_view(ASN, name="bgp-sessions", path="bgp-sessions")
141+
class ASNBGPSessionsView(generic.ObjectChildrenView):
142+
"""View to display BGP sessions associated with an ASN."""
143+
144+
queryset = ASN.objects.all()
145+
child_model = BGPSession
146+
filterset = BGPSessionFilterSet
147+
table = BGPSessionTable
148+
template_name = "generic/object_children.html"
149+
hide_if_empty = False
150+
151+
@staticmethod
152+
def _get_asn_bgp_sessions(asn: ASN) -> QuerySet[BGPSession]:
153+
"""Helper to get BGP sessions related to an ASN."""
154+
return BGPSession.objects.filter(Q(local_as=asn) | Q(remote_as=asn)).distinct()
155+
156+
tab = ViewTab(
157+
label="BGP Sessions",
158+
badge=lambda obj: ASNBGPSessionsView._get_asn_bgp_sessions(obj).count(),
159+
permission="netbox_bgp.view_bgpsession",
160+
)
161+
162+
def get_children(self, request: HttpRequest, parent: ASN) -> QuerySet[BGPSession]:
163+
"""Get BGP sessions where the ASN is either the local or remote AS."""
164+
return ASNBGPSessionsView._get_asn_bgp_sessions(parent)
165+
166+
140167
# Register only when device_ext_page is set to 'tab';
141168
class DeviceBGPSessionsView(generic.ObjectChildrenView):
142169
"""View to display BGP sessions associated with a device."""

0 commit comments

Comments
 (0)