Skip to content

Commit

Permalink
Merge pull request #126 from k01ek/develop
Browse files Browse the repository at this point in the history
0.9.0
  • Loading branch information
k01ek authored Jan 25, 2023
2 parents e5a930c + da7170b commit f04e40f
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 32 deletions.
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
5 changes: 3 additions & 2 deletions netbox_bgp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ class BGPConfig(PluginConfig):
author_email = '[email protected]'
base_url = 'bgp'
required_settings = []
min_version = '3.2.0'
max_version = '3.3.99'
min_version = '3.4.0'
max_version = '3.4.99'
default_settings = {
'device_ext_page': 'right',
'top_level_menu' : False,
}


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
4 changes: 2 additions & 2 deletions netbox_bgp/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class AFISAFIChoices(ChoiceSet):

class IPAddressFamilyChoices(ChoiceSet):

FAMILY_4 = 4
FAMILY_6 = 6
FAMILY_4 = 'ipv4'
FAMILY_6 = 'ipv6'

CHOICES = (
(FAMILY_4, 'IPv4'),
Expand Down
73 changes: 64 additions & 9 deletions netbox_bgp/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
from django.db.models import Q
from netaddr.core import AddrFormatError
from extras.filters import TagFilter
from netbox.filtersets import NetBoxModelFilterSet

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


class CommunityFilterSet(django_filters.FilterSet):
class CommunityFilterSet(NetBoxModelFilterSet):
q = django_filters.CharFilter(
method='search',
label='Search',
Expand All @@ -32,15 +33,15 @@ def search(self, queryset, name, value):
return queryset.filter(qs_filter)


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

remote_as = django_filters.ModelMultipleChoiceFilter(
field_name='remote_as__number',
field_name='remote_as__asn',
queryset=ASN.objects.all(),
to_field_name='asn',
label='Remote AS (Number)',
Expand All @@ -52,7 +53,7 @@ class BGPSessionFilterSet(django_filters.FilterSet):
label='Remote AS (ID)',
)
local_as = django_filters.ModelMultipleChoiceFilter(
field_name='local_as__number',
field_name='local_as__asn',
queryset=ASN.objects.all(),
to_field_name='asn',
label='Local AS (Number)',
Expand Down Expand Up @@ -126,9 +127,9 @@ def search(self, queryset, name, value):
if not value.strip():
return queryset
qs_filter = (
Q(remote_as__number__icontains=value)
Q(remote_as__asn__icontains=value)
| Q(name__icontains=value)
| Q(local_as__number__icontains=value)
| Q(local_as__asn__icontains=value)
| Q(description__icontains=value)
)
return queryset.filter(qs_filter)
Expand All @@ -152,7 +153,7 @@ def search_by_local_ip(self, queryset, name, value):
return queryset.none()


class RoutingPolicyFilterSet(django_filters.FilterSet):
class RoutingPolicyFilterSet(NetBoxModelFilterSet):
q = django_filters.CharFilter(
method='search',
label='Search',
Expand All @@ -174,6 +175,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 All @@ -196,7 +223,7 @@ def search(self, queryset, name, value):
return queryset.filter(qs_filter)


class PrefixListFilterSet(django_filters.FilterSet):
class PrefixListFilterSet(NetBoxModelFilterSet):
q = django_filters.CharFilter(
method='search',
label='Search',
Expand All @@ -216,3 +243,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),
),
]
18 changes: 16 additions & 2 deletions netbox_bgp/navigation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from extras.plugins import PluginMenuButton, PluginMenuItem
from django.conf import settings

from extras.plugins import PluginMenuButton, PluginMenuItem, PluginMenu
from utilities.choices import ButtonColorChoices

menu_items = (

_menu_items = (
PluginMenuItem(
link='plugins:netbox_bgp:community_list',
link_text='Communities',
Expand Down Expand Up @@ -73,3 +76,14 @@
),
)
)

plugin_settings = settings.PLUGINS_CONFIG.get('netbox_bgp', {})

if plugin_settings.get('top_level_menu'):
menu = PluginMenu(
label="BGP",
groups=(("BGP", _menu_items),),
icon_class="mdi mdi-bootstrap",
)
else:
menu_items = _menu_items
4 changes: 2 additions & 2 deletions netbox_bgp/templates/netbox_bgp/prefixlist.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
<span class="mdi mdi-plus-thick" aria-hidden="true"></span> Rule
</a>
{% endif %}
{% if perms.netbox_bgp.change_policy %}
{% if perms.netbox_bgp.change_routingpolicy %}
<a href="{% url 'plugins:netbox_bgp:prefixlist_edit' pk=object.pk %}" class="btn btn-warning">
<span class="mdi mdi-pencil" aria-hidden="true"></span> Edit
</a>
{% endif %}
{% if perms.netbox_bgp.delete_policy %}
{% if perms.netbox_bgp.delete_routingpolicy %}
<a href="{% url 'plugins:netbox_bgp:prefixlist_delete' pk=object.pk %}" class="btn btn-danger">
<span class="mdi mdi-trash-can-outline" aria-hidden="true"></span> Delete
</a>
Expand Down
6 changes: 3 additions & 3 deletions netbox_bgp/templates/netbox_bgp/routingpolicy.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
{% endblock %}
{% block controls %}
<div class="pull-right noprint">
{% if perms.netbox_bgp.change_policy %}
{% if perms.netbox_bgp.change_routingpolicy %}
<a href="{% url 'plugins:netbox_bgp:routingpolicyrule_add' %}?routing_policy={{ object.pk }}" class="btn btn-success">
<span class="mdi mdi-plus-thick" aria-hidden="true"></span> Rule
</a>
{% endif %}
{% if perms.netbox_bgp.change_policy %}
{% if perms.netbox_bgp.change_routingpolicy %}
<a href="{% url 'plugins:netbox_bgp:routingpolicy_edit' pk=object.pk %}" class="btn btn-warning">
<span class="mdi mdi-pencil" aria-hidden="true"></span> Edit
</a>
{% endif %}
{% if perms.netbox_bgp.delete_policy %}
{% if perms.netbox_bgp.delete_routingpolicy %}
<a href="{% url 'plugins:netbox_bgp:routingpolicy_delete' pk=object.pk %}" class="btn btn-danger">
<span class="mdi mdi-trash-can-outline" aria-hidden="true"></span> Delete
</a>
Expand Down
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"
12 changes: 6 additions & 6 deletions netbox_bgp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ class BGPSessionView(generic.ObjectView):
template_name = 'netbox_bgp/bgpsession.html'

def get_extra_context(self, request, instance):
if instance.peer_group:
import_policies_qs = instance.import_policies.all() | instance.peer_group.import_policies.all()
export_policies_qs = instance.export_policies.all() | instance.peer_group.export_policies.all()
else:
import_policies_qs = instance.import_policies.all()
export_policies_qs = instance.export_policies.all()
import_policies_qs = instance.import_policies.all()
if not import_policies_qs and instance.peer_group:
import_policies_qs = instance.peer_group.import_policies.all()
export_policies_qs = instance.export_policies.all()
if not export_policies_qs and instance.peer_group:
export_policies_qs = instance.peer_group.export_policies.all()

import_policies_table = tables.RoutingPolicyTable(
import_policies_qs,
Expand Down

0 comments on commit f04e40f

Please sign in to comment.