Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 175 additions & 5 deletions netbox_bgp/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
from rest_framework.serializers import HyperlinkedIdentityField, ValidationError
from django.contrib.contenttypes.models import ContentType
from drf_spectacular.utils import extend_schema_field
from ipam.constants import VLANGROUP_SCOPE_TYPES
from rest_framework.serializers import (
CharField,
JSONField,
HyperlinkedIdentityField,
IntegerField,
SerializerMethodField,
ValidationError
)
from rest_framework.relations import PrimaryKeyRelatedField
from netbox.api.fields import ChoiceField, SerializedPKRelatedField
from netbox.api.fields import ChoiceField, ContentTypeField, SerializedPKRelatedField
from netbox.api.serializers import NetBoxModelSerializer
from ipam.api.serializers import IPAddressSerializer, ASNSerializer, PrefixSerializer
from ipam.api.serializers import IPAddressSerializer, ASNSerializer, PrefixSerializer, VRFSerializer
from tenancy.api.serializers import TenantSerializer
from dcim.api.serializers import SiteSerializer, DeviceSerializer
from ipam.api.field_serializers import IPNetworkField
from utilities.api import get_serializer_for_model
from virtualization.api.serializers import VirtualMachineSerializer

from netbox_bgp.models import (
Expand All @@ -19,15 +30,27 @@
CommunityList,
CommunityListRule,
ASPathList,
ASPathListRule
ASPathListRule,
Redistributing
)

from netbox_bgp.choices import CommunityStatusChoices, SessionStatusChoices
from netbox_bgp.choices import CommunityStatusChoices, SessionStatusChoices, RedistributeSourceChoices


class ASPathListSerializer(NetBoxModelSerializer):
url = HyperlinkedIdentityField(view_name="plugins-api:netbox_bgp-api:aspathlist-detail")

scope_type = ContentTypeField(
queryset=ContentType.objects.filter(
model__in=VLANGROUP_SCOPE_TYPES
),
allow_null=True,
required=False,
default=None
)
scope_id = IntegerField(allow_null=True, required=False, default=None)
scope = SerializerMethodField(read_only=True)

class Meta:
model = ASPathList
fields = [
Expand All @@ -36,12 +59,22 @@ class Meta:
"name",
"display",
"description",
"scope_type",
"scope_id",
"scope",
"tags",
"custom_fields",
"comments",
]
brief_fields = ("id", "url", "display", "name", "description")

@extend_schema_field(JSONField(allow_null=True))
def get_scope(self, obj):
if obj.scope_id is None:
return None
serializer = get_serializer_for_model(obj.scope)
context = {"request": self.context["request"]}
return serializer(obj.scope, nested=True, context=context).data

class ASPathListRuleSerializer(NetBoxModelSerializer):
aspath_list = ASPathListSerializer(nested=True)
Expand All @@ -68,6 +101,17 @@ class Meta:
class RoutingPolicySerializer(NetBoxModelSerializer):
url = HyperlinkedIdentityField(view_name="plugins-api:netbox_bgp-api:routingpolicy-detail")

scope_type = ContentTypeField(
queryset=ContentType.objects.filter(
model__in=VLANGROUP_SCOPE_TYPES
),
allow_null=True,
required=False,
default=None
)
scope_id = IntegerField(allow_null=True, required=False, default=None)
scope = SerializerMethodField(read_only=True)

class Meta:
model = RoutingPolicy
fields = (
Expand All @@ -77,16 +121,38 @@ class Meta:
"name",
"description",
"weight",
"scope_type",
"scope_id",
"scope",
"redistributing",
"tags",
"custom_fields",
"comments",
)
brief_fields = ("id", "url", "display", "name", "description")

@extend_schema_field(JSONField(allow_null=True))
def get_scope(self, obj):
if obj.scope_id is None:
return None
serializer = get_serializer_for_model(obj.scope)
context = {"request": self.context["request"]}
return serializer(obj.scope, nested=True, context=context).data

class PrefixListSerializer(NetBoxModelSerializer):
url = HyperlinkedIdentityField(view_name="plugins-api:netbox_bgp-api:prefixlist-detail")

scope_type = ContentTypeField(
queryset=ContentType.objects.filter(
model__in=VLANGROUP_SCOPE_TYPES
),
allow_null=True,
required=False,
default=None
)
scope_id = IntegerField(allow_null=True, required=False, default=None)
scope = SerializerMethodField(read_only=True)

class Meta:
model = PrefixList
fields = (
Expand All @@ -95,17 +161,37 @@ class Meta:
"name",
"display",
"description",
"scope_type",
"scope_id",
"scope",
"family",
"tags",
"custom_fields",
"comments",
)
brief_fields = ("id", "url", "display", "name", "description")

@extend_schema_field(JSONField(allow_null=True))
def get_scope(self, obj):
if obj.scope_id is None:
return None
serializer = get_serializer_for_model(obj.scope)
context = {"request": self.context["request"]}
return serializer(obj.scope, nested=True, context=context).data

class BGPPeerGroupSerializer(NetBoxModelSerializer):
url = HyperlinkedIdentityField(view_name="plugins-api:netbox_bgp-api:bgppeergroup-detail")

scope_type = ContentTypeField(
queryset=ContentType.objects.filter(
model__in=VLANGROUP_SCOPE_TYPES
),
allow_null=True,
required=False,
default=None
)
scope_id = IntegerField(allow_null=True, required=False, default=None)
scope = SerializerMethodField(read_only=True)
import_policies = SerializedPKRelatedField(
queryset=RoutingPolicy.objects.all(),
serializer=RoutingPolicySerializer,
Expand All @@ -131,13 +217,23 @@ class Meta:
"display",
"name",
"description",
"scope_type",
"scope_id",
"scope",
"import_policies",
"export_policies",
"comments",
"custom_fields",
)
brief_fields = ("id", "url", "display", "name", "description")

@extend_schema_field(JSONField(allow_null=True))
def get_scope(self, obj):
if obj.scope_id is None:
return None
serializer = get_serializer_for_model(obj.scope)
context = {"request": self.context["request"]}
return serializer(obj.scope, nested=True, context=context).data

class BGPSessionSerializer(NetBoxModelSerializer):
url = HyperlinkedIdentityField(view_name="plugins-api:netbox_bgp-api:bgpsession-detail")
Expand Down Expand Up @@ -257,6 +353,17 @@ class Meta:
class CommunityListSerializer(NetBoxModelSerializer):
url = HyperlinkedIdentityField(view_name="plugins-api:netbox_bgp-api:communitylist-detail")

scope_type = ContentTypeField(
queryset=ContentType.objects.filter(
model__in=VLANGROUP_SCOPE_TYPES
),
allow_null=True,
required=False,
default=None
)
scope_id = IntegerField(allow_null=True, required=False, default=None)
scope = SerializerMethodField(read_only=True)

class Meta:
model = CommunityList
fields = (
Expand All @@ -265,12 +372,22 @@ class Meta:
"name",
"display",
"description",
"scope_type",
"scope_id",
"scope",
"tags",
"custom_fields",
"comments",
)
brief_fields = ("id", "url", "display", "name", "description")

@extend_schema_field(JSONField(allow_null=True))
def get_scope(self, obj):
if obj.scope_id is None:
return None
serializer = get_serializer_for_model(obj.scope)
context = {"request": self.context["request"]}
return serializer(obj.scope, nested=True, context=context).data

class CommunityListRuleSerializer(NetBoxModelSerializer):
community_list = CommunityListSerializer(nested=True)
Expand Down Expand Up @@ -388,3 +505,56 @@ class Meta:
)
brief_fields = ("id", "display", "description")


class RedistributingSerializer(NetBoxModelSerializer):
url = HyperlinkedIdentityField(view_name="plugins-api:netbox_bgp-api:redistributing-detail")
name = CharField(required=True, allow_null=False)
scope_type = ContentTypeField(
queryset=ContentType.objects.filter(
model__in=VLANGROUP_SCOPE_TYPES
),
allow_null=True,
required=False,
default=None
)
scope_id = IntegerField(allow_null=True, required=False, default=None)
scope = SerializerMethodField(read_only=True)
vrf = VRFSerializer(nested=True, required=False, allow_null=True)
tenant = TenantSerializer(nested=True, required=False, allow_null=True)
device = DeviceSerializer(nested=True, required=False, allow_null=True)
virtualmachine = VirtualMachineSerializer(nested=True, required=False, allow_null=True)
redistribute_source = ChoiceField(choices=RedistributeSourceChoices, required=True, allow_null=False)
redistribute_policy = RoutingPolicySerializer(required=True, allow_null=False)

class Meta:
model = Redistributing
fields = (
"id",
"url",
"tags",
"custom_fields",
"display",
"scope_type",
"scope_id",
"scope",
"vrf",
"tenant",
"device",
"virtualmachine",
"redistribute_source",
"redistribute_policy",
"created",
"last_updated",
"name",
"description",
"comments",
)
brief_fields = ("id", "url", "name", "display", "device", "virtualmachine", "redistribute_source", "redistribute_policy")

@extend_schema_field(JSONField(allow_null=True))
def get_scope(self, obj):
if obj.scope_id is None:
return None
serializer = get_serializer_for_model(obj.scope)
context = {"request": self.context["request"]}
return serializer(obj.scope, nested=True, context=context).data
3 changes: 2 additions & 1 deletion netbox_bgp/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
BGPSessionViewSet, RoutingPolicyViewSet, BGPPeerGroupViewSet, CommunityViewSet,
PrefixListViewSet, PrefixListRuleViewSet, RoutingPolicyRuleViewSet,
CommunityListViewSet, CommunityListRuleViewSet, RootView,
ASPathListViewSet, ASPathListRuleViewSet
ASPathListViewSet, ASPathListRuleViewSet, RedistributingViewSet,
)


Expand All @@ -23,5 +23,6 @@
router.register('community-list-rule', CommunityListRuleViewSet)
router.register('aspath-list', ASPathListViewSet)
router.register('aspath-list-rule', ASPathListRuleViewSet)
router.register('redistributing', RedistributingViewSet, 'redistributing')

urlpatterns = router.urls
12 changes: 9 additions & 3 deletions netbox_bgp/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
BGPSessionSerializer, RoutingPolicySerializer, BGPPeerGroupSerializer,
CommunitySerializer, PrefixListSerializer, PrefixListRuleSerializer,
RoutingPolicyRuleSerializer, CommunityListSerializer, CommunityListRuleSerializer,
ASPathListSerializer, ASPathListRuleSerializer
ASPathListSerializer, ASPathListRuleSerializer, RedistributingSerializer,
)
from netbox_bgp.models import (
BGPSession, RoutingPolicy, BGPPeerGroup,
Community, PrefixList, PrefixListRule,
RoutingPolicyRule, CommunityList, CommunityListRule,
ASPathList, ASPathListRule
ASPathList, ASPathListRule, Redistributing,
)
from netbox_bgp.filtersets import (
BGPSessionFilterSet, RoutingPolicyFilterSet, BGPPeerGroupFilterSet,
CommunityFilterSet, PrefixListFilterSet, PrefixListRuleFilterSet,
RoutingPolicyRuleFilterSet, CommunityListFilterSet, CommunityListRuleFilterSet,
ASPathListFilterSet, ASPathListRuleFilterSet
ASPathListFilterSet, ASPathListRuleFilterSet, RedistributingFilterSet,
)

class RootView(APIRootView):
Expand Down Expand Up @@ -89,3 +89,9 @@ class ASPathListRuleViewSet(NetBoxModelViewSet):
queryset = ASPathListRule.objects.all()
serializer_class = ASPathListRuleSerializer
filterset_class = ASPathListRuleFilterSet


class RedistributingViewSet(NetBoxModelViewSet):
queryset = Redistributing.objects.all()
serializer_class = RedistributingSerializer
filterset_class = RedistributingFilterSet
22 changes: 22 additions & 0 deletions netbox_bgp/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@ class SessionStatusChoices(ChoiceSet):
]


class RedistributeSourceChoices(ChoiceSet):
key = "Redistributing.redistribute_source"

REDISTRIBUTE_DIRECT = 'Direct'
REDISTRIBUTE_STATIC = 'Static'
REDISTRIBUTE_OSPF = 'OSPF'
REDISTRIBUTE_ISIS = 'IS-IS'
REDISTRIBUTE_EIGRP = 'EIGRP'
REDISTRIBUTE_RIP = 'RIP'
REDISTRIBUTE_BGP = 'BGP'

CHOICES = [
(REDISTRIBUTE_DIRECT, 'Direct', 'cyan'),
(REDISTRIBUTE_STATIC, 'Static', 'orange'),
(REDISTRIBUTE_OSPF, 'OSPF', 'green'),
(REDISTRIBUTE_ISIS, 'IS-IS', 'yellow'),
(REDISTRIBUTE_EIGRP, 'EIGRP', 'red'),
(REDISTRIBUTE_RIP, 'RIP', 'black'),
(REDISTRIBUTE_BGP, 'BGP', 'blue'),
]


class ActionChoices(ChoiceSet):
key = "Action.status"

Expand Down
Loading