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

Links the VRF to the session #127

Closed
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
1 change: 1 addition & 0 deletions netbox_bgp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class BGPConfig(PluginConfig):
max_version = '3.4.99'
default_settings = {
'device_ext_page': 'right',
'vrf_ext_page': 'right',
}


Expand Down
3 changes: 2 additions & 1 deletion netbox_bgp/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from netbox.api.serializers import NetBoxModelSerializer
from dcim.api.nested_serializers import NestedSiteSerializer, NestedDeviceSerializer
from tenancy.api.nested_serializers import NestedTenantSerializer
from ipam.api.nested_serializers import NestedIPAddressSerializer, NestedASNSerializer
from ipam.api.nested_serializers import NestedIPAddressSerializer, NestedASNSerializer, NestedVRFSerializer


from netbox_bgp.models import (
Expand Down Expand Up @@ -100,6 +100,7 @@ class BGPSessionSerializer(NetBoxModelSerializer):
allow_null=True,
many=True
)
vrf = NestedVRFSerializer(required=False, allow_null=True)

class Meta:
model = BGPSession
Expand Down
14 changes: 13 additions & 1 deletion netbox_bgp/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from netbox.filtersets import NetBoxModelFilterSet

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


Expand Down Expand Up @@ -117,6 +117,18 @@ class BGPSessionFilterSet(NetBoxModelFilterSet):
method='search_by_local_ip',
label='Local Address',
)
vrf_id = django_filters.ModelMultipleChoiceFilter(
field_name='vrf__id',
queryset=VRF.objects.all(),
to_field_name='id',
label='VRF (id)',
)
vrf = django_filters.ModelMultipleChoiceFilter(
field_name='vrf__name',
queryset=VRF.objects.all(),
to_field_name='name',
label='VRF (name)',
)

class Meta:
model = BGPSession
Expand Down
16 changes: 13 additions & 3 deletions netbox_bgp/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from extras.models import Tag
from tenancy.models import Tenant
from dcim.models import Device, Site
from ipam.models import IPAddress, Prefix, ASN
from ipam.models import IPAddress, Prefix, ASN, VRF
from ipam.formfields import IPNetworkFormField
from utilities.forms import (
DynamicModelChoiceField,
Expand Down Expand Up @@ -161,16 +161,20 @@ class BGPSessionForm(NetBoxModelForm):
api_url='/api/plugins/bgp/routing-policy/'
)
)
vrf = DynamicModelChoiceField(
queryset=VRF.objects.all(),
required=False,
)

class Meta:
model = BGPSession
fields = [
'name', 'site', 'device',
'name', 'site', 'device', 'vrf',
'local_as', 'remote_as', 'local_address', 'remote_address',
'description', 'status', 'peer_group', 'tenant', 'tags', 'import_policies', 'export_policies'
]
fieldsets = (
('Session', ('name', 'site', 'device', 'description', 'status', 'peer_group', 'tenant', 'tags')),
('Session', ('name', 'site', 'device', 'vrf', 'description', 'status', 'peer_group', 'tenant', 'tags')),
('Remote', ('remote_as', 'remote_address')),
('Local', ('local_as', 'local_address')),
('Policies', ('import_policies', 'export_policies'))
Expand Down Expand Up @@ -254,6 +258,12 @@ class BGPSessionFilterForm(NetBoxModelFilterSetForm):
required=False
)

vrf_id = DynamicModelChoiceField(
queryset=VRF.objects.all(),
required=False,
label=_('VRF')
)

tag = TagFilterField(model)


Expand Down
18 changes: 17 additions & 1 deletion netbox_bgp/migrations/0027_netbox_bgp.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
# Generated by Django 4.1.4 on 2023-01-04 14:44

from django.db import migrations, models
import utilities.json
try:
import utilities.json
except ImportError:
import decimal
from django.core.serializers.json import DjangoJSONEncoder

class CustomFieldJSONEncoder(DjangoJSONEncoder):
"""
Override Django's built-in JSON encoder to save decimal values as JSON numbers.
"""

def default(self, o):
if isinstance(o, decimal.Decimal):
return float(o)
return super().default(o)
utilities = type('utilities', (object,), {})
utilities.json = type('json', (object,), {})
utilities.json.CustomFieldJSONEncoder = CustomFieldJSONEncoder

class Migration(migrations.Migration):

Expand Down
56 changes: 56 additions & 0 deletions netbox_bgp/migrations/0028_netbox_bgp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Generated by Django 4.0.6 on 2023-01-16 10:33

import django.core.serializers.json
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('ipam', '0057_created_datetimefield'),
('netbox_bgp', '0027_netbox_bgp'),
]

operations = [
migrations.AddField(
model_name='bgpsession',
name='vrf',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='ipam.vrf'),
),
migrations.AlterField(
model_name='bgppeergroup',
name='custom_field_data',
field=models.JSONField(blank=True, default=dict, encoder=django.core.serializers.json.DjangoJSONEncoder),
),
migrations.AlterField(
model_name='bgpsession',
name='custom_field_data',
field=models.JSONField(blank=True, default=dict, encoder=django.core.serializers.json.DjangoJSONEncoder),
),
migrations.AlterField(
model_name='community',
name='custom_field_data',
field=models.JSONField(blank=True, default=dict, encoder=django.core.serializers.json.DjangoJSONEncoder),
),
migrations.AlterField(
model_name='prefixlist',
name='custom_field_data',
field=models.JSONField(blank=True, default=dict, encoder=django.core.serializers.json.DjangoJSONEncoder),
),
migrations.AlterField(
model_name='prefixlistrule',
name='custom_field_data',
field=models.JSONField(blank=True, default=dict, encoder=django.core.serializers.json.DjangoJSONEncoder),
),
migrations.AlterField(
model_name='routingpolicy',
name='custom_field_data',
field=models.JSONField(blank=True, default=dict, encoder=django.core.serializers.json.DjangoJSONEncoder),
),
migrations.AlterField(
model_name='routingpolicyrule',
name='custom_field_data',
field=models.JSONField(blank=True, default=dict, encoder=django.core.serializers.json.DjangoJSONEncoder),
),
]
7 changes: 7 additions & 0 deletions netbox_bgp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ class BGPSession(NetBoxModel):

afi_safi = None # for future use

vrf = models.ForeignKey(
to="ipam.vrf",
on_delete=models.PROTECT,
blank=True,
null=True,
)

class Meta:
verbose_name_plural = 'BGP Sessions'
unique_together = ['device', 'local_address', 'local_as', 'remote_address', 'remote_as']
Expand Down
5 changes: 3 additions & 2 deletions netbox_bgp/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,19 @@ class BGPSessionTable(NetBoxTable):
tenant = tables.TemplateColumn(
template_code=COL_TENANT
)
vrf = tables.LinkColumn()

class Meta(NetBoxTable.Meta):
model = BGPSession
fields = (
'pk', 'name', 'device', 'local_address', 'local_as',
'remote_address', 'remote_as', 'description', 'peer_group',
'site', 'status', 'tenant'
'site', 'status', 'tenant', 'vrf'
)
default_columns = (
'pk', 'name', 'device', 'local_address', 'local_as',
'remote_address', 'remote_as', 'description',
'site', 'status', 'tenant'
'site', 'status', 'tenant', 'vrf'
)


Expand Down
30 changes: 29 additions & 1 deletion netbox_bgp/template_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,32 @@ def x_page(self):
)


template_extensions = [DeviceBGPSession]
class VRFBGPSession(PluginTemplateExtension):
model = 'ipam.vrf'

def left_page(self):
if self.context['config'].get('vrf_ext_page') == 'left':
return self.x_page()
return ''

def right_page(self):
if self.context['config'].get('vrf_ext_page') == 'right':
return self.x_page()
return ''

def full_width_page(self):
if self.context['config'].get('vrf_ext_page') == 'full_width':
return self.x_page()
return ''

def x_page(self):
obj = self.context['object']
sess = BGPSession.objects.filter(vrf=obj)
sess_table = BGPSessionTable(sess)
return self.render(
'netbox_bgp/vrf_extend.html',
extra_context={'related_session_table': sess_table},
)


template_extensions = [DeviceBGPSession, VRFBGPSession]
9 changes: 9 additions & 0 deletions netbox_bgp/templates/netbox_bgp/vrf_extend.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% load render_table from django_tables2 %}
<div class="card">
<h5 class="card-header">
Related BGP Sessions
</h5>
<div class="card-body">
{% render_table related_session_table 'inc/table.html' %}
</div>
</div>
6 changes: 5 additions & 1 deletion netbox_bgp/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from tenancy.models import Tenant
from dcim.models import Site, Device, Manufacturer, DeviceRole, DeviceType
from ipam.models import IPAddress, ASN, RIR
from ipam.models import IPAddress, ASN, RIR, VRF

from netbox_bgp.models import BGPSession, Community, RoutingPolicy, BGPPeerGroup

Expand Down Expand Up @@ -165,6 +165,9 @@ def setUp(self):
self.remote_ip = IPAddress.objects.create(
address='1.1.1.2/32'
)
self.vrf = VRF.objects.create(
name='vrf-test'
)
self.session = BGPSession.objects.create(
name='session',
site=self.site,
Expand All @@ -176,6 +179,7 @@ def setUp(self):
remote_as=self.remote_as,
status='active',
peer_group=self.peer_group,
vrf=self.vrf,
)

def test_create_session(self):
Expand Down