2023-07-31 23:52:38 +07:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2023-04-14 10:33:53 -04:00
|
|
|
|
2021-09-28 10:25:07 -04:00
|
|
|
from dcim.choices import InterfaceModeChoices
|
2022-05-25 16:01:10 -04:00
|
|
|
from dcim.models import Device, DeviceRole, Platform, Site
|
2023-08-15 01:13:28 +05:30
|
|
|
from extras.models import ConfigTemplate
|
2022-02-07 09:46:38 -05:00
|
|
|
from ipam.models import VRF
|
2022-11-15 12:24:57 -05:00
|
|
|
from netbox.forms import NetBoxModelImportForm
|
2021-09-28 10:25:07 -04:00
|
|
|
from tenancy.models import Tenant
|
2023-04-14 10:33:53 -04:00
|
|
|
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__ = (
|
2022-11-15 12:24:57 -05:00
|
|
|
'ClusterImportForm',
|
|
|
|
'ClusterGroupImportForm',
|
|
|
|
'ClusterTypeImportForm',
|
2023-11-17 12:02:56 -08:00
|
|
|
'VirtualDiskImportForm',
|
2022-11-15 12:24:57 -05:00
|
|
|
'VirtualMachineImportForm',
|
|
|
|
'VMInterfaceImportForm',
|
2021-09-28 10:25:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-11-15 12:24:57 -05:00
|
|
|
class ClusterTypeImportForm(NetBoxModelImportForm):
|
2021-09-28 10:25:07 -04:00
|
|
|
slug = SlugField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ClusterType
|
2022-11-04 07:50:43 -07:00
|
|
|
fields = ('name', 'slug', 'description', 'tags')
|
2021-09-28 10:25:07 -04:00
|
|
|
|
|
|
|
|
2022-11-15 12:24:57 -05:00
|
|
|
class ClusterGroupImportForm(NetBoxModelImportForm):
|
2021-09-28 10:25:07 -04:00
|
|
|
slug = SlugField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ClusterGroup
|
2022-11-04 07:50:43 -07:00
|
|
|
fields = ('name', 'slug', 'description', 'tags')
|
2021-09-28 10:25:07 -04:00
|
|
|
|
|
|
|
|
2022-11-15 12:24:57 -05:00
|
|
|
class ClusterImportForm(NetBoxModelImportForm):
|
2021-09-28 10:25:07 -04:00
|
|
|
type = CSVModelChoiceField(
|
2023-07-31 23:52:38 +07:00
|
|
|
label=_('Type'),
|
2021-09-28 10:25:07 -04:00
|
|
|
queryset=ClusterType.objects.all(),
|
|
|
|
to_field_name='name',
|
2022-11-03 11:58:26 -07:00
|
|
|
help_text=_('Type of cluster')
|
2021-09-28 10:25:07 -04:00
|
|
|
)
|
|
|
|
group = CSVModelChoiceField(
|
2023-07-31 23:52:38 +07:00
|
|
|
label=_('Group'),
|
2021-09-28 10:25:07 -04:00
|
|
|
queryset=ClusterGroup.objects.all(),
|
|
|
|
to_field_name='name',
|
|
|
|
required=False,
|
2022-11-03 11:58:26 -07:00
|
|
|
help_text=_('Assigned cluster group')
|
2021-09-28 10:25:07 -04:00
|
|
|
)
|
2022-05-19 16:13:22 -04:00
|
|
|
status = CSVChoiceField(
|
2023-07-31 23:52:38 +07:00
|
|
|
label=_('Status'),
|
2022-05-19 16:13:22 -04:00
|
|
|
choices=ClusterStatusChoices,
|
2022-11-03 11:58:26 -07:00
|
|
|
help_text=_('Operational status')
|
2022-05-19 16:13:22 -04:00
|
|
|
)
|
2021-09-28 10:25:07 -04:00
|
|
|
site = CSVModelChoiceField(
|
2023-07-31 23:52:38 +07:00
|
|
|
label=_('Site'),
|
2021-09-28 10:25:07 -04:00
|
|
|
queryset=Site.objects.all(),
|
|
|
|
to_field_name='name',
|
|
|
|
required=False,
|
2022-11-03 11:58:26 -07:00
|
|
|
help_text=_('Assigned site')
|
2021-09-28 10:25:07 -04:00
|
|
|
)
|
|
|
|
tenant = CSVModelChoiceField(
|
2023-07-31 23:52:38 +07:00
|
|
|
label=_('Tenant'),
|
2021-09-28 10:25:07 -04:00
|
|
|
queryset=Tenant.objects.all(),
|
|
|
|
to_field_name='name',
|
|
|
|
required=False,
|
2022-11-03 11:58:26 -07:00
|
|
|
help_text=_('Assigned tenant')
|
2021-09-28 10:25:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Cluster
|
2023-05-22 13:07:40 -04:00
|
|
|
fields = ('name', 'type', 'group', 'status', 'site', 'tenant', 'description', 'comments', 'tags')
|
2021-09-28 10:25:07 -04:00
|
|
|
|
|
|
|
|
2022-11-15 12:24:57 -05:00
|
|
|
class VirtualMachineImportForm(NetBoxModelImportForm):
|
2021-09-28 10:25:07 -04:00
|
|
|
status = CSVChoiceField(
|
2023-07-31 23:52:38 +07:00
|
|
|
label=_('Status'),
|
2021-09-28 10:25:07 -04:00
|
|
|
choices=VirtualMachineStatusChoices,
|
2022-11-03 11:58:26 -07:00
|
|
|
help_text=_('Operational status')
|
2021-09-28 10:25:07 -04:00
|
|
|
)
|
2022-05-26 14:59:49 -04:00
|
|
|
site = CSVModelChoiceField(
|
2023-07-31 23:52:38 +07:00
|
|
|
label=_('Site'),
|
2022-05-26 14:59:49 -04:00
|
|
|
queryset=Site.objects.all(),
|
|
|
|
to_field_name='name',
|
|
|
|
required=False,
|
2022-11-03 11:58:26 -07:00
|
|
|
help_text=_('Assigned site')
|
2022-05-26 14:59:49 -04:00
|
|
|
)
|
2021-09-28 10:25:07 -04:00
|
|
|
cluster = CSVModelChoiceField(
|
2023-07-31 23:52:38 +07:00
|
|
|
label=_('Cluster'),
|
2021-09-28 10:25:07 -04:00
|
|
|
queryset=Cluster.objects.all(),
|
|
|
|
to_field_name='name',
|
2022-05-26 14:59:49 -04:00
|
|
|
required=False,
|
2022-11-03 11:58:26 -07:00
|
|
|
help_text=_('Assigned cluster')
|
2021-09-28 10:25:07 -04:00
|
|
|
)
|
2022-05-25 16:01:10 -04:00
|
|
|
device = CSVModelChoiceField(
|
2023-07-31 23:52:38 +07:00
|
|
|
label=_('Device'),
|
2022-05-25 16:01:10 -04:00
|
|
|
queryset=Device.objects.all(),
|
|
|
|
to_field_name='name',
|
|
|
|
required=False,
|
2022-11-03 11:58:26 -07:00
|
|
|
help_text=_('Assigned device within cluster')
|
2022-05-25 16:01:10 -04:00
|
|
|
)
|
2021-09-28 10:25:07 -04:00
|
|
|
role = CSVModelChoiceField(
|
2023-07-31 23:52:38 +07:00
|
|
|
label=_('Role'),
|
2021-09-28 10:25:07 -04:00
|
|
|
queryset=DeviceRole.objects.filter(
|
|
|
|
vm_role=True
|
|
|
|
),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2022-11-03 11:58:26 -07:00
|
|
|
help_text=_('Functional role')
|
2021-09-28 10:25:07 -04:00
|
|
|
)
|
|
|
|
tenant = CSVModelChoiceField(
|
2023-07-31 23:52:38 +07:00
|
|
|
label=_('Tenant'),
|
2021-09-28 10:25:07 -04:00
|
|
|
queryset=Tenant.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2022-11-03 11:58:26 -07:00
|
|
|
help_text=_('Assigned tenant')
|
2021-09-28 10:25:07 -04:00
|
|
|
)
|
|
|
|
platform = CSVModelChoiceField(
|
2023-07-31 23:52:38 +07:00
|
|
|
label=_('Platform'),
|
2021-09-28 10:25:07 -04:00
|
|
|
queryset=Platform.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2022-11-03 11:58:26 -07:00
|
|
|
help_text=_('Assigned platform')
|
2021-09-28 10:25:07 -04:00
|
|
|
)
|
2023-08-15 01:13:28 +05:30
|
|
|
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 = (
|
2022-05-26 14:59:49 -04:00
|
|
|
'name', 'status', 'role', 'site', 'cluster', 'device', 'tenant', 'platform', 'vcpus', 'memory', 'disk',
|
2023-08-15 01:13:28 +05:30
|
|
|
'description', 'config_template', 'comments', 'tags',
|
2021-09-28 10:25:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-11-15 12:24:57 -05:00
|
|
|
class VMInterfaceImportForm(NetBoxModelImportForm):
|
2021-09-28 10:25:07 -04:00
|
|
|
virtual_machine = CSVModelChoiceField(
|
2023-07-31 23:52:38 +07:00
|
|
|
label=_('Virtual machine'),
|
2021-09-28 10:25:07 -04:00
|
|
|
queryset=VirtualMachine.objects.all(),
|
|
|
|
to_field_name='name'
|
|
|
|
)
|
2021-10-21 16:30:18 -04:00
|
|
|
parent = CSVModelChoiceField(
|
2023-07-31 23:52:38 +07:00
|
|
|
label=_('Parent'),
|
2021-10-21 16:30:18 -04:00
|
|
|
queryset=VMInterface.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2022-11-03 11:58:26 -07:00
|
|
|
help_text=_('Parent interface')
|
2021-10-21 16:30:18 -04:00
|
|
|
)
|
|
|
|
bridge = CSVModelChoiceField(
|
2023-07-31 23:52:38 +07:00
|
|
|
label=_('Bridge'),
|
2021-10-21 16:30:18 -04:00
|
|
|
queryset=VMInterface.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2022-11-03 11:58:26 -07:00
|
|
|
help_text=_('Bridged interface')
|
2021-10-21 16:30:18 -04:00
|
|
|
)
|
2021-09-28 10:25:07 -04:00
|
|
|
mode = CSVChoiceField(
|
2023-07-31 23:52:38 +07:00
|
|
|
label=_('Mode'),
|
2021-09-28 10:25:07 -04:00
|
|
|
choices=InterfaceModeChoices,
|
|
|
|
required=False,
|
2022-11-03 11:58:26 -07:00
|
|
|
help_text=_('IEEE 802.1Q operational mode (for L2 interfaces)')
|
2021-09-28 10:25:07 -04:00
|
|
|
)
|
2022-02-07 09:46:38 -05:00
|
|
|
vrf = CSVModelChoiceField(
|
2023-07-31 23:52:38 +07:00
|
|
|
label=_('VRF'),
|
2022-02-07 09:46:38 -05:00
|
|
|
queryset=VRF.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='rd',
|
2022-11-03 11:58:26 -07:00
|
|
|
help_text=_('Assigned VRF')
|
2022-02-07 09:46:38 -05:00
|
|
|
)
|
2021-09-28 10:25:07 -04:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = VMInterface
|
|
|
|
fields = (
|
2021-10-21 16:30:18 -04:00
|
|
|
'virtual_machine', 'name', 'parent', 'bridge', 'enabled', 'mac_address', 'mtu', 'description', 'mode',
|
2022-11-04 07:50:43 -07:00
|
|
|
'vrf', 'tags'
|
2021-09-28 10:25:07 -04:00
|
|
|
)
|
|
|
|
|
2022-04-13 13:43:18 -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']
|
2023-11-17 12:02:56 -08:00
|
|
|
|
|
|
|
|
|
|
|
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'
|
|
|
|
)
|