From f6c2396a3051ca1bc11c6c4889912b6d9b9e35ca Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 15 Jul 2024 10:13:34 -0400 Subject: [PATCH] Require vlan_id_ranges & set default value --- netbox/ipam/forms/bulk_import.py | 2 +- netbox/ipam/forms/model_forms.py | 3 +- .../0070_vlangroup_vlan_id_ranges.py | 7 +++-- netbox/ipam/models/vlans.py | 31 ++++++++++--------- netbox/ipam/tests/test_views.py | 2 +- 5 files changed, 24 insertions(+), 21 deletions(-) diff --git a/netbox/ipam/forms/bulk_import.py b/netbox/ipam/forms/bulk_import.py index c50f194e81..24dbf58952 100644 --- a/netbox/ipam/forms/bulk_import.py +++ b/netbox/ipam/forms/bulk_import.py @@ -413,7 +413,7 @@ class VLANGroupImportForm(NetBoxModelImportForm): label=_('Scope type (app & model)') ) vlan_id_ranges = NumericRangeArrayField( - required=False, + required=False ) class Meta: diff --git a/netbox/ipam/forms/model_forms.py b/netbox/ipam/forms/model_forms.py index 9359677c96..bdbc98d9b2 100644 --- a/netbox/ipam/forms/model_forms.py +++ b/netbox/ipam/forms/model_forms.py @@ -634,8 +634,7 @@ class VLANGroupForm(NetBoxModelForm): ) slug = SlugField() vlan_id_ranges = NumericRangeArrayField( - label=_('VLAN IDs'), - required=False + label=_('VLAN IDs') ) fieldsets = ( diff --git a/netbox/ipam/migrations/0070_vlangroup_vlan_id_ranges.py b/netbox/ipam/migrations/0070_vlangroup_vlan_id_ranges.py index 4ac64b16de..b8cc987c36 100644 --- a/netbox/ipam/migrations/0070_vlangroup_vlan_id_ranges.py +++ b/netbox/ipam/migrations/0070_vlangroup_vlan_id_ranges.py @@ -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') @@ -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( diff --git a/netbox/ipam/models/vlans.py b/netbox/ipam/models/vlans.py index 6b025d0a4a..ceefa8905c 100644 --- a/netbox/ipam/models/vlans.py +++ b/netbox/ipam/models/vlans.py @@ -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 _ @@ -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. @@ -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 @@ -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) @@ -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: diff --git a/netbox/ipam/tests/test_views.py b/netbox/ipam/tests/test_views.py index 0997757357..75396e4984 100644 --- a/netbox/ipam/tests/test_views.py +++ b/netbox/ipam/tests/test_views.py @@ -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 @@ -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], }