Skip to content

Commit 4ea2c7c

Browse files
authored
Merge pull request #250 from pheus/housekeeping/249-use-netboxobjecttype-for-graphql
#249 [Housekeeping]: Use NetBoxObjectType for GraphQL
2 parents 9462874 + 756e82c commit 4ea2c7c

File tree

4 files changed

+70
-90
lines changed

4 files changed

+70
-90
lines changed

netbox_acls/graphql/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
from .schema import *
2-
from .types import *
3-
4-
schema = [
5-
schema.NetBoxACLSQuery
6-
]
1+
from .schema import NetBoxACLSQuery
72

3+
schema = [NetBoxACLSQuery]

netbox_acls/graphql/filters.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
import strawberry_django
2+
from netbox.graphql.filter_mixins import BaseFilterMixin, autotype_decorator
3+
24
from .. import filtersets, models
3-
from netbox.graphql.filter_mixins import autotype_decorator, BaseFilterMixin
45

56
__all__ = (
6-
'AccessListFilter',
7-
'ACLInterfaceAssignmentFilter',
8-
'ACLExtendedRuleFilter',
9-
'ACLStandardRuleFilter',
7+
"AccessListFilter",
8+
"ACLInterfaceAssignmentFilter",
9+
"ACLExtendedRuleFilter",
10+
"ACLStandardRuleFilter",
1011
)
1112

13+
1214
@strawberry_django.filter(models.AccessList, lookups=True)
1315
@autotype_decorator(filtersets.AccessListFilterSet)
1416
class AccessListFilter(BaseFilterMixin):
1517
pass
1618

19+
1720
@strawberry_django.filter(models.ACLStandardRule, lookups=True)
1821
@autotype_decorator(filtersets.ACLStandardRuleFilterSet)
1922
class ACLStandardRuleFilter(BaseFilterMixin):
2023
pass
2124

25+
2226
@strawberry_django.filter(models.ACLExtendedRule, lookups=True)
2327
@autotype_decorator(filtersets.ACLExtendedRuleFilterSet)
2428
class ACLExtendedRuleFilter(BaseFilterMixin):
2529
pass
2630

31+
2732
@strawberry_django.filter(models.ACLInterfaceAssignment, lookups=True)
2833
@autotype_decorator(filtersets.ACLInterfaceAssignmentFilterSet)
2934
class ACLInterfaceAssignmentFilter(BaseFilterMixin):
30-
pass
35+
pass

netbox_acls/graphql/schema.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1+
from typing import List
2+
13
import strawberry
24
import strawberry_django
3-
from .types import *
4-
from ..models import *
5-
from typing import List
5+
6+
from .types import (
7+
AccessListType,
8+
ACLExtendedRuleType,
9+
ACLStandardRuleType,
10+
)
11+
612

713
@strawberry.type(name="Query")
814
class NetBoxACLSQuery:
915
"""
1016
Defines the queries available to this plugin via the graphql api.
1117
"""
18+
1219
access_list: AccessListType = strawberry_django.field()
1320
access_list_list: List[AccessListType] = strawberry_django.field()
1421

@@ -17,4 +24,3 @@ class NetBoxACLSQuery:
1724

1825
acl_standard_rule: ACLStandardRuleType = strawberry_django.field()
1926
acl_standard_rule_list: List[ACLStandardRuleType] = strawberry_django.field()
20-

netbox_acls/graphql/types.py

Lines changed: 47 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,86 @@
11
"""
2-
Define the object types and queries availble via the graphql api.
2+
Define the object types and queries available via the graphql api.
33
"""
44

5+
from typing import Annotated, List, Union
6+
57
import strawberry
68
import strawberry_django
9+
from netbox.graphql.types import NetBoxObjectType
710

8-
9-
from typing import Annotated, List, Union
10-
from .filters import *
1111
from .. import models
12-
from netbox.graphql.types import OrganizationalObjectType
12+
from . import filters
13+
1314

1415
@strawberry_django.type(
1516
models.AccessList,
16-
fields='__all__',
17-
filters=AccessListFilter,
18-
exclude=('assigned_object_type', 'assigned_object_id')
17+
fields="__all__",
18+
exclude=["assigned_object_type", "assigned_object_id"],
19+
filters=filters.AccessListFilter,
1920
)
20-
21-
class AccessListType(OrganizationalObjectType):
21+
class AccessListType(NetBoxObjectType):
2222
"""
2323
Defines the object type for the django model AccessList.
2424
"""
25+
2526
assigned_object_type: Annotated["ContentTypeType", strawberry.lazy("netbox.graphql.types")]
26-
assigned_object: Annotated[Union[
27-
Annotated["DeviceType", strawberry.lazy('dcim.graphql.types')],
28-
Annotated["VirtualMachineType", strawberry.lazy('virtualization.graphql.types')],
29-
], strawberry.union("ACLAssignmentType")]
27+
assigned_object: Annotated[
28+
Union[
29+
Annotated["DeviceType", strawberry.lazy("dcim.graphql.types")],
30+
Annotated["VirtualMachineType", strawberry.lazy("virtualization.graphql.types")],
31+
],
32+
strawberry.union("ACLAssignmentType"),
33+
]
3034

3135

32-
class Meta:
33-
"""
34-
Associates the filterset, fields, and model for the django model AccessList.
35-
"""
36-
@strawberry_django.field
37-
def accesslists(self) -> List[Annotated["AccessList", strawberry.lazy('accesslists.graphql.types')]]:
38-
return self.accesslists.all()
39-
4036
@strawberry_django.type(
4137
models.ACLInterfaceAssignment,
42-
fields='__all__',
43-
exclude=('assigned_object_type', 'assigned_object_id'),
44-
filters=ACLInterfaceAssignmentFilter
38+
fields="__all__",
39+
exclude=["assigned_object_type", "assigned_object_id"],
40+
filters=filters.ACLInterfaceAssignmentFilter,
4541
)
46-
class ACLInterfaceAssignmentType(OrganizationalObjectType):
42+
class ACLInterfaceAssignmentType(NetBoxObjectType):
4743
"""
4844
Defines the object type for the django model AccessList.
4945
"""
46+
5047
access_list: Annotated["AccessListType", strawberry.lazy("netbox_acls.graphql.types")]
5148
assigned_object_type: Annotated["ContentTypeType", strawberry.lazy("netbox.graphql.types")]
52-
assigned_object: Annotated[Union[
53-
Annotated["InterfaceType", strawberry.lazy('dcim.graphql.types')],
54-
Annotated["VMInterfaceType", strawberry.lazy('virtualization.graphql.types')],
55-
], strawberry.union("ACLInterfaceAssignmentType")]
56-
57-
58-
49+
assigned_object: Annotated[
50+
Union[
51+
Annotated["InterfaceType", strawberry.lazy("dcim.graphql.types")],
52+
Annotated["VMInterfaceType", strawberry.lazy("virtualization.graphql.types")],
53+
],
54+
strawberry.union("ACLInterfaceAssignmentType"),
55+
]
5956

60-
class Meta:
61-
"""
62-
Associates the filterset, fields, and model for the django model ACLInterfaceAssignment.
63-
"""
64-
@strawberry_django.field
65-
def aclinterfaceassignments(self) -> List[Annotated["ACLInterfaceAssignment", strawberry.lazy('aclinterfaceassignments.graphql.types')]]:
66-
return self.aclinterfaceassignments.all()
6757

6858
@strawberry_django.type(
69-
models.ACLExtendedRule,
70-
fields='__all__',
71-
filters=ACLExtendedRuleFilter
59+
models.ACLStandardRule,
60+
fields="__all__",
61+
filters=filters.ACLStandardRuleFilter,
7262
)
73-
74-
class ACLExtendedRuleType(OrganizationalObjectType):
63+
class ACLStandardRuleType(NetBoxObjectType):
7564
"""
76-
Defines the object type for the django model ACLExtendedRule.
65+
Defines the object type for the django model ACLStandardRule.
7766
"""
78-
source_ports: List[int]
79-
destination_ports: List[int]
80-
access_list: Annotated["AccessListType", strawberry.lazy("netbox_acls.graphql.types")]
81-
destination_prefix: Annotated["PrefixType", strawberry.lazy("ipam.graphql.types")]
82-
source_prefix: Annotated["PrefixType", strawberry.lazy("ipam.graphql.types")]
8367

84-
class Meta:
85-
"""
86-
Associates the filterset, fields, and model for the django model ACLExtendedRule.
87-
"""
88-
@strawberry_django.field
89-
def aclextendedrules(self) -> List[Annotated["ACLExtendedRule", strawberry.lazy('aclextendedrule.graphql.types')]]:
90-
return self.aclextendedrules.all()
68+
access_list: Annotated["AccessListType", strawberry.lazy("netbox_acls.graphql.types")]
69+
source_prefix: Annotated["PrefixType", strawberry.lazy("ipam.graphql.types")] | None
9170

9271

9372
@strawberry_django.type(
94-
models.ACLStandardRule,
95-
fields='__all__',
96-
filters=ACLStandardRuleFilter
73+
models.ACLExtendedRule,
74+
fields="__all__",
75+
filters=filters.ACLExtendedRuleFilter,
9776
)
98-
99-
class ACLStandardRuleType(OrganizationalObjectType):
77+
class ACLExtendedRuleType(NetBoxObjectType):
10078
"""
101-
Defines the object type for the django model ACLStandardRule.
79+
Defines the object type for the django model ACLExtendedRule.
10280
"""
103-
access_list: Annotated["AccessListType", strawberry.lazy("netbox_acls.graphql.types")]
104-
source_prefix: Annotated["PrefixType", strawberry.lazy("ipam.graphql.types")]
105-
106-
class Meta:
107-
"""
108-
Associates the filterset, fields, and model for the django model ACLExtendedRule.
109-
"""
110-
@strawberry_django.field
111-
def aclstandardrules(self) -> List[Annotated["ACLStandardRule", strawberry.lazy('aclstandardrule.graphql.types')]]:
112-
return self.aclstandardrules.all()
11381

82+
access_list: Annotated["AccessListType", strawberry.lazy("netbox_acls.graphql.types")]
83+
source_prefix: Annotated["PrefixType", strawberry.lazy("ipam.graphql.types")] | None
84+
source_ports: List[int] | None
85+
destination_prefix: Annotated["PrefixType", strawberry.lazy("ipam.graphql.types")] | None
86+
destination_ports: List[int] | None

0 commit comments

Comments
 (0)