Coverage for vpn/graphql/types.py: 84%

41 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 18:35 +0000

1from typing import TYPE_CHECKING, Annotated 

2 

3import strawberry 

4import strawberry_django 

5 

6from dcim.models import Interface 

7from extras.graphql.mixins import ContactsMixin, CustomFieldsMixin, TagsMixin 

8from ipam.models import VLAN 

9from netbox.graphql.optimization import build_gfk_prefetch 

10from netbox.graphql.types import ( 

11 NetBoxObjectType, 

12 ObjectType, 

13 OrganizationalObjectType, 

14 PrimaryObjectType, 

15 register_type, 

16) 

17from virtualization.models import VMInterface 

18from vpn import models 

19 

20from .filters import * 

21 

22if TYPE_CHECKING: 22 ↛ 23line 22 didn't jump to line 23 because the condition on line 22 was never true

23 from dcim.graphql.types import InterfaceType 

24 from ipam.graphql.types import IPAddressType, RouteTargetType, VLANType 

25 from netbox.graphql.types import ContentTypeType 

26 from tenancy.graphql.types import TenantType 

27 from virtualization.graphql.types import VMInterfaceType 

28 

29__all__ = ( 

30 'IKEPolicyType', 

31 'IKEProposalType', 

32 'IPSecPolicyType', 

33 'IPSecProfileType', 

34 'IPSecProposalType', 

35 'L2VPNTerminationType', 

36 'L2VPNType', 

37 'TunnelGroupType', 

38 'TunnelTerminationType', 

39 'TunnelType', 

40) 

41 

42 

43@register_type( 

44 models.TunnelGroup, 

45 fields='__all__', 

46 filters=TunnelGroupFilter, 

47 pagination=True 

48) 

49class TunnelGroupType(ContactsMixin, OrganizationalObjectType): 

50 

51 tunnels: list[Annotated["TunnelType", strawberry.lazy('vpn.graphql.types')]] 

52 

53 

54@register_type( 

55 models.TunnelTermination, 

56 fields='__all__', 

57 filters=TunnelTerminationFilter, 

58 pagination=True 

59) 

60class TunnelTerminationType(CustomFieldsMixin, TagsMixin, ObjectType): 

61 tunnel: Annotated["TunnelType", strawberry.lazy('vpn.graphql.types')] 

62 termination_type: Annotated["ContentTypeType", strawberry.lazy('netbox.graphql.types')] | None 

63 outside_ip: Annotated["IPAddressType", strawberry.lazy('ipam.graphql.types')] | None 

64 

65 

66@register_type( 

67 models.Tunnel, 

68 fields='__all__', 

69 filters=TunnelFilter, 

70 pagination=True 

71) 

72class TunnelType(ContactsMixin, PrimaryObjectType): 

73 group: Annotated["TunnelGroupType", strawberry.lazy('vpn.graphql.types')] | None 

74 ipsec_profile: Annotated["IPSecProfileType", strawberry.lazy('vpn.graphql.types')] | None 

75 tenant: Annotated["TenantType", strawberry.lazy('tenancy.graphql.types')] | None 

76 

77 terminations: list[Annotated["TunnelTerminationType", strawberry.lazy('vpn.graphql.types')]] 

78 

79 

80@register_type( 

81 models.IKEProposal, 

82 fields='__all__', 

83 filters=IKEProposalFilter, 

84 pagination=True 

85) 

86class IKEProposalType(PrimaryObjectType): 

87 ike_policies: list[Annotated["IKEPolicyType", strawberry.lazy('vpn.graphql.types')]] 

88 

89 

90@register_type( 

91 models.IKEPolicy, 

92 fields='__all__', 

93 filters=IKEPolicyFilter, 

94 pagination=True 

95) 

96class IKEPolicyType(PrimaryObjectType): 

97 proposals: list[Annotated["IKEProposalType", strawberry.lazy('vpn.graphql.types')]] 

98 ipsec_profiles: list[Annotated["IPSecProfileType", strawberry.lazy('vpn.graphql.types')]] 

99 

100 

101@register_type( 

102 models.IPSecProposal, 

103 fields='__all__', 

104 filters=IPSecProposalFilter, 

105 pagination=True 

106) 

107class IPSecProposalType(PrimaryObjectType): 

108 ipsec_policies: list[Annotated["IPSecPolicyType", strawberry.lazy('vpn.graphql.types')]] 

109 

110 

111@register_type( 

112 models.IPSecPolicy, 

113 fields='__all__', 

114 filters=IPSecPolicyFilter, 

115 pagination=True 

116) 

117class IPSecPolicyType(PrimaryObjectType): 

118 proposals: list[Annotated["IPSecProposalType", strawberry.lazy('vpn.graphql.types')]] 

119 ipsec_profiles: list[Annotated["IPSecProfileType", strawberry.lazy('vpn.graphql.types')]] 

120 

121 

122@register_type( 

123 models.IPSecProfile, 

124 fields='__all__', 

125 filters=IPSecProfileFilter, 

126 pagination=True 

127) 

128class IPSecProfileType(PrimaryObjectType): 

129 ike_policy: Annotated["IKEPolicyType", strawberry.lazy('vpn.graphql.types')] 

130 ipsec_policy: Annotated["IPSecPolicyType", strawberry.lazy('vpn.graphql.types')] 

131 

132 tunnels: list[Annotated["TunnelType", strawberry.lazy('vpn.graphql.types')]] 

133 

134 

135@register_type( 

136 models.L2VPN, 

137 fields='__all__', 

138 filters=L2VPNFilter, 

139 pagination=True 

140) 

141class L2VPNType(ContactsMixin, PrimaryObjectType): 

142 tenant: Annotated["TenantType", strawberry.lazy('tenancy.graphql.types')] | None 

143 

144 export_targets: list[Annotated["RouteTargetType", strawberry.lazy('ipam.graphql.types')]] 

145 terminations: list[Annotated["L2VPNTerminationType", strawberry.lazy('vpn.graphql.types')]] 

146 import_targets: list[Annotated["RouteTargetType", strawberry.lazy('ipam.graphql.types')]] 

147 

148 

149@register_type( 

150 models.L2VPNTermination, 

151 exclude=['assigned_object_type', 'assigned_object_id'], 

152 filters=L2VPNTerminationFilter, 

153 pagination=True 

154) 

155class L2VPNTerminationType(NetBoxObjectType): 

156 l2vpn: Annotated["L2VPNType", strawberry.lazy('vpn.graphql.types')] 

157 

158 @strawberry_django.field( 

159 prefetch_related=build_gfk_prefetch( 

160 'assigned_object', 

161 [ 

162 Interface, 

163 VMInterface, 

164 VLAN, 

165 ], 

166 ), 

167 only=['assigned_object_type', 'assigned_object_id'], 

168 ) 

169 def assigned_object(self) -> Annotated[ 

170 Annotated['InterfaceType', strawberry.lazy('dcim.graphql.types')] 

171 | Annotated['VLANType', strawberry.lazy('ipam.graphql.types')] 

172 | Annotated['VMInterfaceType', strawberry.lazy('virtualization.graphql.types')], 

173 strawberry.union('L2VPNAssignmentType'), 

174 ]: 

175 return self.assigned_object