1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00
Files
netbox-community-netbox/netbox/ipam/forms/bulk_import.py

529 lines
16 KiB
Python
Raw Normal View History

2021-09-28 09:25:12 -04:00
from django import forms
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import gettext_lazy as _
2021-09-28 09:25:12 -04:00
from dcim.models import Device, Interface, Site
from ipam.choices import *
from ipam.constants import *
from ipam.models import *
from netbox.forms import NetBoxModelImportForm
2021-09-28 09:25:12 -04:00
from tenancy.models import Tenant
from utilities.forms.fields import (
CSVChoiceField, CSVContentTypeField, CSVModelChoiceField, CSVModelMultipleChoiceField, SlugField
)
2021-09-28 09:25:12 -04:00
from virtualization.models import VirtualMachine, VMInterface
__all__ = (
'AggregateImportForm',
'ASNImportForm',
'ASNRangeImportForm',
'FHRPGroupImportForm',
'IPAddressImportForm',
'IPRangeImportForm',
'PrefixImportForm',
'RIRImportForm',
'RoleImportForm',
'RouteTargetImportForm',
'ServiceImportForm',
'ServiceTemplateImportForm',
'VLANImportForm',
'VLANGroupImportForm',
'VRFImportForm',
2021-09-28 09:25:12 -04:00
)
class VRFImportForm(NetBoxModelImportForm):
2021-09-28 09:25:12 -04:00
tenant = CSVModelChoiceField(
label=_('Tenant'),
2021-09-28 09:25:12 -04:00
queryset=Tenant.objects.all(),
required=False,
to_field_name='name',
help_text=_('Assigned tenant')
2021-09-28 09:25:12 -04:00
)
import_targets = CSVModelMultipleChoiceField(
queryset=RouteTarget.objects.all(),
required=False,
to_field_name='name',
help_text=_('Import route targets')
)
export_targets = CSVModelMultipleChoiceField(
queryset=RouteTarget.objects.all(),
required=False,
to_field_name='name',
help_text=_('Export route targets')
)
2021-09-28 09:25:12 -04:00
class Meta:
model = VRF
fields = (
'name', 'rd', 'tenant', 'enforce_unique', 'description', 'import_targets', 'export_targets', 'comments',
'tags',
)
2021-09-28 09:25:12 -04:00
class RouteTargetImportForm(NetBoxModelImportForm):
2021-09-28 09:25:12 -04:00
tenant = CSVModelChoiceField(
label=_('Tenant'),
2021-09-28 09:25:12 -04:00
queryset=Tenant.objects.all(),
required=False,
to_field_name='name',
help_text=_('Assigned tenant')
2021-09-28 09:25:12 -04:00
)
class Meta:
model = RouteTarget
fields = ('name', 'tenant', 'description', 'comments', 'tags')
2021-09-28 09:25:12 -04:00
class RIRImportForm(NetBoxModelImportForm):
2021-09-28 09:25:12 -04:00
slug = SlugField()
class Meta:
model = RIR
fields = ('name', 'slug', 'is_private', 'description', 'tags')
2021-09-28 09:25:12 -04:00
class AggregateImportForm(NetBoxModelImportForm):
2021-09-28 09:25:12 -04:00
rir = CSVModelChoiceField(
label=_('RIR'),
2021-09-28 09:25:12 -04:00
queryset=RIR.objects.all(),
to_field_name='name',
help_text=_('Assigned RIR')
2021-09-28 09:25:12 -04:00
)
tenant = CSVModelChoiceField(
label=_('Tenant'),
2021-09-28 09:25:12 -04:00
queryset=Tenant.objects.all(),
required=False,
to_field_name='name',
help_text=_('Assigned tenant')
2021-09-28 09:25:12 -04:00
)
class Meta:
model = Aggregate
fields = ('prefix', 'rir', 'tenant', 'date_added', 'description', 'comments', 'tags')
2021-09-28 09:25:12 -04:00
class ASNRangeImportForm(NetBoxModelImportForm):
rir = CSVModelChoiceField(
label=_('RIR'),
queryset=RIR.objects.all(),
to_field_name='name',
help_text=_('Assigned RIR')
)
tenant = CSVModelChoiceField(
label=_('Tenant'),
queryset=Tenant.objects.all(),
required=False,
to_field_name='name',
help_text=_('Assigned tenant')
)
class Meta:
model = ASNRange
fields = ('name', 'slug', 'rir', 'start', 'end', 'tenant', 'description', 'tags')
class ASNImportForm(NetBoxModelImportForm):
2021-10-24 23:42:47 -05:00
rir = CSVModelChoiceField(
label=_('RIR'),
2021-10-24 23:42:47 -05:00
queryset=RIR.objects.all(),
to_field_name='name',
help_text=_('Assigned RIR')
2021-10-24 23:42:47 -05:00
)
tenant = CSVModelChoiceField(
label=_('Tenant'),
2021-10-24 23:42:47 -05:00
queryset=Tenant.objects.all(),
required=False,
to_field_name='name',
help_text=_('Assigned tenant')
2021-10-24 23:42:47 -05:00
)
class Meta:
model = ASN
fields = ('asn', 'rir', 'tenant', 'description', 'comments', 'tags')
2021-10-24 23:42:47 -05:00
class RoleImportForm(NetBoxModelImportForm):
2021-09-28 09:25:12 -04:00
slug = SlugField()
class Meta:
model = Role
fields = ('name', 'slug', 'weight', 'description', 'tags')
2021-09-28 09:25:12 -04:00
class PrefixImportForm(NetBoxModelImportForm):
2021-09-28 09:25:12 -04:00
vrf = CSVModelChoiceField(
label=_('VRF'),
2021-09-28 09:25:12 -04:00
queryset=VRF.objects.all(),
to_field_name='name',
required=False,
help_text=_('Assigned VRF')
2021-09-28 09:25:12 -04:00
)
tenant = CSVModelChoiceField(
label=_('Tenant'),
2021-09-28 09:25:12 -04:00
queryset=Tenant.objects.all(),
required=False,
to_field_name='name',
help_text=_('Assigned tenant')
2021-09-28 09:25:12 -04:00
)
site = CSVModelChoiceField(
label=_('Site'),
2021-09-28 09:25:12 -04:00
queryset=Site.objects.all(),
required=False,
to_field_name='name',
help_text=_('Assigned site')
2021-09-28 09:25:12 -04:00
)
vlan_group = CSVModelChoiceField(
label=_('VLAN group'),
2021-09-28 09:25:12 -04:00
queryset=VLANGroup.objects.all(),
required=False,
to_field_name='name',
help_text=_("VLAN's group (if any)")
2021-09-28 09:25:12 -04:00
)
vlan = CSVModelChoiceField(
label=_('VLAN'),
2021-09-28 09:25:12 -04:00
queryset=VLAN.objects.all(),
required=False,
to_field_name='vid',
help_text=_("Assigned VLAN")
2021-09-28 09:25:12 -04:00
)
status = CSVChoiceField(
label=_('Status'),
2021-09-28 09:25:12 -04:00
choices=PrefixStatusChoices,
help_text=_('Operational status')
2021-09-28 09:25:12 -04:00
)
role = CSVModelChoiceField(
label=_('Role'),
2021-09-28 09:25:12 -04:00
queryset=Role.objects.all(),
required=False,
to_field_name='name',
help_text=_('Functional role')
2021-09-28 09:25:12 -04:00
)
class Meta:
model = Prefix
fields = (
'prefix', 'vrf', 'tenant', 'site', 'vlan_group', 'vlan', 'status', 'role', 'is_pool', 'mark_utilized',
'description', 'comments', 'tags',
2021-09-28 09:25:12 -04:00
)
def __init__(self, data=None, *args, **kwargs):
super().__init__(data, *args, **kwargs)
if not data:
return
2021-09-28 09:25:12 -04:00
site = data.get('site')
vlan_group = data.get('vlan_group')
# Limit VLAN queryset by assigned site and/or group (if specified)
query = Q()
if site:
query |= Q(**{
f"site__{self.fields['site'].to_field_name}": site
})
# Don't Forget to include VLANs without a site in the filter
query |= Q(**{
f"site__{self.fields['site'].to_field_name}__isnull": True
})
if vlan_group:
query &= Q(**{
f"group__{self.fields['vlan_group'].to_field_name}": vlan_group
})
queryset = self.fields['vlan'].queryset.filter(query)
self.fields['vlan'].queryset = queryset
2021-09-28 09:25:12 -04:00
class IPRangeImportForm(NetBoxModelImportForm):
2021-09-28 09:25:12 -04:00
vrf = CSVModelChoiceField(
label=_('VRF'),
2021-09-28 09:25:12 -04:00
queryset=VRF.objects.all(),
to_field_name='name',
required=False,
help_text=_('Assigned VRF')
2021-09-28 09:25:12 -04:00
)
tenant = CSVModelChoiceField(
label=_('Tenant'),
2021-09-28 09:25:12 -04:00
queryset=Tenant.objects.all(),
required=False,
to_field_name='name',
help_text=_('Assigned tenant')
2021-09-28 09:25:12 -04:00
)
status = CSVChoiceField(
label=_('Status'),
2021-09-28 09:25:12 -04:00
choices=IPRangeStatusChoices,
help_text=_('Operational status')
2021-09-28 09:25:12 -04:00
)
role = CSVModelChoiceField(
label=_('Role'),
2021-09-28 09:25:12 -04:00
queryset=Role.objects.all(),
required=False,
to_field_name='name',
help_text=_('Functional role')
2021-09-28 09:25:12 -04:00
)
class Meta:
model = IPRange
fields = (
2023-02-28 10:09:09 -05:00
'start_address', 'end_address', 'vrf', 'tenant', 'status', 'role', 'mark_utilized', 'description',
'comments', 'tags',
2021-09-28 09:25:12 -04:00
)
class IPAddressImportForm(NetBoxModelImportForm):
2021-09-28 09:25:12 -04:00
vrf = CSVModelChoiceField(
label=_('VRF'),
2021-09-28 09:25:12 -04:00
queryset=VRF.objects.all(),
to_field_name='name',
required=False,
help_text=_('Assigned VRF')
2021-09-28 09:25:12 -04:00
)
tenant = CSVModelChoiceField(
label=_('Tenant'),
2021-09-28 09:25:12 -04:00
queryset=Tenant.objects.all(),
to_field_name='name',
required=False,
help_text=_('Assigned tenant')
2021-09-28 09:25:12 -04:00
)
status = CSVChoiceField(
label=_('Status'),
2021-09-28 09:25:12 -04:00
choices=IPAddressStatusChoices,
help_text=_('Operational status')
2021-09-28 09:25:12 -04:00
)
role = CSVChoiceField(
label=_('Role'),
2021-09-28 09:25:12 -04:00
choices=IPAddressRoleChoices,
required=False,
help_text=_('Functional role')
2021-09-28 09:25:12 -04:00
)
device = CSVModelChoiceField(
label=_('Device'),
2021-09-28 09:25:12 -04:00
queryset=Device.objects.all(),
required=False,
to_field_name='name',
help_text=_('Parent device of assigned interface (if any)')
2021-09-28 09:25:12 -04:00
)
virtual_machine = CSVModelChoiceField(
label=_('Virtual machine'),
2021-09-28 09:25:12 -04:00
queryset=VirtualMachine.objects.all(),
required=False,
to_field_name='name',
help_text=_('Parent VM of assigned interface (if any)')
2021-09-28 09:25:12 -04:00
)
interface = CSVModelChoiceField(
label=_('Interface'),
2021-09-28 09:25:12 -04:00
queryset=Interface.objects.none(), # Can also refer to VMInterface
required=False,
to_field_name='name',
help_text=_('Assigned interface')
2021-09-28 09:25:12 -04:00
)
is_primary = forms.BooleanField(
label=_('Is primary'),
help_text=_('Make this the primary IP for the assigned device'),
2021-09-28 09:25:12 -04:00
required=False
)
class Meta:
model = IPAddress
fields = [
'address', 'vrf', 'tenant', 'status', 'role', 'device', 'virtual_machine', 'interface', 'is_primary',
'dns_name', 'description', 'comments', 'tags',
2021-09-28 09:25:12 -04:00
]
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')
2021-09-28 09:25:12 -04:00
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")
})
2021-09-28 09:25:12 -04:00
def save(self, *args, **kwargs):
# Set interface assignment
if self.cleaned_data.get('interface'):
2021-09-28 09:25:12 -04:00
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.get('device') or self.cleaned_data.get('virtual_machine')
2021-09-28 09:25:12 -04:00
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 FHRPGroupImportForm(NetBoxModelImportForm):
2021-11-01 16:14:44 -04:00
protocol = CSVChoiceField(
label=_('Protocol'),
2021-11-01 16:14:44 -04:00
choices=FHRPGroupProtocolChoices
)
auth_type = CSVChoiceField(
label=_('Auth type'),
2021-11-01 16:14:44 -04:00
choices=FHRPGroupAuthTypeChoices,
required=False
)
class Meta:
model = FHRPGroup
fields = ('protocol', 'group_id', 'auth_type', 'auth_key', 'name', 'description', 'comments', 'tags')
2021-11-01 16:14:44 -04:00
class VLANGroupImportForm(NetBoxModelImportForm):
2021-09-28 09:25:12 -04:00
slug = SlugField()
scope_type = CSVContentTypeField(
queryset=ContentType.objects.filter(model__in=VLANGROUP_SCOPE_TYPES),
required=False,
label=_('Scope type (app & model)')
2021-09-28 09:25:12 -04:00
)
min_vid = forms.IntegerField(
min_value=VLAN_VID_MIN,
max_value=VLAN_VID_MAX,
required=False,
label=_('Minimum child VLAN VID (default: {minimum})').format(minimum=VLAN_VID_MIN)
)
max_vid = forms.IntegerField(
min_value=VLAN_VID_MIN,
max_value=VLAN_VID_MAX,
required=False,
label=_('Maximum child VLAN VID (default: {maximum})').format(maximum=VLAN_VID_MIN)
)
2021-09-28 09:25:12 -04:00
class Meta:
model = VLANGroup
fields = ('name', 'slug', 'scope_type', 'scope_id', 'min_vid', 'max_vid', 'description', 'tags')
2021-09-28 09:25:12 -04:00
labels = {
'scope_id': 'Scope ID',
}
class VLANImportForm(NetBoxModelImportForm):
2021-09-28 09:25:12 -04:00
site = CSVModelChoiceField(
label=_('Site'),
2021-09-28 09:25:12 -04:00
queryset=Site.objects.all(),
required=False,
to_field_name='name',
help_text=_('Assigned site')
2021-09-28 09:25:12 -04:00
)
group = CSVModelChoiceField(
label=_('Group'),
2021-09-28 09:25:12 -04:00
queryset=VLANGroup.objects.all(),
required=False,
to_field_name='name',
help_text=_('Assigned VLAN group')
2021-09-28 09:25:12 -04:00
)
tenant = CSVModelChoiceField(
label=_('Tenant'),
2021-09-28 09:25:12 -04:00
queryset=Tenant.objects.all(),
to_field_name='name',
required=False,
help_text=_('Assigned tenant')
2021-09-28 09:25:12 -04:00
)
status = CSVChoiceField(
label=_('Status'),
2021-09-28 09:25:12 -04:00
choices=VLANStatusChoices,
help_text=_('Operational status')
2021-09-28 09:25:12 -04:00
)
role = CSVModelChoiceField(
label=_('Role'),
2021-09-28 09:25:12 -04:00
queryset=Role.objects.all(),
required=False,
to_field_name='name',
help_text=_('Functional role')
2021-09-28 09:25:12 -04:00
)
class Meta:
model = VLAN
fields = ('site', 'group', 'vid', 'name', 'tenant', 'status', 'role', 'description', 'comments', 'tags')
2021-09-28 09:25:12 -04:00
class ServiceTemplateImportForm(NetBoxModelImportForm):
2022-01-12 16:42:28 -05:00
protocol = CSVChoiceField(
label=_('Protocol'),
2022-01-12 16:42:28 -05:00
choices=ServiceProtocolChoices,
help_text=_('IP protocol')
2022-01-12 16:42:28 -05:00
)
class Meta:
model = ServiceTemplate
fields = ('name', 'protocol', 'ports', 'description', 'comments', 'tags')
2022-01-12 16:42:28 -05:00
class ServiceImportForm(NetBoxModelImportForm):
2021-09-28 09:25:12 -04:00
device = CSVModelChoiceField(
label=_('Device'),
2021-09-28 09:25:12 -04:00
queryset=Device.objects.all(),
required=False,
to_field_name='name',
help_text=_('Required if not assigned to a VM')
2021-09-28 09:25:12 -04:00
)
virtual_machine = CSVModelChoiceField(
label=_('Virtual machine'),
2021-09-28 09:25:12 -04:00
queryset=VirtualMachine.objects.all(),
required=False,
to_field_name='name',
help_text=_('Required if not assigned to a device')
2021-09-28 09:25:12 -04:00
)
protocol = CSVChoiceField(
label=_('Protocol'),
2021-09-28 09:25:12 -04:00
choices=ServiceProtocolChoices,
help_text=_('IP protocol')
2021-09-28 09:25:12 -04:00
)
ipaddresses = CSVModelMultipleChoiceField(
queryset=IPAddress.objects.all(),
required=False,
to_field_name='address',
help_text=_('IP Address'),
)
2021-09-28 09:25:12 -04:00
class Meta:
model = Service
fields = (
'device', 'virtual_machine', 'ipaddresses', 'name', 'protocol', 'ports', 'description', 'comments', 'tags',
)
def clean_ipaddresses(self):
parent = self.cleaned_data.get('device') or self.cleaned_data.get('virtual_machine')
for ip_address in self.cleaned_data['ipaddresses']:
if not ip_address.assigned_object or getattr(ip_address.assigned_object, 'parent_object') != parent:
raise forms.ValidationError(
_("{ip} is not assigned to this device/VM.").format(ip=ip_address)
)
return self.cleaned_data['ipaddresses']