providers.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. from django.db import models
  2. from django.db.models import Q
  3. from django.utils.translation import gettext_lazy as _
  4. from netbox.models import PrimaryModel
  5. from netbox.models.features import ContactsMixin
  6. __all__ = (
  7. 'ProviderNetwork',
  8. 'Provider',
  9. 'ProviderAccount',
  10. )
  11. class Provider(ContactsMixin, PrimaryModel):
  12. """
  13. Each Circuit belongs to a Provider. This is usually a telecommunications company or similar organization. This model
  14. stores information pertinent to the user's relationship with the Provider.
  15. """
  16. name = models.CharField(
  17. verbose_name=_('name'),
  18. max_length=100,
  19. unique=True,
  20. help_text=_('Full name of the provider'),
  21. db_collation="natural_sort"
  22. )
  23. slug = models.SlugField(
  24. verbose_name=_('slug'),
  25. max_length=100,
  26. unique=True
  27. )
  28. asns = models.ManyToManyField(
  29. to='ipam.ASN',
  30. related_name='providers',
  31. blank=True
  32. )
  33. clone_fields = ()
  34. class Meta:
  35. ordering = ['name']
  36. verbose_name = _('provider')
  37. verbose_name_plural = _('providers')
  38. def __str__(self):
  39. return self.name
  40. class ProviderAccount(ContactsMixin, PrimaryModel):
  41. """
  42. This is a discrete account within a provider. Each Circuit belongs to a Provider Account.
  43. """
  44. provider = models.ForeignKey(
  45. to='circuits.Provider',
  46. on_delete=models.PROTECT,
  47. related_name='accounts'
  48. )
  49. account = models.CharField(
  50. max_length=100,
  51. verbose_name=_('account ID')
  52. )
  53. name = models.CharField(
  54. verbose_name=_('name'),
  55. max_length=100,
  56. blank=True
  57. )
  58. clone_fields = ('provider', )
  59. class Meta:
  60. ordering = ('provider', 'account')
  61. constraints = (
  62. models.UniqueConstraint(
  63. fields=('provider', 'account'),
  64. name='%(app_label)s_%(class)s_unique_provider_account'
  65. ),
  66. models.UniqueConstraint(
  67. fields=('provider', 'name'),
  68. name='%(app_label)s_%(class)s_unique_provider_name',
  69. condition=~Q(name="")
  70. ),
  71. )
  72. verbose_name = _('provider account')
  73. verbose_name_plural = _('provider accounts')
  74. def __str__(self):
  75. if self.name:
  76. return f'{self.account} ({self.name})'
  77. return f'{self.account}'
  78. class ProviderNetwork(PrimaryModel):
  79. """
  80. This represents a provider network which exists outside of NetBox, the details of which are unknown or
  81. unimportant to the user.
  82. """
  83. name = models.CharField(
  84. verbose_name=_('name'),
  85. max_length=100,
  86. db_collation="natural_sort"
  87. )
  88. provider = models.ForeignKey(
  89. to='circuits.Provider',
  90. on_delete=models.PROTECT,
  91. related_name='networks'
  92. )
  93. service_id = models.CharField(
  94. max_length=100,
  95. blank=True,
  96. verbose_name=_('service ID')
  97. )
  98. class Meta:
  99. ordering = ('provider', 'name')
  100. constraints = (
  101. models.UniqueConstraint(
  102. fields=('provider', 'name'),
  103. name='%(app_label)s_%(class)s_unique_provider_name'
  104. ),
  105. )
  106. verbose_name = _('provider network')
  107. verbose_name_plural = _('provider networks')
  108. def __str__(self):
  109. return self.name