Coverage for virtualization/graphql/types.py: 85%

38 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 Location, Region, Site, SiteGroup 

7from extras.graphql.mixins import ConfigContextMixin, ContactsMixin 

8from ipam.graphql.mixins import IPAddressesMixin, VLANGroupsMixin 

9from netbox.graphql.optimization import build_gfk_prefetch 

10from netbox.graphql.scalars import BigInt 

11from netbox.graphql.types import NetBoxObjectType, OrganizationalObjectType, PrimaryObjectType, register_type 

12from users.graphql.mixins import OwnerMixin 

13from virtualization import models 

14 

15from .filters import * 

16 

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

18 from dcim.graphql.types import ( 

19 DeviceRoleType, 

20 DeviceType, 

21 LocationType, 

22 MACAddressType, 

23 PlatformType, 

24 RegionType, 

25 SiteGroupType, 

26 SiteType, 

27 ) 

28 from extras.graphql.types import ConfigTemplateType 

29 from ipam.graphql.types import IPAddressType, ServiceType, VLANTranslationPolicyType, VLANType, VRFType 

30 from tenancy.graphql.types import TenantType 

31 

32__all__ = ( 

33 'ClusterGroupType', 

34 'ClusterType', 

35 'ClusterTypeType', 

36 'VMInterfaceType', 

37 'VirtualDiskType', 

38 'VirtualMachineType', 

39 'VirtualMachineTypeType', 

40) 

41 

42 

43@strawberry.type 

44class ComponentType(OwnerMixin, NetBoxObjectType): 

45 """ 

46 Base type for device/VM components 

47 """ 

48 virtual_machine: Annotated["VirtualMachineType", strawberry.lazy('virtualization.graphql.types')] 

49 

50 

51@register_type( 

52 models.Cluster, 

53 exclude=['scope_type', 'scope_id', '_location', '_region', '_site', '_site_group'], 

54 filters=ClusterFilter, 

55 pagination=True 

56) 

57class ClusterType(ContactsMixin, VLANGroupsMixin, PrimaryObjectType): 

58 type: Annotated["ClusterTypeType", strawberry.lazy('virtualization.graphql.types')] | None 

59 group: Annotated["ClusterGroupType", strawberry.lazy('virtualization.graphql.types')] | None 

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

61 virtual_machines: list[Annotated["VirtualMachineType", strawberry.lazy('virtualization.graphql.types')]] 

62 devices: list[Annotated["DeviceType", strawberry.lazy('dcim.graphql.types')]] 

63 

64 @strawberry_django.field( 

65 prefetch_related=build_gfk_prefetch( 

66 'scope', 

67 [ 

68 Region, 

69 SiteGroup, 

70 Site, 

71 Location, 

72 ], 

73 ), 

74 only=['scope_type', 'scope_id'], 

75 ) 

76 def scope(self) -> Annotated[ 

77 Annotated['LocationType', strawberry.lazy('dcim.graphql.types')] 

78 | Annotated['RegionType', strawberry.lazy('dcim.graphql.types')] 

79 | Annotated['SiteGroupType', strawberry.lazy('dcim.graphql.types')] 

80 | Annotated['SiteType', strawberry.lazy('dcim.graphql.types')], 

81 strawberry.union('ClusterScopeType'), 

82 ] | None: 

83 return self.scope 

84 

85 

86@register_type( 

87 models.ClusterGroup, 

88 fields='__all__', 

89 filters=ClusterGroupFilter, 

90 pagination=True 

91) 

92class ClusterGroupType(ContactsMixin, VLANGroupsMixin, OrganizationalObjectType): 

93 

94 clusters: list[Annotated["ClusterType", strawberry.lazy('virtualization.graphql.types')]] 

95 

96 

97@register_type( 

98 models.ClusterType, 

99 fields='__all__', 

100 filters=ClusterTypeFilter, 

101 pagination=True 

102) 

103class ClusterTypeType(OrganizationalObjectType): 

104 

105 clusters: list[ClusterType] 

106 

107 

108@register_type( 

109 models.VirtualMachineType, 

110 fields='__all__', 

111 filters=VirtualMachineTypeFilter, 

112 pagination=True 

113) 

114class VirtualMachineTypeType(PrimaryObjectType): 

115 virtual_machine_count: BigInt 

116 default_platform: Annotated['PlatformType', strawberry.lazy('dcim.graphql.types')] | None 

117 

118 instances: list[Annotated['VirtualMachineType', strawberry.lazy('virtualization.graphql.types')]] 

119 

120 

121@register_type( 

122 models.VirtualMachine, 

123 fields='__all__', 

124 filters=VirtualMachineFilter, 

125 pagination=True 

126) 

127class VirtualMachineType(ConfigContextMixin, ContactsMixin, PrimaryObjectType): 

128 interface_count: BigInt 

129 virtual_disk_count: BigInt 

130 interface_count: BigInt 

131 virtual_machine_type: Annotated['VirtualMachineTypeType', strawberry.lazy('virtualization.graphql.types')] | None 

132 config_template: Annotated["ConfigTemplateType", strawberry.lazy('extras.graphql.types')] | None 

133 site: Annotated["SiteType", strawberry.lazy('dcim.graphql.types')] | None 

134 cluster: Annotated["ClusterType", strawberry.lazy('virtualization.graphql.types')] | None 

135 device: Annotated["DeviceType", strawberry.lazy('dcim.graphql.types')] | None 

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

137 platform: Annotated["PlatformType", strawberry.lazy('dcim.graphql.types')] | None 

138 role: Annotated["DeviceRoleType", strawberry.lazy('dcim.graphql.types')] | None 

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

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

141 

142 interfaces: list[Annotated["VMInterfaceType", strawberry.lazy('virtualization.graphql.types')]] 

143 services: list[Annotated["ServiceType", strawberry.lazy('ipam.graphql.types')]] 

144 virtualdisks: list[Annotated["VirtualDiskType", strawberry.lazy('virtualization.graphql.types')]] 

145 

146 

147@register_type( 

148 models.VMInterface, 

149 fields='__all__', 

150 filters=VMInterfaceFilter, 

151 pagination=True 

152) 

153class VMInterfaceType(IPAddressesMixin, ComponentType): 

154 _name: str 

155 mac_address: str | None 

156 parent: Annotated["VMInterfaceType", strawberry.lazy('virtualization.graphql.types')] | None 

157 bridge: Annotated["VMInterfaceType", strawberry.lazy('virtualization.graphql.types')] | None 

158 untagged_vlan: Annotated["VLANType", strawberry.lazy('ipam.graphql.types')] | None 

159 vrf: Annotated["VRFType", strawberry.lazy('ipam.graphql.types')] | None 

160 primary_mac_address: Annotated["MACAddressType", strawberry.lazy('dcim.graphql.types')] | None 

161 qinq_svlan: Annotated["VLANType", strawberry.lazy('ipam.graphql.types')] | None 

162 vlan_translation_policy: Annotated["VLANTranslationPolicyType", strawberry.lazy('ipam.graphql.types')] | None 

163 

164 tagged_vlans: list[Annotated["VLANType", strawberry.lazy('ipam.graphql.types')]] 

165 bridge_interfaces: list[Annotated["VMInterfaceType", strawberry.lazy('virtualization.graphql.types')]] 

166 child_interfaces: list[Annotated["VMInterfaceType", strawberry.lazy('virtualization.graphql.types')]] 

167 mac_addresses: list[Annotated["MACAddressType", strawberry.lazy('dcim.graphql.types')]] 

168 

169 

170@register_type( 

171 models.VirtualDisk, 

172 fields='__all__', 

173 filters=VirtualDiskFilter, 

174 pagination=True 

175) 

176class VirtualDiskType(ComponentType): 

177 pass