| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- from django import forms
- from django.contrib.contenttypes.models import ContentType
- from django.core.exceptions import ValidationError
- from dcim.models import Device, Interface, Site
- from ipam.choices import *
- from ipam.constants import *
- from ipam.models import *
- from netbox.forms import NetBoxModelCSVForm
- from tenancy.models import Tenant
- from utilities.forms import CSVChoiceField, CSVContentTypeField, CSVModelChoiceField, SlugField
- from virtualization.models import VirtualMachine, VMInterface
- __all__ = (
- 'AggregateCSVForm',
- 'ASNCSVForm',
- 'FHRPGroupCSVForm',
- 'IPAddressCSVForm',
- 'IPRangeCSVForm',
- 'L2VPNCSVForm',
- 'L2VPNTerminationCSVForm',
- 'PrefixCSVForm',
- 'RIRCSVForm',
- 'RoleCSVForm',
- 'RouteTargetCSVForm',
- 'ServiceCSVForm',
- 'ServiceTemplateCSVForm',
- 'VLANCSVForm',
- 'VLANGroupCSVForm',
- 'VRFCSVForm',
- )
- class VRFCSVForm(NetBoxModelCSVForm):
- tenant = CSVModelChoiceField(
- queryset=Tenant.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Assigned tenant'
- )
- class Meta:
- model = VRF
- fields = ('name', 'rd', 'tenant', 'enforce_unique', 'description', 'comments', 'tags')
- class RouteTargetCSVForm(NetBoxModelCSVForm):
- tenant = CSVModelChoiceField(
- queryset=Tenant.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Assigned tenant'
- )
- class Meta:
- model = RouteTarget
- fields = ('name', 'tenant', 'description', 'comments', 'tags')
- class RIRCSVForm(NetBoxModelCSVForm):
- slug = SlugField()
- class Meta:
- model = RIR
- fields = ('name', 'slug', 'is_private', 'description', 'tags')
- help_texts = {
- 'name': 'RIR name',
- }
- class AggregateCSVForm(NetBoxModelCSVForm):
- rir = CSVModelChoiceField(
- queryset=RIR.objects.all(),
- to_field_name='name',
- help_text='Assigned RIR'
- )
- tenant = CSVModelChoiceField(
- queryset=Tenant.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Assigned tenant'
- )
- class Meta:
- model = Aggregate
- fields = ('prefix', 'rir', 'tenant', 'date_added', 'description', 'comments', 'tags')
- class ASNCSVForm(NetBoxModelCSVForm):
- rir = CSVModelChoiceField(
- queryset=RIR.objects.all(),
- to_field_name='name',
- help_text='Assigned RIR'
- )
- tenant = CSVModelChoiceField(
- queryset=Tenant.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Assigned tenant'
- )
- class Meta:
- model = ASN
- fields = ('asn', 'rir', 'tenant', 'description', 'comments', 'tags')
- class RoleCSVForm(NetBoxModelCSVForm):
- slug = SlugField()
- class Meta:
- model = Role
- fields = ('name', 'slug', 'weight', 'description', 'tags')
- class PrefixCSVForm(NetBoxModelCSVForm):
- vrf = CSVModelChoiceField(
- queryset=VRF.objects.all(),
- to_field_name='name',
- required=False,
- help_text='Assigned VRF'
- )
- tenant = CSVModelChoiceField(
- queryset=Tenant.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Assigned tenant'
- )
- site = CSVModelChoiceField(
- queryset=Site.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Assigned site'
- )
- vlan_group = CSVModelChoiceField(
- queryset=VLANGroup.objects.all(),
- required=False,
- to_field_name='name',
- help_text="VLAN's group (if any)"
- )
- vlan = CSVModelChoiceField(
- queryset=VLAN.objects.all(),
- required=False,
- to_field_name='vid',
- help_text="Assigned VLAN"
- )
- status = CSVChoiceField(
- choices=PrefixStatusChoices,
- help_text='Operational status'
- )
- role = CSVModelChoiceField(
- queryset=Role.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Functional role'
- )
- class Meta:
- model = Prefix
- fields = (
- 'prefix', 'vrf', 'tenant', 'site', 'vlan_group', 'vlan', 'status', 'role', 'is_pool', 'mark_utilized',
- 'description', 'comments', 'tags',
- )
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit VLAN queryset by assigned site and/or group (if specified)
- params = {}
- if data.get('site'):
- params[f"site__{self.fields['site'].to_field_name}"] = data.get('site')
- if data.get('vlan_group'):
- params[f"group__{self.fields['vlan_group'].to_field_name}"] = data.get('vlan_group')
- if params:
- self.fields['vlan'].queryset = self.fields['vlan'].queryset.filter(**params)
- class IPRangeCSVForm(NetBoxModelCSVForm):
- vrf = CSVModelChoiceField(
- queryset=VRF.objects.all(),
- to_field_name='name',
- required=False,
- help_text='Assigned VRF'
- )
- tenant = CSVModelChoiceField(
- queryset=Tenant.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Assigned tenant'
- )
- status = CSVChoiceField(
- choices=IPRangeStatusChoices,
- help_text='Operational status'
- )
- role = CSVModelChoiceField(
- queryset=Role.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Functional role'
- )
- class Meta:
- model = IPRange
- fields = (
- 'start_address', 'end_address', 'vrf', 'tenant', 'status', 'role', 'description', 'comments', 'tags',
- )
- class IPAddressCSVForm(NetBoxModelCSVForm):
- vrf = CSVModelChoiceField(
- queryset=VRF.objects.all(),
- to_field_name='name',
- required=False,
- help_text='Assigned VRF'
- )
- tenant = CSVModelChoiceField(
- queryset=Tenant.objects.all(),
- to_field_name='name',
- required=False,
- help_text='Assigned tenant'
- )
- status = CSVChoiceField(
- choices=IPAddressStatusChoices,
- help_text='Operational status'
- )
- role = CSVChoiceField(
- choices=IPAddressRoleChoices,
- required=False,
- help_text='Functional role'
- )
- 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'
- )
- is_primary = forms.BooleanField(
- help_text='Make this the primary IP for the assigned device',
- required=False
- )
- class Meta:
- model = IPAddress
- fields = [
- 'address', 'vrf', 'tenant', 'status', 'role', 'device', 'virtual_machine', 'interface', 'is_primary',
- 'dns_name', 'description', 'comments', 'tags',
- ]
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- 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']}
- )
- def clean(self):
- super().clean()
- device = self.cleaned_data.get('device')
- virtual_machine = self.cleaned_data.get('virtual_machine')
- interface = self.cleaned_data.get('interface')
- is_primary = self.cleaned_data.get('is_primary')
- # Validate is_primary
- if is_primary and not device and not virtual_machine:
- raise forms.ValidationError({
- "is_primary": "No device or virtual machine specified; cannot set as primary IP"
- })
- if is_primary and not interface:
- raise forms.ValidationError({
- "is_primary": "No interface specified; cannot set as primary IP"
- })
- def save(self, *args, **kwargs):
- # Set interface assignment
- if self.cleaned_data.get('interface'):
- self.instance.assigned_object = self.cleaned_data['interface']
- ipaddress = super().save(*args, **kwargs)
- # Set as primary for device/VM
- if self.cleaned_data.get('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 FHRPGroupCSVForm(NetBoxModelCSVForm):
- protocol = CSVChoiceField(
- choices=FHRPGroupProtocolChoices
- )
- auth_type = CSVChoiceField(
- choices=FHRPGroupAuthTypeChoices,
- required=False
- )
- class Meta:
- model = FHRPGroup
- fields = ('protocol', 'group_id', 'auth_type', 'auth_key', 'name', 'description', 'comments', 'tags')
- class VLANGroupCSVForm(NetBoxModelCSVForm):
- slug = SlugField()
- scope_type = CSVContentTypeField(
- queryset=ContentType.objects.filter(model__in=VLANGROUP_SCOPE_TYPES),
- required=False,
- label='Scope type (app & model)'
- )
- min_vid = forms.IntegerField(
- min_value=VLAN_VID_MIN,
- max_value=VLAN_VID_MAX,
- required=False,
- label=f'Minimum child VLAN VID (default: {VLAN_VID_MIN})'
- )
- max_vid = forms.IntegerField(
- min_value=VLAN_VID_MIN,
- max_value=VLAN_VID_MAX,
- required=False,
- label=f'Maximum child VLAN VID (default: {VLAN_VID_MIN})'
- )
- class Meta:
- model = VLANGroup
- fields = ('name', 'slug', 'scope_type', 'scope_id', 'min_vid', 'max_vid', 'description', 'tags')
- labels = {
- 'scope_id': 'Scope ID',
- }
- class VLANCSVForm(NetBoxModelCSVForm):
- site = CSVModelChoiceField(
- queryset=Site.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Assigned site'
- )
- group = CSVModelChoiceField(
- queryset=VLANGroup.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Assigned VLAN group'
- )
- tenant = CSVModelChoiceField(
- queryset=Tenant.objects.all(),
- to_field_name='name',
- required=False,
- help_text='Assigned tenant'
- )
- status = CSVChoiceField(
- choices=VLANStatusChoices,
- help_text='Operational status'
- )
- role = CSVModelChoiceField(
- queryset=Role.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Functional role'
- )
- class Meta:
- model = VLAN
- fields = ('site', 'group', 'vid', 'name', 'tenant', 'status', 'role', 'description', 'comments', 'tags')
- help_texts = {
- 'vid': 'Numeric VLAN ID (1-4094)',
- 'name': 'VLAN name',
- }
- class ServiceTemplateCSVForm(NetBoxModelCSVForm):
- protocol = CSVChoiceField(
- choices=ServiceProtocolChoices,
- help_text='IP protocol'
- )
- class Meta:
- model = ServiceTemplate
- fields = ('name', 'protocol', 'ports', 'description', 'comments', 'tags')
- class ServiceCSVForm(NetBoxModelCSVForm):
- device = CSVModelChoiceField(
- queryset=Device.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Required if not assigned to a VM'
- )
- virtual_machine = CSVModelChoiceField(
- queryset=VirtualMachine.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Required if not assigned to a device'
- )
- protocol = CSVChoiceField(
- choices=ServiceProtocolChoices,
- help_text='IP protocol'
- )
- class Meta:
- model = Service
- fields = ('device', 'virtual_machine', 'name', 'protocol', 'ports', 'description', 'comments', 'tags')
- class L2VPNCSVForm(NetBoxModelCSVForm):
- tenant = CSVModelChoiceField(
- queryset=Tenant.objects.all(),
- required=False,
- to_field_name='name',
- )
- type = CSVChoiceField(
- choices=L2VPNTypeChoices,
- help_text='L2VPN type'
- )
- class Meta:
- model = L2VPN
- fields = ('identifier', 'name', 'slug', 'type', 'description', 'comments', 'tags')
- class L2VPNTerminationCSVForm(NetBoxModelCSVForm):
- l2vpn = CSVModelChoiceField(
- queryset=L2VPN.objects.all(),
- required=True,
- to_field_name='name',
- label='L2VPN',
- )
- device = CSVModelChoiceField(
- queryset=Device.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Parent device (for interface)'
- )
- virtual_machine = CSVModelChoiceField(
- queryset=VirtualMachine.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Parent virtual machine (for interface)'
- )
- interface = CSVModelChoiceField(
- queryset=Interface.objects.none(), # Can also refer to VMInterface
- required=False,
- to_field_name='name',
- help_text='Assigned interface (device or VM)'
- )
- vlan = CSVModelChoiceField(
- queryset=VLAN.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Assigned VLAN'
- )
- class Meta:
- model = L2VPNTermination
- fields = ('l2vpn', 'device', 'virtual_machine', 'interface', 'vlan', 'tags')
- def __init__(self, data=None, *args, **kwargs):
- super().__init__(data, *args, **kwargs)
- if data:
- # Limit interface queryset by device or VM
- if data.get('device'):
- self.fields['interface'].queryset = Interface.objects.filter(
- **{f"device__{self.fields['device'].to_field_name}": data['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']}
- )
- def clean(self):
- super().clean()
- if self.cleaned_data.get('device') and self.cleaned_data.get('virtual_machine'):
- raise ValidationError('Cannot import device and VM interface terminations simultaneously.')
- if not (self.cleaned_data.get('interface') or self.cleaned_data.get('vlan')):
- raise ValidationError('Each termination must specify either an interface or a VLAN.')
- if self.cleaned_data.get('interface') and self.cleaned_data.get('vlan'):
- raise ValidationError('Cannot assign both an interface and a VLAN.')
- self.instance.assigned_object = self.cleaned_data.get('interface') or self.cleaned_data.get('vlan')
|