types.py 4.4 KB

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