models.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. status = models.CharField(
  105. max_length=50,
  106. choices=ClusterStatusChoices,
  107. default=ClusterStatusChoices.STATUS_ACTIVE
  108. )
  109. tenant = models.ForeignKey(
  110. to='tenancy.Tenant',
  111. on_delete=models.PROTECT,
  112. related_name='clusters',
  113. blank=True,
  114. null=True
  115. )
  116. site = models.ForeignKey(
  117. to='dcim.Site',
  118. on_delete=models.PROTECT,
  119. related_name='clusters',
  120. blank=True,
  121. null=True
  122. )
  123. comments = models.TextField(
  124. blank=True
  125. )
  126. # Generic relations
  127. vlan_groups = GenericRelation(
  128. to='ipam.VLANGroup',
  129. content_type_field='scope_type',
  130. object_id_field='scope_id',
  131. related_query_name='cluster'
  132. )
  133. contacts = GenericRelation(
  134. to='tenancy.ContactAssignment'
  135. )
  136. clone_fields = [
  137. 'type', 'group', 'tenant', 'site',
  138. ]
  139. class Meta:
  140. ordering = ['name']
  141. unique_together = (
  142. ('group', 'name'),
  143. ('site', 'name'),
  144. )
  145. def __str__(self):
  146. return self.name
  147. def get_absolute_url(self):
  148. return reverse('virtualization:cluster', args=[self.pk])
  149. def get_status_color(self):
  150. return ClusterStatusChoices.colors.get(self.status)
  151. def clean(self):
  152. super().clean()
  153. # If the Cluster is assigned to a Site, verify that all host Devices belong to that Site.
  154. if self.pk and self.site:
  155. nonsite_devices = Device.objects.filter(cluster=self).exclude(site=self.site).count()
  156. if nonsite_devices:
  157. raise ValidationError({
  158. 'site': "{} devices are assigned as hosts for this cluster but are not in site {}".format(
  159. nonsite_devices, self.site
  160. )
  161. })
  162. #
  163. # Virtual machines
  164. #
  165. class VirtualMachine(NetBoxModel, ConfigContextModel):
  166. """
  167. A virtual machine which runs inside a Cluster.
  168. """
  169. cluster = models.ForeignKey(
  170. to='virtualization.Cluster',
  171. on_delete=models.PROTECT,
  172. related_name='virtual_machines'
  173. )
  174. tenant = models.ForeignKey(
  175. to='tenancy.Tenant',
  176. on_delete=models.PROTECT,
  177. related_name='virtual_machines',
  178. blank=True,
  179. null=True
  180. )
  181. platform = models.ForeignKey(
  182. to='dcim.Platform',
  183. on_delete=models.SET_NULL,
  184. related_name='virtual_machines',
  185. blank=True,
  186. null=True
  187. )
  188. name = models.CharField(
  189. max_length=64
  190. )
  191. _name = NaturalOrderingField(
  192. target_field='name',
  193. max_length=100,
  194. blank=True
  195. )
  196. status = models.CharField(
  197. max_length=50,
  198. choices=VirtualMachineStatusChoices,
  199. default=VirtualMachineStatusChoices.STATUS_ACTIVE,
  200. verbose_name='Status'
  201. )
  202. role = models.ForeignKey(
  203. to='dcim.DeviceRole',
  204. on_delete=models.PROTECT,
  205. related_name='virtual_machines',
  206. limit_choices_to={'vm_role': True},
  207. blank=True,
  208. null=True
  209. )
  210. primary_ip4 = models.OneToOneField(
  211. to='ipam.IPAddress',
  212. on_delete=models.SET_NULL,
  213. related_name='+',
  214. blank=True,
  215. null=True,
  216. verbose_name='Primary IPv4'
  217. )
  218. primary_ip6 = models.OneToOneField(
  219. to='ipam.IPAddress',
  220. on_delete=models.SET_NULL,
  221. related_name='+',
  222. blank=True,
  223. null=True,
  224. verbose_name='Primary IPv6'
  225. )
  226. vcpus = models.DecimalField(
  227. max_digits=6,
  228. decimal_places=2,
  229. blank=True,
  230. null=True,
  231. verbose_name='vCPUs',
  232. validators=(
  233. MinValueValidator(0.01),
  234. )
  235. )
  236. memory = models.PositiveIntegerField(
  237. blank=True,
  238. null=True,
  239. verbose_name='Memory (MB)'
  240. )
  241. disk = models.PositiveIntegerField(
  242. blank=True,
  243. null=True,
  244. verbose_name='Disk (GB)'
  245. )
  246. comments = models.TextField(
  247. blank=True
  248. )
  249. # Generic relation
  250. contacts = GenericRelation(
  251. to='tenancy.ContactAssignment'
  252. )
  253. objects = ConfigContextModelQuerySet.as_manager()
  254. clone_fields = [
  255. 'cluster', 'tenant', 'platform', 'status', 'role', 'vcpus', 'memory', 'disk',
  256. ]
  257. class Meta:
  258. ordering = ('_name', 'pk') # Name may be non-unique
  259. unique_together = [
  260. ['cluster', 'tenant', 'name']
  261. ]
  262. def __str__(self):
  263. return self.name
  264. def get_absolute_url(self):
  265. return reverse('virtualization:virtualmachine', args=[self.pk])
  266. def validate_unique(self, exclude=None):
  267. # Check for a duplicate name on a VM assigned to the same Cluster and no Tenant. This is necessary
  268. # because Django does not consider two NULL fields to be equal, and thus will not trigger a violation
  269. # of the uniqueness constraint without manual intervention.
  270. if self.tenant is None and VirtualMachine.objects.exclude(pk=self.pk).filter(
  271. name=self.name, cluster=self.cluster, tenant__isnull=True
  272. ):
  273. raise ValidationError({
  274. 'name': 'A virtual machine with this name already exists in the assigned cluster.'
  275. })
  276. super().validate_unique(exclude)
  277. def clean(self):
  278. super().clean()
  279. # Validate primary IP addresses
  280. interfaces = self.interfaces.all()
  281. for field in ['primary_ip4', 'primary_ip6']:
  282. ip = getattr(self, field)
  283. if ip is not None:
  284. if ip.assigned_object in interfaces:
  285. pass
  286. elif ip.nat_inside is not None and ip.nat_inside.assigned_object in interfaces:
  287. pass
  288. else:
  289. raise ValidationError({
  290. field: f"The specified IP address ({ip}) is not assigned to this VM.",
  291. })
  292. def get_status_color(self):
  293. return VirtualMachineStatusChoices.colors.get(self.status)
  294. @property
  295. def primary_ip(self):
  296. if get_config().PREFER_IPV4 and self.primary_ip4:
  297. return self.primary_ip4
  298. elif self.primary_ip6:
  299. return self.primary_ip6
  300. elif self.primary_ip4:
  301. return self.primary_ip4
  302. else:
  303. return None
  304. @property
  305. def site(self):
  306. return self.cluster.site
  307. #
  308. # Interfaces
  309. #
  310. class VMInterface(NetBoxModel, BaseInterface):
  311. virtual_machine = models.ForeignKey(
  312. to='virtualization.VirtualMachine',
  313. on_delete=models.CASCADE,
  314. related_name='interfaces'
  315. )
  316. name = models.CharField(
  317. max_length=64
  318. )
  319. _name = NaturalOrderingField(
  320. target_field='name',
  321. naturalize_function=naturalize_interface,
  322. max_length=100,
  323. blank=True
  324. )
  325. description = models.CharField(
  326. max_length=200,
  327. blank=True
  328. )
  329. untagged_vlan = models.ForeignKey(
  330. to='ipam.VLAN',
  331. on_delete=models.SET_NULL,
  332. related_name='vminterfaces_as_untagged',
  333. null=True,
  334. blank=True,
  335. verbose_name='Untagged VLAN'
  336. )
  337. tagged_vlans = models.ManyToManyField(
  338. to='ipam.VLAN',
  339. related_name='vminterfaces_as_tagged',
  340. blank=True,
  341. verbose_name='Tagged VLANs'
  342. )
  343. ip_addresses = GenericRelation(
  344. to='ipam.IPAddress',
  345. content_type_field='assigned_object_type',
  346. object_id_field='assigned_object_id',
  347. related_query_name='vminterface'
  348. )
  349. vrf = models.ForeignKey(
  350. to='ipam.VRF',
  351. on_delete=models.SET_NULL,
  352. related_name='vminterfaces',
  353. null=True,
  354. blank=True,
  355. verbose_name='VRF'
  356. )
  357. fhrp_group_assignments = GenericRelation(
  358. to='ipam.FHRPGroupAssignment',
  359. content_type_field='interface_type',
  360. object_id_field='interface_id',
  361. related_query_name='+'
  362. )
  363. class Meta:
  364. verbose_name = 'interface'
  365. ordering = ('virtual_machine', CollateAsChar('_name'))
  366. unique_together = ('virtual_machine', 'name')
  367. def __str__(self):
  368. return self.name
  369. def get_absolute_url(self):
  370. return reverse('virtualization:vminterface', kwargs={'pk': self.pk})
  371. def clean(self):
  372. super().clean()
  373. # Parent validation
  374. # An interface cannot be its own parent
  375. if self.pk and self.parent_id == self.pk:
  376. raise ValidationError({'parent': "An interface cannot be its own parent."})
  377. # An interface's parent must belong to the same virtual machine
  378. if self.parent and self.parent.virtual_machine != self.virtual_machine:
  379. raise ValidationError({
  380. 'parent': f"The selected parent interface ({self.parent}) belongs to a different virtual machine "
  381. f"({self.parent.virtual_machine})."
  382. })
  383. # Bridge validation
  384. # An interface cannot be bridged to itself
  385. if self.pk and self.bridge_id == self.pk:
  386. raise ValidationError({'bridge': "An interface cannot be bridged to itself."})
  387. # A bridged interface belong to the same virtual machine
  388. if self.bridge and self.bridge.virtual_machine != self.virtual_machine:
  389. raise ValidationError({
  390. 'bridge': f"The selected bridge interface ({self.bridge}) belongs to a different virtual machine "
  391. f"({self.bridge.virtual_machine})."
  392. })
  393. # VLAN validation
  394. # Validate untagged VLAN
  395. if self.untagged_vlan and self.untagged_vlan.site not in [self.virtual_machine.site, None]:
  396. raise ValidationError({
  397. 'untagged_vlan': f"The untagged VLAN ({self.untagged_vlan}) must belong to the same site as the "
  398. f"interface's parent virtual machine, or it must be global."
  399. })
  400. def to_objectchange(self, action):
  401. objectchange = super().to_objectchange(action)
  402. objectchange.related_object = self.virtual_machine
  403. return objectchange
  404. @property
  405. def parent_object(self):
  406. return self.virtual_machine