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

DRAFT: 9627 vlan group #16685

Closed
wants to merge 23 commits into from
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
12 changes: 11 additions & 1 deletion netbox/ipam/api/serializers_/vlans.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from netbox.api.serializers import NetBoxModelSerializer
from tenancy.api.serializers_.tenants import TenantSerializer
from utilities.api import get_serializer_for_model
from utilities.data import ranges_to_string, string_to_range_array
from vpn.api.serializers_.l2vpn import L2VPNTerminationSerializer
from .roles import RoleSerializer

Expand All @@ -21,6 +22,14 @@
)


class NumericRangeArraySerializer(serializers.CharField):
def to_internal_value(self, data):
return string_to_range_array(data)

def to_representation(self, instance):
return ranges_to_string(instance)


class VLANGroupSerializer(NetBoxModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='ipam-api:vlangroup-detail')
scope_type = ContentTypeField(
Expand All @@ -37,11 +46,12 @@ class VLANGroupSerializer(NetBoxModelSerializer):

# Related object counts
vlan_count = RelatedObjectCountField('vlans')
vlan_id_ranges = NumericRangeArraySerializer(required=False)

class Meta:
model = VLANGroup
fields = [
'id', 'url', 'display', 'name', 'slug', 'scope_type', 'scope_id', 'scope', 'min_vid', 'max_vid',
'id', 'url', 'display', 'name', 'slug', 'scope_type', 'scope_id', 'scope', 'vlan_id_ranges',
'description', 'tags', 'custom_fields', 'created', 'last_updated', 'vlan_count', 'utilization'
]
brief_fields = ('id', 'url', 'display', 'name', 'slug', 'description', 'vlan_count')
Expand Down
2 changes: 1 addition & 1 deletion netbox/ipam/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ class VLANGroupFilterSet(OrganizationalModelFilterSet):

class Meta:
model = VLANGroup
fields = ('id', 'name', 'slug', 'min_vid', 'max_vid', 'description', 'scope_id')
fields = ('id', 'name', 'slug', 'description', 'scope_id')

def search(self, queryset, name, value):
if not value.strip():
Expand Down
20 changes: 7 additions & 13 deletions netbox/ipam/forms/bulk_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,18 +408,6 @@ class FHRPGroupBulkEditForm(NetBoxModelBulkEditForm):


class VLANGroupBulkEditForm(NetBoxModelBulkEditForm):
min_vid = forms.IntegerField(
min_value=VLAN_VID_MIN,
max_value=VLAN_VID_MAX,
required=False,
label=_('Minimum child VLAN VID')
)
max_vid = forms.IntegerField(
min_value=VLAN_VID_MIN,
max_value=VLAN_VID_MAX,
required=False,
label=_('Maximum child VLAN VID')
)
description = forms.CharField(
label=_('Description'),
max_length=200,
Expand Down Expand Up @@ -483,10 +471,16 @@ class VLANGroupBulkEditForm(NetBoxModelBulkEditForm):
'group_id': '$clustergroup',
}
)
allowed_vids = NumericArrayField(
required=False,
label=_('min/max VLAN IDs'),
base_field=forms.IntegerField(),
help_text=_('Comma-separated list of numeric VLAN IDs. A range may be specified using a hyphen.'),
)

model = VLANGroup
fieldsets = (
FieldSet('site', 'min_vid', 'max_vid', 'description'),
FieldSet('site', 'allowed_vids', 'description'),
FieldSet(
'scope_type', 'region', 'sitegroup', 'site', 'location', 'rack', 'clustergroup', 'cluster', name=_('Scope')
),
Expand Down
19 changes: 7 additions & 12 deletions netbox/ipam/forms/bulk_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from netbox.forms import NetBoxModelImportForm
from tenancy.models import Tenant
from utilities.forms.fields import (
CSVChoiceField, CSVContentTypeField, CSVModelChoiceField, CSVModelMultipleChoiceField, SlugField
CSVChoiceField, CSVContentTypeField, CSVModelChoiceField, CSVModelMultipleChoiceField, SlugField,
NumericArrayField,
)
from virtualization.models import VirtualMachine, VMInterface

Expand Down Expand Up @@ -411,22 +412,16 @@ class VLANGroupImportForm(NetBoxModelImportForm):
required=False,
label=_('Scope type (app & model)')
)
min_vid = forms.IntegerField(
min_value=VLAN_VID_MIN,
max_value=VLAN_VID_MAX,
allowed_vids = NumericArrayField(
required=False,
label=_('Minimum child VLAN VID (default: {minimum})').format(minimum=VLAN_VID_MIN)
)
max_vid = forms.IntegerField(
min_value=VLAN_VID_MIN,
max_value=VLAN_VID_MAX,
required=False,
label=_('Maximum child VLAN VID (default: {maximum})').format(maximum=VLAN_VID_MIN)
label=_('min/max VLAN IDs'),
base_field=forms.IntegerField(),
help_text=_('Comma-separated list of numeric VLAN IDs. A range may be specified using a hyphen.'),
)

class Meta:
model = VLANGroup
fields = ('name', 'slug', 'scope_type', 'scope_id', 'min_vid', 'max_vid', 'description', 'tags')
fields = ('name', 'slug', 'scope_type', 'scope_id', 'allowed_vids', 'description', 'tags')
labels = {
'scope_id': 'Scope ID',
}
Expand Down
13 changes: 0 additions & 13 deletions netbox/ipam/forms/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,6 @@ class VLANGroupFilterForm(NetBoxModelFilterSetForm):
FieldSet('q', 'filter_id', 'tag'),
FieldSet('region', 'sitegroup', 'site', 'location', 'rack', name=_('Location')),
FieldSet('cluster_group', 'cluster', name=_('Cluster')),
FieldSet('min_vid', 'max_vid', name=_('VLAN ID')),
)
model = VLANGroup
region = DynamicModelMultipleChoiceField(
Expand Down Expand Up @@ -441,18 +440,6 @@ class VLANGroupFilterForm(NetBoxModelFilterSetForm):
required=False,
label=_('Rack')
)
min_vid = forms.IntegerField(
required=False,
min_value=VLAN_VID_MIN,
max_value=VLAN_VID_MAX,
label=_('Minimum VID')
)
max_vid = forms.IntegerField(
required=False,
min_value=VLAN_VID_MIN,
max_value=VLAN_VID_MAX,
label=_('Maximum VID')
)
cluster = DynamicModelMultipleChoiceField(
queryset=Cluster.objects.all(),
required=False,
Expand Down
13 changes: 10 additions & 3 deletions netbox/ipam/forms/model_forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django import forms
from django.contrib.contenttypes.models import ContentType
from django.contrib.postgres.forms import IntegerRangeField, SimpleArrayField
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

Expand All @@ -14,7 +15,7 @@
from utilities.forms import add_blank_choice
from utilities.forms.fields import (
CommentField, ContentTypeChoiceField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, NumericArrayField,
SlugField,
SlugField
)
from utilities.forms.rendering import FieldSet, InlineFields, ObjectAttribute, TabbedGroups
from utilities.forms.widgets import DatePicker
Expand Down Expand Up @@ -632,10 +633,16 @@ class VLANGroupForm(NetBoxModelForm):
}
)
slug = SlugField()
allowed_vids = NumericArrayField(
required=False,
label=_('min/max VLAN IDs'),
base_field=forms.IntegerField(),
help_text=_('Comma-separated list of numeric VLAN IDs. A range may be specified using a hyphen.'),
)

fieldsets = (
FieldSet('name', 'slug', 'description', 'tags', name=_('VLAN Group')),
FieldSet('min_vid', 'max_vid', name=_('Child VLANs')),
FieldSet('allowed_vids', name=_('Child VLANs')),
FieldSet(
'scope_type', 'region', 'sitegroup', 'site', 'location', 'rack', 'clustergroup', 'cluster',
name=_('Scope')
Expand All @@ -646,7 +653,7 @@ class Meta:
model = VLANGroup
fields = [
'name', 'slug', 'description', 'scope_type', 'region', 'sitegroup', 'site', 'location', 'rack',
'clustergroup', 'cluster', 'min_vid', 'max_vid', 'tags',
'clustergroup', 'cluster', 'allowed_vids', 'tags',
]

def __init__(self, *args, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions netbox/ipam/graphql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ class VLANType(NetBoxObjectType):
class VLANGroupType(OrganizationalObjectType):

vlans: List[VLANType]
allowed_vids: str

@strawberry_django.field
def scope(self) -> Annotated[Union[
Expand Down
52 changes: 52 additions & 0 deletions netbox/ipam/migrations/0070_vlangroup_allowed_vids.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Generated by Django 4.2.11 on 2024-06-20 19:28

import django.contrib.postgres.fields
import django.contrib.postgres.fields.ranges
from django.db import migrations, models
from django.db.backends.postgresql.psycopg_any import NumericRange
from ipam.constants import VLAN_VID_MIN, VLAN_VID_MAX
import ipam.models.vlans


def move_min_max(apps, schema_editor):
VLANGroup = apps.get_model('ipam', 'VLANGroup')
for group in VLANGroup.objects.all():
if group.min_vid or group.max_vid:
group.vlan_id_ranges = [NumericRange(group.min_vid, group.max_vid)]

group._total_vlan_ids = 0
for vlan_range in group.vlan_id_ranges:
group._total_vlan_ids += int(vlan_range.upper) - int(vlan_range.lower) + 1

group.save()


class Migration(migrations.Migration):

dependencies = [
('ipam', '0069_gfk_indexes'),
]

operations = [
migrations.AddField(
model_name='vlangroup',
name='allowed_vids',
field=django.contrib.postgres.fields.ArrayField(
base_field=models.PositiveSmallIntegerField(),
default=ipam.models.vlans.get_default_allowed_vids,
size=None,
),
),
migrations.RunPython(
code=move_min_max,
reverse_code=migrations.RunPython.noop
),
migrations.RemoveField(
model_name='vlangroup',
name='max_vid',
),
migrations.RemoveField(
model_name='vlangroup',
name='min_vid',
),
]
75 changes: 46 additions & 29 deletions netbox/ipam/models/vlans.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.postgres.fields import ArrayField
from django.core.exceptions import ValidationError
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
Expand All @@ -10,6 +11,7 @@
from ipam.constants import *
from ipam.querysets import VLANQuerySet, VLANGroupQuerySet
from netbox.models import OrganizationalModel, PrimaryModel
from utilities.data import check_ranges_overlap, ranges_to_string
from virtualization.models import VMInterface

__all__ = (
Expand All @@ -18,6 +20,10 @@
)


def get_default_allowed_vids():
return list(range(VLAN_VID_MIN, VLAN_VID_MAX + 1))


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 @@ -45,23 +51,11 @@ class VLANGroup(OrganizationalModel):
ct_field='scope_type',
fk_field='scope_id'
)
min_vid = models.PositiveSmallIntegerField(
verbose_name=_('minimum VLAN ID'),
default=VLAN_VID_MIN,
validators=(
MinValueValidator(VLAN_VID_MIN),
MaxValueValidator(VLAN_VID_MAX)
),
help_text=_('Lowest permissible ID of a child VLAN')
)
max_vid = models.PositiveSmallIntegerField(
verbose_name=_('maximum VLAN ID'),
default=VLAN_VID_MAX,
validators=(
MinValueValidator(VLAN_VID_MIN),
MaxValueValidator(VLAN_VID_MAX)
),
help_text=_('Highest permissible ID of a child VLAN')
allowed_vids = ArrayField(
verbose_name=_('min/max VLAN IDs'),
base_field=models.PositiveSmallIntegerField(),
default=get_default_allowed_vids,
help_text=_('Ranges of Minimum-maximum child VLAN VID'),
)

objects = VLANGroupQuerySet.as_manager()
Expand Down Expand Up @@ -96,17 +90,30 @@ def clean(self):
if self.scope_id and not self.scope_type:
raise ValidationError(_("Cannot set scope_id without scope_type."))

# Validate min/max child VID limits
if self.max_vid < self.min_vid:
raise ValidationError({
'max_vid': _("Maximum child VID must be greater than or equal to minimum child VID")
})
# Validate vlan ranges
if 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:
raise ValidationError({
'vlan_id_ranges': _("Maximum child VID must be greater than or equal to minimum child VID Invalid range ({value})").format(value=ranges)
})

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

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

def get_available_vids(self):
"""
Return all available VLANs within this group.
"""
available_vlans = {vid for vid in range(self.min_vid, self.max_vid + 1)}
available_vlans = {}
for vlan_range in self.vlan_id_ranges:
available_vlans = {vid for vid in range(vlan_range.lower, vlan_range.upper + 1)}
available_vlans -= set(VLAN.objects.filter(group=self).values_list('vid', flat=True))

return sorted(available_vlans)
Expand All @@ -126,6 +133,10 @@ def get_child_vlans(self):
"""
return VLAN.objects.filter(group=self).order_by('vid')

@property
def vlan_ranges(self):
return ranges_to_string(self.vlan_id_ranges)


class VLAN(PrimaryModel):
"""
Expand Down Expand Up @@ -232,12 +243,18 @@ def clean(self):
)

# Validate group min/max VIDs
if self.group and not self.group.min_vid <= self.vid <= self.group.max_vid:
raise ValidationError({
'vid': _(
"VID must be between {minimum} and {maximum} for VLANs in group {group}"
).format(minimum=self.group.min_vid, maximum=self.group.max_vid, group=self.group)
})
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:
in_bounds = True

if not in_bounds:
raise ValidationError({
'vid': _(
"VID must be in ranges {ranges} for VLANs in group {group}"
).format(ranges=ranges_to_string(self.group.vlan_id_ranges), group=self.group)
})

def get_status_color(self):
return VLANStatusChoices.colors.get(self.status)
Expand Down
5 changes: 3 additions & 2 deletions netbox/ipam/querysets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib.contenttypes.models import ContentType
from django.db.models import Count, F, OuterRef, Q, Subquery, Value
from django.db.models import Count, F, Func, OuterRef, Q, Subquery, Value
from django.db.models.expressions import RawSQL
from django.db.models.functions import Round

Expand Down Expand Up @@ -63,7 +63,8 @@ def annotate_utilization(self):

return self.annotate(
vlan_count=count_related(VLAN, 'group'),
utilization=Round(F('vlan_count') / (F('max_vid') - F('min_vid') + 1.0) * 100, 2)
total_allowed_vids=Func(F('allowed_vids'), function='CARDINALITY'),
utilization=Round(F('vlan_count') * 100 / F('total_allowed_vids'), 2)
)


Expand Down
Loading
Loading