Skip to content

Commit a12c4fa

Browse files
committed
feat(graphql): Add generic source/destination field support
Introduces generic source and destination fields in GraphQL types for ACLStandardRule and ACLExtendedRule. Adds support for ContentType relationships, caching related objects, and enhanced flexibility in object handling. Aligns GraphQL implementation with the updated generic model structure for consistency.
1 parent d4dae20 commit a12c4fa

File tree

1 file changed

+110
-5
lines changed

1 file changed

+110
-5
lines changed

netbox_acls/graphql/types.py

Lines changed: 110 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22
Define the object types and queries available via the graphql api.
33
"""
44

5-
from typing import Annotated, List, Union
5+
from typing import Annotated, List, Union, TYPE_CHECKING
66

77
import strawberry
88
import strawberry_django
9-
from netbox.graphql.types import NetBoxObjectType
9+
from netbox.graphql.types import ContentTypeType, NetBoxObjectType
1010

1111
from .. import models
1212
from . import filters
1313

14+
if TYPE_CHECKING:
15+
from dcim.graphql.types import DeviceType, InterfaceType
16+
from ipam.graphql.types import AggregateType, IPAddressType, IPRangeType, PrefixType
17+
from virtualization.graphql.types import VirtualMachineType, VMInterfaceType
18+
1419

1520
@strawberry_django.type(
1621
models.AccessList,
@@ -74,6 +79,10 @@ class ACLInterfaceAssignmentType(NetBoxObjectType):
7479
@strawberry_django.type(
7580
models.ACLStandardRule,
7681
fields="__all__",
82+
exclude=[
83+
"source_id",
84+
"source_type",
85+
],
7786
filters=filters.ACLStandardRuleFilter,
7887
)
7988
class ACLStandardRuleType(NetBoxObjectType):
@@ -83,12 +92,48 @@ class ACLStandardRuleType(NetBoxObjectType):
8392

8493
# Model fields
8594
access_list: Annotated["AccessListType", strawberry.lazy("netbox_acls.graphql.types")]
86-
source_prefix: Annotated["PrefixType", strawberry.lazy("ipam.graphql.types")] | None
95+
source_type: Annotated["ContentTypeType", strawberry.lazy("netbox.graphql.types")] | None
96+
source: (
97+
Annotated[
98+
Union[
99+
Annotated[
100+
"AggregateType",
101+
strawberry.lazy("ipam.graphql.types"),
102+
],
103+
Annotated[
104+
"IPAddressType",
105+
strawberry.lazy("ipam.graphql.types"),
106+
],
107+
Annotated[
108+
"IPRangeType",
109+
strawberry.lazy("ipam.graphql.types"),
110+
],
111+
Annotated[
112+
"PrefixType",
113+
strawberry.lazy("ipam.graphql.types"),
114+
],
115+
],
116+
strawberry.union("ACLStandardRuleObjectType"),
117+
]
118+
| None
119+
)
120+
121+
# Cached related source objects
122+
_source_aggregate: Annotated["AggregateType", strawberry.lazy("ipam.graphql.types")] | None
123+
_source_ipaddress: Annotated["IPAddressType", strawberry.lazy("ipam.graphql.types")] | None
124+
_source_iprange: Annotated["IPRangeType", strawberry.lazy("ipam.graphql.types")] | None
125+
_source_prefix: Annotated["PrefixType", strawberry.lazy("ipam.graphql.types")] | None
87126

88127

89128
@strawberry_django.type(
90129
models.ACLExtendedRule,
91130
fields="__all__",
131+
exclude=[
132+
"source_id",
133+
"source_type",
134+
"destination_id",
135+
"destination_type",
136+
],
92137
filters=filters.ACLExtendedRuleFilter,
93138
)
94139
class ACLExtendedRuleType(NetBoxObjectType):
@@ -98,7 +143,67 @@ class ACLExtendedRuleType(NetBoxObjectType):
98143

99144
# Model fields
100145
access_list: Annotated["AccessListType", strawberry.lazy("netbox_acls.graphql.types")]
101-
source_prefix: Annotated["PrefixType", strawberry.lazy("ipam.graphql.types")] | None
146+
source_type: Annotated["ContentTypeType", strawberry.lazy("netbox.graphql.types")] | None
147+
source: (
148+
Annotated[
149+
Union[
150+
Annotated[
151+
"AggregateType",
152+
strawberry.lazy("ipam.graphql.types"),
153+
],
154+
Annotated[
155+
"IPAddressType",
156+
strawberry.lazy("ipam.graphql.types"),
157+
],
158+
Annotated[
159+
"IPRangeType",
160+
strawberry.lazy("ipam.graphql.types"),
161+
],
162+
Annotated[
163+
"PrefixType",
164+
strawberry.lazy("ipam.graphql.types"),
165+
],
166+
],
167+
strawberry.union("ACLStandardRuleObjectType"),
168+
]
169+
| None
170+
)
102171
source_ports: List[int] | None
103-
destination_prefix: Annotated["PrefixType", strawberry.lazy("ipam.graphql.types")] | None
172+
destination_type: Annotated["ContentTypeType", strawberry.lazy("netbox.graphql.types")] | None
173+
destination: (
174+
Annotated[
175+
Union[
176+
Annotated[
177+
"AggregateType",
178+
strawberry.lazy("ipam.graphql.types"),
179+
],
180+
Annotated[
181+
"IPAddressType",
182+
strawberry.lazy("ipam.graphql.types"),
183+
],
184+
Annotated[
185+
"IPRangeType",
186+
strawberry.lazy("ipam.graphql.types"),
187+
],
188+
Annotated[
189+
"PrefixType",
190+
strawberry.lazy("ipam.graphql.types"),
191+
],
192+
],
193+
strawberry.union("ACLStandardRuleObjectType"),
194+
]
195+
| None
196+
)
104197
destination_ports: List[int] | None
198+
199+
# Cached related source objects
200+
_source_aggregate: Annotated["AggregateType", strawberry.lazy("ipam.graphql.types")] | None
201+
_source_ipaddress: Annotated["IPAddressType", strawberry.lazy("ipam.graphql.types")] | None
202+
_source_iprange: Annotated["IPRangeType", strawberry.lazy("ipam.graphql.types")] | None
203+
_source_prefix: Annotated["PrefixType", strawberry.lazy("ipam.graphql.types")] | None
204+
205+
# Cached related destination objects
206+
_destination_aggregate: Annotated["AggregateType", strawberry.lazy("ipam.graphql.types")] | None
207+
_destination_ipaddress: Annotated["IPAddressType", strawberry.lazy("ipam.graphql.types")] | None
208+
_destination_iprange: Annotated["IPRangeType", strawberry.lazy("ipam.graphql.types")] | None
209+
_destination_prefix: Annotated["PrefixType", strawberry.lazy("ipam.graphql.types")] | None

0 commit comments

Comments
 (0)