2016-03-01 11:23:03 -05:00
|
|
|
from django import forms
|
2017-06-15 14:13:20 -04:00
|
|
|
from django.core.exceptions import MultipleObjectsReturned
|
2018-06-21 15:55:27 -04:00
|
|
|
from django.core.validators import MaxValueValidator, MinValueValidator
|
2018-05-10 12:53:11 -04:00
|
|
|
from taggit.forms import TagField
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2016-10-24 15:07:11 -04:00
|
|
|
from dcim.models import Site, Rack, Device, Interface
|
2018-07-10 10:00:21 -04:00
|
|
|
from extras.forms import AddRemoveTagsForm, CustomFieldForm, CustomFieldBulkEditForm, CustomFieldFilterForm
|
2019-10-04 12:08:48 -04:00
|
|
|
from tenancy.forms import TenancyFilterForm, TenancyForm
|
2016-07-27 11:29:20 -04:00
|
|
|
from tenancy.models import Tenant
|
2016-09-14 16:27:26 -04:00
|
|
|
from utilities.forms import (
|
2019-01-10 21:19:13 -05:00
|
|
|
add_blank_choice, APISelect, APISelectMultiple, BootstrapMixin, BulkEditNullBooleanSelect, ChainedModelChoiceField,
|
|
|
|
CSVChoiceField, ExpandableIPAddressField, FilterChoiceField, FlexibleModelChoiceField, ReturnURLForm, SlugField,
|
|
|
|
StaticSelect2, StaticSelect2Multiple, BOOLEAN_WITH_BLANK_CHOICES
|
2016-09-14 16:27:26 -04:00
|
|
|
)
|
2017-09-18 13:30:20 -04:00
|
|
|
from virtualization.models import VirtualMachine
|
2019-10-04 12:08:48 -04:00
|
|
|
from .constants import *
|
2017-11-07 11:08:23 -05:00
|
|
|
from .models import Aggregate, IPAddress, Prefix, RIR, Role, Service, VLAN, VLANGroup, VRF
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2016-09-13 10:35:55 -04:00
|
|
|
IP_FAMILY_CHOICES = [
|
2016-09-13 12:10:21 -04:00
|
|
|
('', 'All'),
|
|
|
|
(4, 'IPv4'),
|
|
|
|
(6, 'IPv6'),
|
|
|
|
]
|
2016-06-29 10:52:06 -04:00
|
|
|
|
2017-09-14 14:45:45 -04:00
|
|
|
PREFIX_MASK_LENGTH_CHOICES = add_blank_choice([(i, i) for i in range(1, 128)])
|
|
|
|
IPADDRESS_MASK_LENGTH_CHOICES = add_blank_choice([(i, i) for i in range(1, 129)])
|
2017-03-01 14:23:52 -05:00
|
|
|
|
2016-06-29 10:52:06 -04:00
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
#
|
|
|
|
# VRFs
|
|
|
|
#
|
|
|
|
|
2017-05-11 17:35:20 -04:00
|
|
|
class VRFForm(BootstrapMixin, TenancyForm, CustomFieldForm):
|
2018-11-27 11:57:29 -05:00
|
|
|
tags = TagField(
|
|
|
|
required=False
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = VRF
|
2018-11-27 11:57:29 -05:00
|
|
|
fields = [
|
|
|
|
'name', 'rd', 'enforce_unique', 'description', 'tenant_group', 'tenant', 'tags',
|
|
|
|
]
|
2016-05-09 14:11:53 -04:00
|
|
|
labels = {
|
|
|
|
'rd': "RD",
|
|
|
|
}
|
2016-03-01 11:23:03 -05:00
|
|
|
help_texts = {
|
|
|
|
'rd': "Route distinguisher in any format",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-02 16:07:11 -04:00
|
|
|
class VRFCSVForm(forms.ModelForm):
|
|
|
|
tenant = forms.ModelChoiceField(
|
|
|
|
queryset=Tenant.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
|
|
|
help_text='Name of assigned tenant',
|
|
|
|
error_messages={
|
|
|
|
'invalid_choice': 'Tenant not found.',
|
|
|
|
}
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = VRF
|
2018-02-02 14:26:16 -05:00
|
|
|
fields = VRF.csv_headers
|
2017-06-07 15:51:11 -04:00
|
|
|
help_texts = {
|
|
|
|
'name': 'VRF name',
|
|
|
|
}
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
2018-07-10 10:00:21 -04:00
|
|
|
class VRFBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
|
2018-11-27 11:57:29 -05:00
|
|
|
pk = forms.ModelMultipleChoiceField(
|
|
|
|
queryset=VRF.objects.all(),
|
|
|
|
widget=forms.MultipleHiddenInput()
|
|
|
|
)
|
|
|
|
tenant = forms.ModelChoiceField(
|
|
|
|
queryset=Tenant.objects.all(),
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/tenancy/tenants/"
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
2017-04-28 12:32:27 -04:00
|
|
|
enforce_unique = forms.NullBooleanField(
|
2018-11-27 11:57:29 -05:00
|
|
|
required=False,
|
|
|
|
widget=BulkEditNullBooleanSelect(),
|
|
|
|
label='Enforce unique space'
|
|
|
|
)
|
|
|
|
description = forms.CharField(
|
|
|
|
max_length=100,
|
|
|
|
required=False
|
2017-04-28 12:32:27 -04:00
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2016-09-30 16:17:41 -04:00
|
|
|
class Meta:
|
2018-11-27 11:57:29 -05:00
|
|
|
nullable_fields = [
|
|
|
|
'tenant', 'description',
|
|
|
|
]
|
2016-09-30 16:17:41 -04:00
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2019-03-05 08:10:10 -06:00
|
|
|
class VRFFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm):
|
2016-08-23 12:05:28 -04:00
|
|
|
model = VRF
|
2019-05-09 14:32:49 -04:00
|
|
|
field_order = ['q', 'tenant_group', 'tenant']
|
2018-11-27 11:57:29 -05:00
|
|
|
q = forms.CharField(
|
|
|
|
required=False,
|
|
|
|
label='Search'
|
|
|
|
)
|
2016-07-27 11:29:20 -04:00
|
|
|
|
|
|
|
|
2016-05-16 13:04:45 -04:00
|
|
|
#
|
|
|
|
# RIRs
|
|
|
|
#
|
|
|
|
|
2016-12-21 14:15:18 -05:00
|
|
|
class RIRForm(BootstrapMixin, forms.ModelForm):
|
2016-05-20 15:32:17 -04:00
|
|
|
slug = SlugField()
|
2016-05-16 13:04:45 -04:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = RIR
|
2018-11-27 11:57:29 -05:00
|
|
|
fields = [
|
|
|
|
'name', 'slug', 'is_private',
|
|
|
|
]
|
2016-12-06 13:59:13 -05:00
|
|
|
|
|
|
|
|
2017-10-09 15:42:06 -04:00
|
|
|
class RIRCSVForm(forms.ModelForm):
|
|
|
|
slug = SlugField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = RIR
|
2018-02-02 14:26:16 -05:00
|
|
|
fields = RIR.csv_headers
|
2017-10-09 15:42:06 -04:00
|
|
|
help_texts = {
|
|
|
|
'name': 'RIR name',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-21 14:15:18 -05:00
|
|
|
class RIRFilterForm(BootstrapMixin, forms.Form):
|
2018-11-27 11:57:29 -05:00
|
|
|
is_private = forms.NullBooleanField(
|
|
|
|
required=False,
|
|
|
|
label='Private',
|
2019-01-10 21:19:13 -05:00
|
|
|
widget=StaticSelect2(
|
|
|
|
choices=BOOLEAN_WITH_BLANK_CHOICES
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
)
|
2016-05-16 13:04:45 -04:00
|
|
|
|
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
#
|
|
|
|
# Aggregates
|
|
|
|
#
|
|
|
|
|
2016-08-15 15:24:23 -04:00
|
|
|
class AggregateForm(BootstrapMixin, CustomFieldForm):
|
2018-11-27 11:57:29 -05:00
|
|
|
tags = TagField(
|
|
|
|
required=False
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Aggregate
|
2018-11-27 11:57:29 -05:00
|
|
|
fields = [
|
|
|
|
'prefix', 'rir', 'date_added', 'description', 'tags',
|
|
|
|
]
|
2016-03-01 11:23:03 -05:00
|
|
|
help_texts = {
|
|
|
|
'prefix': "IPv4 or IPv6 network",
|
|
|
|
'rir': "Regional Internet Registry responsible for this prefix",
|
|
|
|
'date_added': "Format: YYYY-MM-DD",
|
|
|
|
}
|
2019-01-10 21:19:13 -05:00
|
|
|
widgets = {
|
|
|
|
'rir': APISelect(
|
|
|
|
api_url="/api/ipam/rirs/"
|
|
|
|
)
|
|
|
|
}
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
2017-06-02 16:07:11 -04:00
|
|
|
class AggregateCSVForm(forms.ModelForm):
|
|
|
|
rir = forms.ModelChoiceField(
|
|
|
|
queryset=RIR.objects.all(),
|
|
|
|
to_field_name='name',
|
|
|
|
help_text='Name of parent RIR',
|
|
|
|
error_messages={
|
|
|
|
'invalid_choice': 'RIR not found.',
|
|
|
|
}
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Aggregate
|
2018-02-02 14:26:16 -05:00
|
|
|
fields = Aggregate.csv_headers
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
2018-07-10 10:00:21 -04:00
|
|
|
class AggregateBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
|
2018-11-27 11:57:29 -05:00
|
|
|
pk = forms.ModelMultipleChoiceField(
|
|
|
|
queryset=Aggregate.objects.all(),
|
|
|
|
widget=forms.MultipleHiddenInput()
|
|
|
|
)
|
|
|
|
rir = forms.ModelChoiceField(
|
|
|
|
queryset=RIR.objects.all(),
|
|
|
|
required=False,
|
2019-01-10 21:19:13 -05:00
|
|
|
label='RIR',
|
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/ipam/rirs/"
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
date_added = forms.DateField(
|
|
|
|
required=False
|
|
|
|
)
|
|
|
|
description = forms.CharField(
|
|
|
|
max_length=100,
|
|
|
|
required=False
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2016-09-30 16:17:41 -04:00
|
|
|
class Meta:
|
2018-11-27 11:57:29 -05:00
|
|
|
nullable_fields = [
|
|
|
|
'date_added', 'description',
|
|
|
|
]
|
2016-09-30 16:17:41 -04:00
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2016-08-23 12:05:28 -04:00
|
|
|
class AggregateFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|
|
|
model = Aggregate
|
2018-11-27 11:57:29 -05:00
|
|
|
q = forms.CharField(
|
|
|
|
required=False,
|
|
|
|
label='Search'
|
|
|
|
)
|
|
|
|
family = forms.ChoiceField(
|
|
|
|
required=False,
|
|
|
|
choices=IP_FAMILY_CHOICES,
|
2019-01-10 21:19:13 -05:00
|
|
|
label='Address family',
|
|
|
|
widget=StaticSelect2()
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
2017-03-01 13:09:19 -05:00
|
|
|
rir = FilterChoiceField(
|
2019-01-10 21:19:13 -05:00
|
|
|
queryset=RIR.objects.all(),
|
2017-03-01 13:09:19 -05:00
|
|
|
to_field_name='slug',
|
2019-01-10 21:19:13 -05:00
|
|
|
label='RIR',
|
|
|
|
widget=APISelectMultiple(
|
|
|
|
api_url="/api/ipam/rirs/",
|
|
|
|
value_field="slug",
|
|
|
|
)
|
2017-03-01 13:09:19 -05:00
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
2016-05-17 15:04:16 -04:00
|
|
|
#
|
|
|
|
# Roles
|
|
|
|
#
|
|
|
|
|
2016-12-21 14:15:18 -05:00
|
|
|
class RoleForm(BootstrapMixin, forms.ModelForm):
|
2016-05-20 15:32:17 -04:00
|
|
|
slug = SlugField()
|
2016-05-17 15:04:16 -04:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Role
|
2018-11-27 11:57:29 -05:00
|
|
|
fields = [
|
2019-11-06 09:39:21 -05:00
|
|
|
'name', 'slug', 'weight',
|
2018-11-27 11:57:29 -05:00
|
|
|
]
|
2016-05-17 15:04:16 -04:00
|
|
|
|
|
|
|
|
2017-10-09 15:42:06 -04:00
|
|
|
class RoleCSVForm(forms.ModelForm):
|
|
|
|
slug = SlugField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Role
|
2018-02-02 14:26:16 -05:00
|
|
|
fields = Role.csv_headers
|
2017-10-09 15:42:06 -04:00
|
|
|
help_texts = {
|
|
|
|
'name': 'Role name',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
#
|
|
|
|
# Prefixes
|
|
|
|
#
|
|
|
|
|
2017-05-11 17:35:20 -04:00
|
|
|
class PrefixForm(BootstrapMixin, TenancyForm, CustomFieldForm):
|
2017-05-11 16:24:57 -04:00
|
|
|
site = forms.ModelChoiceField(
|
2017-05-25 14:33:50 -04:00
|
|
|
queryset=Site.objects.all(),
|
|
|
|
required=False,
|
|
|
|
label='Site',
|
2019-01-10 21:19:13 -05:00
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/dcim/sites/",
|
|
|
|
filter_for={
|
|
|
|
'vlan_group': 'site_id',
|
|
|
|
'vlan': 'site_id',
|
|
|
|
},
|
2018-11-27 11:57:29 -05:00
|
|
|
attrs={
|
|
|
|
'nullable': 'true',
|
|
|
|
}
|
2017-06-09 14:15:12 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
vlan_group = ChainedModelChoiceField(
|
|
|
|
queryset=VLANGroup.objects.all(),
|
|
|
|
chains=(
|
|
|
|
('site', 'site'),
|
|
|
|
),
|
|
|
|
required=False,
|
|
|
|
label='VLAN group',
|
|
|
|
widget=APISelect(
|
2019-01-10 21:19:13 -05:00
|
|
|
api_url='/api/ipam/vlan-groups/',
|
|
|
|
filter_for={
|
|
|
|
'vlan': 'group_id'
|
|
|
|
},
|
2018-11-27 11:57:29 -05:00
|
|
|
attrs={
|
|
|
|
'nullable': 'true',
|
|
|
|
}
|
2017-05-11 16:24:57 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
vlan = ChainedModelChoiceField(
|
2017-05-25 14:33:50 -04:00
|
|
|
queryset=VLAN.objects.all(),
|
|
|
|
chains=(
|
|
|
|
('site', 'site'),
|
2017-06-09 14:15:12 -04:00
|
|
|
('group', 'vlan_group'),
|
2017-05-25 14:33:50 -04:00
|
|
|
),
|
|
|
|
required=False,
|
|
|
|
label='VLAN',
|
|
|
|
widget=APISelect(
|
2019-01-10 21:19:13 -05:00
|
|
|
api_url='/api/ipam/vlans/',
|
2018-11-27 11:57:29 -05:00
|
|
|
display_field='display_name'
|
2017-05-11 16:24:57 -04:00
|
|
|
)
|
|
|
|
)
|
2018-05-10 12:53:11 -04:00
|
|
|
tags = TagField(required=False)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Prefix
|
2018-05-10 12:53:11 -04:00
|
|
|
fields = [
|
|
|
|
'prefix', 'vrf', 'site', 'vlan', 'status', 'role', 'is_pool', 'description', 'tenant_group', 'tenant',
|
|
|
|
'tags',
|
|
|
|
]
|
2019-01-10 21:19:13 -05:00
|
|
|
widgets = {
|
|
|
|
'vrf': APISelect(
|
|
|
|
api_url="/api/ipam/vrfs/"
|
|
|
|
),
|
|
|
|
'status': StaticSelect2(),
|
|
|
|
'role': APISelect(
|
|
|
|
api_url="/api/ipam/roles/"
|
|
|
|
)
|
|
|
|
}
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-06-09 14:15:12 -04:00
|
|
|
|
|
|
|
# Initialize helper selectors
|
|
|
|
instance = kwargs.get('instance')
|
2017-07-11 14:52:50 -04:00
|
|
|
initial = kwargs.get('initial', {}).copy()
|
2017-06-09 14:15:12 -04:00
|
|
|
if instance and instance.vlan is not None:
|
|
|
|
initial['vlan_group'] = instance.vlan.group
|
|
|
|
kwargs['initial'] = initial
|
|
|
|
|
2018-11-27 10:52:24 -05:00
|
|
|
super().__init__(*args, **kwargs)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
self.fields['vrf'].empty_label = 'Global'
|
|
|
|
|
|
|
|
|
2017-06-02 16:07:11 -04:00
|
|
|
class PrefixCSVForm(forms.ModelForm):
|
2019-03-11 12:34:19 -04:00
|
|
|
vrf = FlexibleModelChoiceField(
|
2017-06-02 16:07:11 -04:00
|
|
|
queryset=VRF.objects.all(),
|
|
|
|
to_field_name='rd',
|
2019-03-11 12:34:19 -04:00
|
|
|
required=False,
|
|
|
|
help_text='Route distinguisher of parent VRF (or {ID})',
|
2017-06-02 16:07:11 -04:00
|
|
|
error_messages={
|
|
|
|
'invalid_choice': 'VRF not found.',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
tenant = forms.ModelChoiceField(
|
|
|
|
queryset=Tenant.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
|
|
|
help_text='Name of assigned tenant',
|
|
|
|
error_messages={
|
|
|
|
'invalid_choice': 'Tenant not found.',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
site = forms.ModelChoiceField(
|
|
|
|
queryset=Site.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
|
|
|
help_text='Name of parent site',
|
|
|
|
error_messages={
|
|
|
|
'invalid_choice': 'Site not found.',
|
|
|
|
}
|
|
|
|
)
|
2017-06-07 14:19:08 -04:00
|
|
|
vlan_group = forms.CharField(
|
2017-06-02 16:07:11 -04:00
|
|
|
help_text='Group name of assigned VLAN',
|
|
|
|
required=False
|
|
|
|
)
|
|
|
|
vlan_vid = forms.IntegerField(
|
|
|
|
help_text='Numeric ID of assigned VLAN',
|
|
|
|
required=False
|
|
|
|
)
|
2017-06-07 13:22:06 -04:00
|
|
|
status = CSVChoiceField(
|
2017-07-12 10:31:16 -04:00
|
|
|
choices=PREFIX_STATUS_CHOICES,
|
2017-06-07 15:51:11 -04:00
|
|
|
help_text='Operational status'
|
2017-06-02 16:07:11 -04:00
|
|
|
)
|
|
|
|
role = forms.ModelChoiceField(
|
|
|
|
queryset=Role.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2017-06-07 15:51:11 -04:00
|
|
|
help_text='Functional role',
|
2017-06-02 16:07:11 -04:00
|
|
|
error_messages={
|
|
|
|
'invalid_choice': 'Invalid role.',
|
|
|
|
}
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Prefix
|
2018-02-02 14:26:16 -05:00
|
|
|
fields = Prefix.csv_headers
|
2016-07-15 14:25:30 -04:00
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
|
2018-11-27 10:52:24 -05:00
|
|
|
super().clean()
|
2017-06-07 14:19:08 -04:00
|
|
|
|
2016-07-15 14:25:30 -04:00
|
|
|
site = self.cleaned_data.get('site')
|
2017-06-07 14:19:08 -04:00
|
|
|
vlan_group = self.cleaned_data.get('vlan_group')
|
2016-07-15 14:25:30 -04:00
|
|
|
vlan_vid = self.cleaned_data.get('vlan_vid')
|
2017-04-07 14:50:08 -04:00
|
|
|
|
2017-06-07 14:19:08 -04:00
|
|
|
# Validate VLAN
|
|
|
|
if vlan_group and vlan_vid:
|
2016-07-15 14:25:30 -04:00
|
|
|
try:
|
2017-06-07 14:19:08 -04:00
|
|
|
self.instance.vlan = VLAN.objects.get(site=site, group__name=vlan_group, vid=vlan_vid)
|
|
|
|
except VLAN.DoesNotExist:
|
2017-04-07 14:50:08 -04:00
|
|
|
if site:
|
2017-06-07 14:19:08 -04:00
|
|
|
raise forms.ValidationError("VLAN {} not found in site {} group {}".format(
|
|
|
|
vlan_vid, site, vlan_group
|
|
|
|
))
|
2017-04-07 14:50:08 -04:00
|
|
|
else:
|
2017-06-07 14:19:08 -04:00
|
|
|
raise forms.ValidationError("Global VLAN {} not found in group {}".format(vlan_vid, vlan_group))
|
2017-06-15 14:13:20 -04:00
|
|
|
except MultipleObjectsReturned:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
"Multiple VLANs with VID {} found in group {}".format(vlan_vid, vlan_group)
|
|
|
|
)
|
2017-06-07 14:19:08 -04:00
|
|
|
elif vlan_vid:
|
2016-07-15 14:25:30 -04:00
|
|
|
try:
|
2017-06-07 14:19:08 -04:00
|
|
|
self.instance.vlan = VLAN.objects.get(site=site, group__isnull=True, vid=vlan_vid)
|
2017-01-17 15:01:30 -05:00
|
|
|
except VLAN.DoesNotExist:
|
2017-04-07 14:50:08 -04:00
|
|
|
if site:
|
2017-06-07 14:19:08 -04:00
|
|
|
raise forms.ValidationError("VLAN {} not found in site {}".format(vlan_vid, site))
|
|
|
|
else:
|
|
|
|
raise forms.ValidationError("Global VLAN {} not found".format(vlan_vid))
|
2017-06-15 14:13:20 -04:00
|
|
|
except MultipleObjectsReturned:
|
|
|
|
raise forms.ValidationError("Multiple VLANs with VID {} found".format(vlan_vid))
|
2016-05-18 11:16:56 -04:00
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2018-07-10 10:00:21 -04:00
|
|
|
class PrefixBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
|
2018-11-27 11:57:29 -05:00
|
|
|
pk = forms.ModelMultipleChoiceField(
|
|
|
|
queryset=Prefix.objects.all(),
|
|
|
|
widget=forms.MultipleHiddenInput()
|
|
|
|
)
|
|
|
|
site = forms.ModelChoiceField(
|
|
|
|
queryset=Site.objects.all(),
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/dcim/sites/"
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
vrf = forms.ModelChoiceField(
|
|
|
|
queryset=VRF.objects.all(),
|
|
|
|
required=False,
|
2019-01-10 21:19:13 -05:00
|
|
|
label='VRF',
|
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/ipam/vrfs/"
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
2019-01-03 16:21:21 -05:00
|
|
|
prefix_length = forms.IntegerField(
|
|
|
|
min_value=1,
|
|
|
|
max_value=127,
|
|
|
|
required=False
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
tenant = forms.ModelChoiceField(
|
|
|
|
queryset=Tenant.objects.all(),
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/tenancy/tenants/"
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
status = forms.ChoiceField(
|
|
|
|
choices=add_blank_choice(PREFIX_STATUS_CHOICES),
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=StaticSelect2()
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
role = forms.ModelChoiceField(
|
|
|
|
queryset=Role.objects.all(),
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/ipam/roles/"
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
is_pool = forms.NullBooleanField(
|
|
|
|
required=False,
|
|
|
|
widget=BulkEditNullBooleanSelect(),
|
|
|
|
label='Is a pool'
|
|
|
|
)
|
|
|
|
description = forms.CharField(
|
|
|
|
max_length=100,
|
|
|
|
required=False
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2016-09-30 16:17:41 -04:00
|
|
|
class Meta:
|
2018-11-27 11:57:29 -05:00
|
|
|
nullable_fields = [
|
|
|
|
'site', 'vrf', 'tenant', 'role', 'description',
|
|
|
|
]
|
2016-09-30 16:17:41 -04:00
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2019-03-05 08:10:10 -06:00
|
|
|
class PrefixFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm):
|
2016-08-23 12:05:28 -04:00
|
|
|
model = Prefix
|
2019-05-09 14:32:49 -04:00
|
|
|
field_order = [
|
|
|
|
'q', 'within_include', 'family', 'mask_length', 'vrf_id', 'status', 'site', 'role', 'tenant_group', 'tenant',
|
|
|
|
'is_pool', 'expand',
|
|
|
|
]
|
2018-11-27 11:57:29 -05:00
|
|
|
q = forms.CharField(
|
|
|
|
required=False,
|
|
|
|
label='Search'
|
|
|
|
)
|
|
|
|
within_include = forms.CharField(
|
|
|
|
required=False,
|
|
|
|
widget=forms.TextInput(
|
|
|
|
attrs={
|
|
|
|
'placeholder': 'Prefix',
|
|
|
|
}
|
|
|
|
),
|
|
|
|
label='Search within'
|
|
|
|
)
|
|
|
|
family = forms.ChoiceField(
|
|
|
|
required=False,
|
|
|
|
choices=IP_FAMILY_CHOICES,
|
2019-01-10 21:19:13 -05:00
|
|
|
label='Address family',
|
|
|
|
widget=StaticSelect2()
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
mask_length = forms.ChoiceField(
|
|
|
|
required=False,
|
|
|
|
choices=PREFIX_MASK_LENGTH_CHOICES,
|
2019-01-10 21:19:13 -05:00
|
|
|
label='Mask length',
|
|
|
|
widget=StaticSelect2()
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
2019-03-28 10:06:25 -04:00
|
|
|
vrf_id = FilterChoiceField(
|
2019-01-10 21:19:13 -05:00
|
|
|
queryset=VRF.objects.all(),
|
2017-03-01 13:09:19 -05:00
|
|
|
label='VRF',
|
2019-01-10 21:19:13 -05:00
|
|
|
null_label='-- Global --',
|
|
|
|
widget=APISelectMultiple(
|
|
|
|
api_url="/api/ipam/vrfs/",
|
|
|
|
null_option=True,
|
|
|
|
)
|
2017-03-01 13:09:19 -05:00
|
|
|
)
|
2019-01-10 21:19:13 -05:00
|
|
|
status = forms.MultipleChoiceField(
|
2018-03-07 14:16:38 -05:00
|
|
|
choices=PREFIX_STATUS_CHOICES,
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=StaticSelect2Multiple()
|
2018-03-07 14:16:38 -05:00
|
|
|
)
|
2017-03-01 13:09:19 -05:00
|
|
|
site = FilterChoiceField(
|
2019-01-10 21:19:13 -05:00
|
|
|
queryset=Site.objects.all(),
|
2017-03-01 13:09:19 -05:00
|
|
|
to_field_name='slug',
|
2019-01-10 21:19:13 -05:00
|
|
|
null_label='-- None --',
|
|
|
|
widget=APISelectMultiple(
|
|
|
|
api_url="/api/dcim/sites/",
|
|
|
|
value_field="slug",
|
|
|
|
null_option=True,
|
|
|
|
)
|
2017-03-01 13:09:19 -05:00
|
|
|
)
|
|
|
|
role = FilterChoiceField(
|
2019-01-10 21:19:13 -05:00
|
|
|
queryset=Role.objects.all(),
|
2017-03-01 13:09:19 -05:00
|
|
|
to_field_name='slug',
|
2019-01-10 21:19:13 -05:00
|
|
|
null_label='-- None --',
|
|
|
|
widget=APISelectMultiple(
|
|
|
|
api_url="/api/ipam/roles/",
|
|
|
|
value_field="slug",
|
|
|
|
null_option=True,
|
|
|
|
)
|
2017-03-01 13:09:19 -05:00
|
|
|
)
|
2019-01-17 16:02:16 -05:00
|
|
|
is_pool = forms.NullBooleanField(
|
|
|
|
required=False,
|
|
|
|
label='Is a pool',
|
|
|
|
widget=StaticSelect2(
|
|
|
|
choices=BOOLEAN_WITH_BLANK_CHOICES
|
|
|
|
)
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
expand = forms.BooleanField(
|
|
|
|
required=False,
|
|
|
|
label='Expand prefix hierarchy'
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# IP addresses
|
|
|
|
#
|
|
|
|
|
2017-05-11 17:35:20 -04:00
|
|
|
class IPAddressForm(BootstrapMixin, TenancyForm, ReturnURLForm, CustomFieldForm):
|
2017-08-29 15:26:35 -04:00
|
|
|
interface = forms.ModelChoiceField(
|
2017-05-11 16:24:57 -04:00
|
|
|
queryset=Interface.objects.all(),
|
2017-08-29 15:26:35 -04:00
|
|
|
required=False
|
2017-04-13 14:54:17 -04:00
|
|
|
)
|
|
|
|
nat_site = forms.ModelChoiceField(
|
2017-05-11 16:24:57 -04:00
|
|
|
queryset=Site.objects.all(),
|
|
|
|
required=False,
|
|
|
|
label='Site',
|
2019-01-10 21:19:13 -05:00
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/dcim/sites/",
|
|
|
|
filter_for={
|
|
|
|
'nat_rack': 'site_id',
|
|
|
|
'nat_device': 'site_id'
|
2018-11-27 11:57:29 -05:00
|
|
|
}
|
2017-04-13 14:54:17 -04:00
|
|
|
)
|
|
|
|
)
|
2017-05-11 16:24:57 -04:00
|
|
|
nat_rack = ChainedModelChoiceField(
|
|
|
|
queryset=Rack.objects.all(),
|
2017-05-25 14:33:50 -04:00
|
|
|
chains=(
|
|
|
|
('site', 'nat_site'),
|
|
|
|
),
|
2017-05-11 16:24:57 -04:00
|
|
|
required=False,
|
|
|
|
label='Rack',
|
|
|
|
widget=APISelect(
|
2019-01-10 21:19:13 -05:00
|
|
|
api_url='/api/dcim/racks/',
|
2017-05-11 16:24:57 -04:00
|
|
|
display_field='display_name',
|
2019-01-10 21:19:13 -05:00
|
|
|
filter_for={
|
|
|
|
'nat_device': 'rack_id'
|
|
|
|
},
|
2018-11-27 11:57:29 -05:00
|
|
|
attrs={
|
|
|
|
'nullable': 'true'
|
|
|
|
}
|
2017-05-11 16:24:57 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
nat_device = ChainedModelChoiceField(
|
|
|
|
queryset=Device.objects.all(),
|
2017-05-25 14:33:50 -04:00
|
|
|
chains=(
|
|
|
|
('site', 'nat_site'),
|
|
|
|
('rack', 'nat_rack'),
|
|
|
|
),
|
2017-05-11 16:24:57 -04:00
|
|
|
required=False,
|
|
|
|
label='Device',
|
|
|
|
widget=APISelect(
|
2019-01-10 21:19:13 -05:00
|
|
|
api_url='/api/dcim/devices/',
|
2017-05-11 16:24:57 -04:00
|
|
|
display_field='display_name',
|
2019-01-10 21:19:13 -05:00
|
|
|
filter_for={
|
|
|
|
'nat_inside': 'device_id'
|
|
|
|
}
|
2017-04-13 14:54:17 -04:00
|
|
|
)
|
|
|
|
)
|
2017-05-11 16:24:57 -04:00
|
|
|
nat_inside = ChainedModelChoiceField(
|
|
|
|
queryset=IPAddress.objects.all(),
|
2017-05-25 14:33:50 -04:00
|
|
|
chains=(
|
|
|
|
('interface__device', 'nat_device'),
|
|
|
|
),
|
2017-05-11 16:24:57 -04:00
|
|
|
required=False,
|
|
|
|
label='IP Address',
|
|
|
|
widget=APISelect(
|
2019-01-10 21:19:13 -05:00
|
|
|
api_url='/api/ipam/ip-addresses/',
|
2017-05-11 16:24:57 -04:00
|
|
|
display_field='address'
|
|
|
|
)
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
primary_for_parent = forms.BooleanField(
|
|
|
|
required=False,
|
|
|
|
label='Make this the primary IP for the device/VM'
|
|
|
|
)
|
|
|
|
tags = TagField(
|
|
|
|
required=False
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = IPAddress
|
2017-05-11 17:35:20 -04:00
|
|
|
fields = [
|
2019-04-22 18:10:28 -04:00
|
|
|
'address', 'vrf', 'status', 'role', 'dns_name', 'description', 'interface', 'primary_for_parent',
|
|
|
|
'nat_site', 'nat_rack', 'nat_inside', 'tenant_group', 'tenant', 'tags',
|
2017-05-11 17:35:20 -04:00
|
|
|
]
|
2019-01-10 21:19:13 -05:00
|
|
|
widgets = {
|
|
|
|
'status': StaticSelect2(),
|
|
|
|
'role': StaticSelect2(),
|
|
|
|
'vrf': APISelect(
|
|
|
|
api_url="/api/ipam/vrfs/"
|
|
|
|
)
|
|
|
|
}
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2017-05-11 17:52:23 -04:00
|
|
|
def __init__(self, *args, **kwargs):
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2017-05-11 17:52:23 -04:00
|
|
|
# Initialize helper selectors
|
|
|
|
instance = kwargs.get('instance')
|
2017-07-11 14:52:50 -04:00
|
|
|
initial = kwargs.get('initial', {}).copy()
|
2017-06-13 16:57:25 -04:00
|
|
|
if instance and instance.nat_inside and instance.nat_inside.device is not None:
|
2017-05-11 16:24:57 -04:00
|
|
|
initial['nat_site'] = instance.nat_inside.device.site
|
|
|
|
initial['nat_rack'] = instance.nat_inside.device.rack
|
|
|
|
initial['nat_device'] = instance.nat_inside.device
|
2017-05-11 17:52:23 -04:00
|
|
|
kwargs['initial'] = initial
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2018-11-27 10:52:24 -05:00
|
|
|
super().__init__(*args, **kwargs)
|
2017-04-13 14:54:17 -04:00
|
|
|
|
2017-05-11 16:24:57 -04:00
|
|
|
self.fields['vrf'].empty_label = 'Global'
|
2017-04-13 14:54:17 -04:00
|
|
|
|
2017-08-29 15:26:35 -04:00
|
|
|
# Limit interface selections to those belonging to the parent device/VM
|
|
|
|
if self.instance and self.instance.interface:
|
|
|
|
self.fields['interface'].queryset = Interface.objects.filter(
|
|
|
|
device=self.instance.interface.device, virtual_machine=self.instance.interface.virtual_machine
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.fields['interface'].choices = []
|
|
|
|
|
|
|
|
# Initialize primary_for_parent if IP address is already assigned
|
|
|
|
if self.instance.pk and self.instance.interface is not None:
|
|
|
|
parent = self.instance.interface.parent
|
2017-05-02 16:46:23 -04:00
|
|
|
if (
|
2017-08-29 15:26:35 -04:00
|
|
|
self.instance.address.version == 4 and parent.primary_ip4_id == self.instance.pk or
|
|
|
|
self.instance.address.version == 6 and parent.primary_ip6_id == self.instance.pk
|
2017-05-02 16:46:23 -04:00
|
|
|
):
|
2017-08-29 15:26:35 -04:00
|
|
|
self.initial['primary_for_parent'] = True
|
2017-05-02 16:46:23 -04:00
|
|
|
|
|
|
|
def clean(self):
|
2018-11-27 10:52:24 -05:00
|
|
|
super().clean()
|
2017-05-02 16:46:23 -04:00
|
|
|
|
|
|
|
# Primary IP assignment is only available if an interface has been assigned.
|
2017-08-29 15:26:35 -04:00
|
|
|
if self.cleaned_data.get('primary_for_parent') and not self.cleaned_data.get('interface'):
|
2017-05-02 16:46:23 -04:00
|
|
|
self.add_error(
|
2017-08-29 15:26:35 -04:00
|
|
|
'primary_for_parent', "Only IP addresses assigned to an interface can be designated as primary IPs."
|
2017-05-02 16:46:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
|
2018-11-27 10:52:24 -05:00
|
|
|
ipaddress = super().save(*args, **kwargs)
|
2017-05-02 16:46:23 -04:00
|
|
|
|
2018-04-12 13:03:20 -04:00
|
|
|
# Assign/clear this IPAddress as the primary for the associated Device/VirtualMachine.
|
2017-08-29 15:26:35 -04:00
|
|
|
if self.cleaned_data['primary_for_parent']:
|
|
|
|
parent = self.cleaned_data['interface'].parent
|
2017-05-02 16:46:23 -04:00
|
|
|
if ipaddress.address.version == 4:
|
2017-08-29 15:26:35 -04:00
|
|
|
parent.primary_ip4 = ipaddress
|
2017-05-02 16:46:23 -04:00
|
|
|
else:
|
2017-08-29 15:26:35 -04:00
|
|
|
parent.primary_ip6 = ipaddress
|
|
|
|
parent.save()
|
2018-02-26 14:13:34 -05:00
|
|
|
elif self.cleaned_data['interface']:
|
|
|
|
parent = self.cleaned_data['interface'].parent
|
2018-04-12 13:03:20 -04:00
|
|
|
if ipaddress.address.version == 4 and parent.primary_ip4 == ipaddress:
|
2018-02-26 14:13:34 -05:00
|
|
|
parent.primary_ip4 = None
|
|
|
|
parent.save()
|
2018-04-12 13:03:20 -04:00
|
|
|
elif ipaddress.address.version == 6 and parent.primary_ip6 == ipaddress:
|
2018-02-26 14:13:34 -05:00
|
|
|
parent.primary_ip6 = None
|
|
|
|
parent.save()
|
2017-05-02 16:46:23 -04:00
|
|
|
|
|
|
|
return ipaddress
|
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2017-09-12 13:54:44 -04:00
|
|
|
class IPAddressBulkCreateForm(BootstrapMixin, forms.Form):
|
2018-11-27 11:57:29 -05:00
|
|
|
pattern = ExpandableIPAddressField(
|
|
|
|
label='Address pattern'
|
|
|
|
)
|
2017-05-12 12:00:26 -04:00
|
|
|
|
2017-04-19 14:50:58 -04:00
|
|
|
|
2017-05-12 12:04:06 -04:00
|
|
|
class IPAddressBulkAddForm(BootstrapMixin, TenancyForm, CustomFieldForm):
|
2017-04-19 14:50:58 -04:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = IPAddress
|
2018-11-27 11:57:29 -05:00
|
|
|
fields = [
|
2019-04-22 18:10:28 -04:00
|
|
|
'address', 'vrf', 'status', 'role', 'dns_name', 'description', 'tenant_group', 'tenant',
|
2018-11-27 11:57:29 -05:00
|
|
|
]
|
2019-01-10 21:19:13 -05:00
|
|
|
widgets = {
|
|
|
|
'status': StaticSelect2(),
|
|
|
|
'role': StaticSelect2(),
|
|
|
|
'vrf': APISelect(
|
|
|
|
api_url="/api/ipam/vrfs/"
|
|
|
|
)
|
|
|
|
}
|
2017-05-12 12:00:26 -04:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2018-11-27 10:52:24 -05:00
|
|
|
super().__init__(*args, **kwargs)
|
2017-05-12 12:00:26 -04:00
|
|
|
self.fields['vrf'].empty_label = 'Global'
|
2016-12-20 15:39:22 -05:00
|
|
|
|
|
|
|
|
2017-06-02 16:07:11 -04:00
|
|
|
class IPAddressCSVForm(forms.ModelForm):
|
2019-03-11 12:34:19 -04:00
|
|
|
vrf = FlexibleModelChoiceField(
|
2017-06-02 16:07:11 -04:00
|
|
|
queryset=VRF.objects.all(),
|
|
|
|
to_field_name='rd',
|
2019-03-11 12:34:19 -04:00
|
|
|
required=False,
|
|
|
|
help_text='Route distinguisher of parent VRF (or {ID})',
|
2017-06-02 16:07:11 -04:00
|
|
|
error_messages={
|
|
|
|
'invalid_choice': 'VRF not found.',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
tenant = forms.ModelChoiceField(
|
|
|
|
queryset=Tenant.objects.all(),
|
|
|
|
to_field_name='name',
|
|
|
|
required=False,
|
|
|
|
help_text='Name of the assigned tenant',
|
|
|
|
error_messages={
|
|
|
|
'invalid_choice': 'Tenant not found.',
|
|
|
|
}
|
|
|
|
)
|
2017-06-07 13:22:06 -04:00
|
|
|
status = CSVChoiceField(
|
2017-06-14 16:22:49 -04:00
|
|
|
choices=IPADDRESS_STATUS_CHOICES,
|
2017-06-07 15:51:11 -04:00
|
|
|
help_text='Operational status'
|
2017-06-02 16:07:11 -04:00
|
|
|
)
|
2017-06-14 16:11:13 -04:00
|
|
|
role = CSVChoiceField(
|
|
|
|
choices=IPADDRESS_ROLE_CHOICES,
|
|
|
|
required=False,
|
|
|
|
help_text='Functional role'
|
|
|
|
)
|
2017-06-07 15:51:11 -04:00
|
|
|
device = FlexibleModelChoiceField(
|
2017-06-02 16:07:11 -04:00
|
|
|
queryset=Device.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2017-06-07 15:51:11 -04:00
|
|
|
help_text='Name or ID of assigned device',
|
2017-06-02 16:07:11 -04:00
|
|
|
error_messages={
|
|
|
|
'invalid_choice': 'Device not found.',
|
|
|
|
}
|
|
|
|
)
|
2017-09-18 13:30:20 -04:00
|
|
|
virtual_machine = forms.ModelChoiceField(
|
|
|
|
queryset=VirtualMachine.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
|
|
|
help_text='Name of assigned virtual machine',
|
|
|
|
error_messages={
|
|
|
|
'invalid_choice': 'Virtual machine not found.',
|
|
|
|
}
|
|
|
|
)
|
2017-06-02 16:07:11 -04:00
|
|
|
interface_name = forms.CharField(
|
|
|
|
help_text='Name of assigned interface',
|
|
|
|
required=False
|
|
|
|
)
|
|
|
|
is_primary = forms.BooleanField(
|
2017-06-07 15:51:11 -04:00
|
|
|
help_text='Make this the primary IP for the assigned device',
|
2017-06-02 16:07:11 -04:00
|
|
|
required=False
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = IPAddress
|
2018-02-02 14:26:16 -05:00
|
|
|
fields = IPAddress.csv_headers
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
def clean(self):
|
2018-11-27 10:52:24 -05:00
|
|
|
super().clean()
|
2017-06-07 14:19:08 -04:00
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
device = self.cleaned_data.get('device')
|
2017-09-18 13:30:20 -04:00
|
|
|
virtual_machine = self.cleaned_data.get('virtual_machine')
|
2016-03-01 11:23:03 -05:00
|
|
|
interface_name = self.cleaned_data.get('interface_name')
|
|
|
|
is_primary = self.cleaned_data.get('is_primary')
|
|
|
|
|
|
|
|
# Validate interface
|
2017-09-18 13:30:20 -04:00
|
|
|
if interface_name and device:
|
2016-03-01 11:23:03 -05:00
|
|
|
try:
|
2017-06-07 14:19:08 -04:00
|
|
|
self.instance.interface = Interface.objects.get(device=device, name=interface_name)
|
2016-03-01 11:23:03 -05:00
|
|
|
except Interface.DoesNotExist:
|
2017-09-18 13:30:20 -04:00
|
|
|
raise forms.ValidationError("Invalid interface {} for device {}".format(
|
|
|
|
interface_name, device
|
|
|
|
))
|
|
|
|
elif interface_name and virtual_machine:
|
|
|
|
try:
|
|
|
|
self.instance.interface = Interface.objects.get(virtual_machine=virtual_machine, name=interface_name)
|
|
|
|
except Interface.DoesNotExist:
|
|
|
|
raise forms.ValidationError("Invalid interface {} for virtual machine {}".format(
|
|
|
|
interface_name, virtual_machine
|
|
|
|
))
|
|
|
|
elif interface_name:
|
|
|
|
raise forms.ValidationError("Interface given ({}) but parent device/virtual machine not specified".format(
|
|
|
|
interface_name
|
|
|
|
))
|
|
|
|
elif device:
|
|
|
|
raise forms.ValidationError("Device specified ({}) but interface missing".format(device))
|
|
|
|
elif virtual_machine:
|
|
|
|
raise forms.ValidationError("Virtual machine specified ({}) but interface missing".format(virtual_machine))
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
# Validate is_primary
|
2017-09-18 13:30:20 -04:00
|
|
|
if is_primary and not device and not virtual_machine:
|
|
|
|
raise forms.ValidationError("No device or virtual machine specified; cannot set as primary IP")
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2017-05-17 17:16:02 -04:00
|
|
|
def save(self, *args, **kwargs):
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
# Set interface
|
|
|
|
if self.cleaned_data['device'] and self.cleaned_data['interface_name']:
|
2017-06-16 12:45:42 -04:00
|
|
|
self.instance.interface = Interface.objects.get(
|
|
|
|
device=self.cleaned_data['device'],
|
|
|
|
name=self.cleaned_data['interface_name']
|
|
|
|
)
|
2017-09-18 13:30:20 -04:00
|
|
|
elif self.cleaned_data['virtual_machine'] and self.cleaned_data['interface_name']:
|
|
|
|
self.instance.interface = Interface.objects.get(
|
|
|
|
virtual_machine=self.cleaned_data['virtual_machine'],
|
|
|
|
name=self.cleaned_data['interface_name']
|
|
|
|
)
|
2017-06-16 12:45:42 -04:00
|
|
|
|
2018-11-27 10:52:24 -05:00
|
|
|
ipaddress = super().save(*args, **kwargs)
|
2017-06-16 12:45:42 -04:00
|
|
|
|
2017-09-18 13:30:20 -04:00
|
|
|
# Set as primary for device/VM
|
2016-03-01 11:23:03 -05:00
|
|
|
if self.cleaned_data['is_primary']:
|
2017-09-18 13:30:20 -04:00
|
|
|
parent = self.cleaned_data['device'] or self.cleaned_data['virtual_machine']
|
2016-07-15 15:14:49 -04:00
|
|
|
if self.instance.address.version == 4:
|
2017-09-18 13:30:20 -04:00
|
|
|
parent.primary_ip4 = ipaddress
|
2016-07-15 15:14:49 -04:00
|
|
|
elif self.instance.address.version == 6:
|
2017-09-18 13:30:20 -04:00
|
|
|
parent.primary_ip6 = ipaddress
|
|
|
|
parent.save()
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2017-06-16 12:45:42 -04:00
|
|
|
return ipaddress
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
2018-07-10 10:00:21 -04:00
|
|
|
class IPAddressBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
|
2018-11-27 11:57:29 -05:00
|
|
|
pk = forms.ModelMultipleChoiceField(
|
|
|
|
queryset=IPAddress.objects.all(),
|
|
|
|
widget=forms.MultipleHiddenInput()
|
|
|
|
)
|
|
|
|
vrf = forms.ModelChoiceField(
|
|
|
|
queryset=VRF.objects.all(),
|
|
|
|
required=False,
|
2019-01-10 21:19:13 -05:00
|
|
|
label='VRF',
|
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/ipam/vrfs/"
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
2019-01-03 16:21:21 -05:00
|
|
|
mask_length = forms.IntegerField(
|
|
|
|
min_value=1,
|
|
|
|
max_value=128,
|
|
|
|
required=False
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
tenant = forms.ModelChoiceField(
|
|
|
|
queryset=Tenant.objects.all(),
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/tenancy/tenants/"
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
status = forms.ChoiceField(
|
|
|
|
choices=add_blank_choice(IPADDRESS_STATUS_CHOICES),
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=StaticSelect2()
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
role = forms.ChoiceField(
|
|
|
|
choices=add_blank_choice(IPADDRESS_ROLE_CHOICES),
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=StaticSelect2()
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
2019-04-22 18:10:28 -04:00
|
|
|
dns_name = forms.CharField(
|
|
|
|
max_length=255,
|
|
|
|
required=False
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
description = forms.CharField(
|
2019-04-22 18:10:28 -04:00
|
|
|
max_length=100,
|
|
|
|
required=False
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2016-09-30 16:17:41 -04:00
|
|
|
class Meta:
|
2018-11-27 11:57:29 -05:00
|
|
|
nullable_fields = [
|
2019-04-22 18:10:28 -04:00
|
|
|
'vrf', 'role', 'tenant', 'dns_name', 'description',
|
2018-11-27 11:57:29 -05:00
|
|
|
]
|
2016-09-30 16:17:41 -04:00
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2017-11-10 11:58:59 -05:00
|
|
|
class IPAddressAssignForm(BootstrapMixin, forms.Form):
|
2018-11-27 11:57:29 -05:00
|
|
|
vrf = forms.ModelChoiceField(
|
|
|
|
queryset=VRF.objects.all(),
|
|
|
|
required=False,
|
|
|
|
label='VRF',
|
2019-01-10 21:19:13 -05:00
|
|
|
empty_label='Global',
|
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/ipam/vrfs/"
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
address = forms.CharField(
|
|
|
|
label='IP Address'
|
|
|
|
)
|
2017-11-10 11:58:59 -05:00
|
|
|
|
|
|
|
|
2019-03-05 08:10:10 -06:00
|
|
|
class IPAddressFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm):
|
2016-08-23 12:05:28 -04:00
|
|
|
model = IPAddress
|
2019-05-09 14:32:49 -04:00
|
|
|
field_order = [
|
|
|
|
'q', 'parent', 'family', 'mask_length', 'vrf_id', 'status', 'role', 'tenant_group', 'tenant',
|
|
|
|
]
|
2018-11-27 11:57:29 -05:00
|
|
|
q = forms.CharField(
|
|
|
|
required=False,
|
|
|
|
label='Search'
|
|
|
|
)
|
|
|
|
parent = forms.CharField(
|
|
|
|
required=False,
|
|
|
|
widget=forms.TextInput(
|
|
|
|
attrs={
|
|
|
|
'placeholder': 'Prefix',
|
|
|
|
}
|
|
|
|
),
|
|
|
|
label='Parent Prefix'
|
|
|
|
)
|
|
|
|
family = forms.ChoiceField(
|
|
|
|
required=False,
|
|
|
|
choices=IP_FAMILY_CHOICES,
|
2019-01-10 21:19:13 -05:00
|
|
|
label='Address family',
|
|
|
|
widget=StaticSelect2()
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
mask_length = forms.ChoiceField(
|
|
|
|
required=False,
|
|
|
|
choices=IPADDRESS_MASK_LENGTH_CHOICES,
|
2019-01-10 21:19:13 -05:00
|
|
|
label='Mask length',
|
|
|
|
widget=StaticSelect2()
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
2019-03-28 10:06:25 -04:00
|
|
|
vrf_id = FilterChoiceField(
|
2019-01-10 21:19:13 -05:00
|
|
|
queryset=VRF.objects.all(),
|
2017-03-01 13:09:19 -05:00
|
|
|
label='VRF',
|
2019-01-10 21:19:13 -05:00
|
|
|
null_label='-- Global --',
|
|
|
|
widget=APISelectMultiple(
|
|
|
|
api_url="/api/ipam/vrfs/",
|
|
|
|
null_option=True,
|
|
|
|
)
|
2017-03-01 13:09:19 -05:00
|
|
|
)
|
2019-01-10 21:19:13 -05:00
|
|
|
status = forms.MultipleChoiceField(
|
2018-03-07 14:16:38 -05:00
|
|
|
choices=IPADDRESS_STATUS_CHOICES,
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=StaticSelect2Multiple()
|
2018-03-07 14:16:38 -05:00
|
|
|
)
|
2019-01-10 21:19:13 -05:00
|
|
|
role = forms.MultipleChoiceField(
|
2018-03-07 14:16:38 -05:00
|
|
|
choices=IPADDRESS_ROLE_CHOICES,
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
2019-01-15 15:47:47 -05:00
|
|
|
widget=StaticSelect2Multiple()
|
2018-03-07 14:16:38 -05:00
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
2016-07-15 13:26:54 -04:00
|
|
|
#
|
|
|
|
# VLAN groups
|
|
|
|
#
|
|
|
|
|
2016-12-21 14:15:18 -05:00
|
|
|
class VLANGroupForm(BootstrapMixin, forms.ModelForm):
|
2016-07-15 13:26:54 -04:00
|
|
|
slug = SlugField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = VLANGroup
|
2018-11-27 11:57:29 -05:00
|
|
|
fields = [
|
|
|
|
'site', 'name', 'slug',
|
|
|
|
]
|
2019-01-10 21:19:13 -05:00
|
|
|
widgets = {
|
|
|
|
'site': APISelect(
|
|
|
|
api_url="/api/dcim/sites/"
|
|
|
|
)
|
|
|
|
}
|
2016-07-15 13:26:54 -04:00
|
|
|
|
|
|
|
|
2017-10-09 15:42:06 -04:00
|
|
|
class VLANGroupCSVForm(forms.ModelForm):
|
|
|
|
site = forms.ModelChoiceField(
|
|
|
|
queryset=Site.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
|
|
|
help_text='Name of parent site',
|
|
|
|
error_messages={
|
|
|
|
'invalid_choice': 'Site not found.',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
slug = SlugField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = VLANGroup
|
2018-02-02 14:26:16 -05:00
|
|
|
fields = VLANGroup.csv_headers
|
2017-10-09 15:42:06 -04:00
|
|
|
help_texts = {
|
|
|
|
'name': 'Name of VLAN group',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-21 14:15:18 -05:00
|
|
|
class VLANGroupFilterForm(BootstrapMixin, forms.Form):
|
2017-02-21 14:53:22 -05:00
|
|
|
site = FilterChoiceField(
|
2019-01-10 21:19:13 -05:00
|
|
|
queryset=Site.objects.all(),
|
2017-02-21 14:53:22 -05:00
|
|
|
to_field_name='slug',
|
2019-01-10 21:19:13 -05:00
|
|
|
null_label='-- Global --',
|
|
|
|
widget=APISelectMultiple(
|
|
|
|
api_url="/api/dcim/sites/",
|
|
|
|
value_field="slug",
|
|
|
|
null_option=True,
|
|
|
|
)
|
2017-02-21 14:53:22 -05:00
|
|
|
)
|
2016-07-15 13:26:54 -04:00
|
|
|
|
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
#
|
|
|
|
# VLANs
|
|
|
|
#
|
|
|
|
|
2017-05-11 17:35:20 -04:00
|
|
|
class VLANForm(BootstrapMixin, TenancyForm, CustomFieldForm):
|
2017-05-11 16:24:57 -04:00
|
|
|
site = forms.ModelChoiceField(
|
|
|
|
queryset=Site.objects.all(),
|
2017-05-16 16:30:28 -04:00
|
|
|
required=False,
|
2019-01-10 21:19:13 -05:00
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/dcim/sites/",
|
|
|
|
filter_for={
|
|
|
|
'group': 'site_id'
|
|
|
|
},
|
2018-11-27 11:57:29 -05:00
|
|
|
attrs={
|
|
|
|
'nullable': 'true',
|
|
|
|
}
|
2017-05-11 16:24:57 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
group = ChainedModelChoiceField(
|
|
|
|
queryset=VLANGroup.objects.all(),
|
2017-05-25 14:33:50 -04:00
|
|
|
chains=(
|
|
|
|
('site', 'site'),
|
|
|
|
),
|
2017-05-11 16:24:57 -04:00
|
|
|
required=False,
|
|
|
|
label='Group',
|
|
|
|
widget=APISelect(
|
2019-01-10 21:19:13 -05:00
|
|
|
api_url='/api/ipam/vlan-groups/',
|
2017-05-11 16:24:57 -04:00
|
|
|
)
|
|
|
|
)
|
2018-05-10 12:53:11 -04:00
|
|
|
tags = TagField(required=False)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = VLAN
|
2018-11-27 11:57:29 -05:00
|
|
|
fields = [
|
|
|
|
'site', 'group', 'vid', 'name', 'status', 'role', 'description', 'tenant_group', 'tenant', 'tags',
|
|
|
|
]
|
2016-03-01 11:23:03 -05:00
|
|
|
help_texts = {
|
2017-02-21 14:53:22 -05:00
|
|
|
'site': "Leave blank if this VLAN spans multiple sites",
|
2016-07-15 13:26:54 -04:00
|
|
|
'group': "VLAN group (optional)",
|
2016-03-01 11:23:03 -05:00
|
|
|
'vid': "Configured VLAN ID",
|
|
|
|
'name': "Configured VLAN name",
|
|
|
|
'status': "Operational status of this VLAN",
|
|
|
|
'role': "The primary function of this VLAN",
|
|
|
|
}
|
2019-01-10 21:19:13 -05:00
|
|
|
widgets = {
|
|
|
|
'status': StaticSelect2(),
|
|
|
|
'role': APISelect(
|
|
|
|
api_url="/api/ipam/roles/"
|
|
|
|
)
|
|
|
|
}
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
2017-06-02 16:07:11 -04:00
|
|
|
class VLANCSVForm(forms.ModelForm):
|
2017-04-05 10:13:19 -04:00
|
|
|
site = forms.ModelChoiceField(
|
2017-06-02 16:07:11 -04:00
|
|
|
queryset=Site.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
|
|
|
help_text='Name of parent site',
|
|
|
|
error_messages={
|
|
|
|
'invalid_choice': 'Site not found.',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
group_name = forms.CharField(
|
2017-06-07 14:19:08 -04:00
|
|
|
help_text='Name of VLAN group',
|
2017-06-02 16:07:11 -04:00
|
|
|
required=False
|
2017-04-05 10:13:19 -04:00
|
|
|
)
|
|
|
|
tenant = forms.ModelChoiceField(
|
2017-06-02 16:07:11 -04:00
|
|
|
queryset=Tenant.objects.all(),
|
|
|
|
to_field_name='name',
|
|
|
|
required=False,
|
|
|
|
help_text='Name of assigned tenant',
|
|
|
|
error_messages={
|
|
|
|
'invalid_choice': 'Tenant not found.',
|
|
|
|
}
|
|
|
|
)
|
2017-06-07 13:22:06 -04:00
|
|
|
status = CSVChoiceField(
|
|
|
|
choices=VLAN_STATUS_CHOICES,
|
2017-06-07 15:51:11 -04:00
|
|
|
help_text='Operational status'
|
2017-04-05 10:13:19 -04:00
|
|
|
)
|
|
|
|
role = forms.ModelChoiceField(
|
2017-06-02 16:07:11 -04:00
|
|
|
queryset=Role.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2017-06-07 15:51:11 -04:00
|
|
|
help_text='Functional role',
|
2017-06-02 16:07:11 -04:00
|
|
|
error_messages={
|
|
|
|
'invalid_choice': 'Invalid role.',
|
|
|
|
}
|
2017-04-05 10:13:19 -04:00
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = VLAN
|
2018-02-02 14:26:16 -05:00
|
|
|
fields = VLAN.csv_headers
|
2017-06-07 15:51:11 -04:00
|
|
|
help_texts = {
|
|
|
|
'vid': 'Numeric VLAN ID (1-4095)',
|
|
|
|
'name': 'VLAN name',
|
|
|
|
}
|
2017-04-05 10:13:19 -04:00
|
|
|
|
|
|
|
def clean(self):
|
2018-11-27 10:52:24 -05:00
|
|
|
super().clean()
|
2017-06-07 14:19:08 -04:00
|
|
|
|
|
|
|
site = self.cleaned_data.get('site')
|
2017-04-05 10:13:19 -04:00
|
|
|
group_name = self.cleaned_data.get('group_name')
|
2017-06-07 14:19:08 -04:00
|
|
|
|
|
|
|
# Validate VLAN group
|
2017-04-05 10:13:19 -04:00
|
|
|
if group_name:
|
|
|
|
try:
|
2017-06-07 14:19:08 -04:00
|
|
|
self.instance.group = VLANGroup.objects.get(site=site, name=group_name)
|
2017-04-05 10:13:19 -04:00
|
|
|
except VLANGroup.DoesNotExist:
|
2017-06-07 14:19:08 -04:00
|
|
|
if site:
|
2018-11-27 11:57:29 -05:00
|
|
|
raise forms.ValidationError(
|
|
|
|
"VLAN group {} not found for site {}".format(group_name, site)
|
|
|
|
)
|
2017-06-07 14:19:08 -04:00
|
|
|
else:
|
2018-11-27 11:57:29 -05:00
|
|
|
raise forms.ValidationError(
|
|
|
|
"Global VLAN group {} not found".format(group_name)
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
2018-07-10 10:00:21 -04:00
|
|
|
class VLANBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
|
2018-11-27 11:57:29 -05:00
|
|
|
pk = forms.ModelMultipleChoiceField(
|
|
|
|
queryset=VLAN.objects.all(),
|
|
|
|
widget=forms.MultipleHiddenInput()
|
|
|
|
)
|
|
|
|
site = forms.ModelChoiceField(
|
|
|
|
queryset=Site.objects.all(),
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/dcim/sites/"
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
group = forms.ModelChoiceField(
|
|
|
|
queryset=VLANGroup.objects.all(),
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/ipam/vlan-groups/"
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
tenant = forms.ModelChoiceField(
|
|
|
|
queryset=Tenant.objects.all(),
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/tenancy/tenants/"
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
status = forms.ChoiceField(
|
|
|
|
choices=add_blank_choice(VLAN_STATUS_CHOICES),
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=StaticSelect2()
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
role = forms.ModelChoiceField(
|
|
|
|
queryset=Role.objects.all(),
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/ipam/roles/"
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
description = forms.CharField(
|
|
|
|
max_length=100,
|
|
|
|
required=False
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2016-09-30 16:17:41 -04:00
|
|
|
class Meta:
|
2018-11-27 11:57:29 -05:00
|
|
|
nullable_fields = [
|
|
|
|
'site', 'group', 'tenant', 'role', 'description',
|
|
|
|
]
|
2016-09-30 16:17:41 -04:00
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2019-03-05 08:10:10 -06:00
|
|
|
class VLANFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm):
|
2016-08-23 12:05:28 -04:00
|
|
|
model = VLAN
|
2019-05-09 14:32:49 -04:00
|
|
|
field_order = ['q', 'site', 'group_id', 'status', 'role', 'tenant_group', 'tenant']
|
2018-11-27 11:57:29 -05:00
|
|
|
q = forms.CharField(
|
|
|
|
required=False,
|
|
|
|
label='Search'
|
|
|
|
)
|
2017-03-01 13:09:19 -05:00
|
|
|
site = FilterChoiceField(
|
2019-01-10 21:19:13 -05:00
|
|
|
queryset=Site.objects.all(),
|
2017-03-01 13:09:19 -05:00
|
|
|
to_field_name='slug',
|
2019-01-10 21:19:13 -05:00
|
|
|
null_label='-- Global --',
|
|
|
|
widget=APISelectMultiple(
|
|
|
|
api_url="/api/dcim/sites/",
|
|
|
|
value_field="slug",
|
|
|
|
null_option=True,
|
|
|
|
)
|
2017-03-01 13:09:19 -05:00
|
|
|
)
|
|
|
|
group_id = FilterChoiceField(
|
2019-01-10 21:19:13 -05:00
|
|
|
queryset=VLANGroup.objects.all(),
|
2017-03-01 13:09:19 -05:00
|
|
|
label='VLAN group',
|
2019-01-10 21:19:13 -05:00
|
|
|
null_label='-- None --',
|
|
|
|
widget=APISelectMultiple(
|
|
|
|
api_url="/api/ipam/vlan-groups/",
|
|
|
|
null_option=True,
|
|
|
|
)
|
2017-03-01 13:09:19 -05:00
|
|
|
)
|
2019-01-10 21:19:13 -05:00
|
|
|
status = forms.MultipleChoiceField(
|
2018-03-07 14:16:38 -05:00
|
|
|
choices=VLAN_STATUS_CHOICES,
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=StaticSelect2Multiple()
|
2018-03-07 14:16:38 -05:00
|
|
|
)
|
2017-03-01 13:09:19 -05:00
|
|
|
role = FilterChoiceField(
|
2019-01-10 21:19:13 -05:00
|
|
|
queryset=Role.objects.all(),
|
2017-03-01 13:09:19 -05:00
|
|
|
to_field_name='slug',
|
2019-01-10 21:19:13 -05:00
|
|
|
null_label='-- None --',
|
|
|
|
widget=APISelectMultiple(
|
|
|
|
api_url="/api/ipam/roles/",
|
|
|
|
value_field="slug",
|
|
|
|
null_option=True,
|
|
|
|
)
|
2017-03-01 13:09:19 -05:00
|
|
|
)
|
2016-12-15 15:32:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Services
|
|
|
|
#
|
|
|
|
|
2018-06-21 16:17:18 -04:00
|
|
|
class ServiceForm(BootstrapMixin, CustomFieldForm):
|
2018-11-27 11:57:29 -05:00
|
|
|
tags = TagField(
|
|
|
|
required=False
|
|
|
|
)
|
2016-12-15 15:32:58 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Service
|
2018-11-27 11:57:29 -05:00
|
|
|
fields = [
|
|
|
|
'name', 'protocol', 'port', 'ipaddresses', 'description', 'tags',
|
|
|
|
]
|
2016-12-15 15:32:58 -05:00
|
|
|
help_texts = {
|
|
|
|
'ipaddresses': "IP address assignment is optional. If no IPs are selected, the service is assumed to be "
|
|
|
|
"reachable via all IPs assigned to the device.",
|
|
|
|
}
|
2019-01-10 21:19:13 -05:00
|
|
|
widgets = {
|
|
|
|
'protocol': StaticSelect2(),
|
|
|
|
'ipaddresses': StaticSelect2Multiple(),
|
|
|
|
}
|
2016-12-15 15:32:58 -05:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2018-11-27 10:52:24 -05:00
|
|
|
super().__init__(*args, **kwargs)
|
2016-12-15 15:32:58 -05:00
|
|
|
|
2017-08-31 12:50:35 -04:00
|
|
|
# Limit IP address choices to those assigned to interfaces of the parent device/VM
|
|
|
|
if self.instance.device:
|
2018-02-01 16:11:04 -05:00
|
|
|
vc_interface_ids = [i['id'] for i in self.instance.device.vc_interfaces.values('id')]
|
2017-08-31 12:50:35 -04:00
|
|
|
self.fields['ipaddresses'].queryset = IPAddress.objects.filter(
|
2018-02-01 16:11:04 -05:00
|
|
|
interface_id__in=vc_interface_ids
|
2017-08-31 12:50:35 -04:00
|
|
|
)
|
|
|
|
elif self.instance.virtual_machine:
|
|
|
|
self.fields['ipaddresses'].queryset = IPAddress.objects.filter(
|
|
|
|
interface__virtual_machine=self.instance.virtual_machine
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.fields['ipaddresses'].choices = []
|
2018-06-21 15:55:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ServiceFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|
|
|
model = Service
|
2018-06-21 16:17:18 -04:00
|
|
|
q = forms.CharField(
|
|
|
|
required=False,
|
|
|
|
label='Search'
|
|
|
|
)
|
2018-06-21 15:55:27 -04:00
|
|
|
protocol = forms.ChoiceField(
|
|
|
|
choices=add_blank_choice(IP_PROTOCOL_CHOICES),
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=StaticSelect2Multiple()
|
2018-06-21 15:55:27 -04:00
|
|
|
)
|
|
|
|
port = forms.IntegerField(
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
2018-06-21 15:55:27 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-06-21 16:17:18 -04:00
|
|
|
class ServiceBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
|
2018-11-27 11:57:29 -05:00
|
|
|
pk = forms.ModelMultipleChoiceField(
|
|
|
|
queryset=Service.objects.all(),
|
|
|
|
widget=forms.MultipleHiddenInput()
|
|
|
|
)
|
|
|
|
protocol = forms.ChoiceField(
|
|
|
|
choices=add_blank_choice(IP_PROTOCOL_CHOICES),
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=StaticSelect2()
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
port = forms.IntegerField(
|
|
|
|
validators=[
|
|
|
|
MinValueValidator(1),
|
|
|
|
MaxValueValidator(65535),
|
|
|
|
],
|
|
|
|
required=False
|
|
|
|
)
|
|
|
|
description = forms.CharField(
|
|
|
|
max_length=100,
|
|
|
|
required=False
|
|
|
|
)
|
2018-06-21 15:55:27 -04:00
|
|
|
|
|
|
|
class Meta:
|
2018-11-27 11:57:29 -05:00
|
|
|
nullable_fields = [
|
2019-01-10 21:19:13 -05:00
|
|
|
'site', 'tenant', 'role', 'description',
|
2018-11-27 11:57:29 -05:00
|
|
|
]
|