Skip to content

Commit

Permalink
Merge pull request #181 from k01ek/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
k01ek authored Feb 16, 2024
2 parents 789b547 + ff60d40 commit 644a62e
Show file tree
Hide file tree
Showing 25 changed files with 1,066 additions and 173 deletions.
59 changes: 45 additions & 14 deletions netbox_bgp/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

from netbox_bgp.models import (
BGPSession, RoutingPolicy, BGPPeerGroup,
Community, RoutingPolicyRule, PrefixList, PrefixListRule,
Community, RoutingPolicyRule, PrefixList,
PrefixListRule, CommunityList, CommunityListRule
)

from netbox_bgp.choices import CommunityStatusChoices, SessionStatusChoices
Expand All @@ -32,7 +33,7 @@ def to_representation(self, value):
class RoutingPolicySerializer(NetBoxModelSerializer):
class Meta:
model = RoutingPolicy
fields = ['id', 'name', 'description', 'tags', 'custom_fields']
fields = ['id', 'name', 'description', 'tags', 'custom_fields', 'comments']


class NestedRoutingPolicySerializer(WritableNestedSerializer):
Expand All @@ -44,6 +45,20 @@ class Meta:
validators = []


class NestedPrefixListSerializer(WritableNestedSerializer):
url = HyperlinkedIdentityField(view_name='plugins:netbox_bgp:prefixlist')

class Meta:
model = PrefixList
fields = ['id', 'url', 'display', 'name']


class PrefixListSerializer(NetBoxModelSerializer):
class Meta:
model = PrefixList
fields = ['id', 'name', 'display','description', 'family', 'tags', 'custom_fields', 'comments']


class BGPPeerGroupSerializer(NetBoxModelSerializer):
import_policies = SerializedPKRelatedField(
queryset=RoutingPolicy.objects.all(),
Expand All @@ -62,7 +77,7 @@ class BGPPeerGroupSerializer(NetBoxModelSerializer):

class Meta:
model = BGPPeerGroup
fields = ['id', 'display', 'name', 'description', 'import_policies', 'export_policies']
fields = ['id', 'display', 'name', 'description', 'import_policies', 'export_policies', 'comments']


class NestedBGPPeerGroupSerializer(WritableNestedSerializer):
Expand All @@ -84,6 +99,8 @@ class BGPSessionSerializer(NetBoxModelSerializer):
local_as = NestedASNSerializer(required=True, allow_null=False)
remote_as = NestedASNSerializer(required=True, allow_null=False)
peer_group = NestedBGPPeerGroupSerializer(required=False, allow_null=True)
prefix_list_in = NestedPrefixListSerializer(required=False, allow_null=True)
prefix_list_out = NestedPrefixListSerializer(required=False, allow_null=True)
import_policies = SerializedPKRelatedField(
queryset=RoutingPolicy.objects.all(),
serializer=NestedRoutingPolicySerializer,
Expand All @@ -106,8 +123,9 @@ class Meta:
'display', 'status', 'site', 'tenant',
'device', 'local_address', 'remote_address',
'local_as', 'remote_as', 'peer_group', 'import_policies',
'export_policies', 'created', 'last_updated',
'name', 'description'
'export_policies', 'prefix_list_in','prefix_list_out',
'created', 'last_updated',
'name', 'description', 'comments'
]


Expand Down Expand Up @@ -146,7 +164,7 @@ class Meta:
'id', 'tags', 'custom_fields', 'display',
'status', 'tenant', 'created', 'last_updated',
'description',
'value', 'site', 'role'
'value', 'site', 'role', 'comments'
]

class NestedCommunitySerializer(WritableNestedSerializer):
Expand All @@ -159,18 +177,31 @@ class Meta:
]


class NestedPrefixListSerializer(WritableNestedSerializer):
url = HyperlinkedIdentityField(view_name='plugins:netbox_bgp:prefixlist')
class CommunityListSerializer(NetBoxModelSerializer):
class Meta:
model = CommunityList
fields = ['id', 'name', 'display','description', 'tags', 'custom_fields', 'comments']


class NestedCommunityListSerializer(WritableNestedSerializer):
url = HyperlinkedIdentityField(view_name='plugins:netbox_bgp:communitylist')

class Meta:
model = PrefixList
model = CommunityList
fields = ['id', 'url', 'display', 'name']


class PrefixListSerializer(NetBoxModelSerializer):
class CommunityListRuleSerializer(NetBoxModelSerializer):
community_list = NestedCommunityListSerializer()
community = NestedCommunitySerializer(required=False, allow_null=True)

class Meta:
model = PrefixList
fields = ['id', 'name', 'display','description', 'family', 'tags', 'custom_fields']
model = CommunityListRule
fields = [
'id', 'tags', 'custom_fields', 'display',
'community_list', 'created', 'last_updated',
'action', 'community', 'comments'
]


class RoutingPolicyRuleSerializer(NetBoxModelSerializer):
Expand All @@ -196,7 +227,7 @@ class Meta:
fields = [
'id', 'index', 'display' ,'action', 'match_ip_address',
'routing_policy', 'match_community', 'match_custom', 'set_actions',
'match_ipv6_address', 'description', 'tags', 'custom_fields',
'match_ipv6_address', 'description', 'tags', 'custom_fields', 'comments'
]


Expand All @@ -211,5 +242,5 @@ class Meta:
'id', 'tags', 'custom_fields', 'display',
'prefix_list', 'created', 'last_updated',
'index', 'action',
'prefix_custom', 'ge', 'le', 'prefix'
'prefix_custom', 'ge', 'le', 'prefix', 'comments'
]
6 changes: 4 additions & 2 deletions netbox_bgp/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from .views import (
BGPSessionViewSet, RoutingPolicyViewSet, BGPPeerGroupViewSet, CommunityViewSet,
PrefixListViewSet, PrefixListRuleViewSet, RoutingPolicyRuleViewSet
PrefixListViewSet, PrefixListRuleViewSet, RoutingPolicyRuleViewSet,
CommunityListViewSet, CommunityListRuleViewSet
)

router = routers.DefaultRouter()
Expand All @@ -15,6 +16,7 @@
router.register('community', CommunityViewSet)
router.register('prefix-list', PrefixListViewSet)
router.register('prefix-list-rule', PrefixListRuleViewSet)

router.register('community-list', CommunityListViewSet)
router.register('community-list-rule', CommunityListRuleViewSet)

urlpatterns = router.urls
27 changes: 23 additions & 4 deletions netbox_bgp/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

from .serializers import (
BGPSessionSerializer, RoutingPolicySerializer, BGPPeerGroupSerializer,
CommunitySerializer, PrefixListSerializer, PrefixListRuleSerializer, RoutingPolicyRuleSerializer
CommunitySerializer, PrefixListSerializer, PrefixListRuleSerializer,
RoutingPolicyRuleSerializer, CommunityListSerializer, CommunityListRuleSerializer
)
from netbox_bgp.models import BGPSession, RoutingPolicy, BGPPeerGroup, Community, PrefixList, PrefixListRule, RoutingPolicyRule
from netbox_bgp.filters import (
from netbox_bgp.models import (
BGPSession, RoutingPolicy, BGPPeerGroup,
Community, PrefixList, PrefixListRule,
RoutingPolicyRule, CommunityList, CommunityListRule
)
from netbox_bgp.filtersets import (
BGPSessionFilterSet, RoutingPolicyFilterSet, BGPPeerGroupFilterSet,
CommunityFilterSet, PrefixListFilterSet, PrefixListRuleFilterSet, RoutingPolicyRuleFilterSet
CommunityFilterSet, PrefixListFilterSet, PrefixListRuleFilterSet,
RoutingPolicyRuleFilterSet, CommunityListFilterSet, CommunityListRuleFilterSet
)


Expand Down Expand Up @@ -41,11 +47,24 @@ class CommunityViewSet(NetBoxModelViewSet):
filterset_class = CommunityFilterSet


class CommunityListViewSet(NetBoxModelViewSet):
queryset = CommunityList.objects.all()
serializer_class = CommunityListSerializer
filterset_class = CommunityListFilterSet


class CommunityListRuleViewSet(NetBoxModelViewSet):
queryset = CommunityListRule.objects.all()
serializer_class = CommunityListRuleSerializer
filterset_class = CommunityListRuleFilterSet


class PrefixListViewSet(NetBoxModelViewSet):
queryset = PrefixList.objects.all()
serializer_class = PrefixListSerializer
filterset_class = PrefixListFilterSet


class PrefixListRuleViewSet(NetBoxModelViewSet):

queryset = PrefixListRule.objects.all()
Expand Down
41 changes: 40 additions & 1 deletion netbox_bgp/filters.py → netbox_bgp/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
from extras.filters import TagFilter
from netbox.filtersets import NetBoxModelFilterSet

from .models import Community, BGPSession, RoutingPolicy, RoutingPolicyRule, BGPPeerGroup, PrefixList, PrefixListRule
from .models import (
Community, BGPSession, RoutingPolicy, RoutingPolicyRule,
BGPPeerGroup, PrefixList, PrefixListRule, CommunityList,
CommunityListRule
)
from ipam.models import IPAddress, ASN
from dcim.models import Device, Site

Expand All @@ -27,6 +31,41 @@ def search(self, queryset, name, value):
return queryset.filter(qs_filter)


class CommunityListFilterSet(NetBoxModelFilterSet):

class Meta:
model = CommunityList
fields = ['id', 'name', 'description']

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


class CommunityListRuleFilterSet(NetBoxModelFilterSet):

class Meta:
model = CommunityListRule
fields = ['id', 'action', 'community_list', 'community_list_id']

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


class BGPSessionFilterSet(NetBoxModelFilterSet):

remote_as = django_filters.ModelMultipleChoiceFilter(
Expand Down
Loading

0 comments on commit 644a62e

Please sign in to comment.