types.py 5.4 KB

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