| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354 |
- from django import forms
- from django.core.exceptions import MultipleObjectsReturned
- from django.core.validators import MaxValueValidator, MinValueValidator
- from taggit.forms import TagField
- from dcim.models import Site, Rack, Device, Interface
- from extras.forms import AddRemoveTagsForm, CustomFieldForm, CustomFieldBulkEditForm, CustomFieldFilterForm
- from tenancy.forms import TenancyForm
- from tenancy.models import Tenant
- from utilities.forms import (
- add_blank_choice, APISelect, APISelectMultiple, BootstrapMixin, BulkEditNullBooleanSelect, ChainedModelChoiceField,
- CSVChoiceField, ExpandableIPAddressField, FilterChoiceField, FlexibleModelChoiceField, ReturnURLForm, SlugField,
- StaticSelect2, StaticSelect2Multiple, BOOLEAN_WITH_BLANK_CHOICES
- )
- from virtualization.models import VirtualMachine
- from .constants import (
- IP_PROTOCOL_CHOICES, IPADDRESS_ROLE_CHOICES, IPADDRESS_STATUS_CHOICES, PREFIX_STATUS_CHOICES, VLAN_STATUS_CHOICES,
- )
- from .models import Aggregate, IPAddress, Prefix, RIR, Role, Service, VLAN, VLANGroup, VRF
- IP_FAMILY_CHOICES = [
- ('', 'All'),
- (4, 'IPv4'),
- (6, 'IPv6'),
- ]
- 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)])
- #
- # VRFs
- #
- class VRFForm(BootstrapMixin, TenancyForm, CustomFieldForm):
- tags = TagField(
- required=False
- )
- class Meta:
- model = VRF
- fields = [
- 'name', 'rd', 'enforce_unique', 'description', 'tenant_group', 'tenant', 'tags',
- ]
- labels = {
- 'rd': "RD",
- }
- help_texts = {
- 'rd': "Route distinguisher in any format",
- }
- 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.',
- }
- )
- class Meta:
- model = VRF
- fields = VRF.csv_headers
- help_texts = {
- 'name': 'VRF name',
- }
- class VRFBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
- pk = forms.ModelMultipleChoiceField(
- queryset=VRF.objects.all(),
- widget=forms.MultipleHiddenInput()
- )
- tenant = forms.ModelChoiceField(
- queryset=Tenant.objects.all(),
- required=False,
- widget=APISelect(
- api_url="/api/tenancy/tenants/"
- )
- )
- enforce_unique = forms.NullBooleanField(
- required=False,
- widget=BulkEditNullBooleanSelect(),
- label='Enforce unique space'
- )
- description = forms.CharField(
- max_length=100,
- required=False
- )
- class Meta:
- nullable_fields = [
- 'tenant', 'description',
- ]
- class VRFFilterForm(BootstrapMixin, CustomFieldFilterForm):
- model = VRF
- q = forms.CharField(
- required=False,
- label='Search'
- )
- tenant = FilterChoiceField(
- queryset=Tenant.objects.all(),
- to_field_name='slug',
- null_label='-- None --',
- widget=APISelectMultiple(
- api_url="/api/tenancy/tenants/",
- value_field="slug",
- null_option=True,
- )
- )
- #
- # RIRs
- #
- class RIRForm(BootstrapMixin, forms.ModelForm):
- slug = SlugField()
- class Meta:
- model = RIR
- fields = [
- 'name', 'slug', 'is_private',
- ]
- class RIRCSVForm(forms.ModelForm):
- slug = SlugField()
- class Meta:
- model = RIR
- fields = RIR.csv_headers
- help_texts = {
- 'name': 'RIR name',
- }
- class RIRFilterForm(BootstrapMixin, forms.Form):
- is_private = forms.NullBooleanField(
- required=False,
- label='Private',
- widget=StaticSelect2(
- choices=BOOLEAN_WITH_BLANK_CHOICES
- )
- )
- #
- # Aggregates
- #
- class AggregateForm(BootstrapMixin, CustomFieldForm):
- tags = TagField(
- required=False
- )
- class Meta:
- model = Aggregate
- fields = [
- 'prefix', 'rir', 'date_added', 'description', 'tags',
- ]
- help_texts = {
- 'prefix': "IPv4 or IPv6 network",
- 'rir': "Regional Internet Registry responsible for this prefix",
- 'date_added': "Format: YYYY-MM-DD",
- }
- widgets = {
- 'rir': APISelect(
- api_url="/api/ipam/rirs/"
- )
- }
- 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.',
- }
- )
- class Meta:
- model = Aggregate
- fields = Aggregate.csv_headers
- class AggregateBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
- pk = forms.ModelMultipleChoiceField(
- queryset=Aggregate.objects.all(),
- widget=forms.MultipleHiddenInput()
- )
- rir = forms.ModelChoiceField(
- queryset=RIR.objects.all(),
- required=False,
- label='RIR',
- widget=APISelect(
- api_url="/api/ipam/rirs/"
- )
- )
- date_added = forms.DateField(
- required=False
- )
- description = forms.CharField(
- max_length=100,
- required=False
- )
- class Meta:
- nullable_fields = [
- 'date_added', 'description',
- ]
- class AggregateFilterForm(BootstrapMixin, CustomFieldFilterForm):
- model = Aggregate
- q = forms.CharField(
- required=False,
- label='Search'
- )
- family = forms.ChoiceField(
- required=False,
- choices=IP_FAMILY_CHOICES,
- label='Address family',
- widget=StaticSelect2()
- )
- rir = FilterChoiceField(
- queryset=RIR.objects.all(),
- to_field_name='slug',
- label='RIR',
- widget=APISelectMultiple(
- api_url="/api/ipam/rirs/",
- value_field="slug",
- )
- )
- #
- # Roles
- #
- class RoleForm(BootstrapMixin, forms.ModelForm):
- slug = SlugField()
- class Meta:
- model = Role
- fields = [
- 'name', 'slug',
- ]
- class RoleCSVForm(forms.ModelForm):
- slug = SlugField()
- class Meta:
- model = Role
- fields = Role.csv_headers
- help_texts = {
- 'name': 'Role name',
- }
- #
- # Prefixes
- #
- class PrefixForm(BootstrapMixin, TenancyForm, CustomFieldForm):
- site = forms.ModelChoiceField(
- queryset=Site.objects.all(),
- required=False,
- label='Site',
- widget=APISelect(
- api_url="/api/dcim/sites/",
- filter_for={
- 'vlan_group': 'site_id',
- 'vlan': 'site_id',
- },
- attrs={
- 'nullable': 'true',
- }
- )
- )
- vlan_group = ChainedModelChoiceField(
- queryset=VLANGroup.objects.all(),
- chains=(
- ('site', 'site'),
- ),
- required=False,
- label='VLAN group',
- widget=APISelect(
- api_url='/api/ipam/vlan-groups/',
- filter_for={
- 'vlan': 'group_id'
- },
- attrs={
- 'nullable': 'true',
- }
- )
- )
- vlan = ChainedModelChoiceField(
- queryset=VLAN.objects.all(),
- chains=(
- ('site', 'site'),
- ('group', 'vlan_group'),
- ),
- required=False,
- label='VLAN',
- widget=APISelect(
- api_url='/api/ipam/vlans/',
- display_field='display_name'
- )
- )
- tags = TagField(required=False)
- class Meta:
- model = Prefix
- fields = [
- 'prefix', 'vrf', 'site', 'vlan', 'status', 'role', 'is_pool', 'description', 'tenant_group', 'tenant',
- 'tags',
- ]
- widgets = {
- 'vrf': APISelect(
- api_url="/api/ipam/vrfs/"
- ),
- 'status': StaticSelect2(),
- 'role': APISelect(
- api_url="/api/ipam/roles/"
- )
- }
- def __init__(self, *args, **kwargs):
- # Initialize helper selectors
- instance = kwargs.get('instance')
- initial = kwargs.get('initial', {}).copy()
- if instance and instance.vlan is not None:
- initial['vlan_group'] = instance.vlan.group
- kwargs['initial'] = initial
- super().__init__(*args, **kwargs)
- self.fields['vrf'].empty_label = 'Global'
- class PrefixCSVForm(forms.ModelForm):
- vrf = forms.ModelChoiceField(
- queryset=VRF.objects.all(),
- required=False,
- to_field_name='rd',
- help_text='Route distinguisher of parent VRF',
- 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.',
- }
- )
- vlan_group = forms.CharField(
- help_text='Group name of assigned VLAN',
- required=False
- )
- vlan_vid = forms.IntegerField(
- help_text='Numeric ID of assigned VLAN',
- required=False
- )
- status = CSVChoiceField(
- choices=PREFIX_STATUS_CHOICES,
- help_text='Operational status'
- )
- role = forms.ModelChoiceField(
- queryset=Role.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Functional role',
- error_messages={
- 'invalid_choice': 'Invalid role.',
- }
- )
- class Meta:
- model = Prefix
- fields = Prefix.csv_headers
- def clean(self):
- super().clean()
- site = self.cleaned_data.get('site')
- vlan_group = self.cleaned_data.get('vlan_group')
- vlan_vid = self.cleaned_data.get('vlan_vid')
- # Validate VLAN
- if vlan_group and vlan_vid:
- try:
- self.instance.vlan = VLAN.objects.get(site=site, group__name=vlan_group, vid=vlan_vid)
- except VLAN.DoesNotExist:
- if site:
- raise forms.ValidationError("VLAN {} not found in site {} group {}".format(
- vlan_vid, site, vlan_group
- ))
- else:
- raise forms.ValidationError("Global VLAN {} not found in group {}".format(vlan_vid, vlan_group))
- except MultipleObjectsReturned:
- raise forms.ValidationError(
- "Multiple VLANs with VID {} found in group {}".format(vlan_vid, vlan_group)
- )
- elif vlan_vid:
- try:
- self.instance.vlan = VLAN.objects.get(site=site, group__isnull=True, vid=vlan_vid)
- except VLAN.DoesNotExist:
- if site:
- raise forms.ValidationError("VLAN {} not found in site {}".format(vlan_vid, site))
- else:
- raise forms.ValidationError("Global VLAN {} not found".format(vlan_vid))
- except MultipleObjectsReturned:
- raise forms.ValidationError("Multiple VLANs with VID {} found".format(vlan_vid))
- class PrefixBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
- pk = forms.ModelMultipleChoiceField(
- queryset=Prefix.objects.all(),
- widget=forms.MultipleHiddenInput()
- )
- site = forms.ModelChoiceField(
- queryset=Site.objects.all(),
- required=False,
- widget=APISelect(
- api_url="/api/dcim/sites/"
- )
- )
- vrf = forms.ModelChoiceField(
- queryset=VRF.objects.all(),
- required=False,
- label='VRF',
- widget=APISelect(
- api_url="/api/ipam/vrfs/"
- )
- )
- prefix_length = forms.IntegerField(
- min_value=1,
- max_value=127,
- required=False
- )
- tenant = forms.ModelChoiceField(
- queryset=Tenant.objects.all(),
- required=False,
- widget=APISelect(
- api_url="/api/tenancy/tenants/"
- )
- )
- status = forms.ChoiceField(
- choices=add_blank_choice(PREFIX_STATUS_CHOICES),
- required=False,
- widget=StaticSelect2()
- )
- role = forms.ModelChoiceField(
- queryset=Role.objects.all(),
- required=False,
- widget=APISelect(
- api_url="/api/ipam/roles/"
- )
- )
- is_pool = forms.NullBooleanField(
- required=False,
- widget=BulkEditNullBooleanSelect(),
- label='Is a pool'
- )
- description = forms.CharField(
- max_length=100,
- required=False
- )
- class Meta:
- nullable_fields = [
- 'site', 'vrf', 'tenant', 'role', 'description',
- ]
- class PrefixFilterForm(BootstrapMixin, CustomFieldFilterForm):
- model = Prefix
- 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,
- label='Address family',
- widget=StaticSelect2()
- )
- mask_length = forms.ChoiceField(
- required=False,
- choices=PREFIX_MASK_LENGTH_CHOICES,
- label='Mask length',
- widget=StaticSelect2()
- )
- vrf = FilterChoiceField(
- queryset=VRF.objects.all(),
- to_field_name='rd',
- label='VRF',
- null_label='-- Global --',
- widget=APISelectMultiple(
- api_url="/api/ipam/vrfs/",
- value_field="slug",
- null_option=True,
- )
- )
- tenant = FilterChoiceField(
- queryset=Tenant.objects.all(),
- to_field_name='slug',
- null_label='-- None --',
- widget=APISelectMultiple(
- api_url="/api/tenancy/tenants/",
- value_field="slug",
- null_option=True,
- )
- )
- status = forms.MultipleChoiceField(
- choices=PREFIX_STATUS_CHOICES,
- required=False,
- widget=StaticSelect2Multiple()
- )
- site = FilterChoiceField(
- queryset=Site.objects.all(),
- to_field_name='slug',
- null_label='-- None --',
- widget=APISelectMultiple(
- api_url="/api/dcim/sites/",
- value_field="slug",
- null_option=True,
- )
- )
- role = FilterChoiceField(
- queryset=Role.objects.all(),
- to_field_name='slug',
- null_label='-- None --',
- widget=APISelectMultiple(
- api_url="/api/ipam/roles/",
- value_field="slug",
- null_option=True,
- )
- )
- expand = forms.BooleanField(
- required=False,
- label='Expand prefix hierarchy'
- )
- #
- # IP addresses
- #
- class IPAddressForm(BootstrapMixin, TenancyForm, ReturnURLForm, CustomFieldForm):
- interface = forms.ModelChoiceField(
- queryset=Interface.objects.all(),
- required=False
- )
- nat_site = forms.ModelChoiceField(
- queryset=Site.objects.all(),
- required=False,
- label='Site',
- widget=APISelect(
- api_url="/api/dcim/sites/",
- filter_for={
- 'nat_rack': 'site_id',
- 'nat_device': 'site_id'
- }
- )
- )
- nat_rack = ChainedModelChoiceField(
- queryset=Rack.objects.all(),
- chains=(
- ('site', 'nat_site'),
- ),
- required=False,
- label='Rack',
- widget=APISelect(
- api_url='/api/dcim/racks/',
- display_field='display_name',
- filter_for={
- 'nat_device': 'rack_id'
- },
- attrs={
- 'nullable': 'true'
- }
- )
- )
- nat_device = ChainedModelChoiceField(
- queryset=Device.objects.all(),
- chains=(
- ('site', 'nat_site'),
- ('rack', 'nat_rack'),
- ),
- required=False,
- label='Device',
- widget=APISelect(
- api_url='/api/dcim/devices/',
- display_field='display_name',
- filter_for={
- 'nat_inside': 'device_id'
- }
- )
- )
- nat_inside = ChainedModelChoiceField(
- queryset=IPAddress.objects.all(),
- chains=(
- ('interface__device', 'nat_device'),
- ),
- required=False,
- label='IP Address',
- widget=APISelect(
- api_url='/api/ipam/ip-addresses/',
- display_field='address'
- )
- )
- primary_for_parent = forms.BooleanField(
- required=False,
- label='Make this the primary IP for the device/VM'
- )
- tags = TagField(
- required=False
- )
- class Meta:
- model = IPAddress
- fields = [
- 'address', 'vrf', 'status', 'role', 'description', 'interface', 'primary_for_parent', 'nat_site',
- 'nat_rack', 'nat_inside', 'tenant_group', 'tenant', 'tags',
- ]
- widgets = {
- 'status': StaticSelect2(),
- 'role': StaticSelect2(),
- 'vrf': APISelect(
- api_url="/api/ipam/vrfs/"
- )
- }
- def __init__(self, *args, **kwargs):
- # Initialize helper selectors
- instance = kwargs.get('instance')
- initial = kwargs.get('initial', {}).copy()
- if instance and instance.nat_inside and instance.nat_inside.device is not None:
- initial['nat_site'] = instance.nat_inside.device.site
- initial['nat_rack'] = instance.nat_inside.device.rack
- initial['nat_device'] = instance.nat_inside.device
- kwargs['initial'] = initial
- super().__init__(*args, **kwargs)
- self.fields['vrf'].empty_label = 'Global'
- # 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
- 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
- def clean(self):
- super().clean()
- # Primary IP assignment is only available if an interface has been assigned.
- if self.cleaned_data.get('primary_for_parent') and not self.cleaned_data.get('interface'):
- self.add_error(
- 'primary_for_parent', "Only IP addresses assigned to an interface can be designated as primary IPs."
- )
- def save(self, *args, **kwargs):
- ipaddress = super().save(*args, **kwargs)
- # Assign/clear this IPAddress as the primary for the associated Device/VirtualMachine.
- if self.cleaned_data['primary_for_parent']:
- parent = self.cleaned_data['interface'].parent
- if ipaddress.address.version == 4:
- parent.primary_ip4 = ipaddress
- else:
- parent.primary_ip6 = ipaddress
- parent.save()
- elif self.cleaned_data['interface']:
- parent = self.cleaned_data['interface'].parent
- if ipaddress.address.version == 4 and parent.primary_ip4 == ipaddress:
- parent.primary_ip4 = None
- parent.save()
- elif ipaddress.address.version == 6 and parent.primary_ip6 == ipaddress:
- parent.primary_ip6 = None
- parent.save()
- return ipaddress
- class IPAddressBulkCreateForm(BootstrapMixin, forms.Form):
- pattern = ExpandableIPAddressField(
- label='Address pattern'
- )
- class IPAddressBulkAddForm(BootstrapMixin, TenancyForm, CustomFieldForm):
- class Meta:
- model = IPAddress
- fields = [
- 'address', 'vrf', 'status', 'role', 'description', 'tenant_group', 'tenant',
- ]
- widgets = {
- 'status': StaticSelect2(),
- 'role': StaticSelect2(),
- 'vrf': APISelect(
- api_url="/api/ipam/vrfs/"
- )
- }
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.fields['vrf'].empty_label = 'Global'
- class IPAddressCSVForm(forms.ModelForm):
- vrf = forms.ModelChoiceField(
- queryset=VRF.objects.all(),
- required=False,
- to_field_name='rd',
- help_text='Route distinguisher of the assigned VRF',
- 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.',
- }
- )
- status = CSVChoiceField(
- choices=IPADDRESS_STATUS_CHOICES,
- help_text='Operational status'
- )
- role = CSVChoiceField(
- choices=IPADDRESS_ROLE_CHOICES,
- required=False,
- help_text='Functional role'
- )
- device = FlexibleModelChoiceField(
- queryset=Device.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Name or ID of assigned device',
- error_messages={
- 'invalid_choice': 'Device not found.',
- }
- )
- 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.',
- }
- )
- interface_name = forms.CharField(
- help_text='Name of assigned interface',
- required=False
- )
- is_primary = forms.BooleanField(
- help_text='Make this the primary IP for the assigned device',
- required=False
- )
- class Meta:
- model = IPAddress
- fields = IPAddress.csv_headers
- def clean(self):
- super().clean()
- device = self.cleaned_data.get('device')
- virtual_machine = self.cleaned_data.get('virtual_machine')
- interface_name = self.cleaned_data.get('interface_name')
- is_primary = self.cleaned_data.get('is_primary')
- # Validate interface
- if interface_name and device:
- try:
- self.instance.interface = Interface.objects.get(device=device, name=interface_name)
- except Interface.DoesNotExist:
- 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))
- # Validate is_primary
- if is_primary and not device and not virtual_machine:
- raise forms.ValidationError("No device or virtual machine specified; cannot set as primary IP")
- def save(self, *args, **kwargs):
- # Set interface
- if self.cleaned_data['device'] and self.cleaned_data['interface_name']:
- self.instance.interface = Interface.objects.get(
- device=self.cleaned_data['device'],
- name=self.cleaned_data['interface_name']
- )
- 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']
- )
- ipaddress = super().save(*args, **kwargs)
- # Set as primary for device/VM
- if self.cleaned_data['is_primary']:
- parent = self.cleaned_data['device'] or self.cleaned_data['virtual_machine']
- if self.instance.address.version == 4:
- parent.primary_ip4 = ipaddress
- elif self.instance.address.version == 6:
- parent.primary_ip6 = ipaddress
- parent.save()
- return ipaddress
- class IPAddressBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
- pk = forms.ModelMultipleChoiceField(
- queryset=IPAddress.objects.all(),
- widget=forms.MultipleHiddenInput()
- )
- vrf = forms.ModelChoiceField(
- queryset=VRF.objects.all(),
- required=False,
- label='VRF',
- widget=APISelect(
- api_url="/api/ipam/vrfs/"
- )
- )
- mask_length = forms.IntegerField(
- min_value=1,
- max_value=128,
- required=False
- )
- tenant = forms.ModelChoiceField(
- queryset=Tenant.objects.all(),
- required=False,
- widget=APISelect(
- api_url="/api/tenancy/tenants/"
- )
- )
- status = forms.ChoiceField(
- choices=add_blank_choice(IPADDRESS_STATUS_CHOICES),
- required=False,
- widget=StaticSelect2()
- )
- role = forms.ChoiceField(
- choices=add_blank_choice(IPADDRESS_ROLE_CHOICES),
- required=False,
- widget=StaticSelect2()
- )
- description = forms.CharField(
- max_length=100, required=False
- )
- class Meta:
- nullable_fields = [
- 'vrf', 'role', 'tenant', 'description',
- ]
- class IPAddressAssignForm(BootstrapMixin, forms.Form):
- vrf = forms.ModelChoiceField(
- queryset=VRF.objects.all(),
- required=False,
- label='VRF',
- empty_label='Global',
- widget=APISelect(
- api_url="/api/ipam/vrfs/"
- )
- )
- address = forms.CharField(
- label='IP Address'
- )
- class IPAddressFilterForm(BootstrapMixin, CustomFieldFilterForm):
- model = IPAddress
- 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,
- label='Address family',
- widget=StaticSelect2()
- )
- mask_length = forms.ChoiceField(
- required=False,
- choices=IPADDRESS_MASK_LENGTH_CHOICES,
- label='Mask length',
- widget=StaticSelect2()
- )
- vrf = FilterChoiceField(
- queryset=VRF.objects.all(),
- to_field_name='rd',
- label='VRF',
- null_label='-- Global --',
- widget=APISelectMultiple(
- api_url="/api/ipam/vrfs/",
- value_field="slug",
- null_option=True,
- )
- )
- tenant = FilterChoiceField(
- queryset=Tenant.objects.all(),
- to_field_name='slug',
- null_label='-- None --',
- widget=APISelectMultiple(
- api_url="/api/tenancy/tenants/",
- value_field="slug",
- null_option=True,
- )
- )
- status = forms.MultipleChoiceField(
- choices=IPADDRESS_STATUS_CHOICES,
- required=False,
- widget=StaticSelect2Multiple()
- )
- role = forms.MultipleChoiceField(
- choices=IPADDRESS_ROLE_CHOICES,
- required=False,
- widget=StaticSelect2Multiple()
- )
- #
- # VLAN groups
- #
- class VLANGroupForm(BootstrapMixin, forms.ModelForm):
- slug = SlugField()
- class Meta:
- model = VLANGroup
- fields = [
- 'site', 'name', 'slug',
- ]
- widgets = {
- 'site': APISelect(
- api_url="/api/dcim/sites/"
- )
- }
- 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
- fields = VLANGroup.csv_headers
- help_texts = {
- 'name': 'Name of VLAN group',
- }
- class VLANGroupFilterForm(BootstrapMixin, forms.Form):
- site = FilterChoiceField(
- queryset=Site.objects.all(),
- to_field_name='slug',
- null_label='-- Global --',
- widget=APISelectMultiple(
- api_url="/api/dcim/sites/",
- value_field="slug",
- null_option=True,
- )
- )
- #
- # VLANs
- #
- class VLANForm(BootstrapMixin, TenancyForm, CustomFieldForm):
- site = forms.ModelChoiceField(
- queryset=Site.objects.all(),
- required=False,
- widget=APISelect(
- api_url="/api/dcim/sites/",
- filter_for={
- 'group': 'site_id'
- },
- attrs={
- 'nullable': 'true',
- }
- )
- )
- group = ChainedModelChoiceField(
- queryset=VLANGroup.objects.all(),
- chains=(
- ('site', 'site'),
- ),
- required=False,
- label='Group',
- widget=APISelect(
- api_url='/api/ipam/vlan-groups/',
- )
- )
- tags = TagField(required=False)
- class Meta:
- model = VLAN
- fields = [
- 'site', 'group', 'vid', 'name', 'status', 'role', 'description', 'tenant_group', 'tenant', 'tags',
- ]
- help_texts = {
- 'site': "Leave blank if this VLAN spans multiple sites",
- 'group': "VLAN group (optional)",
- 'vid': "Configured VLAN ID",
- 'name': "Configured VLAN name",
- 'status': "Operational status of this VLAN",
- 'role': "The primary function of this VLAN",
- }
- widgets = {
- 'status': StaticSelect2(),
- 'role': APISelect(
- api_url="/api/ipam/roles/"
- )
- }
- class VLANCSVForm(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.',
- }
- )
- group_name = forms.CharField(
- help_text='Name of VLAN group',
- required=False
- )
- tenant = forms.ModelChoiceField(
- queryset=Tenant.objects.all(),
- to_field_name='name',
- required=False,
- help_text='Name of assigned tenant',
- error_messages={
- 'invalid_choice': 'Tenant not found.',
- }
- )
- status = CSVChoiceField(
- choices=VLAN_STATUS_CHOICES,
- help_text='Operational status'
- )
- role = forms.ModelChoiceField(
- queryset=Role.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Functional role',
- error_messages={
- 'invalid_choice': 'Invalid role.',
- }
- )
- class Meta:
- model = VLAN
- fields = VLAN.csv_headers
- help_texts = {
- 'vid': 'Numeric VLAN ID (1-4095)',
- 'name': 'VLAN name',
- }
- def clean(self):
- super().clean()
- site = self.cleaned_data.get('site')
- group_name = self.cleaned_data.get('group_name')
- # Validate VLAN group
- if group_name:
- try:
- self.instance.group = VLANGroup.objects.get(site=site, name=group_name)
- except VLANGroup.DoesNotExist:
- if site:
- raise forms.ValidationError(
- "VLAN group {} not found for site {}".format(group_name, site)
- )
- else:
- raise forms.ValidationError(
- "Global VLAN group {} not found".format(group_name)
- )
- class VLANBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
- pk = forms.ModelMultipleChoiceField(
- queryset=VLAN.objects.all(),
- widget=forms.MultipleHiddenInput()
- )
- site = forms.ModelChoiceField(
- queryset=Site.objects.all(),
- required=False,
- widget=APISelect(
- api_url="/api/dcim/sites/"
- )
- )
- group = forms.ModelChoiceField(
- queryset=VLANGroup.objects.all(),
- required=False,
- widget=APISelect(
- api_url="/api/ipam/vlan-groups/"
- )
- )
- tenant = forms.ModelChoiceField(
- queryset=Tenant.objects.all(),
- required=False,
- widget=APISelect(
- api_url="/api/tenancy/tenants/"
- )
- )
- status = forms.ChoiceField(
- choices=add_blank_choice(VLAN_STATUS_CHOICES),
- required=False,
- widget=StaticSelect2()
- )
- role = forms.ModelChoiceField(
- queryset=Role.objects.all(),
- required=False,
- widget=APISelect(
- api_url="/api/ipam/roles/"
- )
- )
- description = forms.CharField(
- max_length=100,
- required=False
- )
- class Meta:
- nullable_fields = [
- 'site', 'group', 'tenant', 'role', 'description',
- ]
- class VLANFilterForm(BootstrapMixin, CustomFieldFilterForm):
- model = VLAN
- q = forms.CharField(
- required=False,
- label='Search'
- )
- site = FilterChoiceField(
- queryset=Site.objects.all(),
- to_field_name='slug',
- null_label='-- Global --',
- widget=APISelectMultiple(
- api_url="/api/dcim/sites/",
- value_field="slug",
- null_option=True,
- )
- )
- group_id = FilterChoiceField(
- queryset=VLANGroup.objects.all(),
- label='VLAN group',
- null_label='-- None --',
- widget=APISelectMultiple(
- api_url="/api/ipam/vlan-groups/",
- null_option=True,
- )
- )
- tenant = FilterChoiceField(
- queryset=Tenant.objects.all(),
- to_field_name='slug',
- null_label='-- None --',
- widget=APISelectMultiple(
- api_url="/api/tenancy/tenants/",
- value_field="slug",
- null_option=True,
- )
- )
- status = forms.MultipleChoiceField(
- choices=VLAN_STATUS_CHOICES,
- required=False,
- widget=StaticSelect2Multiple()
- )
- role = FilterChoiceField(
- queryset=Role.objects.all(),
- to_field_name='slug',
- null_label='-- None --',
- widget=APISelectMultiple(
- api_url="/api/ipam/roles/",
- value_field="slug",
- null_option=True,
- )
- )
- #
- # Services
- #
- class ServiceForm(BootstrapMixin, CustomFieldForm):
- tags = TagField(
- required=False
- )
- class Meta:
- model = Service
- fields = [
- 'name', 'protocol', 'port', 'ipaddresses', 'description', 'tags',
- ]
- 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.",
- }
- widgets = {
- 'protocol': StaticSelect2(),
- 'ipaddresses': StaticSelect2Multiple(),
- }
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- # Limit IP address choices to those assigned to interfaces of the parent device/VM
- if self.instance.device:
- vc_interface_ids = [i['id'] for i in self.instance.device.vc_interfaces.values('id')]
- self.fields['ipaddresses'].queryset = IPAddress.objects.filter(
- interface_id__in=vc_interface_ids
- )
- elif self.instance.virtual_machine:
- self.fields['ipaddresses'].queryset = IPAddress.objects.filter(
- interface__virtual_machine=self.instance.virtual_machine
- )
- else:
- self.fields['ipaddresses'].choices = []
- class ServiceFilterForm(BootstrapMixin, CustomFieldFilterForm):
- model = Service
- q = forms.CharField(
- required=False,
- label='Search'
- )
- protocol = forms.ChoiceField(
- choices=add_blank_choice(IP_PROTOCOL_CHOICES),
- required=False,
- widget=StaticSelect2Multiple()
- )
- port = forms.IntegerField(
- required=False,
- )
- class ServiceBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
- pk = forms.ModelMultipleChoiceField(
- queryset=Service.objects.all(),
- widget=forms.MultipleHiddenInput()
- )
- protocol = forms.ChoiceField(
- choices=add_blank_choice(IP_PROTOCOL_CHOICES),
- required=False,
- widget=StaticSelect2()
- )
- port = forms.IntegerField(
- validators=[
- MinValueValidator(1),
- MaxValueValidator(65535),
- ],
- required=False
- )
- description = forms.CharField(
- max_length=100,
- required=False
- )
- class Meta:
- nullable_fields = [
- 'site', 'tenant', 'role', 'description',
- ]
|