From b0f9035e2ddcac88ba935d20e843beddbd61b428 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 21 Feb 2017 14:53:22 -0500 Subject: [PATCH] Additional work on #904 --- docs/data-model/ipam.md | 6 ++++-- netbox/ipam/filters.py | 20 ++----------------- netbox/ipam/forms.py | 14 +++++++------ ..._20170219_0726.py => 0015_global_vlans.py} | 6 +++--- netbox/ipam/models.py | 7 ++++--- netbox/templates/ipam/vlan.html | 4 +--- 6 files changed, 22 insertions(+), 35 deletions(-) rename netbox/ipam/migrations/{0015_auto_20170219_0726.py => 0015_global_vlans.py} (74%) diff --git a/docs/data-model/ipam.md b/docs/data-model/ipam.md index ee54e74d2..6e72d5b87 100644 --- a/docs/data-model/ipam.md +++ b/docs/data-model/ipam.md @@ -83,9 +83,11 @@ One IP address can be designated as the network address translation (NAT) IP add # VLANs -A VLAN represents an isolated layer two domain, identified by a name and a numeric ID (1-4094). Note that while it is good practice, neither VLAN names nor IDs must be unique within a site. This is to accommodate the fact that many real-world network use less-than-optimal VLAN allocations and may have overlapping VLAN ID assignments in practice. +A VLAN represents an isolated layer two domain, identified by a name and a numeric ID (1-4094) as defined in [IEEE 802.1Q](https://en.wikipedia.org/wiki/IEEE_802.1Q). VLANs may be assigned to a site and/or VLAN group. Like prefixes, each VLAN is assigned an operational status and (optionally) a functional role. -Like prefixes, each VLAN is assigned an operational status and (optionally) a functional role. +### VLAN Groups + +VLAN groups can be employed for administrative organization within NetBox. Each VLAN within a group must have a unique ID and name. VLANs which are not assigned to a group may have overlapping names and IDs, including within a site. --- diff --git a/netbox/ipam/filters.py b/netbox/ipam/filters.py index 558f5eade..b83228f9c 100644 --- a/netbox/ipam/filters.py +++ b/netbox/ipam/filters.py @@ -266,25 +266,17 @@ class VLANGroupFilter(django_filters.FilterSet): name='site', queryset=Site.objects.all(), label='Site (ID)', - method='site_search', ) site = NullableModelMultipleChoiceFilter( - name='site__slug', + name='site', queryset=Site.objects.all(), to_field_name='slug', label='Site (slug)', - method='site_search', ) class Meta: model = VLANGroup - def site_search(self, queryset, name, value): - q = Q(**{name: None}) - for v in value: - q |= Q(**{name: v}) - return queryset.filter(q) - class VLANFilter(CustomFieldFilterSet, django_filters.FilterSet): q = django_filters.MethodFilter( @@ -295,14 +287,12 @@ class VLANFilter(CustomFieldFilterSet, django_filters.FilterSet): name='site', queryset=Site.objects.all(), label='Site (ID)', - method='site_search', ) site = NullableModelMultipleChoiceFilter( - name='site__slug', + name='site', queryset=Site.objects.all(), to_field_name='slug', label='Site (slug)', - method='site_search', ) group_id = NullableModelMultipleChoiceFilter( name='group', @@ -359,12 +349,6 @@ class VLANFilter(CustomFieldFilterSet, django_filters.FilterSet): pass return queryset.filter(qs_filter) - def site_search(self, queryset, name, value): - q = Q(**{name: None}) - for v in value: - q |= Q(**{name: v}) - return queryset.filter(q) - class ServiceFilter(django_filters.FilterSet): device_id = django_filters.ModelMultipleChoiceFilter( diff --git a/netbox/ipam/forms.py b/netbox/ipam/forms.py index d6db8c9bc..123ca0131 100644 --- a/netbox/ipam/forms.py +++ b/netbox/ipam/forms.py @@ -153,8 +153,7 @@ class RoleForm(BootstrapMixin, forms.ModelForm): class PrefixForm(BootstrapMixin, CustomFieldForm): site = forms.ModelChoiceField(queryset=Site.objects.all(), required=False, label='Site', - widget=forms.Select(attrs={'filter-for': 'vlan', - 'default_value': '0'})) + widget=forms.Select(attrs={'filter-for': 'vlan', 'nullable': 'true'})) vlan = forms.ModelChoiceField(queryset=VLAN.objects.all(), required=False, label='VLAN', widget=APISelect(api_url='/api/ipam/vlans/?site_id={{site}}', display_field='display_name')) @@ -509,8 +508,11 @@ class VLANGroupForm(BootstrapMixin, forms.ModelForm): class VLANGroupFilterForm(BootstrapMixin, forms.Form): - site = FilterChoiceField(queryset=Site.objects.annotate(filter_count=Count('vlan_groups')), to_field_name='slug', - null_option=(0, 'Global')) + site = FilterChoiceField( + queryset=Site.objects.annotate(filter_count=Count('vlan_groups')), + to_field_name='slug', + null_option=(0, 'Global') + ) # @@ -526,7 +528,7 @@ class VLANForm(BootstrapMixin, CustomFieldForm): model = VLAN fields = ['site', 'group', 'vid', 'name', 'tenant', 'status', 'role', 'description'] help_texts = { - 'site': "The site at which this VLAN exists", + 'site': "Leave blank if this VLAN spans multiple sites", 'group': "VLAN group (optional)", 'vid': "Configured VLAN ID", 'name': "Configured VLAN name", @@ -534,7 +536,7 @@ class VLANForm(BootstrapMixin, CustomFieldForm): 'role': "The primary function of this VLAN", } widgets = { - 'site': forms.Select(attrs={'filter-for': 'group', 'default_value': '0'}), + 'site': forms.Select(attrs={'filter-for': 'group', 'nullable': 'true'}), } def __init__(self, *args, **kwargs): diff --git a/netbox/ipam/migrations/0015_auto_20170219_0726.py b/netbox/ipam/migrations/0015_global_vlans.py similarity index 74% rename from netbox/ipam/migrations/0015_auto_20170219_0726.py rename to netbox/ipam/migrations/0015_global_vlans.py index 38003b2b8..18d82cbaf 100644 --- a/netbox/ipam/migrations/0015_auto_20170219_0726.py +++ b/netbox/ipam/migrations/0015_global_vlans.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.10.5 on 2017-02-19 07:26 +# Generated by Django 1.10.4 on 2017-02-21 18:45 from __future__ import unicode_literals from django.db import migrations, models @@ -16,11 +16,11 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='vlan', name='site', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='vlans', to='dcim.Site'), + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='vlans', to='dcim.Site'), ), migrations.AlterField( model_name='vlangroup', name='site', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='vlan_groups', to='dcim.Site'), + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='vlan_groups', to='dcim.Site'), ), ] diff --git a/netbox/ipam/models.py b/netbox/ipam/models.py index fa9e7834b..a04e4b9ce 100644 --- a/netbox/ipam/models.py +++ b/netbox/ipam/models.py @@ -485,7 +485,7 @@ class VLANGroup(models.Model): """ name = models.CharField(max_length=50) slug = models.SlugField() - site = models.ForeignKey('dcim.Site', related_name='vlan_groups', on_delete=models.SET_NULL, blank=True, null=True) + site = models.ForeignKey('dcim.Site', related_name='vlan_groups', on_delete=models.PROTECT, blank=True, null=True) class Meta: ordering = ['site', 'name'] @@ -497,8 +497,9 @@ class VLANGroup(models.Model): verbose_name_plural = 'VLAN groups' def __str__(self): - site_name = self.site.name if self.site else '__global' - return u'{} - {}'.format(site_name, self.name) + if self.site is None: + return self.name + return u'{} - {}'.format(self.site.name, self.name) def get_absolute_url(self): return "{}?group_id={}".format(reverse('ipam:vlan_list'), self.pk) diff --git a/netbox/templates/ipam/vlan.html b/netbox/templates/ipam/vlan.html index 5d9f219fc..1b6ddb5dd 100644 --- a/netbox/templates/ipam/vlan.html +++ b/netbox/templates/ipam/vlan.html @@ -10,11 +10,9 @@
  • VLANs
  • {% if vlan.site %}
  • {{ vlan.site }}
  • - {% else %} -
  • Global
  • {% endif %} {% if vlan.group %} -
  • {{ vlan.group.name }}
  • +
  • {{ vlan.group.name }}
  • {% endif %}
  • {{ vlan.name }} ({{ vlan.vid }})