types.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. from typing import TYPE_CHECKING, Annotated
  2. import strawberry
  3. import strawberry_django
  4. from extras.graphql.mixins import ContactsMixin, CustomFieldsMixin, TagsMixin
  5. from netbox.graphql.types import NetBoxObjectType, ObjectType, OrganizationalObjectType, PrimaryObjectType
  6. from vpn import models
  7. from .filters import *
  8. if TYPE_CHECKING:
  9. from dcim.graphql.types import InterfaceType
  10. from ipam.graphql.types import IPAddressType, RouteTargetType, VLANType
  11. from netbox.graphql.types import ContentTypeType
  12. from tenancy.graphql.types import TenantType
  13. from virtualization.graphql.types import VMInterfaceType
  14. __all__ = (
  15. 'IKEPolicyType',
  16. 'IKEProposalType',
  17. 'IPSecPolicyType',
  18. 'IPSecProfileType',
  19. 'IPSecProposalType',
  20. 'L2VPNTerminationType',
  21. 'L2VPNType',
  22. 'TunnelGroupType',
  23. 'TunnelTerminationType',
  24. 'TunnelType',
  25. )
  26. @strawberry_django.type(
  27. models.TunnelGroup,
  28. fields='__all__',
  29. filters=TunnelGroupFilter,
  30. pagination=True
  31. )
  32. class TunnelGroupType(ContactsMixin, OrganizationalObjectType):
  33. tunnels: list[Annotated["TunnelType", strawberry.lazy('vpn.graphql.types')]]
  34. @strawberry_django.type(
  35. models.TunnelTermination,
  36. fields='__all__',
  37. filters=TunnelTerminationFilter,
  38. pagination=True
  39. )
  40. class TunnelTerminationType(CustomFieldsMixin, TagsMixin, ObjectType):
  41. tunnel: Annotated["TunnelType", strawberry.lazy('vpn.graphql.types')]
  42. termination_type: Annotated["ContentTypeType", strawberry.lazy('netbox.graphql.types')] | None
  43. outside_ip: Annotated["IPAddressType", strawberry.lazy('ipam.graphql.types')] | None
  44. @strawberry_django.type(
  45. models.Tunnel,
  46. fields='__all__',
  47. filters=TunnelFilter,
  48. pagination=True
  49. )
  50. class TunnelType(ContactsMixin, PrimaryObjectType):
  51. group: Annotated["TunnelGroupType", strawberry.lazy('vpn.graphql.types')] | None
  52. ipsec_profile: Annotated["IPSecProfileType", strawberry.lazy('vpn.graphql.types')] | None
  53. tenant: Annotated["TenantType", strawberry.lazy('tenancy.graphql.types')] | None
  54. terminations: list[Annotated["TunnelTerminationType", strawberry.lazy('vpn.graphql.types')]]
  55. @strawberry_django.type(
  56. models.IKEProposal,
  57. fields='__all__',
  58. filters=IKEProposalFilter,
  59. pagination=True
  60. )
  61. class IKEProposalType(PrimaryObjectType):
  62. ike_policies: list[Annotated["IKEPolicyType", strawberry.lazy('vpn.graphql.types')]]
  63. @strawberry_django.type(
  64. models.IKEPolicy,
  65. fields='__all__',
  66. filters=IKEPolicyFilter,
  67. pagination=True
  68. )
  69. class IKEPolicyType(PrimaryObjectType):
  70. proposals: list[Annotated["IKEProposalType", strawberry.lazy('vpn.graphql.types')]]
  71. ipsec_profiles: list[Annotated["IPSecProfileType", strawberry.lazy('vpn.graphql.types')]]
  72. @strawberry_django.type(
  73. models.IPSecProposal,
  74. fields='__all__',
  75. filters=IPSecProposalFilter,
  76. pagination=True
  77. )
  78. class IPSecProposalType(PrimaryObjectType):
  79. ipsec_policies: list[Annotated["IPSecPolicyType", strawberry.lazy('vpn.graphql.types')]]
  80. @strawberry_django.type(
  81. models.IPSecPolicy,
  82. fields='__all__',
  83. filters=IPSecPolicyFilter,
  84. pagination=True
  85. )
  86. class IPSecPolicyType(PrimaryObjectType):
  87. proposals: list[Annotated["IPSecProposalType", strawberry.lazy('vpn.graphql.types')]]
  88. ipsec_profiles: list[Annotated["IPSecProfileType", strawberry.lazy('vpn.graphql.types')]]
  89. @strawberry_django.type(
  90. models.IPSecProfile,
  91. fields='__all__',
  92. filters=IPSecProfileFilter,
  93. pagination=True
  94. )
  95. class IPSecProfileType(PrimaryObjectType):
  96. ike_policy: Annotated["IKEPolicyType", strawberry.lazy('vpn.graphql.types')]
  97. ipsec_policy: Annotated["IPSecPolicyType", strawberry.lazy('vpn.graphql.types')]
  98. tunnels: list[Annotated["TunnelType", strawberry.lazy('vpn.graphql.types')]]
  99. @strawberry_django.type(
  100. models.L2VPN,
  101. fields='__all__',
  102. filters=L2VPNFilter,
  103. pagination=True
  104. )
  105. class L2VPNType(ContactsMixin, PrimaryObjectType):
  106. tenant: Annotated["TenantType", strawberry.lazy('tenancy.graphql.types')] | None
  107. export_targets: list[Annotated["RouteTargetType", strawberry.lazy('ipam.graphql.types')]]
  108. terminations: list[Annotated["L2VPNTerminationType", strawberry.lazy('vpn.graphql.types')]]
  109. import_targets: list[Annotated["RouteTargetType", strawberry.lazy('ipam.graphql.types')]]
  110. @strawberry_django.type(
  111. models.L2VPNTermination,
  112. exclude=['assigned_object_type', 'assigned_object_id'],
  113. filters=L2VPNTerminationFilter,
  114. pagination=True
  115. )
  116. class L2VPNTerminationType(NetBoxObjectType):
  117. l2vpn: Annotated["L2VPNType", strawberry.lazy('vpn.graphql.types')]
  118. @strawberry_django.field
  119. def assigned_object(self) -> Annotated[
  120. Annotated['InterfaceType', strawberry.lazy('dcim.graphql.types')]
  121. | Annotated['VLANType', strawberry.lazy('ipam.graphql.types')]
  122. | Annotated['VMInterfaceType', strawberry.lazy('virtualization.graphql.types')],
  123. strawberry.union('L2VPNAssignmentType'),
  124. ]:
  125. return self.assigned_object