fhrp.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.db import models
  4. from django.urls import reverse
  5. from extras.utils import extras_features
  6. from netbox.models import ChangeLoggedModel, PrimaryModel
  7. from ipam.choices import *
  8. from utilities.querysets import RestrictedQuerySet
  9. __all__ = (
  10. 'FHRPGroup',
  11. 'FHRPGroupAssignment',
  12. )
  13. @extras_features('custom_fields', 'custom_links', 'export_templates', 'tags', 'webhooks')
  14. class FHRPGroup(PrimaryModel):
  15. """
  16. A grouping of next hope resolution protocol (FHRP) peers. (For instance, VRRP or HSRP.)
  17. """
  18. group_id = models.PositiveSmallIntegerField(
  19. verbose_name='Group ID'
  20. )
  21. protocol = models.CharField(
  22. max_length=50,
  23. choices=FHRPGroupProtocolChoices
  24. )
  25. auth_type = models.CharField(
  26. max_length=50,
  27. choices=FHRPGroupAuthTypeChoices,
  28. blank=True,
  29. verbose_name='Authentication type'
  30. )
  31. auth_key = models.CharField(
  32. max_length=255,
  33. blank=True,
  34. verbose_name='Authentication key'
  35. )
  36. description = models.CharField(
  37. max_length=200,
  38. blank=True
  39. )
  40. ip_addresses = GenericRelation(
  41. to='ipam.IPAddress',
  42. content_type_field='assigned_object_type',
  43. object_id_field='assigned_object_id',
  44. related_query_name='nhrp_group'
  45. )
  46. objects = RestrictedQuerySet.as_manager()
  47. clone_fields = [
  48. 'protocol', 'auth_type', 'auth_key'
  49. ]
  50. class Meta:
  51. ordering = ['protocol', 'group_id', 'pk']
  52. verbose_name = 'FHRP group'
  53. def __str__(self):
  54. return f'{self.get_protocol_display()} group {self.group_id}'
  55. def get_absolute_url(self):
  56. return reverse('ipam:fhrpgroup', args=[self.pk])
  57. @extras_features('webhooks')
  58. class FHRPGroupAssignment(ChangeLoggedModel):
  59. content_type = models.ForeignKey(
  60. to=ContentType,
  61. on_delete=models.CASCADE
  62. )
  63. object_id = models.PositiveIntegerField()
  64. object = GenericForeignKey(
  65. ct_field='content_type',
  66. fk_field='object_id'
  67. )
  68. group = models.ForeignKey(
  69. to='ipam.FHRPGroup',
  70. on_delete=models.CASCADE
  71. )
  72. priority = models.PositiveSmallIntegerField(
  73. blank=True,
  74. null=True
  75. )
  76. objects = RestrictedQuerySet.as_manager()
  77. class Meta:
  78. ordering = ('priority', 'pk')
  79. unique_together = ('content_type', 'object_id', 'group')
  80. verbose_name = 'FHRP group assignment'