types.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from typing import Annotated, List, Union
  2. import strawberry
  3. import strawberry_django
  4. from extras.graphql.mixins import ConfigContextMixin, ContactsMixin
  5. from ipam.graphql.mixins import IPAddressesMixin, VLANGroupsMixin
  6. from netbox.graphql.scalars import BigInt
  7. from netbox.graphql.types import OrganizationalObjectType, NetBoxObjectType
  8. from virtualization import models
  9. from .filters import *
  10. __all__ = (
  11. 'ClusterType',
  12. 'ClusterGroupType',
  13. 'ClusterTypeType',
  14. 'VirtualDiskType',
  15. 'VirtualMachineType',
  16. 'VMInterfaceType',
  17. )
  18. @strawberry.type
  19. class ComponentType(NetBoxObjectType):
  20. """
  21. Base type for device/VM components
  22. """
  23. virtual_machine: Annotated["VirtualMachineType", strawberry.lazy('virtualization.graphql.types')]
  24. @strawberry_django.type(
  25. models.Cluster,
  26. exclude=('scope_type', 'scope_id', '_location', '_region', '_site', '_site_group'),
  27. filters=ClusterFilter
  28. )
  29. class ClusterType(VLANGroupsMixin, NetBoxObjectType):
  30. type: Annotated["ClusterTypeType", strawberry.lazy('virtualization.graphql.types')] | None
  31. group: Annotated["ClusterGroupType", strawberry.lazy('virtualization.graphql.types')] | None
  32. tenant: Annotated["TenantType", strawberry.lazy('tenancy.graphql.types')] | None
  33. virtual_machines: List[Annotated["VirtualMachineType", strawberry.lazy('virtualization.graphql.types')]]
  34. devices: List[Annotated["DeviceType", strawberry.lazy('dcim.graphql.types')]]
  35. @strawberry_django.field
  36. def scope(self) -> Annotated[Union[
  37. Annotated["LocationType", strawberry.lazy('dcim.graphql.types')],
  38. Annotated["RegionType", strawberry.lazy('dcim.graphql.types')],
  39. Annotated["SiteGroupType", strawberry.lazy('dcim.graphql.types')],
  40. Annotated["SiteType", strawberry.lazy('dcim.graphql.types')],
  41. ], strawberry.union("ClusterScopeType")] | None:
  42. return self.scope
  43. @strawberry_django.type(
  44. models.ClusterGroup,
  45. fields='__all__',
  46. filters=ClusterGroupFilter
  47. )
  48. class ClusterGroupType(VLANGroupsMixin, OrganizationalObjectType):
  49. clusters: List[Annotated["ClusterType", strawberry.lazy('virtualization.graphql.types')]]
  50. @strawberry_django.type(
  51. models.ClusterType,
  52. fields='__all__',
  53. filters=ClusterTypeFilter
  54. )
  55. class ClusterTypeType(OrganizationalObjectType):
  56. clusters: List[ClusterType]
  57. @strawberry_django.type(
  58. models.VirtualMachine,
  59. fields='__all__',
  60. filters=VirtualMachineFilter
  61. )
  62. class VirtualMachineType(ConfigContextMixin, ContactsMixin, NetBoxObjectType):
  63. interface_count: BigInt
  64. virtual_disk_count: BigInt
  65. interface_count: BigInt
  66. config_template: Annotated["ConfigTemplateType", strawberry.lazy('extras.graphql.types')] | None
  67. site: Annotated["SiteType", strawberry.lazy('dcim.graphql.types')] | None
  68. cluster: Annotated["ClusterType", strawberry.lazy('virtualization.graphql.types')] | None
  69. device: Annotated["DeviceType", strawberry.lazy('dcim.graphql.types')] | None
  70. tenant: Annotated["TenantType", strawberry.lazy('tenancy.graphql.types')] | None
  71. platform: Annotated["PlatformType", strawberry.lazy('dcim.graphql.types')] | None
  72. role: Annotated["DeviceRoleType", strawberry.lazy('dcim.graphql.types')] | None
  73. primary_ip4: Annotated["IPAddressType", strawberry.lazy('ipam.graphql.types')] | None
  74. primary_ip6: Annotated["IPAddressType", strawberry.lazy('ipam.graphql.types')] | None
  75. interfaces: List[Annotated["VMInterfaceType", strawberry.lazy('virtualization.graphql.types')]]
  76. services: List[Annotated["ServiceType", strawberry.lazy('ipam.graphql.types')]]
  77. virtualdisks: List[Annotated["VirtualDiskType", strawberry.lazy('virtualization.graphql.types')]]
  78. @strawberry_django.type(
  79. models.VMInterface,
  80. fields='__all__',
  81. filters=VMInterfaceFilter
  82. )
  83. class VMInterfaceType(IPAddressesMixin, ComponentType):
  84. _name: str
  85. mac_address: str | None
  86. parent: Annotated["VMInterfaceType", strawberry.lazy('virtualization.graphql.types')] | None
  87. bridge: Annotated["VMInterfaceType", strawberry.lazy('virtualization.graphql.types')] | None
  88. untagged_vlan: Annotated["VLANType", strawberry.lazy('ipam.graphql.types')] | None
  89. vrf: Annotated["VRFType", strawberry.lazy('ipam.graphql.types')] | None
  90. qinq_svlan: Annotated["VLANType", strawberry.lazy('ipam.graphql.types')] | None
  91. vlan_translation_policy: Annotated["VLANTranslationPolicyType", strawberry.lazy('ipam.graphql.types')] | None
  92. tagged_vlans: List[Annotated["VLANType", strawberry.lazy('ipam.graphql.types')]]
  93. bridge_interfaces: List[Annotated["VMInterfaceType", strawberry.lazy('virtualization.graphql.types')]]
  94. child_interfaces: List[Annotated["VMInterfaceType", strawberry.lazy('virtualization.graphql.types')]]
  95. @strawberry_django.type(
  96. models.VirtualDisk,
  97. fields='__all__',
  98. filters=VirtualDiskFilter
  99. )
  100. class VirtualDiskType(ComponentType):
  101. pass