2
0

types.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. import graphene
  2. from dcim import filtersets, models
  3. from extras.graphql.mixins import (
  4. ChangelogMixin, ConfigContextMixin, ContactsMixin, CustomFieldsMixin, ImageAttachmentsMixin, TagsMixin,
  5. )
  6. from ipam.graphql.mixins import IPAddressesMixin, VLANGroupsMixin
  7. from netbox.graphql.scalars import BigInt
  8. from netbox.graphql.types import BaseObjectType, OrganizationalObjectType, NetBoxObjectType
  9. from .mixins import CabledObjectMixin
  10. __all__ = (
  11. 'CableType',
  12. 'ComponentObjectType',
  13. 'ConsolePortType',
  14. 'ConsolePortTemplateType',
  15. 'ConsoleServerPortType',
  16. 'ConsoleServerPortTemplateType',
  17. 'DeviceType',
  18. 'DeviceBayType',
  19. 'DeviceBayTemplateType',
  20. 'DeviceRoleType',
  21. 'DeviceTypeType',
  22. 'FrontPortType',
  23. 'FrontPortTemplateType',
  24. 'InterfaceType',
  25. 'InterfaceTemplateType',
  26. 'InventoryItemType',
  27. 'InventoryItemRoleType',
  28. 'InventoryItemTemplateType',
  29. 'LocationType',
  30. 'ManufacturerType',
  31. 'ModuleType',
  32. 'ModuleBayType',
  33. 'ModuleBayTemplateType',
  34. 'ModuleTypeType',
  35. 'PlatformType',
  36. 'PowerFeedType',
  37. 'PowerOutletType',
  38. 'PowerOutletTemplateType',
  39. 'PowerPanelType',
  40. 'PowerPortType',
  41. 'PowerPortTemplateType',
  42. 'RackType',
  43. 'RackReservationType',
  44. 'RackRoleType',
  45. 'RearPortType',
  46. 'RearPortTemplateType',
  47. 'RegionType',
  48. 'SiteType',
  49. 'SiteGroupType',
  50. 'VirtualChassisType',
  51. )
  52. #
  53. # Base types
  54. #
  55. class ComponentObjectType(
  56. ChangelogMixin,
  57. CustomFieldsMixin,
  58. TagsMixin,
  59. BaseObjectType
  60. ):
  61. """
  62. Base type for device/VM components
  63. """
  64. class Meta:
  65. abstract = True
  66. class ComponentTemplateObjectType(
  67. ChangelogMixin,
  68. BaseObjectType
  69. ):
  70. """
  71. Base type for device/VM components
  72. """
  73. class Meta:
  74. abstract = True
  75. #
  76. # Model types
  77. #
  78. class CableType(NetBoxObjectType):
  79. a_terminations = graphene.List('dcim.graphql.gfk_mixins.CableTerminationTerminationType')
  80. b_terminations = graphene.List('dcim.graphql.gfk_mixins.CableTerminationTerminationType')
  81. class Meta:
  82. model = models.Cable
  83. fields = '__all__'
  84. filterset_class = filtersets.CableFilterSet
  85. def resolve_type(self, info):
  86. return self.type or None
  87. def resolve_length_unit(self, info):
  88. return self.length_unit or None
  89. def resolve_a_terminations(self, info):
  90. return self.a_terminations
  91. def resolve_b_terminations(self, info):
  92. return self.b_terminations
  93. class CableTerminationType(NetBoxObjectType):
  94. termination = graphene.Field('dcim.graphql.gfk_mixins.CableTerminationTerminationType')
  95. class Meta:
  96. model = models.CableTermination
  97. exclude = ('termination_type', 'termination_id')
  98. filterset_class = filtersets.CableTerminationFilterSet
  99. class ConsolePortType(ComponentObjectType, CabledObjectMixin):
  100. class Meta:
  101. model = models.ConsolePort
  102. exclude = ('_path',)
  103. filterset_class = filtersets.ConsolePortFilterSet
  104. def resolve_type(self, info):
  105. return self.type or None
  106. class ConsolePortTemplateType(ComponentTemplateObjectType):
  107. class Meta:
  108. model = models.ConsolePortTemplate
  109. fields = '__all__'
  110. filterset_class = filtersets.ConsolePortTemplateFilterSet
  111. def resolve_type(self, info):
  112. return self.type or None
  113. class ConsoleServerPortType(ComponentObjectType, CabledObjectMixin):
  114. class Meta:
  115. model = models.ConsoleServerPort
  116. exclude = ('_path',)
  117. filterset_class = filtersets.ConsoleServerPortFilterSet
  118. def resolve_type(self, info):
  119. return self.type or None
  120. class ConsoleServerPortTemplateType(ComponentTemplateObjectType):
  121. class Meta:
  122. model = models.ConsoleServerPortTemplate
  123. fields = '__all__'
  124. filterset_class = filtersets.ConsoleServerPortTemplateFilterSet
  125. def resolve_type(self, info):
  126. return self.type or None
  127. class DeviceType(ConfigContextMixin, ImageAttachmentsMixin, ContactsMixin, NetBoxObjectType):
  128. class Meta:
  129. model = models.Device
  130. fields = '__all__'
  131. filterset_class = filtersets.DeviceFilterSet
  132. def resolve_face(self, info):
  133. return self.face or None
  134. def resolve_airflow(self, info):
  135. return self.airflow or None
  136. class DeviceBayType(ComponentObjectType):
  137. class Meta:
  138. model = models.DeviceBay
  139. fields = '__all__'
  140. filterset_class = filtersets.DeviceBayFilterSet
  141. class DeviceBayTemplateType(ComponentTemplateObjectType):
  142. class Meta:
  143. model = models.DeviceBayTemplate
  144. fields = '__all__'
  145. filterset_class = filtersets.DeviceBayTemplateFilterSet
  146. class InventoryItemTemplateType(ComponentTemplateObjectType):
  147. component = graphene.Field('dcim.graphql.gfk_mixins.InventoryItemTemplateComponentType')
  148. class Meta:
  149. model = models.InventoryItemTemplate
  150. exclude = ('component_type', 'component_id')
  151. filterset_class = filtersets.InventoryItemTemplateFilterSet
  152. class DeviceRoleType(OrganizationalObjectType):
  153. class Meta:
  154. model = models.DeviceRole
  155. fields = '__all__'
  156. filterset_class = filtersets.DeviceRoleFilterSet
  157. class DeviceTypeType(NetBoxObjectType):
  158. class Meta:
  159. model = models.DeviceType
  160. fields = '__all__'
  161. filterset_class = filtersets.DeviceTypeFilterSet
  162. def resolve_subdevice_role(self, info):
  163. return self.subdevice_role or None
  164. def resolve_airflow(self, info):
  165. return self.airflow or None
  166. def resolve_weight_unit(self, info):
  167. return self.weight_unit or None
  168. class FrontPortType(ComponentObjectType, CabledObjectMixin):
  169. class Meta:
  170. model = models.FrontPort
  171. fields = '__all__'
  172. filterset_class = filtersets.FrontPortFilterSet
  173. class FrontPortTemplateType(ComponentTemplateObjectType):
  174. class Meta:
  175. model = models.FrontPortTemplate
  176. fields = '__all__'
  177. filterset_class = filtersets.FrontPortTemplateFilterSet
  178. class InterfaceType(IPAddressesMixin, ComponentObjectType, CabledObjectMixin):
  179. class Meta:
  180. model = models.Interface
  181. exclude = ('_path',)
  182. filterset_class = filtersets.InterfaceFilterSet
  183. def resolve_poe_mode(self, info):
  184. return self.poe_mode or None
  185. def resolve_poe_type(self, info):
  186. return self.poe_type or None
  187. def resolve_mode(self, info):
  188. return self.mode or None
  189. def resolve_rf_role(self, info):
  190. return self.rf_role or None
  191. def resolve_rf_channel(self, info):
  192. return self.rf_channel or None
  193. class InterfaceTemplateType(ComponentTemplateObjectType):
  194. class Meta:
  195. model = models.InterfaceTemplate
  196. fields = '__all__'
  197. filterset_class = filtersets.InterfaceTemplateFilterSet
  198. def resolve_poe_mode(self, info):
  199. return self.poe_mode or None
  200. def resolve_poe_type(self, info):
  201. return self.poe_type or None
  202. class InventoryItemType(ComponentObjectType):
  203. component = graphene.Field('dcim.graphql.gfk_mixins.InventoryItemComponentType')
  204. class Meta:
  205. model = models.InventoryItem
  206. exclude = ('component_type', 'component_id')
  207. filterset_class = filtersets.InventoryItemFilterSet
  208. class InventoryItemRoleType(OrganizationalObjectType):
  209. class Meta:
  210. model = models.InventoryItemRole
  211. fields = '__all__'
  212. filterset_class = filtersets.InventoryItemRoleFilterSet
  213. class LocationType(VLANGroupsMixin, ImageAttachmentsMixin, ContactsMixin, OrganizationalObjectType):
  214. class Meta:
  215. model = models.Location
  216. fields = '__all__'
  217. filterset_class = filtersets.LocationFilterSet
  218. class ManufacturerType(OrganizationalObjectType, ContactsMixin):
  219. class Meta:
  220. model = models.Manufacturer
  221. fields = '__all__'
  222. filterset_class = filtersets.ManufacturerFilterSet
  223. class ModuleType(ComponentObjectType):
  224. class Meta:
  225. model = models.Module
  226. fields = '__all__'
  227. filterset_class = filtersets.ModuleFilterSet
  228. class ModuleBayType(ComponentObjectType):
  229. class Meta:
  230. model = models.ModuleBay
  231. fields = '__all__'
  232. filterset_class = filtersets.ModuleBayFilterSet
  233. class ModuleBayTemplateType(ComponentTemplateObjectType):
  234. class Meta:
  235. model = models.ModuleBayTemplate
  236. fields = '__all__'
  237. filterset_class = filtersets.ModuleBayTemplateFilterSet
  238. class ModuleTypeType(NetBoxObjectType):
  239. class Meta:
  240. model = models.ModuleType
  241. fields = '__all__'
  242. filterset_class = filtersets.ModuleTypeFilterSet
  243. def resolve_weight_unit(self, info):
  244. return self.weight_unit or None
  245. class PlatformType(OrganizationalObjectType):
  246. class Meta:
  247. model = models.Platform
  248. fields = '__all__'
  249. filterset_class = filtersets.PlatformFilterSet
  250. class PowerFeedType(NetBoxObjectType, CabledObjectMixin):
  251. class Meta:
  252. model = models.PowerFeed
  253. exclude = ('_path',)
  254. filterset_class = filtersets.PowerFeedFilterSet
  255. class PowerOutletType(ComponentObjectType, CabledObjectMixin):
  256. class Meta:
  257. model = models.PowerOutlet
  258. exclude = ('_path',)
  259. filterset_class = filtersets.PowerOutletFilterSet
  260. def resolve_feed_leg(self, info):
  261. return self.feed_leg or None
  262. def resolve_type(self, info):
  263. return self.type or None
  264. class PowerOutletTemplateType(ComponentTemplateObjectType):
  265. class Meta:
  266. model = models.PowerOutletTemplate
  267. fields = '__all__'
  268. filterset_class = filtersets.PowerOutletTemplateFilterSet
  269. def resolve_feed_leg(self, info):
  270. return self.feed_leg or None
  271. def resolve_type(self, info):
  272. return self.type or None
  273. class PowerPanelType(NetBoxObjectType, ContactsMixin):
  274. class Meta:
  275. model = models.PowerPanel
  276. fields = '__all__'
  277. filterset_class = filtersets.PowerPanelFilterSet
  278. class PowerPortType(ComponentObjectType, CabledObjectMixin):
  279. class Meta:
  280. model = models.PowerPort
  281. exclude = ('_path',)
  282. filterset_class = filtersets.PowerPortFilterSet
  283. def resolve_type(self, info):
  284. return self.type or None
  285. class PowerPortTemplateType(ComponentTemplateObjectType):
  286. class Meta:
  287. model = models.PowerPortTemplate
  288. fields = '__all__'
  289. filterset_class = filtersets.PowerPortTemplateFilterSet
  290. def resolve_type(self, info):
  291. return self.type or None
  292. class RackType(VLANGroupsMixin, ImageAttachmentsMixin, ContactsMixin, NetBoxObjectType):
  293. class Meta:
  294. model = models.Rack
  295. fields = '__all__'
  296. filterset_class = filtersets.RackFilterSet
  297. def resolve_type(self, info):
  298. return self.type or None
  299. def resolve_outer_unit(self, info):
  300. return self.outer_unit or None
  301. def resolve_weight_unit(self, info):
  302. return self.weight_unit or None
  303. class RackReservationType(NetBoxObjectType):
  304. class Meta:
  305. model = models.RackReservation
  306. fields = '__all__'
  307. filterset_class = filtersets.RackReservationFilterSet
  308. class RackRoleType(OrganizationalObjectType):
  309. class Meta:
  310. model = models.RackRole
  311. fields = '__all__'
  312. filterset_class = filtersets.RackRoleFilterSet
  313. class RearPortType(ComponentObjectType, CabledObjectMixin):
  314. class Meta:
  315. model = models.RearPort
  316. fields = '__all__'
  317. filterset_class = filtersets.RearPortFilterSet
  318. class RearPortTemplateType(ComponentTemplateObjectType):
  319. class Meta:
  320. model = models.RearPortTemplate
  321. fields = '__all__'
  322. filterset_class = filtersets.RearPortTemplateFilterSet
  323. class RegionType(VLANGroupsMixin, ContactsMixin, OrganizationalObjectType):
  324. class Meta:
  325. model = models.Region
  326. fields = '__all__'
  327. filterset_class = filtersets.RegionFilterSet
  328. class SiteType(VLANGroupsMixin, ImageAttachmentsMixin, ContactsMixin, NetBoxObjectType):
  329. asn = graphene.Field(BigInt)
  330. class Meta:
  331. model = models.Site
  332. fields = '__all__'
  333. filterset_class = filtersets.SiteFilterSet
  334. class SiteGroupType(VLANGroupsMixin, ContactsMixin, OrganizationalObjectType):
  335. class Meta:
  336. model = models.SiteGroup
  337. fields = '__all__'
  338. filterset_class = filtersets.SiteGroupFilterSet
  339. class VirtualChassisType(NetBoxObjectType):
  340. class Meta:
  341. model = models.VirtualChassis
  342. fields = '__all__'
  343. filterset_class = filtersets.VirtualChassisFilterSet
  344. class VirtualDeviceContextType(NetBoxObjectType):
  345. class Meta:
  346. model = models.VirtualDeviceContext
  347. fields = '__all__'
  348. filterset_class = filtersets.VirtualDeviceContextFilterSet