models.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. from django.contrib.contenttypes.fields import GenericRelation
  2. from django.core.exceptions import ValidationError
  3. from django.core.validators import MinValueValidator
  4. from django.db import models
  5. from django.urls import reverse
  6. from dcim.models import BaseInterface, Device
  7. from extras.models import ConfigContextModel
  8. from extras.querysets import ConfigContextModelQuerySet
  9. from netbox.config import get_config
  10. from netbox.models import OrganizationalModel, NetBoxModel
  11. from utilities.fields import NaturalOrderingField
  12. from utilities.ordering import naturalize_interface
  13. from utilities.query_functions import CollateAsChar
  14. from .choices import *
  15. __all__ = (
  16. 'Cluster',
  17. 'ClusterGroup',
  18. 'ClusterType',
  19. 'VirtualMachine',
  20. 'VMInterface',
  21. )
  22. #
  23. # Cluster types
  24. #
  25. class ClusterType(OrganizationalModel):
  26. """
  27. A type of Cluster.
  28. """
  29. name = models.CharField(
  30. max_length=100,
  31. unique=True
  32. )
  33. slug = models.SlugField(
  34. max_length=100,
  35. unique=True
  36. )
  37. description = models.CharField(
  38. max_length=200,
  39. blank=True
  40. )
  41. class Meta:
  42. ordering = ['name']
  43. def __str__(self):
  44. return self.name
  45. def get_absolute_url(self):
  46. return reverse('virtualization:clustertype', args=[self.pk])
  47. #
  48. # Cluster groups
  49. #
  50. class ClusterGroup(OrganizationalModel):
  51. """
  52. An organizational group of Clusters.
  53. """
  54. name = models.CharField(
  55. max_length=100,
  56. unique=True
  57. )
  58. slug = models.SlugField(
  59. max_length=100,
  60. unique=True
  61. )
  62. description = models.CharField(
  63. max_length=200,
  64. blank=True
  65. )
  66. # Generic relations
  67. vlan_groups = GenericRelation(
  68. to='ipam.VLANGroup',
  69. content_type_field='scope_type',
  70. object_id_field='scope_id',
  71. related_query_name='cluster_group'
  72. )
  73. contacts = GenericRelation(
  74. to='tenancy.ContactAssignment'
  75. )
  76. class Meta:
  77. ordering = ['name']
  78. def __str__(self):
  79. return self.name
  80. def get_absolute_url(self):
  81. return reverse('virtualization:clustergroup', args=[self.pk])
  82. #
  83. # Clusters
  84. #
  85. class Cluster(NetBoxModel):
  86. """
  87. A cluster of VirtualMachines. Each Cluster may optionally be associated with one or more Devices.
  88. """
  89. name = models.CharField(
  90. max_length=100
  91. )
  92. type = models.ForeignKey(
  93. to=ClusterType,
  94. on_delete=models.PROTECT,
  95. related_name='clusters'
  96. )
  97. group = models.ForeignKey(
  98. to=ClusterGroup,
  99. on_delete=models.PROTECT,
  100. related_name='clusters',
  101. blank=True,
  102. null=True
  103. )
  104. tenant = models.ForeignKey(
  105. to='tenancy.Tenant',
  106. on_delete=models.PROTECT,
  107. related_name='clusters',
  108. blank=True,
  109. null=True
  110. )
  111. site = models.ForeignKey(
  112. to='dcim.Site',
  113. on_delete=models.PROTECT,
  114. related_name='clusters',
  115. blank=True,
  116. null=True
  117. )
  118. comments = models.TextField(
  119. blank=True
  120. )
  121. # Generic relations
  122. vlan_groups = GenericRelation(
  123. to='ipam.VLANGroup',
  124. content_type_field='scope_type',
  125. object_id_field='scope_id',
  126. related_query_name='cluster'
  127. )
  128. contacts = GenericRelation(
  129. to='tenancy.ContactAssignment'
  130. )
  131. clone_fields = [
  132. 'type', 'group', 'tenant', 'site',
  133. ]
  134. class Meta:
  135. ordering = ['name']
  136. unique_together = (
  137. ('group', 'name'),
  138. ('site', 'name'),
  139. )
  140. def __str__(self):
  141. return self.name
  142. def get_absolute_url(self):
  143. return reverse('virtualization:cluster', args=[self.pk])
  144. def clean(self):
  145. super().clean()
  146. # If the Cluster is assigned to a Site, verify that all host Devices belong to that Site.
  147. if self.pk and self.site:
  148. nonsite_devices = Device.objects.filter(cluster=self).exclude(site=self.site).count()
  149. if nonsite_devices:
  150. raise ValidationError({
  151. 'site': "{} devices are assigned as hosts for this cluster but are not in site {}".format(
  152. nonsite_devices, self.site
  153. )
  154. })
  155. #
  156. # Virtual machines
  157. #
  158. class VirtualMachine(NetBoxModel, ConfigContextModel):
  159. """
  160. A virtual machine which runs inside a Cluster.
  161. """
  162. cluster = models.ForeignKey(
  163. to='virtualization.Cluster',
  164. on_delete=models.PROTECT,
  165. related_name='virtual_machines'
  166. )
  167. tenant = models.ForeignKey(
  168. to='tenancy.Tenant',
  169. on_delete=models.PROTECT,
  170. related_name='virtual_machines',
  171. blank=True,
  172. null=True
  173. )
  174. platform = models.ForeignKey(
  175. to='dcim.Platform',
  176. on_delete=models.SET_NULL,
  177. related_name='virtual_machines',
  178. blank=True,
  179. null=True
  180. )
  181. name = models.CharField(
  182. max_length=64
  183. )
  184. _name = NaturalOrderingField(
  185. target_field='name',
  186. max_length=100,
  187. blank=True
  188. )
  189. status = models.CharField(
  190. max_length=50,
  191. choices=VirtualMachineStatusChoices,
  192. default=VirtualMachineStatusChoices.STATUS_ACTIVE,
  193. verbose_name='Status'
  194. )
  195. role = models.ForeignKey(
  196. to='dcim.DeviceRole',
  197. on_delete=models.PROTECT,
  198. related_name='virtual_machines',
  199. limit_choices_to={'vm_role': True},
  200. blank=True,
  201. null=True
  202. )
  203. primary_ip4 = models.OneToOneField(
  204. to='ipam.IPAddress',
  205. on_delete=models.SET_NULL,
  206. related_name='+',
  207. blank=True,
  208. null=True,
  209. verbose_name='Primary IPv4'
  210. )
  211. primary_ip6 = models.OneToOneField(
  212. to='ipam.IPAddress',
  213. on_delete=models.SET_NULL,
  214. related_name='+',
  215. blank=True,
  216. null=True,
  217. verbose_name='Primary IPv6'
  218. )
  219. vcpus = models.DecimalField(
  220. max_digits=6,
  221. decimal_places=2,
  222. blank=True,
  223. null=True,
  224. verbose_name='vCPUs',
  225. validators=(
  226. MinValueValidator(0.01),
  227. )
  228. )
  229. memory = models.PositiveIntegerField(
  230. blank=True,
  231. null=True,
  232. verbose_name='Memory (MB)'
  233. )
  234. disk = models.PositiveIntegerField(
  235. blank=True,
  236. null=True,
  237. verbose_name='Disk (GB)'
  238. )
  239. comments = models.TextField(
  240. blank=True
  241. )
  242. # Generic relation
  243. contacts = GenericRelation(
  244. to='tenancy.ContactAssignment'
  245. )
  246. objects = ConfigContextModelQuerySet.as_manager()
  247. clone_fields = [
  248. 'cluster', 'tenant', 'platform', 'status', 'role', 'vcpus', 'memory', 'disk',
  249. ]
  250. class Meta:
  251. ordering = ('_name', 'pk') # Name may be non-unique
  252. unique_together = [
  253. ['cluster', 'tenant', 'name']
  254. ]
  255. def __str__(self):
  256. return self.name
  257. def get_absolute_url(self):
  258. return reverse('virtualization:virtualmachine', args=[self.pk])
  259. def validate_unique(self, exclude=None):
  260. # Check for a duplicate name on a VM assigned to the same Cluster and no Tenant. This is necessary
  261. # because Django does not consider two NULL fields to be equal, and thus will not trigger a violation
  262. # of the uniqueness constraint without manual intervention.
  263. if self.tenant is None and VirtualMachine.objects.exclude(pk=self.pk).filter(
  264. name=self.name, cluster=self.cluster, tenant__isnull=True
  265. ):
  266. raise ValidationError({
  267. 'name': 'A virtual machine with this name already exists in the assigned cluster.'
  268. })
  269. super().validate_unique(exclude)
  270. def clean(self):
  271. super().clean()
  272. # Validate primary IP addresses
  273. interfaces = self.interfaces.all()
  274. for field in ['primary_ip4', 'primary_ip6']:
  275. ip = getattr(self, field)
  276. if ip is not None:
  277. if ip.assigned_object in interfaces:
  278. pass
  279. elif ip.nat_inside is not None and ip.nat_inside.assigned_object in interfaces:
  280. pass
  281. else:
  282. raise ValidationError({
  283. field: f"The specified IP address ({ip}) is not assigned to this VM.",
  284. })
  285. @property
  286. def primary_ip(self):
  287. if get_config().PREFER_IPV4 and self.primary_ip4:
  288. return self.primary_ip4
  289. elif self.primary_ip6:
  290. return self.primary_ip6
  291. elif self.primary_ip4:
  292. return self.primary_ip4
  293. else:
  294. return None
  295. @property
  296. def site(self):
  297. return self.cluster.site
  298. #
  299. # Interfaces
  300. #
  301. class VMInterface(NetBoxModel, BaseInterface):
  302. virtual_machine = models.ForeignKey(
  303. to='virtualization.VirtualMachine',
  304. on_delete=models.CASCADE,
  305. related_name='interfaces'
  306. )
  307. name = models.CharField(
  308. max_length=64
  309. )
  310. _name = NaturalOrderingField(
  311. target_field='name',
  312. naturalize_function=naturalize_interface,
  313. max_length=100,
  314. blank=True
  315. )
  316. description = models.CharField(
  317. max_length=200,
  318. blank=True
  319. )
  320. untagged_vlan = models.ForeignKey(
  321. to='ipam.VLAN',
  322. on_delete=models.SET_NULL,
  323. related_name='vminterfaces_as_untagged',
  324. null=True,
  325. blank=True,
  326. verbose_name='Untagged VLAN'
  327. )
  328. tagged_vlans = models.ManyToManyField(
  329. to='ipam.VLAN',
  330. related_name='vminterfaces_as_tagged',
  331. blank=True,
  332. verbose_name='Tagged VLANs'
  333. )
  334. ip_addresses = GenericRelation(
  335. to='ipam.IPAddress',
  336. content_type_field='assigned_object_type',
  337. object_id_field='assigned_object_id',
  338. related_query_name='vminterface'
  339. )
  340. vrf = models.ForeignKey(
  341. to='ipam.VRF',
  342. on_delete=models.SET_NULL,
  343. related_name='vminterfaces',
  344. null=True,
  345. blank=True,
  346. verbose_name='VRF'
  347. )
  348. fhrp_group_assignments = GenericRelation(
  349. to='ipam.FHRPGroupAssignment',
  350. content_type_field='interface_type',
  351. object_id_field='interface_id',
  352. related_query_name='+'
  353. )
  354. class Meta:
  355. verbose_name = 'interface'
  356. ordering = ('virtual_machine', CollateAsChar('_name'))
  357. unique_together = ('virtual_machine', 'name')
  358. def __str__(self):
  359. return self.name
  360. def get_absolute_url(self):
  361. return reverse('virtualization:vminterface', kwargs={'pk': self.pk})
  362. def clean(self):
  363. super().clean()
  364. # Parent validation
  365. # An interface cannot be its own parent
  366. if self.pk and self.parent_id == self.pk:
  367. raise ValidationError({'parent': "An interface cannot be its own parent."})
  368. # An interface's parent must belong to the same virtual machine
  369. if self.parent and self.parent.virtual_machine != self.virtual_machine:
  370. raise ValidationError({
  371. 'parent': f"The selected parent interface ({self.parent}) belongs to a different virtual machine "
  372. f"({self.parent.virtual_machine})."
  373. })
  374. # Bridge validation
  375. # An interface cannot be bridged to itself
  376. if self.pk and self.bridge_id == self.pk:
  377. raise ValidationError({'bridge': "An interface cannot be bridged to itself."})
  378. # A bridged interface belong to the same virtual machine
  379. if self.bridge and self.bridge.virtual_machine != self.virtual_machine:
  380. raise ValidationError({
  381. 'bridge': f"The selected bridge interface ({self.bridge}) belongs to a different virtual machine "
  382. f"({self.bridge.virtual_machine})."
  383. })
  384. # VLAN validation
  385. # Validate untagged VLAN
  386. if self.untagged_vlan and self.untagged_vlan.site not in [self.virtual_machine.site, None]:
  387. raise ValidationError({
  388. 'untagged_vlan': f"The untagged VLAN ({self.untagged_vlan}) must belong to the same site as the "
  389. f"interface's parent virtual machine, or it must be global."
  390. })
  391. def to_objectchange(self, action):
  392. objectchange = super().to_objectchange(action)
  393. objectchange.related_object = self.virtual_machine
  394. return objectchange
  395. @property
  396. def parent_object(self):
  397. return self.virtual_machine