Skip to content

Commit

Permalink
Require vlan_id_ranges & set default value
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Jul 15, 2024
1 parent e039ba4 commit f6c2396
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
2 changes: 1 addition & 1 deletion netbox/ipam/forms/bulk_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ class VLANGroupImportForm(NetBoxModelImportForm):
label=_('Scope type (app & model)')
)
vlan_id_ranges = NumericRangeArrayField(
required=False,
required=False
)

class Meta:
Expand Down
3 changes: 1 addition & 2 deletions netbox/ipam/forms/model_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,7 @@ class VLANGroupForm(NetBoxModelForm):
)
slug = SlugField()
vlan_id_ranges = NumericRangeArrayField(
label=_('VLAN IDs'),
required=False
label=_('VLAN IDs')
)

fieldsets = (
Expand Down
7 changes: 4 additions & 3 deletions netbox/ipam/migrations/0070_vlangroup_vlan_id_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from django.db import migrations, models
from django.db.backends.postgresql.psycopg_any import NumericRange

import ipam.models.vlans


def move_min_max(apps, schema_editor):
VLANGroup = apps.get_model('ipam', 'VLANGroup')
Expand All @@ -29,9 +31,8 @@ class Migration(migrations.Migration):
name='vlan_id_ranges',
field=django.contrib.postgres.fields.ArrayField(
base_field=django.contrib.postgres.fields.ranges.IntegerRangeField(),
blank=True,
null=True,
size=None,
default=ipam.models.vlans.default_vland_id_ranges,
size=None
),
),
migrations.AddField(
Expand Down
31 changes: 17 additions & 14 deletions netbox/ipam/models/vlans.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.core.exceptions import ValidationError
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.db.backends.postgresql.psycopg_any import NumericRange
from django.urls import reverse
from django.utils.translation import gettext_lazy as _

Expand All @@ -20,6 +21,12 @@
)


def default_vland_id_ranges():
return [
NumericRange(VLAN_VID_MIN, VLAN_VID_MAX, bounds='[]')
]


class VLANGroup(OrganizationalModel):
"""
A VLAN group is an arbitrary collection of VLANs within which VLAN IDs and names must be unique.
Expand Down Expand Up @@ -50,8 +57,7 @@ class VLANGroup(OrganizationalModel):
vlan_id_ranges = ArrayField(
IntegerRangeField(),
verbose_name=_('VLAN ID ranges'),
blank=True,
null=True
default=default_vland_id_ranges
)
_total_vlan_ids = models.PositiveBigIntegerField(
default=VLAN_VID_MAX - VLAN_VID_MIN + 1
Expand Down Expand Up @@ -93,21 +99,18 @@ def clean(self):
if self.vlan_id_ranges and check_ranges_overlap(self.vlan_id_ranges):
raise ValidationError({'vlan_id_ranges': _("Ranges cannot overlap.")})

for ranges in self.vlan_id_ranges:
if ranges.lower >= ranges.upper:
for vid_range in self.vlan_id_ranges:
if vid_range.lower >= vid_range.upper:
raise ValidationError({
'vlan_id_ranges': _(
"Maximum child VID must be greater than or equal to minimum child VID Invalid range ({value})"
).format(value=ranges)
"Maximum child VID must be greater than or equal to minimum child VID ({value})"
).format(value=vid_range)
})

def save(self, *args, **kwargs):
if self.vlan_id_ranges:
self._total_vlan_ids = 0
for vlan_range in self.vlan_id_ranges:
self._total_vlan_ids += vlan_range.upper - vlan_range.lower + 1
else:
self._total_vlan_ids = VLAN_VID_MAX - VLAN_VID_MIN + 1
self._total_vlan_ids = 0
for vid_range in self.vlan_id_ranges:
self._total_vlan_ids += vid_range.upper - vid_range.lower + 1

super().save(*args, **kwargs)

Expand Down Expand Up @@ -249,8 +252,8 @@ def clean(self):
# Validate group min/max VIDs
if self.group and self.group.vlan_id_ranges:
in_bounds = False
for ranges in self.group.vlan_id_ranges:
if ranges.lower <= self.vid <= ranges.upper:
for vid_range in self.group.vlan_id_ranges:
if vid_range.lower <= self.vid <= vid_range.upper:
in_bounds = True

if not in_bounds:
Expand Down
2 changes: 1 addition & 1 deletion netbox/ipam/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from ipam.choices import *
from ipam.models import *
from tenancy.models import Tenant
from utilities.data import string_to_range_array
from utilities.testing import ViewTestCases, create_tags


Expand Down Expand Up @@ -766,6 +765,7 @@ def setUpTestData(cls):
'name': 'VLAN Group X',
'slug': 'vlan-group-x',
'description': 'A new VLAN group',
'vlan_id_ranges': '100-199,300-399',
'tags': [t.pk for t in tags],
}

Expand Down

0 comments on commit f6c2396

Please sign in to comment.