123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935 |
- from __future__ import unicode_literals
- from django import forms
- from django.core.exceptions import MultipleObjectsReturned
- from django.db.models import Count
- from dcim.models import Site, Rack, Device, Interface
- from extras.forms import CustomFieldForm, CustomFieldBulkEditForm, CustomFieldFilterForm
- from tenancy.forms import TenancyForm
- from tenancy.models import Tenant
- from utilities.forms import (
- AnnotatedMultipleChoiceField, APISelect, BootstrapMixin, BulkEditNullBooleanSelect, ChainedModelChoiceField,
- CSVChoiceField, ExpandableIPAddressField, FilterChoiceField, FlexibleModelChoiceField, Livesearch, ReturnURLForm,
- SlugField, add_blank_choice,
- )
- from virtualization.models import VirtualMachine
- from .constants import 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):
- class Meta:
- model = VRF
- fields = ['name', 'rd', 'enforce_unique', 'description', 'tenant_group', 'tenant']
- 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, CustomFieldBulkEditForm):
- pk = forms.ModelMultipleChoiceField(queryset=VRF.objects.all(), widget=forms.MultipleHiddenInput)
- tenant = forms.ModelChoiceField(queryset=Tenant.objects.all(), required=False)
- 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.annotate(filter_count=Count('vrfs')),
- to_field_name='slug',
- null_label='-- None --'
- )
- #
- # 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=forms.Select(choices=[
- ('', '---------'),
- ('True', 'Yes'),
- ('False', 'No'),
- ]))
- #
- # Aggregates
- #
- class AggregateForm(BootstrapMixin, CustomFieldForm):
- class Meta:
- model = Aggregate
- fields = ['prefix', 'rir', 'date_added', 'description']
- help_texts = {
- 'prefix': "IPv4 or IPv6 network",
- 'rir': "Regional Internet Registry responsible for this prefix",
- 'date_added': "Format: YYYY-MM-DD",
- }
- 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, CustomFieldBulkEditForm):
- pk = forms.ModelMultipleChoiceField(queryset=Aggregate.objects.all(), widget=forms.MultipleHiddenInput)
- rir = forms.ModelChoiceField(queryset=RIR.objects.all(), required=False, label='RIR')
- 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')
- rir = FilterChoiceField(
- queryset=RIR.objects.annotate(filter_count=Count('aggregates')),
- to_field_name='slug',
- label='RIR'
- )
- #
- # 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=forms.Select(
- attrs={'filter-for': 'vlan_group', '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/?site_id={{site}}',
- attrs={'filter-for': 'vlan', '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/?site_id={{site}}&group_id={{vlan_group}}', display_field='display_name'
- )
- )
- class Meta:
- model = Prefix
- fields = ['prefix', 'vrf', 'site', 'vlan', 'status', 'role', 'is_pool', 'description', 'tenant_group', 'tenant']
- 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(PrefixForm, self).__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(PrefixCSVForm, self).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, CustomFieldBulkEditForm):
- pk = forms.ModelMultipleChoiceField(queryset=Prefix.objects.all(), widget=forms.MultipleHiddenInput)
- site = forms.ModelChoiceField(queryset=Site.objects.all(), required=False)
- vrf = forms.ModelChoiceField(queryset=VRF.objects.all(), required=False, label='VRF')
- tenant = forms.ModelChoiceField(queryset=Tenant.objects.all(), required=False)
- status = forms.ChoiceField(choices=add_blank_choice(PREFIX_STATUS_CHOICES), required=False)
- role = forms.ModelChoiceField(queryset=Role.objects.all(), required=False)
- 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, label='Search within', widget=forms.TextInput(attrs={
- 'placeholder': 'Prefix',
- }))
- family = forms.ChoiceField(required=False, choices=IP_FAMILY_CHOICES, label='Address family')
- mask_length = forms.ChoiceField(required=False, choices=PREFIX_MASK_LENGTH_CHOICES, label='Mask length')
- vrf = FilterChoiceField(
- queryset=VRF.objects.annotate(filter_count=Count('prefixes')),
- to_field_name='rd',
- label='VRF',
- null_label='-- Global --'
- )
- tenant = FilterChoiceField(
- queryset=Tenant.objects.annotate(filter_count=Count('prefixes')),
- to_field_name='slug',
- null_label='-- None --'
- )
- status = AnnotatedMultipleChoiceField(
- choices=PREFIX_STATUS_CHOICES,
- annotate=Prefix.objects.all(),
- annotate_field='status',
- required=False
- )
- site = FilterChoiceField(
- queryset=Site.objects.annotate(filter_count=Count('prefixes')),
- to_field_name='slug',
- null_label='-- None --'
- )
- role = FilterChoiceField(
- queryset=Role.objects.annotate(filter_count=Count('prefixes')),
- to_field_name='slug',
- null_label='-- None --'
- )
- 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=forms.Select(
- attrs={'filter-for': 'nat_rack'}
- )
- )
- nat_rack = ChainedModelChoiceField(
- queryset=Rack.objects.all(),
- chains=(
- ('site', 'nat_site'),
- ),
- required=False,
- label='Rack',
- widget=APISelect(
- api_url='/api/dcim/racks/?site_id={{nat_site}}',
- display_field='display_name',
- attrs={'filter-for': 'nat_device', '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/?site_id={{nat_site}}&rack_id={{nat_rack}}',
- display_field='display_name',
- attrs={'filter-for': 'nat_inside'}
- )
- )
- 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/?device_id={{nat_device}}',
- display_field='address'
- )
- )
- livesearch = forms.CharField(
- required=False,
- label='Search',
- widget=Livesearch(
- query_key='q',
- query_url='ipam-api:ipaddress-list',
- field_to_update='nat_inside',
- obj_label='address'
- )
- )
- primary_for_parent = forms.BooleanField(required=False, label='Make this the primary IP for the device/VM')
- class Meta:
- model = IPAddress
- fields = [
- 'address', 'vrf', 'status', 'role', 'description', 'interface', 'primary_for_parent', 'nat_site',
- 'nat_rack', 'nat_inside', 'tenant_group', 'tenant',
- ]
- 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(IPAddressForm, self).__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(IPAddressForm, self).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(IPAddressForm, self).save(*args, **kwargs)
- # Assign this IPAddress as the primary for the associated Device.
- 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()
- # Clear assignment as primary for device if set.
- elif self.cleaned_data['interface']:
- parent = self.cleaned_data['interface'].parent
- if ipaddress.address.version == 4 and parent.primary_ip4 == self:
- parent.primary_ip4 = None
- parent.save()
- elif ipaddress.address.version == 6 and parent.primary_ip6 == self:
- 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']
- def __init__(self, *args, **kwargs):
- super(IPAddressBulkAddForm, self).__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(IPAddressCSVForm, self).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(IPAddressCSVForm, self).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, CustomFieldBulkEditForm):
- pk = forms.ModelMultipleChoiceField(queryset=IPAddress.objects.all(), widget=forms.MultipleHiddenInput)
- vrf = forms.ModelChoiceField(queryset=VRF.objects.all(), required=False, label='VRF')
- tenant = forms.ModelChoiceField(queryset=Tenant.objects.all(), required=False)
- status = forms.ChoiceField(choices=add_blank_choice(IPADDRESS_STATUS_CHOICES), required=False)
- role = forms.ChoiceField(choices=add_blank_choice(IPADDRESS_ROLE_CHOICES), required=False)
- 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')
- address = forms.CharField(label='IP Address')
- class IPAddressFilterForm(BootstrapMixin, CustomFieldFilterForm):
- model = IPAddress
- q = forms.CharField(required=False, label='Search')
- parent = forms.CharField(required=False, label='Parent Prefix', widget=forms.TextInput(attrs={
- 'placeholder': 'Prefix',
- }))
- family = forms.ChoiceField(required=False, choices=IP_FAMILY_CHOICES, label='Address family')
- mask_length = forms.ChoiceField(required=False, choices=IPADDRESS_MASK_LENGTH_CHOICES, label='Mask length')
- vrf = FilterChoiceField(
- queryset=VRF.objects.annotate(filter_count=Count('ip_addresses')),
- to_field_name='rd',
- label='VRF',
- null_label='-- Global --'
- )
- tenant = FilterChoiceField(
- queryset=Tenant.objects.annotate(filter_count=Count('ip_addresses')),
- to_field_name='slug',
- null_label='-- None --'
- )
- status = AnnotatedMultipleChoiceField(
- choices=IPADDRESS_STATUS_CHOICES,
- annotate=IPAddress.objects.all(),
- annotate_field='status',
- required=False
- )
- role = AnnotatedMultipleChoiceField(
- choices=IPADDRESS_ROLE_CHOICES,
- annotate=IPAddress.objects.all(),
- annotate_field='role',
- required=False
- )
- #
- # VLAN groups
- #
- class VLANGroupForm(BootstrapMixin, forms.ModelForm):
- slug = SlugField()
- class Meta:
- model = VLANGroup
- fields = ['site', 'name', 'slug']
- 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.annotate(filter_count=Count('vlan_groups')),
- to_field_name='slug',
- null_label='-- Global --'
- )
- #
- # VLANs
- #
- class VLANForm(BootstrapMixin, TenancyForm, CustomFieldForm):
- site = forms.ModelChoiceField(
- queryset=Site.objects.all(),
- required=False,
- widget=forms.Select(
- attrs={'filter-for': 'group', 'nullable': 'true'}
- )
- )
- group = ChainedModelChoiceField(
- queryset=VLANGroup.objects.all(),
- chains=(
- ('site', 'site'),
- ),
- required=False,
- label='Group',
- widget=APISelect(
- api_url='/api/ipam/vlan-groups/?site_id={{site}}',
- )
- )
- class Meta:
- model = VLAN
- fields = ['site', 'group', 'vid', 'name', 'status', 'role', 'description', 'tenant_group', 'tenant']
- 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",
- }
- 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(VLANCSVForm, self).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, CustomFieldBulkEditForm):
- pk = forms.ModelMultipleChoiceField(queryset=VLAN.objects.all(), widget=forms.MultipleHiddenInput)
- site = forms.ModelChoiceField(queryset=Site.objects.all(), required=False)
- group = forms.ModelChoiceField(queryset=VLANGroup.objects.all(), required=False)
- tenant = forms.ModelChoiceField(queryset=Tenant.objects.all(), required=False)
- status = forms.ChoiceField(choices=add_blank_choice(VLAN_STATUS_CHOICES), required=False)
- role = forms.ModelChoiceField(queryset=Role.objects.all(), required=False)
- 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.annotate(filter_count=Count('vlans')),
- to_field_name='slug',
- null_label='-- Global --'
- )
- group_id = FilterChoiceField(
- queryset=VLANGroup.objects.annotate(filter_count=Count('vlans')),
- label='VLAN group',
- null_label='-- None --'
- )
- tenant = FilterChoiceField(
- queryset=Tenant.objects.annotate(filter_count=Count('vlans')),
- to_field_name='slug',
- null_label='-- None --'
- )
- status = AnnotatedMultipleChoiceField(
- choices=VLAN_STATUS_CHOICES,
- annotate=VLAN.objects.all(),
- annotate_field='status',
- required=False
- )
- role = FilterChoiceField(
- queryset=Role.objects.annotate(filter_count=Count('vlans')),
- to_field_name='slug',
- null_label='-- None --'
- )
- #
- # Services
- #
- class ServiceForm(BootstrapMixin, forms.ModelForm):
- class Meta:
- model = Service
- fields = ['name', 'protocol', 'port', 'ipaddresses', 'description']
- 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.",
- }
- def __init__(self, *args, **kwargs):
- super(ServiceForm, self).__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 = []
|