2017-08-04 17:02:52 -04:00
|
|
|
from django.contrib.contenttypes.fields import GenericRelation
|
2017-09-22 12:53:09 -04:00
|
|
|
from django.core.exceptions import ValidationError
|
2021-03-16 11:52:59 -04:00
|
|
|
from django.core.validators import MinValueValidator
|
2017-08-04 17:02:52 -04:00
|
|
|
from django.db import models
|
2022-09-14 09:16:25 -04:00
|
|
|
from django.db.models import Q
|
2022-09-27 16:19:39 -04:00
|
|
|
from django.db.models.functions import Lower
|
2017-08-04 17:02:52 -04:00
|
|
|
from django.urls import reverse
|
|
|
|
|
2022-11-04 13:49:31 -04:00
|
|
|
from dcim.models import BaseInterface
|
2021-03-10 14:32:50 -05:00
|
|
|
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-11-04 13:49:31 -04:00
|
|
|
from netbox.models import NetBoxModel, PrimaryModel
|
2023-07-25 20:39:05 +07:00
|
|
|
from utilities.fields import CounterCacheField, NaturalOrderingField
|
2020-07-02 12:08:19 -04:00
|
|
|
from utilities.ordering import naturalize_interface
|
2020-06-22 13:10:56 -04:00
|
|
|
from utilities.query_functions import CollateAsChar
|
2023-07-25 20:39:05 +07:00
|
|
|
from utilities.tracking import TrackingModelMixin
|
2022-11-04 13:49:31 -04:00
|
|
|
from virtualization.choices import *
|
2017-08-04 17:02:52 -04:00
|
|
|
|
2020-01-14 12:01:23 -05:00
|
|
|
__all__ = (
|
|
|
|
'VirtualMachine',
|
2020-06-23 13:16:21 -04:00
|
|
|
'VMInterface',
|
2020-01-14 12:01:23 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-11-04 08:28:09 -04:00
|
|
|
class VirtualMachine(PrimaryModel, ConfigContextModel):
|
2017-08-04 17:02:52 -04:00
|
|
|
"""
|
|
|
|
A virtual machine which runs inside a Cluster.
|
|
|
|
"""
|
2022-05-26 14:59:49 -04:00
|
|
|
site = models.ForeignKey(
|
|
|
|
to='dcim.Site',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name='virtual_machines',
|
|
|
|
blank=True,
|
|
|
|
null=True
|
|
|
|
)
|
2017-08-04 17:02:52 -04:00
|
|
|
cluster = models.ForeignKey(
|
2018-03-30 13:57:26 -04:00
|
|
|
to='virtualization.Cluster',
|
2017-08-04 17:02:52 -04:00
|
|
|
on_delete=models.PROTECT,
|
2022-05-26 14:59:49 -04:00
|
|
|
related_name='virtual_machines',
|
|
|
|
blank=True,
|
|
|
|
null=True
|
2017-08-04 17:02:52 -04:00
|
|
|
)
|
2022-05-25 16:01:10 -04:00
|
|
|
device = models.ForeignKey(
|
|
|
|
to='dcim.Device',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name='virtual_machines',
|
|
|
|
blank=True,
|
|
|
|
null=True
|
2017-08-04 17:02:52 -04:00
|
|
|
)
|
|
|
|
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(
|
2019-12-09 11:59:30 -05:00
|
|
|
max_length=64
|
2017-08-04 17:02:52 -04:00
|
|
|
)
|
2021-04-20 09:17:14 -04:00
|
|
|
_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,
|
2017-09-14 14:35:34 -04:00
|
|
|
verbose_name='Status'
|
|
|
|
)
|
2017-09-29 11:13:41 -04:00
|
|
|
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},
|
2017-09-29 11:13:41 -04:00
|
|
|
blank=True,
|
|
|
|
null=True
|
|
|
|
)
|
2017-08-04 17:02:52 -04:00
|
|
|
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'
|
|
|
|
)
|
2021-03-16 11:52:59 -04:00
|
|
|
vcpus = models.DecimalField(
|
|
|
|
max_digits=6,
|
|
|
|
decimal_places=2,
|
2017-08-16 15:25:33 -04:00
|
|
|
blank=True,
|
2017-08-16 17:00:17 -04:00
|
|
|
null=True,
|
2021-03-16 11:52:59 -04:00
|
|
|
verbose_name='vCPUs',
|
|
|
|
validators=(
|
|
|
|
MinValueValidator(0.01),
|
|
|
|
)
|
2017-08-16 15:25:33 -04:00
|
|
|
)
|
|
|
|
memory = models.PositiveIntegerField(
|
|
|
|
blank=True,
|
2017-08-16 17:00:17 -04:00
|
|
|
null=True,
|
2017-08-16 15:25:33 -04:00
|
|
|
verbose_name='Memory (MB)'
|
|
|
|
)
|
|
|
|
disk = models.PositiveIntegerField(
|
|
|
|
blank=True,
|
2017-08-16 17:00:17 -04:00
|
|
|
null=True,
|
2017-08-16 15:25:33 -04:00
|
|
|
verbose_name='Disk (GB)'
|
|
|
|
)
|
2018-05-10 12:53:11 -04:00
|
|
|
|
2023-07-25 20:39:05 +07:00
|
|
|
# Counter fields
|
|
|
|
interface_count = CounterCacheField(
|
|
|
|
to_model='virtualization.VMInterface',
|
|
|
|
to_field='virtual_machine'
|
|
|
|
)
|
|
|
|
|
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()
|
2020-05-29 16:27:36 -04:00
|
|
|
|
2022-08-11 09:58:37 -04:00
|
|
|
clone_fields = (
|
2022-05-26 14:59:49 -04:00
|
|
|
'site', 'cluster', 'device', 'tenant', 'platform', 'status', 'role', 'vcpus', 'memory', 'disk',
|
2022-08-11 09:58:37 -04:00
|
|
|
)
|
2022-11-16 17:22:09 -05:00
|
|
|
prerequisite_models = (
|
|
|
|
'virtualization.Cluster',
|
|
|
|
)
|
2017-09-18 13:12:58 -04:00
|
|
|
|
2017-08-04 17:02:52 -04:00
|
|
|
class Meta:
|
2021-04-20 09:17:14 -04:00
|
|
|
ordering = ('_name', 'pk') # Name may be non-unique
|
2022-09-14 09:16:25 -04:00
|
|
|
constraints = (
|
|
|
|
models.UniqueConstraint(
|
2022-09-27 16:19:39 -04:00
|
|
|
Lower('name'), 'cluster', 'tenant',
|
2022-09-27 15:44:38 -04:00
|
|
|
name='%(app_label)s_%(class)s_unique_name_cluster_tenant'
|
2022-09-14 09:16:25 -04:00
|
|
|
),
|
|
|
|
models.UniqueConstraint(
|
2022-09-27 16:19:39 -04:00
|
|
|
Lower('name'), 'cluster',
|
2022-09-27 15:44:38 -04:00
|
|
|
name='%(app_label)s_%(class)s_unique_name_cluster',
|
2022-09-14 09:16:25 -04:00
|
|
|
condition=Q(tenant__isnull=True),
|
2022-09-27 16:19:39 -04:00
|
|
|
violation_error_message="Virtual machine name must be unique per cluster."
|
2022-09-14 09:16:25 -04:00
|
|
|
),
|
|
|
|
)
|
2017-08-04 17:02:52 -04:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('virtualization:virtualmachine', args=[self.pk])
|
2017-09-14 14:35:34 -04:00
|
|
|
|
2018-08-09 16:34:17 -04:00
|
|
|
def clean(self):
|
2019-10-10 10:41:08 -04:00
|
|
|
super().clean()
|
|
|
|
|
2022-05-26 14:59:49 -04:00
|
|
|
# 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
|
2022-10-03 15:35:45 -04:00
|
|
|
if self.cluster and self.site and self.cluster.site != self.site:
|
2022-05-26 14:59:49 -04:00
|
|
|
raise ValidationError({
|
2022-10-04 15:46:55 -04:00
|
|
|
'cluster': f'The selected cluster ({self.cluster}) is not assigned to this site ({self.site}).'
|
2022-05-26 14:59:49 -04:00
|
|
|
})
|
|
|
|
|
2022-05-25 16:01:10 -04:00
|
|
|
# Validate assigned cluster device
|
2022-07-15 10:19:56 -04:00
|
|
|
if self.device and not self.cluster:
|
|
|
|
raise ValidationError({
|
|
|
|
'device': f'Must specify a cluster when assigning a host device.'
|
|
|
|
})
|
2022-05-25 16:01:10 -04:00
|
|
|
if self.device and self.device not in self.cluster.devices.all():
|
|
|
|
raise ValidationError({
|
2022-10-04 15:46:55 -04:00
|
|
|
'device': f'The selected device ({self.device}) is not assigned to this cluster ({self.cluster}).'
|
2022-05-25 16:01:10 -04:00
|
|
|
})
|
|
|
|
|
2018-08-09 16:34:17 -04:00
|
|
|
# Validate primary IP addresses
|
2022-09-12 09:59:37 -07:00
|
|
|
interfaces = self.interfaces.all() if self.pk else None
|
2022-09-01 09:31:42 -04:00
|
|
|
for family in (4, 6):
|
|
|
|
field = f'primary_ip{family}'
|
2018-08-09 16:34:17 -04:00
|
|
|
ip = getattr(self, field)
|
|
|
|
if ip is not None:
|
2022-09-01 09:31:42 -04:00
|
|
|
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:
|
2018-08-09 16:34:17 -04:00
|
|
|
pass
|
2020-09-04 16:09:05 -04:00
|
|
|
elif ip.nat_inside is not None and ip.nat_inside.assigned_object in interfaces:
|
2018-08-09 16:34:17 -04:00
|
|
|
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.",
|
2018-08-09 16:34:17 -04:00
|
|
|
})
|
|
|
|
|
2023-04-03 16:26:07 -04:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
|
|
|
|
# Assign site from cluster if not set
|
|
|
|
if self.cluster and not self.site:
|
|
|
|
self.site = self.cluster.site
|
|
|
|
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2022-02-11 14:25:13 -05:00
|
|
|
def get_status_color(self):
|
|
|
|
return VirtualMachineStatusChoices.colors.get(self.status)
|
|
|
|
|
2017-11-09 09:33:40 -05:00
|
|
|
@property
|
|
|
|
def primary_ip(self):
|
2021-10-26 13:41:56 -04:00
|
|
|
if get_config().PREFER_IPV4 and self.primary_ip4:
|
2017-11-09 09:33:40 -05:00
|
|
|
return self.primary_ip4
|
|
|
|
elif self.primary_ip6:
|
|
|
|
return self.primary_ip6
|
|
|
|
elif self.primary_ip4:
|
|
|
|
return self.primary_ip4
|
|
|
|
else:
|
|
|
|
return None
|
2017-11-14 15:22:40 -05:00
|
|
|
|
2020-06-22 13:10:56 -04:00
|
|
|
|
2023-07-25 20:39:05 +07:00
|
|
|
class VMInterface(NetBoxModel, BaseInterface, TrackingModelMixin):
|
2020-06-22 13:10:56 -04:00
|
|
|
virtual_machine = models.ForeignKey(
|
|
|
|
to='virtualization.VirtualMachine',
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name='interfaces'
|
|
|
|
)
|
2020-07-02 12:08:19 -04:00
|
|
|
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,
|
2020-06-24 09:27:30 -04:00
|
|
|
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',
|
2020-06-24 09:27:30 -04:00
|
|
|
related_name='vminterfaces_as_tagged',
|
2020-06-22 13:10:56 -04:00
|
|
|
blank=True,
|
|
|
|
verbose_name='Tagged VLANs'
|
|
|
|
)
|
2020-06-22 16:13:18 -04:00
|
|
|
ip_addresses = GenericRelation(
|
2020-06-22 13:10:56 -04:00
|
|
|
to='ipam.IPAddress',
|
|
|
|
content_type_field='assigned_object_type',
|
2020-06-22 16:27:13 -04:00
|
|
|
object_id_field='assigned_object_id',
|
2020-06-24 09:27:30 -04:00
|
|
|
related_query_name='vminterface'
|
2020-06-22 13:10:56 -04:00
|
|
|
)
|
2022-02-07 09:46:38 -05: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',
|
2021-11-02 15:10:02 -04:00
|
|
|
content_type_field='interface_type',
|
|
|
|
object_id_field='interface_id',
|
|
|
|
related_query_name='+'
|
2021-11-01 16:14:44 -04:00
|
|
|
)
|
2022-07-06 13:31:31 -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:
|
|
|
|
ordering = ('virtual_machine', CollateAsChar('_name'))
|
2022-09-27 15:35:24 -04:00
|
|
|
constraints = (
|
|
|
|
models.UniqueConstraint(
|
|
|
|
fields=('virtual_machine', 'name'),
|
|
|
|
name='%(app_label)s_%(class)s_unique_virtual_machine_name'
|
|
|
|
),
|
|
|
|
)
|
|
|
|
verbose_name = 'interface'
|
2020-06-22 13:10:56 -04:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
2020-06-23 14:38:45 -04:00
|
|
|
return reverse('virtualization:vminterface', kwargs={'pk': self.pk})
|
2020-06-22 13:10:56 -04:00
|
|
|
|
|
|
|
def clean(self):
|
2020-12-28 12:54:42 -05:00
|
|
|
super().clean()
|
2020-06-22 13:10:56 -04:00
|
|
|
|
2021-10-21 16:30:18 -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."})
|
|
|
|
|
2021-04-09 10:53:05 -04:00
|
|
|
# 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})."
|
|
|
|
})
|
|
|
|
|
2021-10-21 16:30:18 -04:00
|
|
|
# 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
|
2021-04-16 09:18:58 -04:00
|
|
|
|
2020-06-22 13:10:56 -04:00
|
|
|
# Validate untagged VLAN
|
2020-06-23 15:31:53 -04:00
|
|
|
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({
|
2020-12-02 14:19:02 -05:00
|
|
|
'untagged_vlan': f"The untagged VLAN ({self.untagged_vlan}) must belong to the same site as the "
|
2021-10-21 16:30:18 -04:00
|
|
|
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
|
2022-07-06 13:31:31 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def l2vpn_termination(self):
|
|
|
|
return self.l2vpn_terminations.first()
|