Skip to content

Commit

Permalink
Merge pull request #160 from k01ek/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
k01ek authored Nov 13, 2023
2 parents 57a8b99 + d655f62 commit 9bb67da
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 80 deletions.
14 changes: 10 additions & 4 deletions netbox_bgp/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from dcim.api.nested_serializers import NestedSiteSerializer, NestedDeviceSerializer
from tenancy.api.nested_serializers import NestedTenantSerializer
from ipam.api.nested_serializers import NestedIPAddressSerializer, NestedASNSerializer, NestedPrefixSerializer
from ipam.api.field_serializers import IPNetworkField


from netbox_bgp.models import (
Expand All @@ -31,7 +32,7 @@ def to_representation(self, value):
class RoutingPolicySerializer(NetBoxModelSerializer):
class Meta:
model = RoutingPolicy
fields = '__all__'
fields = ['id', 'name', 'description']


class NestedRoutingPolicySerializer(WritableNestedSerializer):
Expand All @@ -42,6 +43,7 @@ class Meta:
fields = ['id', 'url', 'name', 'display', 'description']
validators = []


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

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


class NestedBGPPeerGroupSerializer(WritableNestedSerializer):
Expand Down Expand Up @@ -164,10 +166,12 @@ class Meta:
model = PrefixList
fields = ['id', 'url', 'display', 'name']


class PrefixListSerializer(NetBoxModelSerializer):
class Meta:
model = PrefixList
fields = '__all__'
fields = ['id', 'name', 'description', 'family']


class RoutingPolicyRuleSerializer(NetBoxModelSerializer):
match_ip_address = SerializedPKRelatedField(
Expand All @@ -189,11 +193,13 @@ class RoutingPolicyRuleSerializer(NetBoxModelSerializer):

class Meta:
model = RoutingPolicyRule
fields = '__all__'
fields = ['id', 'index', 'action', 'match_ip_address', 'routing_policy', 'match_community']


class PrefixListRuleSerializer(NetBoxModelSerializer):
prefix_list = NestedPrefixListSerializer()
prefix = NestedPrefixSerializer(required=False, allow_null=True)
prefix_custom = IPNetworkField(required=False, allow_null=True)

class Meta:
model = PrefixListRule
Expand Down
74 changes: 24 additions & 50 deletions netbox_bgp/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,27 @@

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


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

class Meta:
model = Community
fields = ['value', 'description', 'status', 'tenant']
fields = ['id', 'value', 'description', 'status', 'tenant']

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


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

remote_as = django_filters.ModelMultipleChoiceFilter(
field_name='remote_as__asn',
Expand Down Expand Up @@ -109,6 +98,18 @@ class BGPSessionFilterSet(NetBoxModelFilterSet):
to_field_name='name',
label='Device (name)',
)
site_id = django_filters.ModelMultipleChoiceFilter(
field_name='site__id',
queryset=Site.objects.all(),
to_field_name='id',
label='Site (ID)',
)
site = django_filters.ModelMultipleChoiceFilter(
field_name='site__name',
queryset=Site.objects.all(),
to_field_name='name',
label='DSite (name)',
)
by_remote_address = django_filters.CharFilter(
method='search_by_remote_ip',
label='Remote Address',
Expand All @@ -120,7 +121,7 @@ class BGPSessionFilterSet(NetBoxModelFilterSet):

class Meta:
model = BGPSession
fields = ['name', 'description', 'status', 'tenant']
fields = ['id', 'name', 'description', 'status', 'tenant']

def search(self, queryset, name, value):
"""Perform the filtered search."""
Expand Down Expand Up @@ -154,15 +155,10 @@ def search_by_local_ip(self, queryset, name, value):


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

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

def search(self, queryset, name, value):
"""Perform the filtered search."""
Expand All @@ -175,12 +171,7 @@ def search(self, queryset, name, value):
return queryset.filter(qs_filter)


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

class Meta:
model = RoutingPolicyRule
Expand All @@ -191,8 +182,7 @@ def search(self, queryset, name, value):
if not value.strip():
return queryset
qs_filter = (
Q(id__icontains=value)
| Q(index__icontains=value)
Q(index__icontains=value)
| Q(action__icontains=value)
| Q(description__icontains=value)
| Q(routing_policy_id__icontains=value)
Expand All @@ -201,16 +191,11 @@ def search(self, queryset, name, value):
return queryset.filter(qs_filter)


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

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

def search(self, queryset, name, value):
"""Perform the filtered search."""
Expand All @@ -224,15 +209,10 @@ def search(self, queryset, name, value):


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

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

def search(self, queryset, name, value):
"""Perform the filtered search."""
Expand All @@ -244,12 +224,7 @@ def search(self, queryset, name, value):
)
return queryset.filter(qs_filter)

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

class Meta:
model = PrefixListRule
Expand All @@ -261,8 +236,7 @@ def search(self, queryset, name, value):
if not value.strip():
return queryset
qs_filter = (
Q(id__icontains=value)
| Q(index__icontains=value)
Q(index__icontains=value)
| Q(action__icontains=value)
#| Q(prefix_custom__icontains=value)
| Q(ge__icontains=value)
Expand Down
30 changes: 7 additions & 23 deletions netbox_bgp/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist, ValidationError
from django.utils.translation import gettext as _

from extras.models import Tag
from tenancy.models import Tenant
from dcim.models import Device, Site
from ipam.models import IPAddress, Prefix, ASN
Expand All @@ -25,10 +24,6 @@


class CommunityForm(NetBoxModelForm):
tags = DynamicModelMultipleChoiceField(
queryset=Tag.objects.all(),
required=False
)
status = forms.ChoiceField(
required=False,
choices=CommunityStatusChoices,
Expand Down Expand Up @@ -114,10 +109,6 @@ class BGPSessionForm(NetBoxModelForm):
max_length=64,
required=True
)
tags = DynamicModelMultipleChoiceField(
queryset=Tag.objects.all(),
required=False
)
site = DynamicModelChoiceField(
queryset=Site.objects.all(),
required=False
Expand Down Expand Up @@ -236,6 +227,11 @@ class BGPSessionFilterForm(NetBoxModelFilterSetForm):
required=False,
label=_('Device')
)
site_id = DynamicModelMultipleChoiceField(
queryset=Site.objects.all(),
required=False,
label=_('Site')
)
status = forms.MultipleChoiceField(
choices=SessionStatusChoices,
required=False,
Expand Down Expand Up @@ -280,10 +276,6 @@ class RoutingPolicyFilterForm(NetBoxModelFilterSetForm):


class RoutingPolicyForm(NetBoxModelForm):
tags = DynamicModelMultipleChoiceField(
queryset=Tag.objects.all(),
required=False
)

class Meta:
model = RoutingPolicy
Expand Down Expand Up @@ -315,10 +307,6 @@ class BGPPeerGroupForm(NetBoxModelForm):
api_url='/api/plugins/bgp/routing-policy/'
)
)
tags = DynamicModelMultipleChoiceField(
queryset=Tag.objects.all(),
required=False
)

class Meta:
model = BGPPeerGroup
Expand Down Expand Up @@ -361,7 +349,7 @@ class Meta:
fields = [
'routing_policy', 'index', 'action', 'continue_entry', 'match_community',
'match_ip_address', 'match_ipv6_address', 'match_custom',
'set_actions', 'description',
'set_actions', 'description', 'tags'
]


Expand All @@ -376,10 +364,6 @@ class PrefixListFilterForm(NetBoxModelFilterSetForm):


class PrefixListForm(NetBoxModelForm):
tags = DynamicModelMultipleChoiceField(
queryset=Tag.objects.all(),
required=False
)

class Meta:
model = PrefixList
Expand Down Expand Up @@ -411,5 +395,5 @@ class Meta:
fields = [
'prefix_list', 'index',
'action', 'prefix', 'prefix_custom',
'ge', 'le'
'ge', 'le', 'tags'
]
Loading

0 comments on commit 9bb67da

Please sign in to comment.