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/virtualization/models.py

528 lines
15 KiB
Python
Raw Normal View History

from django.contrib.contenttypes.fields import GenericRelation
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator
from django.db import models
from django.urls import reverse
2020-06-22 13:10:56 -04:00
from dcim.models import BaseInterface, Device
from extras.models import ConfigContextModel
2020-10-23 01:18:04 -04:00
from extras.querysets import ConfigContextModelQuerySet
2021-10-26 13:41:56 -04:00
from netbox.config import get_config
2022-01-26 20:57:14 -05:00
from netbox.models import OrganizationalModel, NetBoxModel
from utilities.fields import NaturalOrderingField
from utilities.ordering import naturalize_interface
2020-06-22 13:10:56 -04:00
from utilities.query_functions import CollateAsChar
2019-12-04 20:40:18 -05:00
from .choices import *
__all__ = (
'Cluster',
'ClusterGroup',
'ClusterType',
'VirtualMachine',
2020-06-23 13:16:21 -04:00
'VMInterface',
)
#
# Cluster types
#
class ClusterType(OrganizationalModel):
"""
A type of Cluster.
"""
name = models.CharField(
max_length=100,
unique=True
)
slug = models.SlugField(
max_length=100,
unique=True
)
description = models.CharField(
max_length=200,
blank=True
)
class Meta:
ordering = ['name']
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('virtualization:clustertype', args=[self.pk])
#
# Cluster groups
#
class ClusterGroup(OrganizationalModel):
"""
An organizational group of Clusters.
"""
name = models.CharField(
max_length=100,
unique=True
)
slug = models.SlugField(
max_length=100,
unique=True
)
description = models.CharField(
max_length=200,
blank=True
)
2021-10-18 15:09:57 -04:00
# Generic relations
vlan_groups = GenericRelation(
to='ipam.VLANGroup',
content_type_field='scope_type',
object_id_field='scope_id',
related_query_name='cluster_group'
)
2021-10-18 15:09:57 -04:00
contacts = GenericRelation(
to='tenancy.ContactAssignment'
)
class Meta:
ordering = ['name']
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('virtualization:clustergroup', args=[self.pk])
#
# Clusters
#
2022-01-26 20:57:14 -05:00
class Cluster(NetBoxModel):
"""
A cluster of VirtualMachines. Each Cluster may optionally be associated with one or more Devices.
"""
name = models.CharField(
max_length=100
)
type = models.ForeignKey(
to=ClusterType,
on_delete=models.PROTECT,
related_name='clusters'
)
group = models.ForeignKey(
to=ClusterGroup,
on_delete=models.PROTECT,
related_name='clusters',
blank=True,
null=True
)
status = models.CharField(
max_length=50,
choices=ClusterStatusChoices,
default=ClusterStatusChoices.STATUS_ACTIVE
)
2019-10-07 08:29:32 +02:00
tenant = models.ForeignKey(
to='tenancy.Tenant',
on_delete=models.PROTECT,
related_name='clusters',
2019-10-07 08:29:32 +02:00
blank=True,
null=True
)
site = models.ForeignKey(
to='dcim.Site',
on_delete=models.PROTECT,
related_name='clusters',
blank=True,
null=True
)
comments = models.TextField(
blank=True
)
2021-10-18 15:09:57 -04:00
# Generic relations
vlan_groups = GenericRelation(
to='ipam.VLANGroup',
content_type_field='scope_type',
object_id_field='scope_id',
related_query_name='cluster'
)
2021-10-18 15:09:57 -04:00
contacts = GenericRelation(
to='tenancy.ContactAssignment'
)
clone_fields = (
'type', 'group', 'status', 'tenant', 'site',
)
class Meta:
ordering = ['name']
unique_together = (
('group', 'name'),
('site', 'name'),
)
def __str__(self):
return self.name
2022-08-18 15:11:03 -07:00
@classmethod
def get_prerequisite_models(cls):
return [ClusterType, ]
def get_absolute_url(self):
return reverse('virtualization:cluster', args=[self.pk])
def get_status_color(self):
return ClusterStatusChoices.colors.get(self.status)
def clean(self):
super().clean()
# If the Cluster is assigned to a Site, verify that all host Devices belong to that Site.
if self.pk and self.site:
nonsite_devices = Device.objects.filter(cluster=self).exclude(site=self.site).count()
if nonsite_devices:
raise ValidationError({
'site': "{} devices are assigned as hosts for this cluster but are not in site {}".format(
nonsite_devices, self.site
)
})
#
# Virtual machines
#
2022-01-26 20:57:14 -05:00
class VirtualMachine(NetBoxModel, ConfigContextModel):
"""
A virtual machine which runs inside a Cluster.
"""
site = models.ForeignKey(
to='dcim.Site',
on_delete=models.PROTECT,
related_name='virtual_machines',
blank=True,
null=True
)
cluster = models.ForeignKey(
2018-03-30 13:57:26 -04:00
to='virtualization.Cluster',
on_delete=models.PROTECT,
related_name='virtual_machines',
blank=True,
null=True
)
device = models.ForeignKey(
to='dcim.Device',
on_delete=models.PROTECT,
related_name='virtual_machines',
blank=True,
null=True
)
tenant = models.ForeignKey(
to='tenancy.Tenant',
on_delete=models.PROTECT,
related_name='virtual_machines',
blank=True,
null=True
)
platform = models.ForeignKey(
to='dcim.Platform',
on_delete=models.SET_NULL,
related_name='virtual_machines',
blank=True,
null=True
)
name = models.CharField(
max_length=64
)
_name = NaturalOrderingField(
target_field='name',
max_length=100,
blank=True
)
2019-12-04 20:40:18 -05:00
status = models.CharField(
max_length=50,
choices=VirtualMachineStatusChoices,
default=VirtualMachineStatusChoices.STATUS_ACTIVE,
verbose_name='Status'
)
role = models.ForeignKey(
to='dcim.DeviceRole',
on_delete=models.PROTECT,
related_name='virtual_machines',
2018-03-30 13:57:26 -04:00
limit_choices_to={'vm_role': True},
blank=True,
null=True
)
primary_ip4 = models.OneToOneField(
to='ipam.IPAddress',
on_delete=models.SET_NULL,
related_name='+',
blank=True,
null=True,
verbose_name='Primary IPv4'
)
primary_ip6 = models.OneToOneField(
to='ipam.IPAddress',
on_delete=models.SET_NULL,
related_name='+',
blank=True,
null=True,
verbose_name='Primary IPv6'
)
vcpus = models.DecimalField(
max_digits=6,
decimal_places=2,
blank=True,
2017-08-16 17:00:17 -04:00
null=True,
verbose_name='vCPUs',
validators=(
MinValueValidator(0.01),
)
)
memory = models.PositiveIntegerField(
blank=True,
2017-08-16 17:00:17 -04:00
null=True,
verbose_name='Memory (MB)'
)
disk = models.PositiveIntegerField(
blank=True,
2017-08-16 17:00:17 -04:00
null=True,
verbose_name='Disk (GB)'
)
comments = models.TextField(
blank=True
)
2021-10-18 15:09:57 -04:00
# Generic relation
contacts = GenericRelation(
to='tenancy.ContactAssignment'
)
2020-10-23 01:18:04 -04:00
objects = ConfigContextModelQuerySet.as_manager()
clone_fields = (
'site', 'cluster', 'device', 'tenant', 'platform', 'status', 'role', 'vcpus', 'memory', 'disk',
)
class Meta:
ordering = ('_name', 'pk') # Name may be non-unique
unique_together = [
['cluster', 'tenant', 'name']
]
def __str__(self):
return self.name
2022-08-18 15:11:03 -07:00
@classmethod
def get_prerequisite_models(cls):
return [Cluster, ]
def get_absolute_url(self):
return reverse('virtualization:virtualmachine', args=[self.pk])
def validate_unique(self, exclude=None):
# Check for a duplicate name on a VM assigned to the same Cluster and no Tenant. This is necessary
# because Django does not consider two NULL fields to be equal, and thus will not trigger a violation
# of the uniqueness constraint without manual intervention.
if self.tenant is None and VirtualMachine.objects.exclude(pk=self.pk).filter(
name=self.name, cluster=self.cluster, tenant__isnull=True
):
raise ValidationError({
'name': 'A virtual machine with this name already exists in the assigned cluster.'
})
super().validate_unique(exclude)
def clean(self):
super().clean()
# Must be assigned to a site and/or cluster
if not self.site and not self.cluster:
raise ValidationError({
'cluster': f'A virtual machine must be assigned to a site and/or cluster.'
})
# Validate site for cluster & device
if self.cluster and self.cluster.site != self.site:
raise ValidationError({
'cluster': f'The selected cluster ({self.cluster} is not assigned to this site ({self.site}).'
})
if self.device and self.device.site != self.site:
raise ValidationError({
'device': f'The selected device ({self.device} is not assigned to this site ({self.site}).'
})
# Validate assigned cluster device
if self.device and not self.cluster:
raise ValidationError({
'device': f'Must specify a cluster when assigning a host device.'
})
if self.device and self.device not in self.cluster.devices.all():
raise ValidationError({
'device': f'The selected device ({self.device} is not assigned to this cluster ({self.cluster}).'
})
# Validate primary IP addresses
interfaces = self.interfaces.all()
for family in (4, 6):
field = f'primary_ip{family}'
ip = getattr(self, field)
if ip is not None:
if ip.address.version != family:
raise ValidationError({
field: f"Must be an IPv{family} address. ({ip} is an IPv{ip.address.version} address.)",
})
2020-09-04 16:09:05 -04:00
if ip.assigned_object in interfaces:
pass
2020-09-04 16:09:05 -04:00
elif ip.nat_inside is not None and ip.nat_inside.assigned_object in interfaces:
pass
else:
raise ValidationError({
2020-09-04 16:09:05 -04:00
field: f"The specified IP address ({ip}) is not assigned to this VM.",
})
def get_status_color(self):
return VirtualMachineStatusChoices.colors.get(self.status)
@property
def primary_ip(self):
2021-10-26 13:41:56 -04:00
if get_config().PREFER_IPV4 and self.primary_ip4:
return self.primary_ip4
elif self.primary_ip6:
return self.primary_ip6
elif self.primary_ip4:
return self.primary_ip4
else:
return None
2020-06-22 13:10:56 -04:00
#
# Interfaces
#
2022-01-26 20:57:14 -05:00
class VMInterface(NetBoxModel, BaseInterface):
2020-06-22 13:10:56 -04:00
virtual_machine = models.ForeignKey(
to='virtualization.VirtualMachine',
on_delete=models.CASCADE,
related_name='interfaces'
)
name = models.CharField(
max_length=64
)
_name = NaturalOrderingField(
target_field='name',
naturalize_function=naturalize_interface,
max_length=100,
blank=True
)
2020-06-22 13:10:56 -04:00
description = models.CharField(
max_length=200,
blank=True
)
untagged_vlan = models.ForeignKey(
to='ipam.VLAN',
on_delete=models.SET_NULL,
related_name='vminterfaces_as_untagged',
2020-06-22 13:10:56 -04:00
null=True,
blank=True,
verbose_name='Untagged VLAN'
)
tagged_vlans = models.ManyToManyField(
to='ipam.VLAN',
related_name='vminterfaces_as_tagged',
2020-06-22 13:10:56 -04:00
blank=True,
verbose_name='Tagged VLANs'
)
ip_addresses = GenericRelation(
2020-06-22 13:10:56 -04:00
to='ipam.IPAddress',
content_type_field='assigned_object_type',
object_id_field='assigned_object_id',
related_query_name='vminterface'
2020-06-22 13:10:56 -04:00
)
vrf = models.ForeignKey(
to='ipam.VRF',
on_delete=models.SET_NULL,
related_name='vminterfaces',
null=True,
blank=True,
verbose_name='VRF'
)
2021-11-01 16:14:44 -04:00
fhrp_group_assignments = GenericRelation(
to='ipam.FHRPGroupAssignment',
content_type_field='interface_type',
object_id_field='interface_id',
related_query_name='+'
2021-11-01 16:14:44 -04:00
)
l2vpn_terminations = GenericRelation(
to='ipam.L2VPNTermination',
content_type_field='assigned_object_type',
object_id_field='assigned_object_id',
related_query_name='vminterface',
)
2020-06-22 13:10:56 -04:00
class Meta:
2020-06-23 13:16:21 -04:00
verbose_name = 'interface'
2020-06-22 13:10:56 -04:00
ordering = ('virtual_machine', CollateAsChar('_name'))
unique_together = ('virtual_machine', 'name')
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('virtualization:vminterface', kwargs={'pk': self.pk})
2020-06-22 13:10:56 -04:00
def clean(self):
super().clean()
2020-06-22 13:10:56 -04:00
# Parent validation
# An interface cannot be its own parent
if self.pk and self.parent_id == self.pk:
raise ValidationError({'parent': "An interface cannot be its own parent."})
# An interface's parent must belong to the same virtual machine
if self.parent and self.parent.virtual_machine != self.virtual_machine:
raise ValidationError({
'parent': f"The selected parent interface ({self.parent}) belongs to a different virtual machine "
f"({self.parent.virtual_machine})."
})
# Bridge validation
# An interface cannot be bridged to itself
if self.pk and self.bridge_id == self.pk:
raise ValidationError({'bridge': "An interface cannot be bridged to itself."})
# A bridged interface belong to the same virtual machine
if self.bridge and self.bridge.virtual_machine != self.virtual_machine:
raise ValidationError({
'bridge': f"The selected bridge interface ({self.bridge}) belongs to a different virtual machine "
f"({self.bridge.virtual_machine})."
})
# VLAN validation
2020-06-22 13:10:56 -04:00
# Validate untagged VLAN
if self.untagged_vlan and self.untagged_vlan.site not in [self.virtual_machine.site, None]:
2020-06-22 13:10:56 -04:00
raise ValidationError({
'untagged_vlan': f"The untagged VLAN ({self.untagged_vlan}) must belong to the same site as the "
f"interface's parent virtual machine, or it must be global."
2020-06-22 13:10:56 -04:00
})
def to_objectchange(self, action):
2022-01-26 20:25:23 -05:00
objectchange = super().to_objectchange(action)
objectchange.related_object = self.virtual_machine
return objectchange
2020-06-22 13:10:56 -04:00
2020-06-23 16:39:43 -04:00
@property
2021-03-17 16:44:34 -04:00
def parent_object(self):
2020-06-23 16:39:43 -04:00
return self.virtual_machine
@property
def l2vpn_termination(self):
return self.l2vpn_terminations.first()