2016-03-01 11:23:03 -05:00
|
|
|
from django import forms
|
2018-06-21 15:55:27 -04:00
|
|
|
from django.core.validators import MaxValueValidator, MinValueValidator
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2020-06-23 16:09:31 -04:00
|
|
|
from dcim.models import Device, Interface, Rack, Region, Site
|
2020-01-29 13:53:26 -05:00
|
|
|
from extras.forms import (
|
|
|
|
AddRemoveTagsForm, CustomFieldBulkEditForm, CustomFieldModelCSVForm, CustomFieldModelForm, CustomFieldFilterForm,
|
|
|
|
)
|
2020-06-12 09:58:59 -04:00
|
|
|
from extras.models import Tag
|
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 (
|
2020-08-12 12:58:32 -04:00
|
|
|
add_blank_choice, BootstrapMixin, BulkEditNullBooleanSelect, CSVChoiceField, CSVModelChoiceField, CSVModelForm,
|
|
|
|
DatePicker, DynamicModelChoiceField, DynamicModelMultipleChoiceField, ExpandableIPAddressField, ReturnURLForm,
|
|
|
|
SlugField, StaticSelect2, StaticSelect2Multiple, TagFilterField, BOOLEAN_WITH_BLANK_CHOICES,
|
2016-09-14 16:27:26 -04:00
|
|
|
)
|
2020-08-13 10:42:19 -04:00
|
|
|
from virtualization.models import Cluster, VirtualMachine, VMInterface
|
2019-11-27 21:46:53 -05:00
|
|
|
from .choices import *
|
2020-05-01 13:40:52 -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
|
|
|
|
2020-01-24 15:50:45 -05:00
|
|
|
PREFIX_MASK_LENGTH_CHOICES = add_blank_choice([
|
|
|
|
(i, i) for i in range(PREFIX_LENGTH_MIN, PREFIX_LENGTH_MAX + 1)
|
|
|
|
])
|
|
|
|
|
|
|
|
IPADDRESS_MASK_LENGTH_CHOICES = add_blank_choice([
|
|
|
|
(i, i) for i in range(IPADDRESS_MASK_LENGTH_MIN, IPADDRESS_MASK_LENGTH_MAX + 1)
|
|
|
|
])
|
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
|
|
|
|
#
|
|
|
|
|
2020-01-29 10:49:02 -05:00
|
|
|
class VRFForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
|
2020-06-12 09:58:59 -04:00
|
|
|
tags = DynamicModelMultipleChoiceField(
|
|
|
|
queryset=Tag.objects.all(),
|
2018-11-27 11:57:29 -05:00
|
|
|
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",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-29 13:53:26 -05:00
|
|
|
class VRFCSVForm(CustomFieldModelCSVForm):
|
2020-05-06 09:43:10 -04:00
|
|
|
tenant = CSVModelChoiceField(
|
2017-06-02 16:07:11 -04:00
|
|
|
queryset=Tenant.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2020-05-06 09:58:12 -04:00
|
|
|
help_text='Assigned tenant'
|
2017-06-02 16:07:11 -04:00
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = VRF
|
2018-02-02 14:26:16 -05:00
|
|
|
fields = VRF.csv_headers
|
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()
|
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
tenant = DynamicModelChoiceField(
|
2018-11-27 11:57:29 -05:00
|
|
|
queryset=Tenant.objects.all(),
|
2020-03-16 14:08:48 -04:00
|
|
|
required=False
|
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'
|
|
|
|
)
|
2020-01-14 08:22:27 +00:00
|
|
|
tag = TagFilterField(model)
|
2020-01-13 20:16:13 +00:00
|
|
|
|
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 = [
|
2020-03-13 16:24:37 -04:00
|
|
|
'name', 'slug', 'is_private', 'description',
|
2018-11-27 11:57:29 -05:00
|
|
|
]
|
2016-12-06 13:59:13 -05:00
|
|
|
|
|
|
|
|
2020-05-05 16:15:09 -04:00
|
|
|
class RIRCSVForm(CSVModelForm):
|
2017-10-09 15:42:06 -04:00
|
|
|
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
|
|
|
|
#
|
|
|
|
|
2020-01-29 10:49:02 -05:00
|
|
|
class AggregateForm(BootstrapMixin, CustomFieldModelForm):
|
2020-02-11 10:43:22 -05:00
|
|
|
rir = DynamicModelChoiceField(
|
2020-03-16 14:08:48 -04:00
|
|
|
queryset=RIR.objects.all()
|
2020-02-11 10:43:22 -05:00
|
|
|
)
|
2020-06-12 09:58:59 -04:00
|
|
|
tags = DynamicModelMultipleChoiceField(
|
|
|
|
queryset=Tag.objects.all(),
|
2018-11-27 11:57:29 -05:00
|
|
|
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",
|
|
|
|
}
|
2019-01-10 21:19:13 -05:00
|
|
|
widgets = {
|
2019-12-28 22:55:00 +00:00
|
|
|
'date_added': DatePicker(),
|
2019-01-10 21:19:13 -05:00
|
|
|
}
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
2020-01-29 13:53:26 -05:00
|
|
|
class AggregateCSVForm(CustomFieldModelCSVForm):
|
2020-05-06 09:43:10 -04:00
|
|
|
rir = CSVModelChoiceField(
|
2017-06-02 16:07:11 -04:00
|
|
|
queryset=RIR.objects.all(),
|
|
|
|
to_field_name='name',
|
2020-05-06 09:58:12 -04:00
|
|
|
help_text='Assigned RIR'
|
2017-06-02 16:07:11 -04:00
|
|
|
)
|
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()
|
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
rir = DynamicModelChoiceField(
|
2018-11-27 11:57:29 -05:00
|
|
|
queryset=RIR.objects.all(),
|
|
|
|
required=False,
|
2020-03-16 14:08:48 -04:00
|
|
|
label='RIR'
|
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',
|
|
|
|
]
|
2019-12-28 22:55:00 +00:00
|
|
|
widgets = {
|
|
|
|
'date_added': DatePicker(),
|
|
|
|
}
|
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,
|
2020-01-24 15:40:03 -05:00
|
|
|
choices=add_blank_choice(IPAddressFamilyChoices),
|
2019-01-10 21:19:13 -05:00
|
|
|
label='Address family',
|
|
|
|
widget=StaticSelect2()
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
2020-02-11 09:26:39 -05:00
|
|
|
rir = DynamicModelMultipleChoiceField(
|
2019-01-10 21:19:13 -05:00
|
|
|
queryset=RIR.objects.all(),
|
2017-03-01 13:09:19 -05:00
|
|
|
to_field_name='slug',
|
2020-02-11 09:26:39 -05:00
|
|
|
required=False,
|
2020-08-11 17:19:40 -04:00
|
|
|
label='RIR'
|
2017-03-01 13:09:19 -05:00
|
|
|
)
|
2020-01-14 08:22:27 +00:00
|
|
|
tag = TagFilterField(model)
|
2020-01-13 20:16:13 +00: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-12-10 12:59:10 -05:00
|
|
|
'name', 'slug', 'weight', 'description',
|
2018-11-27 11:57:29 -05:00
|
|
|
]
|
2016-05-17 15:04:16 -04:00
|
|
|
|
|
|
|
|
2020-05-05 16:15:09 -04:00
|
|
|
class RoleCSVForm(CSVModelForm):
|
2017-10-09 15:42:06 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
#
|
|
|
|
# Prefixes
|
|
|
|
#
|
|
|
|
|
2020-01-29 10:49:02 -05:00
|
|
|
class PrefixForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
|
2020-02-11 10:43:22 -05:00
|
|
|
vrf = DynamicModelChoiceField(
|
|
|
|
queryset=VRF.objects.all(),
|
|
|
|
required=False,
|
2020-08-13 09:27:21 -04:00
|
|
|
label='VRF',
|
|
|
|
display_field='display_name'
|
2020-02-11 10:43:22 -05:00
|
|
|
)
|
2020-11-04 15:27:41 -05:00
|
|
|
region = DynamicModelChoiceField(
|
|
|
|
queryset=Region.objects.all(),
|
|
|
|
required=False,
|
|
|
|
initial_params={
|
|
|
|
'sites': '$site'
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
site = DynamicModelChoiceField(
|
2017-05-25 14:33:50 -04:00
|
|
|
queryset=Site.objects.all(),
|
|
|
|
required=False,
|
2020-11-04 15:27:41 -05:00
|
|
|
null_option='None',
|
|
|
|
query_params={
|
|
|
|
'region_id': '$region'
|
|
|
|
}
|
2017-06-09 14:15:12 -04:00
|
|
|
)
|
2020-02-10 17:23:52 -05:00
|
|
|
vlan_group = DynamicModelChoiceField(
|
2017-06-09 14:15:12 -04:00
|
|
|
queryset=VLANGroup.objects.all(),
|
|
|
|
required=False,
|
|
|
|
label='VLAN group',
|
2020-08-12 10:05:12 -04:00
|
|
|
null_option='None',
|
2020-08-12 12:36:53 -04:00
|
|
|
query_params={
|
|
|
|
'site_id': '$site'
|
2020-11-04 11:09:13 -05:00
|
|
|
},
|
|
|
|
initial_params={
|
|
|
|
'vlans': '$vlan'
|
2020-08-12 12:36:53 -04:00
|
|
|
}
|
2017-05-11 16:24:57 -04:00
|
|
|
)
|
2020-02-10 17:23:52 -05:00
|
|
|
vlan = DynamicModelChoiceField(
|
2017-05-25 14:33:50 -04:00
|
|
|
queryset=VLAN.objects.all(),
|
|
|
|
required=False,
|
|
|
|
label='VLAN',
|
2020-08-12 12:36:53 -04:00
|
|
|
display_field='display_name',
|
|
|
|
query_params={
|
|
|
|
'site_id': '$site',
|
|
|
|
'group_id': '$vlan_group',
|
|
|
|
}
|
2017-05-11 16:24:57 -04:00
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
role = DynamicModelChoiceField(
|
|
|
|
queryset=Role.objects.all(),
|
2020-03-16 14:08:48 -04:00
|
|
|
required=False
|
2020-02-11 10:43:22 -05:00
|
|
|
)
|
2020-06-12 09:58:59 -04:00
|
|
|
tags = DynamicModelMultipleChoiceField(
|
|
|
|
queryset=Tag.objects.all(),
|
|
|
|
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 = {
|
|
|
|
'status': StaticSelect2(),
|
|
|
|
}
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
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'
|
|
|
|
|
|
|
|
|
2020-01-29 13:53:26 -05:00
|
|
|
class PrefixCSVForm(CustomFieldModelCSVForm):
|
2020-05-06 09:43:10 -04:00
|
|
|
vrf = CSVModelChoiceField(
|
2017-06-02 16:07:11 -04:00
|
|
|
queryset=VRF.objects.all(),
|
2020-03-30 15:54:35 -04:00
|
|
|
to_field_name='name',
|
2019-03-11 12:34:19 -04:00
|
|
|
required=False,
|
2020-05-06 09:58:12 -04:00
|
|
|
help_text='Assigned VRF'
|
2017-06-02 16:07:11 -04:00
|
|
|
)
|
2020-05-06 09:43:10 -04:00
|
|
|
tenant = CSVModelChoiceField(
|
2017-06-02 16:07:11 -04:00
|
|
|
queryset=Tenant.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2020-05-06 09:58:12 -04:00
|
|
|
help_text='Assigned tenant'
|
2017-06-02 16:07:11 -04:00
|
|
|
)
|
2020-05-06 09:43:10 -04:00
|
|
|
site = CSVModelChoiceField(
|
2017-06-02 16:07:11 -04:00
|
|
|
queryset=Site.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2020-05-06 09:58:12 -04:00
|
|
|
help_text='Assigned site'
|
2017-06-02 16:07:11 -04:00
|
|
|
)
|
2020-05-06 09:43:10 -04:00
|
|
|
vlan_group = CSVModelChoiceField(
|
2020-05-05 16:15:09 -04:00
|
|
|
queryset=VLANGroup.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2020-05-06 09:58:12 -04:00
|
|
|
help_text="VLAN's group (if any)"
|
2017-06-02 16:07:11 -04:00
|
|
|
)
|
2020-05-06 09:43:10 -04:00
|
|
|
vlan = CSVModelChoiceField(
|
2020-05-05 16:15:09 -04:00
|
|
|
queryset=VLAN.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='vid',
|
2020-05-06 09:58:12 -04:00
|
|
|
help_text="Assigned VLAN"
|
2017-06-02 16:07:11 -04:00
|
|
|
)
|
2017-06-07 13:22:06 -04:00
|
|
|
status = CSVChoiceField(
|
2019-11-27 21:46:53 -05:00
|
|
|
choices=PrefixStatusChoices,
|
2017-06-07 15:51:11 -04:00
|
|
|
help_text='Operational status'
|
2017-06-02 16:07:11 -04:00
|
|
|
)
|
2020-05-06 09:43:10 -04:00
|
|
|
role = CSVModelChoiceField(
|
2017-06-02 16:07:11 -04:00
|
|
|
queryset=Role.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2020-05-06 09:58:12 -04:00
|
|
|
help_text='Functional role'
|
2017-06-02 16:07:11 -04:00
|
|
|
)
|
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
|
|
|
|
2020-05-05 16:15:09 -04:00
|
|
|
def __init__(self, data=None, *args, **kwargs):
|
|
|
|
super().__init__(data, *args, **kwargs)
|
2016-07-15 14:25:30 -04:00
|
|
|
|
2020-05-05 16:15:09 -04:00
|
|
|
if data:
|
2017-06-07 14:19:08 -04:00
|
|
|
|
2020-05-05 16:15:09 -04:00
|
|
|
# Limit vlan queryset by assigned site and group
|
|
|
|
params = {
|
|
|
|
f"site__{self.fields['site'].to_field_name}": data.get('site'),
|
|
|
|
f"group__{self.fields['vlan_group'].to_field_name}": data.get('vlan_group'),
|
|
|
|
}
|
|
|
|
self.fields['vlan'].queryset = self.fields['vlan'].queryset.filter(**params)
|
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()
|
|
|
|
)
|
2020-11-04 15:27:41 -05:00
|
|
|
region = DynamicModelChoiceField(
|
|
|
|
queryset=Region.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='slug'
|
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
site = DynamicModelChoiceField(
|
2018-11-27 11:57:29 -05:00
|
|
|
queryset=Site.objects.all(),
|
2020-11-04 15:27:41 -05:00
|
|
|
required=False,
|
|
|
|
query_params={
|
|
|
|
'region': '$region'
|
|
|
|
}
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
vrf = DynamicModelChoiceField(
|
2018-11-27 11:57:29 -05:00
|
|
|
queryset=VRF.objects.all(),
|
|
|
|
required=False,
|
2020-08-13 10:32:57 -04:00
|
|
|
label='VRF',
|
|
|
|
display_field='display_name'
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
2019-01-03 16:21:21 -05:00
|
|
|
prefix_length = forms.IntegerField(
|
2020-01-24 14:21:59 -05:00
|
|
|
min_value=PREFIX_LENGTH_MIN,
|
|
|
|
max_value=PREFIX_LENGTH_MAX,
|
2019-01-03 16:21:21 -05:00
|
|
|
required=False
|
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
tenant = DynamicModelChoiceField(
|
2018-11-27 11:57:29 -05:00
|
|
|
queryset=Tenant.objects.all(),
|
2020-03-16 14:08:48 -04:00
|
|
|
required=False
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
status = forms.ChoiceField(
|
2019-11-27 21:46:53 -05:00
|
|
|
choices=add_blank_choice(PrefixStatusChoices),
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=StaticSelect2()
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
role = DynamicModelChoiceField(
|
2018-11-27 11:57:29 -05:00
|
|
|
queryset=Role.objects.all(),
|
2020-03-16 14:08:48 -04:00
|
|
|
required=False
|
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 = [
|
2020-01-03 13:52:50 -05:00
|
|
|
'q', 'within_include', 'family', 'mask_length', 'vrf_id', 'status', 'region', 'site', 'role', 'tenant_group',
|
|
|
|
'tenant', 'is_pool', 'expand',
|
2019-05-09 14:32:49 -04:00
|
|
|
]
|
2020-08-06 09:20:20 -04:00
|
|
|
mask_length__lte = forms.IntegerField(
|
|
|
|
widget=forms.HiddenInput()
|
|
|
|
)
|
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,
|
2020-01-24 15:40:03 -05:00
|
|
|
choices=add_blank_choice(IPAddressFamilyChoices),
|
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
|
|
|
)
|
2020-02-11 09:26:39 -05:00
|
|
|
vrf_id = DynamicModelMultipleChoiceField(
|
2019-01-10 21:19:13 -05:00
|
|
|
queryset=VRF.objects.all(),
|
2020-02-11 09:26:39 -05:00
|
|
|
required=False,
|
2017-03-01 13:09:19 -05:00
|
|
|
label='VRF',
|
2020-08-12 09:47:13 -04:00
|
|
|
null_option='Global'
|
2017-03-01 13:09:19 -05:00
|
|
|
)
|
2019-01-10 21:19:13 -05:00
|
|
|
status = forms.MultipleChoiceField(
|
2019-11-27 21:46:53 -05:00
|
|
|
choices=PrefixStatusChoices,
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=StaticSelect2Multiple()
|
2018-03-07 14:16:38 -05:00
|
|
|
)
|
2020-02-11 09:26:39 -05:00
|
|
|
region = DynamicModelMultipleChoiceField(
|
2020-01-03 13:52:50 -05:00
|
|
|
queryset=Region.objects.all(),
|
|
|
|
to_field_name='slug',
|
2020-08-12 12:36:53 -04:00
|
|
|
required=False
|
2020-01-03 13:52:50 -05:00
|
|
|
)
|
2020-02-11 09:26:39 -05:00
|
|
|
site = DynamicModelMultipleChoiceField(
|
2019-01-10 21:19:13 -05:00
|
|
|
queryset=Site.objects.all(),
|
2017-03-01 13:09:19 -05:00
|
|
|
to_field_name='slug',
|
2020-02-11 09:26:39 -05:00
|
|
|
required=False,
|
2020-08-12 12:36:53 -04:00
|
|
|
null_option='None',
|
|
|
|
query_params={
|
|
|
|
'region': '$region'
|
|
|
|
}
|
2017-03-01 13:09:19 -05:00
|
|
|
)
|
2020-02-11 09:26:39 -05:00
|
|
|
role = DynamicModelMultipleChoiceField(
|
2019-01-10 21:19:13 -05:00
|
|
|
queryset=Role.objects.all(),
|
2017-03-01 13:09:19 -05:00
|
|
|
to_field_name='slug',
|
2020-02-11 09:26:39 -05:00
|
|
|
required=False,
|
2020-08-12 09:47:13 -04:00
|
|
|
null_option='None'
|
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
|
|
|
|
)
|
|
|
|
)
|
2020-01-14 08:22:27 +00:00
|
|
|
tag = TagFilterField(model)
|
2020-01-13 20:16:13 +00:00
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
#
|
|
|
|
# IP addresses
|
|
|
|
#
|
|
|
|
|
2020-01-29 10:49:02 -05:00
|
|
|
class IPAddressForm(BootstrapMixin, TenancyForm, ReturnURLForm, CustomFieldModelForm):
|
2020-06-24 11:30:28 -04:00
|
|
|
device = DynamicModelChoiceField(
|
|
|
|
queryset=Device.objects.all(),
|
2020-08-13 09:27:21 -04:00
|
|
|
required=False,
|
2020-11-04 11:09:13 -05:00
|
|
|
display_field='display_name',
|
|
|
|
initial_params={
|
|
|
|
'interfaces': '$interface'
|
|
|
|
}
|
2020-06-24 11:30:28 -04:00
|
|
|
)
|
|
|
|
interface = DynamicModelChoiceField(
|
|
|
|
queryset=Interface.objects.all(),
|
2020-08-12 12:36:53 -04:00
|
|
|
required=False,
|
|
|
|
query_params={
|
|
|
|
'device_id': '$device'
|
|
|
|
}
|
2020-06-24 11:30:28 -04:00
|
|
|
)
|
|
|
|
virtual_machine = DynamicModelChoiceField(
|
|
|
|
queryset=VirtualMachine.objects.all(),
|
2020-11-04 11:09:13 -05:00
|
|
|
required=False,
|
|
|
|
initial_params={
|
|
|
|
'interfaces': '$vminterface'
|
|
|
|
}
|
2020-06-24 11:30:28 -04:00
|
|
|
)
|
|
|
|
vminterface = DynamicModelChoiceField(
|
|
|
|
queryset=VMInterface.objects.all(),
|
|
|
|
required=False,
|
2020-08-12 12:36:53 -04:00
|
|
|
label='Interface',
|
|
|
|
query_params={
|
|
|
|
'virtual_machine_id': '$virtual_machine'
|
|
|
|
}
|
2020-06-24 11:30:28 -04:00
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
vrf = DynamicModelChoiceField(
|
|
|
|
queryset=VRF.objects.all(),
|
|
|
|
required=False,
|
2020-08-13 10:32:57 -04:00
|
|
|
label='VRF',
|
|
|
|
display_field='display_name'
|
2020-02-11 10:43:22 -05:00
|
|
|
)
|
2020-11-04 15:27:41 -05:00
|
|
|
nat_region = DynamicModelChoiceField(
|
|
|
|
queryset=Region.objects.all(),
|
|
|
|
required=False,
|
|
|
|
label='Region',
|
|
|
|
initial_params={
|
|
|
|
'sites': '$nat_site'
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
nat_site = DynamicModelChoiceField(
|
2017-05-11 16:24:57 -04:00
|
|
|
queryset=Site.objects.all(),
|
|
|
|
required=False,
|
2020-11-04 15:27:41 -05:00
|
|
|
label='Site',
|
|
|
|
query_params={
|
|
|
|
'region_id': '$nat_region'
|
|
|
|
}
|
2017-04-13 14:54:17 -04:00
|
|
|
)
|
2020-02-10 17:23:52 -05:00
|
|
|
nat_rack = DynamicModelChoiceField(
|
2017-05-11 16:24:57 -04:00
|
|
|
queryset=Rack.objects.all(),
|
|
|
|
required=False,
|
|
|
|
label='Rack',
|
2020-08-11 17:00:28 -04:00
|
|
|
display_field='display_name',
|
2020-08-12 10:05:12 -04:00
|
|
|
null_option='None',
|
2020-08-12 12:36:53 -04:00
|
|
|
query_params={
|
|
|
|
'site_id': '$site'
|
|
|
|
}
|
2017-05-11 16:24:57 -04:00
|
|
|
)
|
2020-02-10 17:23:52 -05:00
|
|
|
nat_device = DynamicModelChoiceField(
|
2017-05-11 16:24:57 -04:00
|
|
|
queryset=Device.objects.all(),
|
|
|
|
required=False,
|
|
|
|
label='Device',
|
2020-08-11 17:00:28 -04:00
|
|
|
display_field='display_name',
|
2020-08-12 12:36:53 -04:00
|
|
|
query_params={
|
|
|
|
'site_id': '$site',
|
|
|
|
'rack_id': '$nat_rack',
|
|
|
|
}
|
2017-04-13 14:54:17 -04:00
|
|
|
)
|
2020-08-13 10:42:19 -04:00
|
|
|
nat_cluster = DynamicModelChoiceField(
|
|
|
|
queryset=Cluster.objects.all(),
|
|
|
|
required=False,
|
|
|
|
label='Cluster'
|
|
|
|
)
|
|
|
|
nat_virtual_machine = DynamicModelChoiceField(
|
|
|
|
queryset=VirtualMachine.objects.all(),
|
|
|
|
required=False,
|
|
|
|
label='Virtual Machine',
|
|
|
|
query_params={
|
|
|
|
'cluster_id': '$nat_cluster',
|
|
|
|
}
|
|
|
|
)
|
2020-03-16 14:29:01 -04:00
|
|
|
nat_vrf = DynamicModelChoiceField(
|
2020-01-28 10:22:28 -05:00
|
|
|
queryset=VRF.objects.all(),
|
|
|
|
required=False,
|
2020-08-13 10:32:57 -04:00
|
|
|
label='VRF',
|
|
|
|
display_field='display_name'
|
2020-01-28 10:22:28 -05:00
|
|
|
)
|
2020-02-10 17:23:52 -05:00
|
|
|
nat_inside = DynamicModelChoiceField(
|
2017-05-11 16:24:57 -04:00
|
|
|
queryset=IPAddress.objects.all(),
|
|
|
|
required=False,
|
|
|
|
label='IP Address',
|
2020-08-12 12:36:53 -04:00
|
|
|
display_field='address',
|
|
|
|
query_params={
|
|
|
|
'device_id': '$nat_device',
|
2020-08-13 10:42:19 -04:00
|
|
|
'virtual_machine_id': '$nat_virtual_machine',
|
2020-08-13 09:27:21 -04:00
|
|
|
'vrf_id': '$nat_vrf',
|
2020-08-12 12:36:53 -04:00
|
|
|
}
|
2017-05-11 16:24:57 -04:00
|
|
|
)
|
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'
|
|
|
|
)
|
2020-06-12 09:58:59 -04:00
|
|
|
tags = DynamicModelMultipleChoiceField(
|
|
|
|
queryset=Tag.objects.all(),
|
2018-11-27 11:57:29 -05:00
|
|
|
required=False
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = IPAddress
|
2017-05-11 17:35:20 -04:00
|
|
|
fields = [
|
2020-06-22 14:46:25 -04:00
|
|
|
'address', 'vrf', 'status', 'role', 'dns_name', 'description', 'primary_for_parent', 'nat_site', 'nat_rack',
|
2020-08-13 10:42:19 -04:00
|
|
|
'nat_device', 'nat_cluster', 'nat_virtual_machine', 'nat_vrf', '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(),
|
|
|
|
}
|
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()
|
2020-06-24 11:30:28 -04:00
|
|
|
if instance:
|
|
|
|
if type(instance.assigned_object) is Interface:
|
|
|
|
initial['interface'] = instance.assigned_object
|
|
|
|
elif type(instance.assigned_object) is VMInterface:
|
|
|
|
initial['vminterface'] = instance.assigned_object
|
2020-08-20 14:38:58 -04:00
|
|
|
if instance.nat_inside:
|
|
|
|
nat_inside_parent = instance.nat_inside.assigned_object
|
|
|
|
if type(nat_inside_parent) is Interface:
|
|
|
|
initial['nat_site'] = nat_inside_parent.device.site.pk
|
|
|
|
initial['nat_rack'] = nat_inside_parent.device.rack.pk
|
|
|
|
initial['nat_device'] = nat_inside_parent.device.pk
|
|
|
|
elif type(nat_inside_parent) is VMInterface:
|
|
|
|
initial['nat_cluster'] = nat_inside_parent.virtual_machine.cluster.pk
|
|
|
|
initial['nat_virtual_machine'] = nat_inside_parent.virtual_machine.pk
|
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
|
|
|
|
2020-06-24 11:30:28 -04:00
|
|
|
# Initialize primary_for_parent if IP address is already assigned
|
|
|
|
if self.instance.pk and self.instance.assigned_object:
|
|
|
|
parent = self.instance.assigned_object.parent
|
|
|
|
if (
|
|
|
|
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
|
|
|
|
):
|
|
|
|
self.initial['primary_for_parent'] = True
|
2017-05-02 16:46:23 -04:00
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
|
2020-06-24 11:30:28 -04:00
|
|
|
# Cannot select both a device interface and a VM interface
|
|
|
|
if self.cleaned_data.get('interface') and self.cleaned_data.get('vminterface'):
|
|
|
|
raise forms.ValidationError("Cannot select both a device interface and a virtual machine interface")
|
2020-09-16 11:10:30 -04:00
|
|
|
self.instance.assigned_object = self.cleaned_data.get('interface') or self.cleaned_data.get('vminterface')
|
2020-06-24 11:30:28 -04:00
|
|
|
|
2017-05-02 16:46:23 -04:00
|
|
|
# Primary IP assignment is only available if an interface has been assigned.
|
2020-06-24 11:30:28 -04:00
|
|
|
interface = self.cleaned_data.get('interface') or self.cleaned_data.get('vminterface')
|
|
|
|
if self.cleaned_data.get('primary_for_parent') and not 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.
|
2020-09-16 11:10:30 -04:00
|
|
|
interface = self.instance.assigned_object
|
2020-06-24 11:30:28 -04:00
|
|
|
if interface and self.cleaned_data['primary_for_parent']:
|
2017-05-02 16:46:23 -04:00
|
|
|
if ipaddress.address.version == 4:
|
2020-06-24 11:30:28 -04:00
|
|
|
interface.parent.primary_ip4 = ipaddress
|
2017-05-02 16:46:23 -04:00
|
|
|
else:
|
2020-09-16 11:10:30 -04:00
|
|
|
interface.parent.primary_ip6 = ipaddress
|
2020-06-24 11:30:28 -04:00
|
|
|
interface.parent.save()
|
|
|
|
elif interface and ipaddress.address.version == 4 and interface.parent.primary_ip4 == ipaddress:
|
|
|
|
interface.parent.primary_ip4 = None
|
|
|
|
interface.parent.save()
|
|
|
|
elif interface and ipaddress.address.version == 6 and interface.parent.primary_ip6 == ipaddress:
|
2020-09-16 11:10:30 -04:00
|
|
|
interface.parent.primary_ip6 = None
|
2020-06-24 11:30:28 -04:00
|
|
|
interface.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
|
|
|
|
2020-01-29 10:49:02 -05:00
|
|
|
class IPAddressBulkAddForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
|
2020-02-11 10:43:22 -05:00
|
|
|
vrf = DynamicModelChoiceField(
|
|
|
|
queryset=VRF.objects.all(),
|
|
|
|
required=False,
|
2020-08-13 10:32:57 -04:00
|
|
|
label='VRF',
|
|
|
|
display_field='display_name'
|
2020-02-11 10:43:22 -05:00
|
|
|
)
|
2020-06-17 13:29:27 -04:00
|
|
|
tags = DynamicModelMultipleChoiceField(
|
|
|
|
queryset=Tag.objects.all(),
|
2020-06-15 13:24:34 -04:00
|
|
|
required=False
|
|
|
|
)
|
2017-04-19 14:50:58 -04:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = IPAddress
|
2018-11-27 11:57:29 -05:00
|
|
|
fields = [
|
2020-06-15 13:24:34 -04:00
|
|
|
'address', 'vrf', 'status', 'role', 'dns_name', 'description', 'tenant_group', 'tenant', 'tags',
|
2018-11-27 11:57:29 -05:00
|
|
|
]
|
2019-01-10 21:19:13 -05:00
|
|
|
widgets = {
|
|
|
|
'status': StaticSelect2(),
|
|
|
|
'role': StaticSelect2(),
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
2020-01-29 13:53:26 -05:00
|
|
|
class IPAddressCSVForm(CustomFieldModelCSVForm):
|
2020-05-06 09:43:10 -04:00
|
|
|
vrf = CSVModelChoiceField(
|
2017-06-02 16:07:11 -04:00
|
|
|
queryset=VRF.objects.all(),
|
2020-03-30 15:54:35 -04:00
|
|
|
to_field_name='name',
|
2019-03-11 12:34:19 -04:00
|
|
|
required=False,
|
2020-05-06 09:58:12 -04:00
|
|
|
help_text='Assigned VRF'
|
2017-06-02 16:07:11 -04:00
|
|
|
)
|
2020-05-06 09:43:10 -04:00
|
|
|
tenant = CSVModelChoiceField(
|
2017-06-02 16:07:11 -04:00
|
|
|
queryset=Tenant.objects.all(),
|
|
|
|
to_field_name='name',
|
|
|
|
required=False,
|
2020-05-06 09:58:12 -04:00
|
|
|
help_text='Assigned tenant'
|
2017-06-02 16:07:11 -04:00
|
|
|
)
|
2017-06-07 13:22:06 -04:00
|
|
|
status = CSVChoiceField(
|
2019-11-27 21:54:01 -05:00
|
|
|
choices=IPAddressStatusChoices,
|
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(
|
2019-11-27 22:09:16 -05:00
|
|
|
choices=IPAddressRoleChoices,
|
2017-06-14 16:11:13 -04:00
|
|
|
required=False,
|
|
|
|
help_text='Functional role'
|
|
|
|
)
|
2020-06-23 16:09:31 -04:00
|
|
|
device = CSVModelChoiceField(
|
|
|
|
queryset=Device.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
|
|
|
help_text='Parent device of assigned interface (if any)'
|
|
|
|
)
|
|
|
|
virtual_machine = CSVModelChoiceField(
|
|
|
|
queryset=VirtualMachine.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
|
|
|
help_text='Parent VM of assigned interface (if any)'
|
|
|
|
)
|
|
|
|
interface = CSVModelChoiceField(
|
|
|
|
queryset=Interface.objects.none(), # Can also refer to VMInterface
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
|
|
|
help_text='Assigned interface'
|
|
|
|
)
|
2017-06-02 16:07:11 -04:00
|
|
|
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
|
|
|
|
2020-05-05 16:15:09 -04:00
|
|
|
def __init__(self, data=None, *args, **kwargs):
|
|
|
|
super().__init__(data, *args, **kwargs)
|
|
|
|
|
2020-06-23 16:09:31 -04:00
|
|
|
if data:
|
|
|
|
|
|
|
|
# Limit interface queryset by assigned device
|
|
|
|
if data.get('device'):
|
|
|
|
self.fields['interface'].queryset = Interface.objects.filter(
|
|
|
|
**{f"device__{self.fields['device'].to_field_name}": data['device']}
|
|
|
|
)
|
|
|
|
|
|
|
|
# Limit interface queryset by assigned device
|
|
|
|
elif data.get('virtual_machine'):
|
|
|
|
self.fields['interface'].queryset = VMInterface.objects.filter(
|
|
|
|
**{f"virtual_machine__{self.fields['virtual_machine'].to_field_name}": data['virtual_machine']}
|
|
|
|
)
|
2020-05-05 16:15:09 -04:00
|
|
|
|
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
|
|
|
is_primary = self.cleaned_data.get('is_primary')
|
|
|
|
|
|
|
|
# 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
|
|
|
|
2020-06-23 16:09:31 -04:00
|
|
|
# Set interface assignment
|
|
|
|
if self.cleaned_data['interface']:
|
|
|
|
self.instance.assigned_object = self.cleaned_data['interface']
|
|
|
|
|
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()
|
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
vrf = DynamicModelChoiceField(
|
2018-11-27 11:57:29 -05:00
|
|
|
queryset=VRF.objects.all(),
|
|
|
|
required=False,
|
2020-08-13 10:32:57 -04:00
|
|
|
label='VRF',
|
|
|
|
display_field='display_name'
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
2019-01-03 16:21:21 -05:00
|
|
|
mask_length = forms.IntegerField(
|
2020-01-24 14:21:59 -05:00
|
|
|
min_value=IPADDRESS_MASK_LENGTH_MIN,
|
|
|
|
max_value=IPADDRESS_MASK_LENGTH_MAX,
|
2019-01-03 16:21:21 -05:00
|
|
|
required=False
|
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
tenant = DynamicModelChoiceField(
|
2018-11-27 11:57:29 -05:00
|
|
|
queryset=Tenant.objects.all(),
|
2020-03-16 14:08:48 -04:00
|
|
|
required=False
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
status = forms.ChoiceField(
|
2019-11-27 21:54:01 -05:00
|
|
|
choices=add_blank_choice(IPAddressStatusChoices),
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=StaticSelect2()
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
role = forms.ChoiceField(
|
2019-11-27 22:09:16 -05:00
|
|
|
choices=add_blank_choice(IPAddressRoleChoices),
|
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):
|
2020-02-11 10:43:22 -05:00
|
|
|
vrf_id = DynamicModelChoiceField(
|
2018-11-27 11:57:29 -05:00
|
|
|
queryset=VRF.objects.all(),
|
|
|
|
required=False,
|
|
|
|
label='VRF',
|
2020-03-16 14:08:48 -04:00
|
|
|
empty_label='Global'
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
2020-01-09 16:26:11 +00:00
|
|
|
q = forms.CharField(
|
2020-01-09 10:00:02 +00:00
|
|
|
required=False,
|
2020-01-09 16:26:11 +00:00
|
|
|
label='Search',
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
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 = [
|
2019-12-31 15:24:00 -05:00
|
|
|
'q', 'parent', 'family', 'mask_length', 'vrf_id', 'status', 'role', 'assigned_to_interface', 'tenant_group',
|
|
|
|
'tenant',
|
2019-05-09 14:32:49 -04:00
|
|
|
]
|
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,
|
2020-01-24 15:40:03 -05:00
|
|
|
choices=add_blank_choice(IPAddressFamilyChoices),
|
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
|
|
|
)
|
2020-02-11 09:26:39 -05:00
|
|
|
vrf_id = DynamicModelMultipleChoiceField(
|
2019-01-10 21:19:13 -05:00
|
|
|
queryset=VRF.objects.all(),
|
2020-02-11 09:26:39 -05:00
|
|
|
required=False,
|
2017-03-01 13:09:19 -05:00
|
|
|
label='VRF',
|
2020-08-12 09:47:13 -04:00
|
|
|
null_option='Global'
|
2017-03-01 13:09:19 -05:00
|
|
|
)
|
2019-01-10 21:19:13 -05:00
|
|
|
status = forms.MultipleChoiceField(
|
2019-11-27 21:54:01 -05:00
|
|
|
choices=IPAddressStatusChoices,
|
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(
|
2019-11-27 22:09:16 -05:00
|
|
|
choices=IPAddressRoleChoices,
|
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
|
|
|
)
|
2019-12-31 15:24:00 -05:00
|
|
|
assigned_to_interface = forms.NullBooleanField(
|
|
|
|
required=False,
|
|
|
|
label='Assigned to an interface',
|
|
|
|
widget=StaticSelect2(
|
|
|
|
choices=BOOLEAN_WITH_BLANK_CHOICES
|
|
|
|
)
|
|
|
|
)
|
2020-01-14 08:22:27 +00:00
|
|
|
tag = TagFilterField(model)
|
2020-01-13 20:16:13 +00: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):
|
2020-11-04 15:27:41 -05:00
|
|
|
region = DynamicModelChoiceField(
|
|
|
|
queryset=Region.objects.all(),
|
|
|
|
required=False,
|
|
|
|
initial_params={
|
|
|
|
'sites': '$site'
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
site = DynamicModelChoiceField(
|
|
|
|
queryset=Site.objects.all(),
|
2020-11-04 15:27:41 -05:00
|
|
|
required=False,
|
|
|
|
query_params={
|
|
|
|
'region_id': '$region'
|
|
|
|
}
|
2020-02-11 10:43:22 -05:00
|
|
|
)
|
2016-07-15 13:26:54 -04:00
|
|
|
slug = SlugField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = VLANGroup
|
2018-11-27 11:57:29 -05:00
|
|
|
fields = [
|
2020-11-04 15:27:41 -05:00
|
|
|
'region', 'site', 'name', 'slug', 'description',
|
2018-11-27 11:57:29 -05:00
|
|
|
]
|
2016-07-15 13:26:54 -04:00
|
|
|
|
|
|
|
|
2020-05-05 16:15:09 -04:00
|
|
|
class VLANGroupCSVForm(CSVModelForm):
|
2020-05-06 09:43:10 -04:00
|
|
|
site = CSVModelChoiceField(
|
2017-10-09 15:42:06 -04:00
|
|
|
queryset=Site.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2020-05-06 09:58:12 -04:00
|
|
|
help_text='Assigned site'
|
2017-10-09 15:42:06 -04:00
|
|
|
)
|
|
|
|
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
|
|
|
|
|
|
|
|
2016-12-21 14:15:18 -05:00
|
|
|
class VLANGroupFilterForm(BootstrapMixin, forms.Form):
|
2020-02-11 09:26:39 -05:00
|
|
|
region = DynamicModelMultipleChoiceField(
|
2020-01-03 13:52:50 -05:00
|
|
|
queryset=Region.objects.all(),
|
|
|
|
to_field_name='slug',
|
2020-08-12 12:36:53 -04:00
|
|
|
required=False
|
2020-01-03 13:52:50 -05:00
|
|
|
)
|
2020-02-11 09:26:39 -05:00
|
|
|
site = DynamicModelMultipleChoiceField(
|
2019-01-10 21:19:13 -05:00
|
|
|
queryset=Site.objects.all(),
|
2017-02-21 14:53:22 -05:00
|
|
|
to_field_name='slug',
|
2020-02-11 09:26:39 -05:00
|
|
|
required=False,
|
2020-08-12 12:36:53 -04:00
|
|
|
null_option='None',
|
|
|
|
query_params={
|
|
|
|
'region': '$region'
|
|
|
|
}
|
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
|
|
|
|
#
|
|
|
|
|
2020-01-29 10:49:02 -05:00
|
|
|
class VLANForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
|
2020-11-04 15:27:41 -05:00
|
|
|
region = DynamicModelChoiceField(
|
|
|
|
queryset=Region.objects.all(),
|
|
|
|
required=False,
|
|
|
|
initial_params={
|
|
|
|
'sites': '$site'
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
site = DynamicModelChoiceField(
|
2017-05-11 16:24:57 -04:00
|
|
|
queryset=Site.objects.all(),
|
2017-05-16 16:30:28 -04:00
|
|
|
required=False,
|
2020-11-04 15:27:41 -05:00
|
|
|
null_option='None',
|
|
|
|
query_params={
|
|
|
|
'region_id': '$region'
|
|
|
|
}
|
2017-05-11 16:24:57 -04:00
|
|
|
)
|
2020-02-10 17:23:52 -05:00
|
|
|
group = DynamicModelChoiceField(
|
2017-05-11 16:24:57 -04:00
|
|
|
queryset=VLANGroup.objects.all(),
|
2020-08-12 12:36:53 -04:00
|
|
|
required=False,
|
|
|
|
query_params={
|
|
|
|
'site_id': '$site'
|
|
|
|
}
|
2017-05-11 16:24:57 -04:00
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
role = DynamicModelChoiceField(
|
|
|
|
queryset=Role.objects.all(),
|
2020-03-16 14:08:48 -04:00
|
|
|
required=False
|
2020-02-11 10:43:22 -05:00
|
|
|
)
|
2020-06-12 09:58:59 -04:00
|
|
|
tags = DynamicModelMultipleChoiceField(
|
|
|
|
queryset=Tag.objects.all(),
|
|
|
|
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(),
|
|
|
|
}
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
2020-01-29 13:53:26 -05:00
|
|
|
class VLANCSVForm(CustomFieldModelCSVForm):
|
2020-05-06 09:43:10 -04:00
|
|
|
site = CSVModelChoiceField(
|
2017-06-02 16:07:11 -04:00
|
|
|
queryset=Site.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2020-05-06 09:58:12 -04:00
|
|
|
help_text='Assigned site'
|
2017-06-02 16:07:11 -04:00
|
|
|
)
|
2020-05-06 09:43:10 -04:00
|
|
|
group = CSVModelChoiceField(
|
2020-05-05 16:15:09 -04:00
|
|
|
queryset=VLANGroup.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2020-05-06 09:58:12 -04:00
|
|
|
help_text='Assigned VLAN group'
|
2017-04-05 10:13:19 -04:00
|
|
|
)
|
2020-05-06 09:43:10 -04:00
|
|
|
tenant = CSVModelChoiceField(
|
2017-06-02 16:07:11 -04:00
|
|
|
queryset=Tenant.objects.all(),
|
|
|
|
to_field_name='name',
|
|
|
|
required=False,
|
2020-05-06 09:58:12 -04:00
|
|
|
help_text='Assigned tenant'
|
2017-06-02 16:07:11 -04:00
|
|
|
)
|
2017-06-07 13:22:06 -04:00
|
|
|
status = CSVChoiceField(
|
2019-11-27 22:15:59 -05:00
|
|
|
choices=VLANStatusChoices,
|
2017-06-07 15:51:11 -04:00
|
|
|
help_text='Operational status'
|
2017-04-05 10:13:19 -04:00
|
|
|
)
|
2020-05-06 09:43:10 -04:00
|
|
|
role = CSVModelChoiceField(
|
2017-06-02 16:07:11 -04:00
|
|
|
queryset=Role.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2020-05-06 09:58:12 -04:00
|
|
|
help_text='Functional 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
|
|
|
|
2020-05-05 16:15:09 -04:00
|
|
|
def __init__(self, data=None, *args, **kwargs):
|
|
|
|
super().__init__(data, *args, **kwargs)
|
|
|
|
|
|
|
|
if data:
|
2017-06-07 14:19:08 -04:00
|
|
|
|
2020-05-05 16:15:09 -04:00
|
|
|
# Limit vlan queryset by assigned group
|
|
|
|
params = {f"site__{self.fields['site'].to_field_name}": data.get('site')}
|
|
|
|
self.fields['group'].queryset = self.fields['group'].queryset.filter(**params)
|
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()
|
|
|
|
)
|
2020-11-04 15:27:41 -05:00
|
|
|
region = DynamicModelChoiceField(
|
|
|
|
queryset=Region.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='slug'
|
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
site = DynamicModelChoiceField(
|
2018-11-27 11:57:29 -05:00
|
|
|
queryset=Site.objects.all(),
|
2020-11-04 15:27:41 -05:00
|
|
|
required=False,
|
|
|
|
query_params={
|
|
|
|
'region': '$region'
|
|
|
|
}
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
group = DynamicModelChoiceField(
|
2018-11-27 11:57:29 -05:00
|
|
|
queryset=VLANGroup.objects.all(),
|
2020-08-12 12:36:53 -04:00
|
|
|
required=False,
|
|
|
|
query_params={
|
|
|
|
'site_id': '$site'
|
|
|
|
}
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
tenant = DynamicModelChoiceField(
|
2018-11-27 11:57:29 -05:00
|
|
|
queryset=Tenant.objects.all(),
|
2020-03-16 14:08:48 -04:00
|
|
|
required=False
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
status = forms.ChoiceField(
|
2019-11-27 22:15:59 -05:00
|
|
|
choices=add_blank_choice(VLANStatusChoices),
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=StaticSelect2()
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
2020-02-11 10:43:22 -05:00
|
|
|
role = DynamicModelChoiceField(
|
2018-11-27 11:57:29 -05:00
|
|
|
queryset=Role.objects.all(),
|
2020-03-16 14:08:48 -04:00
|
|
|
required=False
|
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
|
2020-01-03 13:52:50 -05:00
|
|
|
field_order = ['q', 'region', 'site', 'group_id', 'status', 'role', 'tenant_group', 'tenant']
|
2018-11-27 11:57:29 -05:00
|
|
|
q = forms.CharField(
|
|
|
|
required=False,
|
|
|
|
label='Search'
|
|
|
|
)
|
2020-02-11 09:26:39 -05:00
|
|
|
region = DynamicModelMultipleChoiceField(
|
2020-01-03 13:52:50 -05:00
|
|
|
queryset=Region.objects.all(),
|
|
|
|
to_field_name='slug',
|
2020-08-12 12:36:53 -04:00
|
|
|
required=False
|
2020-01-03 13:52:50 -05:00
|
|
|
)
|
2020-02-11 09:26:39 -05:00
|
|
|
site = DynamicModelMultipleChoiceField(
|
2019-01-10 21:19:13 -05:00
|
|
|
queryset=Site.objects.all(),
|
2017-03-01 13:09:19 -05:00
|
|
|
to_field_name='slug',
|
2020-02-11 09:26:39 -05:00
|
|
|
required=False,
|
2020-08-12 12:36:53 -04:00
|
|
|
null_option='None',
|
|
|
|
query_params={
|
|
|
|
'region': '$region'
|
|
|
|
}
|
2017-03-01 13:09:19 -05:00
|
|
|
)
|
2020-02-11 09:26:39 -05:00
|
|
|
group_id = DynamicModelMultipleChoiceField(
|
2019-01-10 21:19:13 -05:00
|
|
|
queryset=VLANGroup.objects.all(),
|
2020-02-11 09:26:39 -05:00
|
|
|
required=False,
|
2017-03-01 13:09:19 -05:00
|
|
|
label='VLAN group',
|
2020-08-12 12:36:53 -04:00
|
|
|
null_option='None',
|
|
|
|
query_params={
|
|
|
|
'region': '$region'
|
|
|
|
}
|
2017-03-01 13:09:19 -05:00
|
|
|
)
|
2019-01-10 21:19:13 -05:00
|
|
|
status = forms.MultipleChoiceField(
|
2019-11-27 22:15:59 -05:00
|
|
|
choices=VLANStatusChoices,
|
2019-01-10 21:19:13 -05:00
|
|
|
required=False,
|
|
|
|
widget=StaticSelect2Multiple()
|
2018-03-07 14:16:38 -05:00
|
|
|
)
|
2020-02-11 09:26:39 -05:00
|
|
|
role = DynamicModelMultipleChoiceField(
|
2019-01-10 21:19:13 -05:00
|
|
|
queryset=Role.objects.all(),
|
2017-03-01 13:09:19 -05:00
|
|
|
to_field_name='slug',
|
2020-02-11 09:26:39 -05:00
|
|
|
required=False,
|
2020-08-12 09:47:13 -04:00
|
|
|
null_option='None'
|
2017-03-01 13:09:19 -05:00
|
|
|
)
|
2020-01-14 08:22:27 +00:00
|
|
|
tag = TagFilterField(model)
|
2020-01-13 20:16:13 +00:00
|
|
|
|
2016-12-15 15:32:58 -05:00
|
|
|
|
|
|
|
#
|
|
|
|
# Services
|
|
|
|
#
|
|
|
|
|
2020-01-29 10:49:02 -05:00
|
|
|
class ServiceForm(BootstrapMixin, CustomFieldModelForm):
|
2019-12-05 21:22:34 -05:00
|
|
|
port = forms.IntegerField(
|
2020-01-24 14:21:59 -05:00
|
|
|
min_value=SERVICE_PORT_MIN,
|
|
|
|
max_value=SERVICE_PORT_MAX
|
2019-12-05 21:22:34 -05:00
|
|
|
)
|
2020-06-12 09:58:59 -04:00
|
|
|
tags = DynamicModelMultipleChoiceField(
|
|
|
|
queryset=Tag.objects.all(),
|
2018-11-27 11:57:29 -05:00
|
|
|
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:
|
|
|
|
self.fields['ipaddresses'].queryset = IPAddress.objects.filter(
|
2020-06-22 16:27:13 -04:00
|
|
|
interface__in=self.instance.device.vc_interfaces.values_list('id', flat=True)
|
2017-08-31 12:50:35 -04:00
|
|
|
)
|
|
|
|
elif self.instance.virtual_machine:
|
|
|
|
self.fields['ipaddresses'].queryset = IPAddress.objects.filter(
|
2020-06-24 09:27:30 -04:00
|
|
|
vminterface__in=self.instance.virtual_machine.interfaces.values_list('id', flat=True)
|
2017-08-31 12:50:35 -04:00
|
|
|
)
|
|
|
|
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(
|
2019-11-27 22:27:06 -05:00
|
|
|
choices=add_blank_choice(ServiceProtocolChoices),
|
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
|
|
|
)
|
2020-01-14 08:22:27 +00:00
|
|
|
tag = TagFilterField(model)
|
2020-01-13 20:16:13 +00:00
|
|
|
|
2018-06-21 15:55:27 -04:00
|
|
|
|
2020-03-06 16:33:43 -05:00
|
|
|
class ServiceCSVForm(CustomFieldModelCSVForm):
|
2020-05-06 09:43:10 -04:00
|
|
|
device = CSVModelChoiceField(
|
2020-03-06 16:33:43 -05:00
|
|
|
queryset=Device.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2020-05-06 09:58:12 -04:00
|
|
|
help_text='Required if not assigned to a VM'
|
2020-03-06 16:33:43 -05:00
|
|
|
)
|
2020-05-06 09:43:10 -04:00
|
|
|
virtual_machine = CSVModelChoiceField(
|
2020-03-06 16:33:43 -05:00
|
|
|
queryset=VirtualMachine.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2020-05-06 09:58:12 -04:00
|
|
|
help_text='Required if not assigned to a device'
|
2020-03-06 16:33:43 -05:00
|
|
|
)
|
|
|
|
protocol = CSVChoiceField(
|
|
|
|
choices=ServiceProtocolChoices,
|
|
|
|
help_text='IP protocol'
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Service
|
|
|
|
fields = Service.csv_headers
|
|
|
|
|
|
|
|
|
2020-05-06 15:00:01 -04:00
|
|
|
class ServiceBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
|
2018-11-27 11:57:29 -05:00
|
|
|
pk = forms.ModelMultipleChoiceField(
|
|
|
|
queryset=Service.objects.all(),
|
|
|
|
widget=forms.MultipleHiddenInput()
|
|
|
|
)
|
|
|
|
protocol = forms.ChoiceField(
|
2019-11-27 22:27:06 -05:00
|
|
|
choices=add_blank_choice(ServiceProtocolChoices),
|
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 = [
|
2020-02-03 12:25:20 -05:00
|
|
|
'description',
|
2018-11-27 11:57:29 -05:00
|
|
|
]
|