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

Closes: #49 - Add "Area Type" field to OSPF Area model #55

Merged
merged 3 commits into from
Nov 21, 2024
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
4 changes: 2 additions & 2 deletions netbox_routing/api/_serializers/ospf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class OSPFAreaSerializer(NetBoxModelSerializer):

class Meta:
model = OSPFArea
fields = ('url', 'id', 'display', 'area_id', 'description', 'comments',)
brief_fields = ('url', 'id', 'display', 'area_id',)
fields = ('url', 'id', 'display', 'area_id', 'area_type', 'description', 'comments',)
brief_fields = ('url', 'id', 'display', 'area_id', 'area_type')


class OSPFInterfaceSerializer(NetBoxModelSerializer):
Expand Down
19 changes: 19 additions & 0 deletions netbox_routing/choices/ospf.py
LuPo marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from utilities.choices import ChoiceSet


class OSPFAreaTypeChoices(ChoiceSet):
STANDARD = 'standard'
BACKBONE = 'backbone'
STUB = 'stub'
TSA = 'tsa'
NSSA = 'nssa'
TNSSA = 'tnssa'

CHOICES = [
(STANDARD, 'Standard Area'),
(BACKBONE, 'Backbone Area'),
(STUB, 'Stub Area'),
(TSA, 'Totally Stubby Area'),
(NSSA, 'Not-So-Stubby Area'),
(TNSSA, 'Totally Not-So-Stubby Area'),
]
8 changes: 7 additions & 1 deletion netbox_routing/filtersets/ospf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from utilities.filters import MultiValueCharFilter

from netbox.filtersets import NetBoxModelFilterSet
from netbox_routing.choices.ospf import OSPFAreaTypeChoices
from netbox_routing.models import OSPFArea, OSPFInstance, OSPFInterface


Expand Down Expand Up @@ -75,9 +76,14 @@ class OSPFAreaFilterSet(NetBoxModelFilterSet):
label=_('Area ID'),
)

area_type = django_filters.MultipleChoiceFilter(
choices=OSPFAreaTypeChoices,
label=_('Area Type'),
)

class Meta:
model = OSPFArea
fields = ('area_id', )
fields = ('area_id', 'area_type')

def search(self, queryset, name, value):
if not value.strip():
Expand Down
3 changes: 2 additions & 1 deletion netbox_routing/forms/bulk_edit/ospf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from utilities.forms.fields import DynamicModelChoiceField, CommentField

from netbox_routing import choices
from netbox_routing.choices.ospf import OSPFAreaTypeChoices
from netbox_routing.models import OSPFArea, OSPFInstance, OSPFInterface

__all__ = (
Expand Down Expand Up @@ -49,7 +50,7 @@ class OSPFInstanceBulkEditForm(NetBoxModelBulkEditForm):


class OSPFAreaBulkEditForm(NetBoxModelBulkEditForm):

area_type = forms.ChoiceField(label=_('Area Type'), choices=OSPFAreaTypeChoices, required=False)
description = forms.CharField(
label=_('Description'),
max_length=200,
Expand Down
2 changes: 1 addition & 1 deletion netbox_routing/forms/bulk_import/ospf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class OSPFAreaImportForm(NetBoxModelImportForm):

class Meta:
model = OSPFArea
fields = ('area_id', 'description', 'comments', 'tags',)
fields = ('area_id', 'area_type', 'description', 'comments', 'tags',)


class OSPFInterfaceImportForm(NetBoxModelImportForm):
Expand Down
8 changes: 7 additions & 1 deletion netbox_routing/forms/filtersets/ospf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ipam.models import VRF
from netbox.forms import NetBoxModelFilterSetForm
from netbox_routing.choices import AuthenticationChoices
from netbox_routing.choices.ospf import OSPFAreaTypeChoices
from utilities.forms import BOOLEAN_WITH_BLANK_CHOICES, add_blank_choice
from utilities.forms.fields import TagFilterField, DynamicModelMultipleChoiceField

Expand Down Expand Up @@ -43,7 +44,12 @@ class OSPFInstanceFilterForm(NetBoxModelFilterSetForm):
class OSPFAreaFilterForm(NetBoxModelFilterSetForm):
model = OSPFArea
fieldsets = (
FieldSet('q', 'filter_id', 'tag'),
FieldSet('q', 'area_type', 'filter_id', 'tag'),
)
area_type = forms.ChoiceField(
choices=add_blank_choice(OSPFAreaTypeChoices),
label='Area Type',
required=False
)
tag = TagFilterField(model)

Expand Down
2 changes: 1 addition & 1 deletion netbox_routing/forms/ospf.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class OSPFAreaForm(NetBoxModelForm):

class Meta:
model = OSPFArea
fields = ('area_id', 'description', 'comments', )
fields = ('area_id', 'area_type', 'description', 'comments', )


class OSPFInterfaceForm(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 @@ -60,6 +60,7 @@ class OSPFInstanceType(NetBoxObjectType):
class OSPFAreaType(NetBoxObjectType):

area_id: str
area_type: str


@strawberry_django.type(
Expand Down
18 changes: 18 additions & 0 deletions netbox_routing/migrations/0013_ospfarea_area_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.9 on 2024-11-10 22:42

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("netbox_routing", "0012_osfp_instance_vrf"),
]

operations = [
migrations.AddField(
model_name="ospfarea",
name="area_type",
field=models.CharField(default="standard"),
),
]
9 changes: 8 additions & 1 deletion netbox_routing/models/ospf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from netbox.models import PrimaryModel

from netbox_routing import choices
from netbox_routing.choices.ospf import OSPFAreaTypeChoices
from netbox_routing.fields.ip import IPAddressField


Expand Down Expand Up @@ -54,7 +55,13 @@ def get_absolute_url(self):

class OSPFArea(PrimaryModel):
area_id = models.CharField(max_length=100, verbose_name='Area ID')

area_type = models.CharField(
verbose_name=_('Area Type'),
choices=OSPFAreaTypeChoices,
blank=False,
null=False,
default='standard'
)
prerequisite_models = ()
clone_fields = ()
class Meta:
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 @@ -22,8 +22,8 @@ class Meta(NetBoxTable.Meta):
class OSPFAreaTable(NetBoxTable):
class Meta(NetBoxTable.Meta):
model = OSPFArea
fields = ('pk', 'id', 'area_id')
default_columns = ('pk', 'id', 'area_id')
fields = ('pk', 'id', 'area_id', 'area_type')
default_columns = ('pk', 'id', 'area_id', 'area_type')


class OSPFInterfaceTable(NetBoxTable):
Expand Down
19 changes: 19 additions & 0 deletions netbox_routing/templates/netbox_routing/ospfarea.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@
{% block content %}
<div class="row">
<div class="col col-md-6">
<div class="card">
<h5 class="card-header">OSPF Area Details</h5>
<div class="card-body">
<table class="table table-hover attr-table">
<tr>
<th scope="row">Area ID</th>
<td>
{{ object.area_id }}
</td>
</tr>
<tr>
<th scope="row">Area Type</th>
<td>
{{ object.get_area_type_display }}
</td>
</tr>
</table>
</div>
</div>
{% plugin_left_page object %}
</div>
<div class="col col-md-6">
Expand Down
9 changes: 5 additions & 4 deletions netbox_routing/tests/ospf/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def setUpTestData(cls):
class OSPFAreaTestCase(IPAddressFieldMixin , APIViewTestCases.APIViewTestCase):
model = OSPFArea
view_namespace = 'plugins-api:netbox_routing'
brief_fields = ['area_id', 'display', 'id', 'url', ]
brief_fields = ['area_id', 'area_type', 'display', 'id', 'url', ]

bulk_update_data = {
'description': "A test description"
Expand All @@ -59,15 +59,16 @@ class OSPFAreaTestCase(IPAddressFieldMixin , APIViewTestCases.APIViewTestCase):
def setUpTestData(cls):

data = (
cls.model(area_id='1'),
cls.model(area_id='2'),
cls.model(area_id='3'),
cls.model(area_id='1', area_type='stub'),
cls.model(area_id='2', area_type='stub'),
cls.model(area_id='3', area_type='stub'),
)
cls.model.objects.bulk_create(data)

cls.create_data = [
{
'area_id': '4',
'area_type': 'stub',
},
]

Expand Down
3 changes: 3 additions & 0 deletions netbox_routing/tests/ospf/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,23 @@ def setUpTestData(cls):
def test_valid_area_id_with_ip(self):
form = OSPFAreaForm(data={
'area_id': '0.0.0.0',
'area_type': 'standard',
})
self.assertTrue(form.is_valid())
self.assertTrue(form.save())

def test_valid_area_id_with_integer(self):
form = OSPFAreaForm(data={
'area_id': '0',
'area_type': 'standard',
})
self.assertTrue(form.is_valid())
self.assertTrue(form.save())

def test_invalid_area(self):
form = OSPFAreaForm(data={
'area_id': 'a.a.a.a',
'area_type': 'standard',
})
self.assertFalse(form.is_valid())
with self.assertRaises(ValueError):
Expand Down
10 changes: 6 additions & 4 deletions netbox_routing/tests/ospf/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,20 @@ class OSPFAreaTestCase(
@classmethod
def setUpTestData(cls):
areas = (
cls.model(area_id='0.0.0.0'),
cls.model(area_id='1.1.1.1'),
cls.model(area_id='2.2.2.2'),
cls.model(area_id='0.0.0.0', area_type='stub'),
cls.model(area_id='1.1.1.1', area_type='tsa'),
cls.model(area_id='2.2.2.2', area_type='nssa'),
)
cls.model.objects.bulk_create(areas)

cls.form_data = {
'area_id': '4.4.4.4',
'area_type': 'standard',
}

cls.bulk_edit_data = {
'description': 'A test Area description'
'description': 'A test Area description',
'area_type': 'backbone',
}

def _get_base_url(self):
Expand Down