Skip to content

Commit

Permalink
Move Session model to use the NetBox core ASN model
Browse files Browse the repository at this point in the history
ported @nahun's commit to latest k01ek/netbox-bgp@develop
Credits: Nathan Wheeler <[email protected]>
nahun@6b870b0
  • Loading branch information
christianpinger committed Jul 29, 2022
1 parent 33f1d5b commit 99a01b0
Show file tree
Hide file tree
Showing 21 changed files with 97 additions and 685 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ BGP Session
BGP Sessions
![BGP Session Table](docs/img/bgp_sess_list.png)

ASN
![ASN](docs/img/asn.png)

ASNs
![ASN Table](docs/img/asn_list.png)

Community
![Community](docs/img/commun.png)

Expand Down
2 changes: 1 addition & 1 deletion develop/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG python_ver=3.7
ARG python_ver=3.8
FROM python:${python_ver}

ARG netbox_ver=master
Expand Down
Binary file removed docs/img/asn.png
Binary file not shown.
Binary file removed docs/img/asn_list.png
Binary file not shown.
7 changes: 1 addition & 6 deletions netbox_bgp/admin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
from django.contrib import admin
from .models import ASN, Community, BGPSession, BGPPeerGroup, RoutingPolicy


@admin.register(ASN)
class ASNAdmin(admin.ModelAdmin):
fields = ('number', 'status', 'description')
from .models import Community, BGPSession, BGPPeerGroup, RoutingPolicy


@admin.register(Community)
Expand Down
39 changes: 3 additions & 36 deletions netbox_bgp/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from dcim.api.nested_serializers import NestedSiteSerializer, NestedDeviceSerializer
from tenancy.api.nested_serializers import NestedTenantSerializer
from extras.api.nested_serializers import NestedTagSerializer
from ipam.api.nested_serializers import NestedIPAddressSerializer
from ipam.api.nested_serializers import NestedIPAddressSerializer, NestedASNSerializer


from netbox_bgp.models import (
ASN, ASNStatusChoices, BGPSession, SessionStatusChoices, RoutingPolicy, BGPPeerGroup,
CommunityStatusChoices, BGPSession, SessionStatusChoices, RoutingPolicy, BGPPeerGroup,
Community, RoutingPolicyRule
)

Expand All @@ -25,39 +25,6 @@ def to_representation(self, value):
return self.serializer(value, context={'request': self.context['request']}).data


class ASNSerializer(NetBoxModelSerializer):
status = ChoiceField(choices=ASNStatusChoices, required=False)
site = NestedSiteSerializer(required=False, allow_null=True)
tenant = NestedTenantSerializer(required=False, allow_null=True)

def validate(self, attrs):
try:
number = attrs['number']
tenant = attrs.get('tenant')
except KeyError:
# this is patch
return attrs
if ASN.objects.filter(number=number, tenant=tenant).exists():
raise ValidationError(
{'error': 'Asn with this Number and Tenant already exists.'}
)
return attrs

class Meta:
ref_name = 'BGP_ASN'
model = ASN
fields = ['number', 'id', 'display', 'status', 'description', 'custom_fields', 'site', 'tenant', 'tags']


class NestedASNSerializer(WritableNestedSerializer):
url = HyperlinkedIdentityField(view_name='plugins:netbox_bgp:asn')

class Meta:
ref_name = 'BGP_ASN_Nested'
model = ASN
fields = ['id', 'url', 'number', 'description']


class RoutingPolicySerializer(NetBoxModelSerializer):
class Meta:
model = RoutingPolicy
Expand Down Expand Up @@ -170,7 +137,7 @@ class Meta:
validators = []

class CommunitySerializer(NetBoxModelSerializer):
status = ChoiceField(choices=ASNStatusChoices, required=False)
status = ChoiceField(choices=CommunityStatusChoices, required=False)
tenant = NestedTenantSerializer(required=False, allow_null=True)

class Meta:
Expand Down
6 changes: 1 addition & 5 deletions netbox_bgp/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
from rest_framework import routers

from .views import (
ASNViewSet, BGPSessionViewSet, RoutingPolicyViewSet, BGPPeerGroupViewSet,
CommunityViewSet
)
from .views import BGPSessionViewSet, RoutingPolicyViewSet, BGPPeerGroupViewSet, CommunityViewSet

router = routers.DefaultRouter()
router.register('asn', ASNViewSet)
router.register('session', BGPSessionViewSet, 'session')
router.register('bgpsession', BGPSessionViewSet, 'bgpsession')
router.register('routing-policy', RoutingPolicyViewSet)
Expand Down
12 changes: 3 additions & 9 deletions netbox_bgp/api/views.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
from rest_framework.viewsets import ModelViewSet

from .serializers import (
ASNSerializer, BGPSessionSerializer, RoutingPolicySerializer, BGPPeerGroupSerializer,
BGPSessionSerializer, RoutingPolicySerializer, BGPPeerGroupSerializer,
CommunitySerializer
)
from netbox_bgp.models import ASN, BGPSession, RoutingPolicy, BGPPeerGroup, Community
from netbox_bgp.models import BGPSession, RoutingPolicy, BGPPeerGroup, Community
from netbox_bgp.filters import (
ASNFilterSet, BGPSessionFilterSet, RoutingPolicyFilterSet, BGPPeerGroupFilterSet,
BGPSessionFilterSet, RoutingPolicyFilterSet, BGPPeerGroupFilterSet,
CommunityFilterSet
)


class ASNViewSet(ModelViewSet):
queryset = ASN.objects.all()
serializer_class = ASNSerializer
filterset_class = ASNFilterSet


class BGPSessionViewSet(ModelViewSet):
queryset = BGPSession.objects.all()
serializer_class = BGPSessionSerializer
Expand Down
31 changes: 4 additions & 27 deletions netbox_bgp/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,11 @@
from netaddr.core import AddrFormatError
from extras.filters import TagFilter

from .models import ASN, Community, BGPSession, RoutingPolicy, BGPPeerGroup
from ipam.models import IPAddress
from .models import Community, BGPSession, RoutingPolicy, BGPPeerGroup
from ipam.models import IPAddress, ASN
from dcim.models import Device


class ASNFilterSet(django_filters.FilterSet):
q = django_filters.CharFilter(
method='search',
label='Search',
)
tag = TagFilter()

class Meta:
model = ASN
fields = ['number', 'description', 'status', 'tenant', 'site']

def search(self, queryset, name, value):
"""Perform the filtered search."""
if not value.strip():
return queryset
qs_filter = (
Q(id__icontains=value)
| Q(number__icontains=value)
| Q(description__icontains=value)
)
return queryset.filter(qs_filter)


class CommunityFilterSet(django_filters.FilterSet):
q = django_filters.CharFilter(
method='search',
Expand Down Expand Up @@ -65,7 +42,7 @@ class BGPSessionFilterSet(django_filters.FilterSet):
remote_as = django_filters.ModelMultipleChoiceFilter(
field_name='remote_as__number',
queryset=ASN.objects.all(),
to_field_name='number',
to_field_name='asn',
label='Remote AS (Number)',
)
remote_as_id = django_filters.ModelMultipleChoiceFilter(
Expand All @@ -77,7 +54,7 @@ class BGPSessionFilterSet(django_filters.FilterSet):
local_as = django_filters.ModelMultipleChoiceFilter(
field_name='local_as__number',
queryset=ASN.objects.all(),
to_field_name='number',
to_field_name='asn',
label='Local AS (Number)',
)
local_as_id = django_filters.ModelMultipleChoiceFilter(
Expand Down
Loading

0 comments on commit 99a01b0

Please sign in to comment.