Skip to content

Commit

Permalink
Merge pull request #47 from DanSheps/develop
Browse files Browse the repository at this point in the history
Closes: #21 - Add VRF to OSPF Instance model
  • Loading branch information
DanSheps authored Sep 28, 2024
2 parents 184ebc1 + 0638f48 commit 1c6aeb5
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 35 deletions.
8 changes: 6 additions & 2 deletions netbox_routing/api/_serializers/ospf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from dcim.api.serializers_.device_components import InterfaceSerializer
from dcim.api.serializers_.devices import DeviceSerializer
from ipam.api.serializers_.vrfs import VRFSerializer
from netbox.api.serializers import NetBoxModelSerializer
from netbox_routing.models import OSPFInstance, OSPFArea, OSPFInterface

Expand All @@ -16,11 +17,14 @@
class OSPFInstanceSerializer(NetBoxModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='plugins-api:netbox_routing-api:ospfinstance-detail')
device = DeviceSerializer(nested=True)
vrf = VRFSerializer(nested=True)

class Meta:
model = OSPFInstance
fields = ('url', 'id', 'display', 'name', 'router_id', 'process_id', 'device', 'description', 'comments', )
brief_fields = ('url', 'id', 'display', 'name', 'router_id', 'process_id', 'device', )
fields = (
'url', 'id', 'display', 'name', 'router_id', 'process_id', 'device', 'vrf', 'description', 'comments',
)
brief_fields = ('url', 'id', 'display', 'name', 'router_id', 'process_id', 'device', 'vrf', )


class OSPFAreaSerializer(NetBoxModelSerializer):
Expand Down
25 changes: 24 additions & 1 deletion netbox_routing/filtersets/ospf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.utils.translation import gettext as _

from dcim.models import Device, Interface
from ipam.models import VRF
from utilities.filters import MultiValueCharFilter

from netbox.filtersets import NetBoxModelFilterSet
Expand All @@ -30,6 +31,17 @@ class OSPFInstanceFilterSet(NetBoxModelFilterSet):
to_field_name='name',
label='Device',
)
vrf_id = django_filters.ModelMultipleChoiceFilter(
field_name='vrf',
queryset=VRF.objects.all(),
label='VRF (ID)',
)
vrf = django_filters.ModelMultipleChoiceFilter(
field_name='vrf__name',
queryset=VRF.objects.all(),
to_field_name='name',
label='VRF',
)

router_id = MultiValueCharFilter(
method='filter_rid',
Expand All @@ -38,7 +50,7 @@ class OSPFInstanceFilterSet(NetBoxModelFilterSet):

class Meta:
model = OSPFInstance
fields = ('device_id', 'device', 'name', 'device', 'router_id', 'process_id')
fields = ('device_id', 'device', 'name', 'vrf_id', 'vrf', 'router_id', 'process_id')

def search(self, queryset, name, value):
if not value.strip():
Expand Down Expand Up @@ -94,6 +106,17 @@ class OSPFInterfaceFilterSet(NetBoxModelFilterSet):
to_field_name='name',
label='Instance',
)
vrf_id = django_filters.ModelMultipleChoiceFilter(
field_name='instance__vrf',
queryset=VRF.objects.all(),
label='VRF (ID)',
)
vrf = django_filters.ModelMultipleChoiceFilter(
field_name='instance__vrf__name',
queryset=VRF.objects.all(),
to_field_name='name',
label='VRF',
)
area_id = django_filters.ModelMultipleChoiceFilter(
field_name='area',
queryset=OSPFArea.objects.all(),
Expand Down
11 changes: 9 additions & 2 deletions netbox_routing/forms/bulk_edit/ospf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.utils.translation import gettext as _

from dcim.models import Device
from ipam.models import VRF
from netbox.forms import NetBoxModelBulkEditForm
from utilities.forms import BOOLEAN_WITH_BLANK_CHOICES, add_blank_choice
from utilities.forms.fields import DynamicModelChoiceField, CommentField
Expand All @@ -25,6 +26,12 @@ class OSPFInstanceBulkEditForm(NetBoxModelBulkEditForm):
required=False,
selector=True
)
vrf = DynamicModelChoiceField(
queryset=VRF.objects.all(),
label=_('VRF'),
required=False,
selector=True
)

description = forms.CharField(
label=_('Description'),
Expand All @@ -35,10 +42,10 @@ class OSPFInstanceBulkEditForm(NetBoxModelBulkEditForm):

model = OSPFInstance
fieldsets = (
FieldSet('device', name='OSPF'),
FieldSet('device', 'vrf', name='OSPF'),
FieldSet('description', ),
)
nullable_fields = ()
nullable_fields = ('vrf', 'description', )


class OSPFAreaBulkEditForm(NetBoxModelBulkEditForm):
Expand Down
15 changes: 11 additions & 4 deletions netbox_routing/forms/bulk_import/ospf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.utils.translation import gettext as _

from dcim.models import Interface
from dcim.models import Interface, Device
from ipam.models import VRF
from netbox.forms import NetBoxModelImportForm
from utilities.forms.fields import CSVModelChoiceField

Expand All @@ -16,15 +17,21 @@

class OSPFInstanceImportForm(NetBoxModelImportForm):
device = CSVModelChoiceField(
queryset=Interface.objects.all(),
required=False,
queryset=Device.objects.all(),
required=True,
to_field_name='name',
help_text=_('Name of device')
)
vrf = CSVModelChoiceField(
queryset=VRF.objects.all(),
required=False,
to_field_name='name',
help_text=_('Name of VRF')
)

class Meta:
model = OSPFInstance
fields = ('name', 'router_id', 'process_id', 'device', 'description', 'comments', 'tags',)
fields = ('name', 'router_id', 'process_id', 'device', 'vrf', 'description', 'comments', 'tags',)


class OSPFAreaImportForm(NetBoxModelImportForm):
Expand Down
25 changes: 22 additions & 3 deletions netbox_routing/forms/filtersets/ospf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.utils.translation import gettext as _

from dcim.models import Interface, Device
from ipam.models import VRF
from netbox.forms import NetBoxModelFilterSetForm
from netbox_routing.choices import AuthenticationChoices
from utilities.forms import BOOLEAN_WITH_BLANK_CHOICES, add_blank_choice
Expand All @@ -26,6 +27,12 @@ class OSPFInstanceFilterForm(NetBoxModelFilterSetForm):
selector=True,
label=_('Device'),
)
vrf = DynamicModelMultipleChoiceField(
queryset=VRF.objects.all(),
required=False,
selector=True,
label=_('VRF'),
)
model = OSPFInstance
fieldsets = (
FieldSet('q', 'filter_id', 'tag', 'device'),
Expand All @@ -49,19 +56,31 @@ class OSPFInterfaceFilterForm(NetBoxModelFilterSetForm):
FieldSet('interface', name=_('Device')),
FieldSet('priority', 'bfd', 'authentication', name=_('Attributes'))
)
instance = DynamicModelMultipleChoiceField(
device_id = DynamicModelMultipleChoiceField(
queryset=Device.objects.all(),
required=False,
selector=True,
label=_('Device'),
)
vrf_id = DynamicModelMultipleChoiceField(
queryset=VRF.objects.all(),
required=False,
selector=True,
label=_('VRF'),
)
instance_id = DynamicModelMultipleChoiceField(
queryset=OSPFInstance.objects.all(),
required=False,
selector=True,
label=_('Instance'),
)
area = DynamicModelMultipleChoiceField(
area_id = DynamicModelMultipleChoiceField(
queryset=OSPFArea.objects.all(),
required=False,
selector=True,
label=_('Area'),
)
interface = DynamicModelMultipleChoiceField(
interface_id = DynamicModelMultipleChoiceField(
queryset=Interface.objects.all(),
required=False,
selector=True,
Expand Down
9 changes: 8 additions & 1 deletion netbox_routing/forms/ospf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.utils.translation import gettext as _

from dcim.models import Interface, Device
from ipam.models import VRF
from netbox.forms import NetBoxModelForm
from utilities.forms import BOOLEAN_WITH_BLANK_CHOICES
from utilities.forms.fields import DynamicModelChoiceField, DynamicModelMultipleChoiceField, CommentField
Expand All @@ -24,11 +25,17 @@ class OSPFInstanceForm(NetBoxModelForm):
selector=True,
label=_('Device'),
)
vrf = DynamicModelChoiceField(
queryset=VRF.objects.all(),
required=False,
selector=True,
label=_('VRF'),
)
comments = CommentField()

class Meta:
model = OSPFInstance
fields = ('name', 'router_id', 'process_id', 'device', 'description', 'comments', )
fields = ('name', 'router_id', 'process_id', 'device', 'vrf', 'description', 'comments', )


class OSPFAreaForm(NetBoxModelForm):
Expand Down
1 change: 1 addition & 0 deletions netbox_routing/graphql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class OSPFInstanceType(NetBoxObjectType):

name: str
device: Annotated["DeviceType", strawberry.lazy('dcim.graphql.types')]
vrf: Annotated["VRFType", strawberry.lazy('ipam.graphql.types')] | None
router_id: str
process_id: str

Expand Down
26 changes: 26 additions & 0 deletions netbox_routing/migrations/0012_osfp_instance_vrf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 5.0.8 on 2024-09-28 04:42

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('ipam', '0070_vlangroup_vlan_id_ranges'),
('netbox_routing', '0011_osfp_passive_interface'),
]

operations = [
migrations.AddField(
model_name='ospfinstance',
name='vrf',
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name='ospf_instances',
to='ipam.vrf',
),
),
]
8 changes: 8 additions & 0 deletions netbox_routing/models/ospf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ class OSPFInstance(PrimaryModel):
blank=False,
null=False
)
vrf = models.ForeignKey(
verbose_name=_('VRF'),
to='ipam.VRF',
related_name='ospf_instances',
on_delete=models.CASCADE,
blank=True,
null=True
)

clone_fields = ('device',)
prerequisite_models = (
Expand Down
4 changes: 2 additions & 2 deletions netbox_routing/tables/ospf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
class OSPFInstanceTable(NetBoxTable):
class Meta(NetBoxTable.Meta):
model = OSPFInstance
fields = ('pk', 'id', 'name', 'router_id', 'process_id', 'device')
default_columns = ('pk', 'id', 'name', 'router_id', 'process_id', 'device')
fields = ('pk', 'id', 'name', 'router_id', 'process_id', 'device', 'vrf', )
default_columns = ('pk', 'id', 'name', 'router_id', 'process_id', 'device', )


class OSPFAreaTable(NetBoxTable):
Expand Down
6 changes: 6 additions & 0 deletions netbox_routing/templates/netbox_routing/ospfinstance.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ <h5 class="card-header">OSPF Details</h5>
{{ object.device|linkify }}
</td>
</tr>
<tr>
<th scope="row">VRF</th>
<td>
{{ object.vrf|linkify }}
</td>
</tr>
</table>
</div>
</div>
Expand Down
14 changes: 8 additions & 6 deletions netbox_routing/tests/ospf/test_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dcim.models import Interface
from ipam.models import VRF
from utilities.testing import APIViewTestCases, create_test_device

from netbox_routing.models import OSPFInstance, OSPFArea, OSPFInterface
Expand All @@ -14,7 +15,7 @@
class OSPFInstanceTestCase(IPAddressFieldMixin, APIViewTestCases.APIViewTestCase):
model = OSPFInstance
view_namespace = 'plugins-api:netbox_routing'
brief_fields = ['device', 'display', 'id', 'name', 'process_id', 'router_id', 'url', ]
brief_fields = ['device', 'display', 'id', 'name', 'process_id', 'router_id', 'url', 'vrf', ]

user_permissions = ('dcim.view_device', )

Expand All @@ -24,13 +25,13 @@ class OSPFInstanceTestCase(IPAddressFieldMixin, APIViewTestCases.APIViewTestCase

@classmethod
def setUpTestData(cls):

vrf = VRF.objects.create(name='Test VRF')
device = create_test_device(name='Test Device')

data = (
cls.model(name='Instance 1', device=device, router_id='1.1.1.1', process_id=1),
cls.model(name='Instance 2', device=device, router_id='2.2.2.2', process_id=2),
cls.model(name='Instance 3', device=device, router_id='3.3.3.3', process_id=3),
cls.model(name='Instance 1', device=device, router_id='1.1.1.1', process_id=1, vrf=None),
cls.model(name='Instance 2', device=device, router_id='2.2.2.2', process_id=2, vrf=vrf),
cls.model(name='Instance 3', device=device, router_id='3.3.3.3', process_id=3, vrf=None),
)
cls.model.objects.bulk_create(data)

Expand All @@ -39,7 +40,8 @@ def setUpTestData(cls):
'name': 'Instance X',
'device': device.pk,
'router_id': '4.4.4.4',
'process_id': 4
'process_id': 4,
'vrf': vrf.pk,
},
]

Expand Down
Loading

0 comments on commit 1c6aeb5

Please sign in to comment.