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

217 lines
6.3 KiB
Python
Raw Normal View History

from django.utils.translation import gettext_lazy as _
2021-09-28 10:25:07 -04:00
from dcim.choices import InterfaceModeChoices
from dcim.models import Device, DeviceRole, Platform, Site
from extras.models import ConfigTemplate
from ipam.models import VRF
from netbox.forms import NetBoxModelImportForm
2021-09-28 10:25:07 -04:00
from tenancy.models import Tenant
from utilities.forms.fields import CSVChoiceField, CSVModelChoiceField, SlugField
2021-09-28 10:25:07 -04:00
from virtualization.choices import *
from virtualization.models import *
__all__ = (
'ClusterImportForm',
'ClusterGroupImportForm',
'ClusterTypeImportForm',
'VirtualDiskImportForm',
'VirtualMachineImportForm',
'VMInterfaceImportForm',
2021-09-28 10:25:07 -04:00
)
class ClusterTypeImportForm(NetBoxModelImportForm):
2021-09-28 10:25:07 -04:00
slug = SlugField()
class Meta:
model = ClusterType
fields = ('name', 'slug', 'description', 'tags')
2021-09-28 10:25:07 -04:00
class ClusterGroupImportForm(NetBoxModelImportForm):
2021-09-28 10:25:07 -04:00
slug = SlugField()
class Meta:
model = ClusterGroup
fields = ('name', 'slug', 'description', 'tags')
2021-09-28 10:25:07 -04:00
class ClusterImportForm(NetBoxModelImportForm):
2021-09-28 10:25:07 -04:00
type = CSVModelChoiceField(
label=_('Type'),
2021-09-28 10:25:07 -04:00
queryset=ClusterType.objects.all(),
to_field_name='name',
help_text=_('Type of cluster')
2021-09-28 10:25:07 -04:00
)
group = CSVModelChoiceField(
label=_('Group'),
2021-09-28 10:25:07 -04:00
queryset=ClusterGroup.objects.all(),
to_field_name='name',
required=False,
help_text=_('Assigned cluster group')
2021-09-28 10:25:07 -04:00
)
status = CSVChoiceField(
label=_('Status'),
choices=ClusterStatusChoices,
help_text=_('Operational status')
)
2021-09-28 10:25:07 -04:00
site = CSVModelChoiceField(
label=_('Site'),
2021-09-28 10:25:07 -04:00
queryset=Site.objects.all(),
to_field_name='name',
required=False,
help_text=_('Assigned site')
2021-09-28 10:25:07 -04:00
)
tenant = CSVModelChoiceField(
label=_('Tenant'),
2021-09-28 10:25:07 -04:00
queryset=Tenant.objects.all(),
to_field_name='name',
required=False,
help_text=_('Assigned tenant')
2021-09-28 10:25:07 -04:00
)
class Meta:
model = Cluster
fields = ('name', 'type', 'group', 'status', 'site', 'tenant', 'description', 'comments', 'tags')
2021-09-28 10:25:07 -04:00
class VirtualMachineImportForm(NetBoxModelImportForm):
2021-09-28 10:25:07 -04:00
status = CSVChoiceField(
label=_('Status'),
2021-09-28 10:25:07 -04:00
choices=VirtualMachineStatusChoices,
help_text=_('Operational status')
2021-09-28 10:25:07 -04:00
)
site = CSVModelChoiceField(
label=_('Site'),
queryset=Site.objects.all(),
to_field_name='name',
required=False,
help_text=_('Assigned site')
)
2021-09-28 10:25:07 -04:00
cluster = CSVModelChoiceField(
label=_('Cluster'),
2021-09-28 10:25:07 -04:00
queryset=Cluster.objects.all(),
to_field_name='name',
required=False,
help_text=_('Assigned cluster')
2021-09-28 10:25:07 -04:00
)
device = CSVModelChoiceField(
label=_('Device'),
queryset=Device.objects.all(),
to_field_name='name',
required=False,
help_text=_('Assigned device within cluster')
)
2021-09-28 10:25:07 -04:00
role = CSVModelChoiceField(
label=_('Role'),
2021-09-28 10:25:07 -04:00
queryset=DeviceRole.objects.filter(
vm_role=True
),
required=False,
to_field_name='name',
help_text=_('Functional role')
2021-09-28 10:25:07 -04:00
)
tenant = CSVModelChoiceField(
label=_('Tenant'),
2021-09-28 10:25:07 -04:00
queryset=Tenant.objects.all(),
required=False,
to_field_name='name',
help_text=_('Assigned tenant')
2021-09-28 10:25:07 -04:00
)
platform = CSVModelChoiceField(
label=_('Platform'),
2021-09-28 10:25:07 -04:00
queryset=Platform.objects.all(),
required=False,
to_field_name='name',
help_text=_('Assigned platform')
2021-09-28 10:25:07 -04:00
)
config_template = CSVModelChoiceField(
queryset=ConfigTemplate.objects.all(),
to_field_name='name',
required=False,
label=_('Config template'),
help_text=_('Config template')
)
2021-09-28 10:25:07 -04:00
class Meta:
model = VirtualMachine
fields = (
'name', 'status', 'role', 'site', 'cluster', 'device', 'tenant', 'platform', 'vcpus', 'memory', 'disk',
'description', 'config_template', 'comments', 'tags',
2021-09-28 10:25:07 -04:00
)
class VMInterfaceImportForm(NetBoxModelImportForm):
2021-09-28 10:25:07 -04:00
virtual_machine = CSVModelChoiceField(
label=_('Virtual machine'),
2021-09-28 10:25:07 -04:00
queryset=VirtualMachine.objects.all(),
to_field_name='name'
)
parent = CSVModelChoiceField(
label=_('Parent'),
queryset=VMInterface.objects.all(),
required=False,
to_field_name='name',
help_text=_('Parent interface')
)
bridge = CSVModelChoiceField(
label=_('Bridge'),
queryset=VMInterface.objects.all(),
required=False,
to_field_name='name',
help_text=_('Bridged interface')
)
2021-09-28 10:25:07 -04:00
mode = CSVChoiceField(
label=_('Mode'),
2021-09-28 10:25:07 -04:00
choices=InterfaceModeChoices,
required=False,
help_text=_('IEEE 802.1Q operational mode (for L2 interfaces)')
2021-09-28 10:25:07 -04:00
)
vrf = CSVModelChoiceField(
label=_('VRF'),
queryset=VRF.objects.all(),
required=False,
to_field_name='rd',
help_text=_('Assigned VRF')
)
2021-09-28 10:25:07 -04:00
class Meta:
model = VMInterface
fields = (
'virtual_machine', 'name', 'parent', 'bridge', 'enabled', 'mac_address', 'mtu', 'description', 'mode',
'vrf', 'tags'
2021-09-28 10:25:07 -04:00
)
def __init__(self, data=None, *args, **kwargs):
super().__init__(data, *args, **kwargs)
if data:
# Limit interface choices for parent & bridge interfaces to the assigned VM
if virtual_machine := data.get('virtual_machine'):
params = {
f"virtual_machine__{self.fields['virtual_machine'].to_field_name}": virtual_machine
}
self.fields['parent'].queryset = self.fields['parent'].queryset.filter(**params)
self.fields['bridge'].queryset = self.fields['bridge'].queryset.filter(**params)
2021-09-28 10:25:07 -04:00
def clean_enabled(self):
# Make sure enabled is True when it's not included in the uploaded data
if 'enabled' not in self.data:
return True
else:
return self.cleaned_data['enabled']
class VirtualDiskImportForm(NetBoxModelImportForm):
virtual_machine = CSVModelChoiceField(
label=_('Virtual machine'),
queryset=VirtualMachine.objects.all(),
to_field_name='name'
)
class Meta:
model = VirtualDisk
fields = (
'virtual_machine', 'name', 'size', 'description', 'tags'
)