Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API support for Prefix List Rules and Routing Policy Rules #124

Merged
Merged
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PYTHON_VER?=3.8
NETBOX_VER?=v3.3.3
NETBOX_VER?=v3.4.0

NAME=netbox-bgp

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This plugin provide following Models:
| NetBox 3.1 | 0.5.0 |
| NetBox 3.2 | >= 0.6.0 |
| NetBox 3.3 | >= 0.8.1 |
| NetBox 3.4 | >= 0.9.0 |

## Installation

Expand Down
2 changes: 1 addition & 1 deletion netbox_bgp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BGPConfig(PluginConfig):
base_url = 'bgp'
required_settings = []
min_version = '3.2.0'
max_version = '3.3.99'
max_version = '3.4.99'
default_settings = {
'device_ext_page': 'right',
}
Expand Down
4 changes: 3 additions & 1 deletion netbox_bgp/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

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

router = routers.DefaultRouter()
router.register('session', BGPSessionViewSet, 'session')
router.register('bgpsession', BGPSessionViewSet, 'bgpsession')
router.register('routing-policy', RoutingPolicyViewSet)
router.register('routing-policy-rule', RoutingPolicyRuleViewSet)
router.register('peer-group', BGPPeerGroupViewSet, 'peergroup')
router.register('bgppeergroup', BGPPeerGroupViewSet, 'bgppeergroup')
router.register('community', CommunityViewSet)
router.register('prefix-list', PrefixListViewSet)
router.register('prefix-list-rule', PrefixListRuleViewSet)


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

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


Expand All @@ -23,6 +23,12 @@ class RoutingPolicyViewSet(ModelViewSet):
filterset_class = RoutingPolicyFilterSet


class RoutingPolicyRuleViewSet(ModelViewSet):
queryset = RoutingPolicyRule.objects.all()
serializer_class = RoutingPolicyRuleSerializer
filterset_class = RoutingPolicyRuleFilterSet


class BGPPeerGroupViewSet(ModelViewSet):
queryset = BGPPeerGroup.objects.all()
serializer_class = BGPPeerGroupSerializer
Expand All @@ -39,3 +45,8 @@ class PrefixListViewSet(ModelViewSet):
queryset = PrefixList.objects.all()
serializer_class = PrefixListSerializer
filterset_class = PrefixListFilterSet

class PrefixListRuleViewSet(ModelViewSet):
queryset = PrefixListRule.objects.all()
serializer_class = PrefixListRuleSerializer
filterset_class = PrefixListRuleFilterSet
56 changes: 55 additions & 1 deletion netbox_bgp/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from netaddr.core import AddrFormatError
from extras.filters import TagFilter

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

Expand Down Expand Up @@ -174,6 +174,32 @@ 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 Meta:
model = RoutingPolicyRule
fields = ['id', 'index', 'action', 'description', 'routing_policy_id', 'continue_entry']

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


class BGPPeerGroupFilterSet(django_filters.FilterSet):
q = django_filters.CharFilter(
method='search',
Expand Down Expand Up @@ -216,3 +242,31 @@ def search(self, queryset, name, value):
| Q(description__icontains=value)
)
return queryset.filter(qs_filter)

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

class Meta:
model = PrefixListRule
#fields = ['index', 'action', 'prefix_custom', 'ge', 'le', 'prefix_list', 'prefix_list_id']
fields = ['id', 'index', 'action', 'ge', 'le', 'prefix_list', 'prefix_list_id']

def search(self, queryset, name, value):
"""Perform the filtered search."""
if not value.strip():
return queryset
qs_filter = (
Q(id__icontains=value)
| Q(index__icontains=value)
| Q(action__icontains=value)
#| Q(prefix_custom__icontains=value)
| Q(ge__icontains=value)
| Q(le__icontains=value)
| Q(prefix_list__icontains=value)
| Q(prefix_list_id__icontains=value)
)
return queryset.filter(qs_filter)
49 changes: 49 additions & 0 deletions netbox_bgp/migrations/0027_netbox_bgp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Generated by Django 4.1.4 on 2023-01-04 14:44

from django.db import migrations, models
import utilities.json


class Migration(migrations.Migration):

dependencies = [
('netbox_bgp', '0026_netbox_bgp'),
]

operations = [
migrations.AlterField(
model_name='bgppeergroup',
name='custom_field_data',
field=models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder),
),
migrations.AlterField(
model_name='bgpsession',
name='custom_field_data',
field=models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder),
),
migrations.AlterField(
model_name='community',
name='custom_field_data',
field=models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder),
),
migrations.AlterField(
model_name='prefixlist',
name='custom_field_data',
field=models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder),
),
migrations.AlterField(
model_name='prefixlistrule',
name='custom_field_data',
field=models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder),
),
migrations.AlterField(
model_name='routingpolicy',
name='custom_field_data',
field=models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder),
),
migrations.AlterField(
model_name='routingpolicyrule',
name='custom_field_data',
field=models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder),
),
]
2 changes: 1 addition & 1 deletion netbox_bgp/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.8.1"
__version__ = "0.9.0"