types.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. from typing import TYPE_CHECKING, Annotated
  2. import strawberry
  3. import strawberry_django
  4. from strawberry.scalars import JSON
  5. from core.graphql.mixins import SyncedDataMixin
  6. from extras import models
  7. from extras.graphql.mixins import CustomFieldsMixin, TagsMixin
  8. from netbox.graphql.types import BaseObjectType, ContentTypeType, ObjectType, PrimaryObjectType
  9. from users.graphql.mixins import OwnerMixin
  10. from .filters import *
  11. if TYPE_CHECKING:
  12. from dcim.graphql.types import (
  13. DeviceRoleType,
  14. DeviceType,
  15. DeviceTypeType,
  16. LocationType,
  17. PlatformType,
  18. RegionType,
  19. SiteGroupType,
  20. SiteType,
  21. )
  22. from tenancy.graphql.types import TenantGroupType, TenantType
  23. from users.graphql.types import GroupType, UserType
  24. from virtualization.graphql.types import ClusterGroupType, ClusterType, ClusterTypeType, VirtualMachineType
  25. __all__ = (
  26. 'ConfigContextProfileType',
  27. 'ConfigContextType',
  28. 'ConfigTemplateType',
  29. 'CustomFieldChoiceSetType',
  30. 'CustomFieldType',
  31. 'CustomLinkType',
  32. 'EventRuleType',
  33. 'ExportTemplateType',
  34. 'ImageAttachmentType',
  35. 'JournalEntryType',
  36. 'NotificationGroupType',
  37. 'NotificationType',
  38. 'SavedFilterType',
  39. 'SubscriptionType',
  40. 'TableConfigType',
  41. 'TagType',
  42. 'WebhookType',
  43. )
  44. @strawberry_django.type(
  45. models.ConfigContextProfile,
  46. fields='__all__',
  47. filters=ConfigContextProfileFilter,
  48. pagination=True
  49. )
  50. class ConfigContextProfileType(SyncedDataMixin, PrimaryObjectType):
  51. pass
  52. @strawberry_django.type(
  53. models.ConfigContext,
  54. fields='__all__',
  55. filters=ConfigContextFilter,
  56. pagination=True
  57. )
  58. class ConfigContextType(SyncedDataMixin, OwnerMixin, ObjectType):
  59. profile: ConfigContextProfileType | None
  60. roles: list[Annotated["DeviceRoleType", strawberry.lazy('dcim.graphql.types')]]
  61. device_types: list[Annotated["DeviceTypeType", strawberry.lazy('dcim.graphql.types')]]
  62. tags: list[Annotated["TagType", strawberry.lazy('extras.graphql.types')]]
  63. platforms: list[Annotated["PlatformType", strawberry.lazy('dcim.graphql.types')]]
  64. regions: list[Annotated["RegionType", strawberry.lazy('dcim.graphql.types')]]
  65. cluster_groups: list[Annotated["ClusterGroupType", strawberry.lazy('virtualization.graphql.types')]]
  66. tenant_groups: list[Annotated["TenantGroupType", strawberry.lazy('tenancy.graphql.types')]]
  67. cluster_types: list[Annotated["ClusterTypeType", strawberry.lazy('virtualization.graphql.types')]]
  68. clusters: list[Annotated["ClusterType", strawberry.lazy('virtualization.graphql.types')]]
  69. locations: list[Annotated["LocationType", strawberry.lazy('dcim.graphql.types')]]
  70. sites: list[Annotated["SiteType", strawberry.lazy('dcim.graphql.types')]]
  71. tenants: list[Annotated["TenantType", strawberry.lazy('tenancy.graphql.types')]]
  72. site_groups: list[Annotated["SiteGroupType", strawberry.lazy('dcim.graphql.types')]]
  73. @strawberry_django.type(
  74. models.ConfigTemplate,
  75. fields='__all__',
  76. filters=ConfigTemplateFilter,
  77. pagination=True
  78. )
  79. class ConfigTemplateType(SyncedDataMixin, OwnerMixin, TagsMixin, ObjectType):
  80. virtualmachines: list[Annotated["VirtualMachineType", strawberry.lazy('virtualization.graphql.types')]]
  81. devices: list[Annotated["DeviceType", strawberry.lazy('dcim.graphql.types')]]
  82. platforms: list[Annotated["PlatformType", strawberry.lazy('dcim.graphql.types')]]
  83. device_roles: list[Annotated["DeviceRoleType", strawberry.lazy('dcim.graphql.types')]]
  84. @strawberry_django.type(
  85. models.CustomField,
  86. fields='__all__',
  87. filters=CustomFieldFilter,
  88. pagination=True
  89. )
  90. class CustomFieldType(OwnerMixin, ObjectType):
  91. related_object_type: Annotated["ContentTypeType", strawberry.lazy('netbox.graphql.types')] | None
  92. choice_set: Annotated["CustomFieldChoiceSetType", strawberry.lazy('extras.graphql.types')] | None
  93. @strawberry_django.type(
  94. models.CustomFieldChoiceSet,
  95. exclude=['extra_choices', 'choice_colors'],
  96. filters=CustomFieldChoiceSetFilter,
  97. pagination=True
  98. )
  99. class CustomFieldChoiceSetType(OwnerMixin, ObjectType):
  100. choices_for: list[Annotated["CustomFieldType", strawberry.lazy('extras.graphql.types')]]
  101. extra_choices: list[list[str]] | None
  102. choice_colors: JSON
  103. @strawberry_django.type(
  104. models.CustomLink,
  105. fields='__all__',
  106. filters=CustomLinkFilter,
  107. pagination=True
  108. )
  109. class CustomLinkType(OwnerMixin, ObjectType):
  110. pass
  111. @strawberry_django.type(
  112. models.ExportTemplate,
  113. fields='__all__',
  114. filters=ExportTemplateFilter,
  115. pagination=True
  116. )
  117. class ExportTemplateType(SyncedDataMixin, OwnerMixin, ObjectType):
  118. pass
  119. @strawberry_django.type(
  120. models.ImageAttachment,
  121. fields='__all__',
  122. filters=ImageAttachmentFilter,
  123. pagination=True
  124. )
  125. class ImageAttachmentType(BaseObjectType):
  126. object_type: Annotated["ContentTypeType", strawberry.lazy('netbox.graphql.types')] | None
  127. @strawberry_django.type(
  128. models.JournalEntry,
  129. fields='__all__',
  130. filters=JournalEntryFilter,
  131. pagination=True
  132. )
  133. class JournalEntryType(CustomFieldsMixin, TagsMixin, ObjectType):
  134. assigned_object_type: Annotated["ContentTypeType", strawberry.lazy('netbox.graphql.types')] | None
  135. created_by: Annotated["UserType", strawberry.lazy('users.graphql.types')] | None
  136. @strawberry_django.type(
  137. models.Notification,
  138. filters=NotificationFilter,
  139. pagination=True
  140. )
  141. class NotificationType(ObjectType):
  142. user: Annotated["UserType", strawberry.lazy('users.graphql.types')] | None
  143. @strawberry_django.type(
  144. models.NotificationGroup,
  145. filters=NotificationGroupFilter,
  146. pagination=True
  147. )
  148. class NotificationGroupType(ObjectType):
  149. users: list[Annotated["UserType", strawberry.lazy('users.graphql.types')]]
  150. groups: list[Annotated["GroupType", strawberry.lazy('users.graphql.types')]]
  151. @strawberry_django.type(
  152. models.SavedFilter,
  153. exclude=['content_types',],
  154. filters=SavedFilterFilter,
  155. pagination=True
  156. )
  157. class SavedFilterType(OwnerMixin, ObjectType):
  158. user: Annotated["UserType", strawberry.lazy('users.graphql.types')] | None
  159. @strawberry_django.type(
  160. models.Subscription,
  161. filters=SubscriptionFilter,
  162. pagination=True
  163. )
  164. class SubscriptionType(ObjectType):
  165. user: Annotated["UserType", strawberry.lazy('users.graphql.types')] | None
  166. @strawberry_django.type(
  167. models.TableConfig,
  168. fields='__all__',
  169. filters=TableConfigFilter,
  170. pagination=True
  171. )
  172. class TableConfigType(ObjectType):
  173. object_type: Annotated["ContentTypeType", strawberry.lazy('netbox.graphql.types')] | None
  174. user: Annotated["UserType", strawberry.lazy('users.graphql.types')] | None
  175. @strawberry_django.type(
  176. models.Tag,
  177. exclude=['extras_taggeditem_items', ],
  178. filters=TagFilter,
  179. pagination=True
  180. )
  181. class TagType(OwnerMixin, ObjectType):
  182. color: str
  183. object_types: list[ContentTypeType]
  184. @strawberry_django.type(
  185. models.Webhook,
  186. exclude=['content_types',],
  187. filters=WebhookFilter,
  188. pagination=True
  189. )
  190. class WebhookType(OwnerMixin, CustomFieldsMixin, TagsMixin, ObjectType):
  191. pass
  192. @strawberry_django.type(
  193. models.EventRule,
  194. exclude=['content_types',],
  195. filters=EventRuleFilter,
  196. pagination=True
  197. )
  198. class EventRuleType(OwnerMixin, CustomFieldsMixin, TagsMixin, ObjectType):
  199. action_object_type: Annotated["ContentTypeType", strawberry.lazy('netbox.graphql.types')] | None