Skip to content

Commit 79be0d6

Browse files
authored
Merge pull request #199 from cruse1977/dev
Netbox 4.0 Compatibility
2 parents 68f9860 + f15248a commit 79be0d6

File tree

13 files changed

+147
-60
lines changed

13 files changed

+147
-60
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ARG NETBOX_VARIANT=v3.7
1+
ARG NETBOX_VARIANT=v4.0
22

33
FROM netboxcommunity/netbox:${NETBOX_VARIANT}
44

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Each Plugin Version listed below has been tested with its corresponding NetBox V
3838

3939
| NetBox Version | Plugin Version |
4040
|:--------------:|:--------------:|
41+
| >= 4.0.2 | 1.6.0 |
4142
| 3.7 | 1.5.0 |
4243
| 3.6 | 1.4.0 |
4344
| 3.5 | 1.3.0 |

netbox_acls/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @ryanmerolle @abhi1693 @cruse1977 @natm

netbox_acls/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Define the NetBox Plugin
33
"""
44

5-
from extras.plugins import PluginConfig
5+
from netbox.plugins import PluginConfig
66

77
from .version import __version__
88

@@ -17,8 +17,8 @@ class NetBoxACLsConfig(PluginConfig):
1717
version = __version__
1818
description = "Manage simple ACLs in NetBox"
1919
base_url = "access-lists"
20-
min_version = "3.7.0"
21-
max_version = "3.7.99"
20+
min_version = "4.0.2"
21+
max_version = "4.0.99"
2222

2323

2424
config = NetBoxACLsConfig

netbox_acls/api/serializers.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class Meta:
7575
"last_updated",
7676
"rule_count",
7777
)
78+
brief_fields = ("id", "url", "name", "display")
7879

7980
@extend_schema_field(serializers.DictField())
8081
def get_assigned_object(self, obj):
@@ -139,6 +140,7 @@ class Meta:
139140
"created",
140141
"last_updated",
141142
)
143+
brief_fields = ("id", "url", "access_list")
142144

143145
@extend_schema_field(serializers.DictField())
144146
def get_assigned_object(self, obj):
@@ -212,6 +214,7 @@ class Meta:
212214
"last_updated",
213215
"source_prefix",
214216
)
217+
brief_fields = ("id", "url", "display")
215218

216219
def validate(self, data):
217220
"""
@@ -284,7 +287,7 @@ class Meta:
284287
"protocol",
285288
"remark",
286289
)
287-
290+
brief_fields = ("id", "url", "display")
288291
def validate(self, data):
289292
"""
290293
Validate the ACLExtendedRule django model's inputs before allowing it to update the instance:
@@ -328,7 +331,7 @@ def validate(self, data):
328331
if data.get("protocol"):
329332
error_message["protocol"] = [
330333
"Action is set to remark, Protocol CANNOT be set.",
331-
]
334+
]
332335

333336
if error_message:
334337
raise serializers.ValidationError(error_message)

netbox_acls/graphql/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
from .schema import *
22
from .types import *
3+
4+
schema = [
5+
schema.NetBoxACLSAccessListQuery,
6+
schema.NetBoxACLSStandardRuleQuery,
7+
schema.NetBoxACLSACLExtendedRuleQuery
8+
]
9+

netbox_acls/graphql/filters.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import strawberry_django
2+
from .. import filtersets, models
3+
from netbox.graphql.filter_mixins import autotype_decorator, BaseFilterMixin
4+
5+
__all__ = (
6+
'AccessListFilter',
7+
'ACLInterfaceAssignmentFilter',
8+
'ACLExtendedRuleFilter',
9+
'ACLStandardRuleFilter',
10+
)
11+
12+
@strawberry_django.filter(models.AccessList, lookups=True)
13+
@autotype_decorator(filtersets.AccessListFilterSet)
14+
class AccessListFilter(BaseFilterMixin):
15+
pass
16+
17+
@strawberry_django.filter(models.ACLStandardRule, lookups=True)
18+
@autotype_decorator(filtersets.ACLStandardRuleFilterSet)
19+
class ACLStandardRuleFilter(BaseFilterMixin):
20+
pass
21+
22+
@strawberry_django.filter(models.ACLExtendedRule, lookups=True)
23+
@autotype_decorator(filtersets.ACLExtendedRuleFilterSet)
24+
class ACLExtendedRuleFilter(BaseFilterMixin):
25+
pass
26+
27+
@strawberry_django.filter(models.ACLInterfaceAssignment, lookups=True)
28+
@autotype_decorator(filtersets.ACLInterfaceAssignmentFilterSet)
29+
class ACLInterfaceAssignmentFilter(BaseFilterMixin):
30+
pass

netbox_acls/graphql/schema.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
1-
from graphene import ObjectType
2-
from netbox.graphql.fields import ObjectField, ObjectListField
3-
1+
import strawberry
2+
import strawberry_django
43
from .types import *
4+
from ..models import *
55

6-
7-
class Query(ObjectType):
6+
@strawberry.type
7+
class NetBoxACLSAccessListQuery:
88
"""
99
Defines the queries available to this plugin via the graphql api.
1010
"""
11+
@strawberry.field
12+
def access_list(self, id: int) -> AccessListType:
13+
return AccessList.objects.get(pk=id)
14+
access_list_list: list[AccessListType] = strawberry_django.field()
15+
16+
@strawberry.type
17+
class NetBoxACLSACLExtendedRuleQuery:
18+
@strawberry.field
19+
def acl_extended_rule(self, id: int) -> ACLExtendedRuleType:
20+
return ACLExtendedRule.objects.get(pk=id)
21+
acl_extended_rule_list: list[ACLExtendedRuleType] = strawberry_django.field()
1122

12-
access_list = ObjectField(AccessListType)
13-
access_list_list = ObjectListField(AccessListType)
1423

15-
acl_extended_rule = ObjectField(ACLExtendedRuleType)
16-
acl_extended_rule_list = ObjectListField(ACLExtendedRuleType)
1724

18-
acl_standard_rule = ObjectField(ACLStandardRuleType)
19-
acl_standard_rule_list = ObjectListField(ACLStandardRuleType)
25+
@strawberry.type
26+
class NetBoxACLSStandardRuleQuery:
27+
@strawberry.field
28+
def acl_standard_rule(self, id: int) -> ACLStandardRuleType:
29+
return ACLStandardRule.objects.get(pk=id)
30+
acl_standard_rule_list: list[ACLStandardRuleType] = strawberry_django.field()
2031

2132

22-
schema = Query

netbox_acls/graphql/types.py

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,112 @@
22
Define the object types and queries availble via the graphql api.
33
"""
44

5-
from netbox.graphql.types import NetBoxObjectType
5+
import strawberry
6+
import strawberry_django
67

7-
from .. import filtersets, models
88

9-
__all__ = (
10-
"AccessListType",
11-
"ACLInterfaceAssignmentType",
12-
"ACLExtendedRuleType",
13-
"ACLStandardRuleType",
14-
)
9+
from typing import Annotated, List, Union
10+
from .filters import *
11+
from .. import models
12+
from netbox.graphql.types import OrganizationalObjectType
1513

14+
@strawberry_django.type(
15+
models.AccessList,
16+
fields='__all__',
17+
filters=AccessListFilter,
18+
exclude=('assigned_object_type', 'assigned_object_id')
19+
)
1620

17-
class AccessListType(NetBoxObjectType):
21+
class AccessListType(OrganizationalObjectType):
1822
"""
1923
Defines the object type for the django model AccessList.
2024
"""
25+
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")]
30+
2131

2232
class Meta:
2333
"""
2434
Associates the filterset, fields, and model for the django model AccessList.
2535
"""
26-
27-
model = models.AccessList
28-
fields = "__all__"
29-
filterset_class = filtersets.AccessListFilterSet
30-
31-
32-
class ACLInterfaceAssignmentType(NetBoxObjectType):
36+
@strawberry_django.field
37+
def accesslists(self) -> List[Annotated["AccessList", strawberry.lazy('accesslists.graphql.types')]]:
38+
return self.accesslists.all()
39+
40+
@strawberry_django.type(
41+
models.ACLInterfaceAssignment,
42+
fields='__all__',
43+
exclude=('assigned_object_type', 'assigned_object_id'),
44+
filters=ACLInterfaceAssignmentFilter
45+
)
46+
class ACLInterfaceAssignmentType(OrganizationalObjectType):
3347
"""
3448
Defines the object type for the django model AccessList.
3549
"""
50+
access_list: Annotated["AccessListType", strawberry.lazy("netbox_acls.graphql.types")]
51+
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+
3659

3760
class Meta:
3861
"""
3962
Associates the filterset, fields, and model for the django model ACLInterfaceAssignment.
4063
"""
64+
@strawberry_django.field
65+
def aclinterfaceassignments(self) -> List[Annotated["ACLInterfaceAssignment", strawberry.lazy('aclinterfaceassignments.graphql.types')]]:
66+
return self.aclinterfaceassignments.all()
67+
68+
@strawberry_django.type(
69+
models.ACLExtendedRule,
70+
fields='__all__',
71+
filters=ACLExtendedRuleFilter
72+
)
4173

42-
model = models.ACLInterfaceAssignment
43-
fields = "__all__"
44-
filterset_class = filtersets.ACLInterfaceAssignmentFilterSet
45-
46-
47-
class ACLExtendedRuleType(NetBoxObjectType):
74+
class ACLExtendedRuleType(OrganizationalObjectType):
4875
"""
4976
Defines the object type for the django model ACLExtendedRule.
5077
"""
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")]
5183

5284
class Meta:
5385
"""
5486
Associates the filterset, fields, and model for the django model ACLExtendedRule.
5587
"""
88+
@strawberry_django.field
89+
def aclextendedrules(self) -> List[Annotated["ACLExtendedRule", strawberry.lazy('aclextendedrule.graphql.types')]]:
90+
return self.aclextendedrules.all()
5691

57-
model = models.ACLExtendedRule
58-
fields = "__all__"
59-
filterset_class = filtersets.ACLExtendedRuleFilterSet
6092

93+
@strawberry_django.type(
94+
models.ACLStandardRule,
95+
fields='__all__',
96+
filters=ACLStandardRuleFilter
97+
)
6198

62-
class ACLStandardRuleType(NetBoxObjectType):
99+
class ACLStandardRuleType(OrganizationalObjectType):
63100
"""
64101
Defines the object type for the django model ACLStandardRule.
65102
"""
103+
access_list: Annotated["AccessListType", strawberry.lazy("netbox_acls.graphql.types")]
104+
source_prefix: Annotated["PrefixType", strawberry.lazy("ipam.graphql.types")]
66105

67106
class Meta:
68107
"""
69-
Associates the filterset, fields, and model for the django model ACLStandardRule.
108+
Associates the filterset, fields, and model for the django model ACLExtendedRule.
70109
"""
110+
@strawberry_django.field
111+
def aclstandardrules(self) -> List[Annotated["ACLStandardRule", strawberry.lazy('aclstandardrule.graphql.types')]]:
112+
return self.aclstandardrules.all()
71113

72-
model = models.ACLStandardRule
73-
fields = "__all__"
74-
filterset_class = filtersets.ACLStandardRuleFilterSet

netbox_acls/navigation.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"""
44

55
from django.conf import settings
6-
from extras.plugins import PluginMenu, PluginMenuButton, PluginMenuItem
7-
from utilities.choices import ButtonColorChoices
6+
from netbox.plugins import PluginMenu, PluginMenuButton, PluginMenuItem
7+
88

99
plugin_settings = settings.PLUGINS_CONFIG["netbox_acls"]
1010

@@ -21,7 +21,6 @@
2121
link="plugins:netbox_acls:accesslist_add",
2222
title="Add",
2323
icon_class="mdi mdi-plus-thick",
24-
color=ButtonColorChoices.GREEN,
2524
permissions=["netbox_acls.add_accesslist"],
2625
),
2726
),
@@ -35,7 +34,6 @@
3534
link="plugins:netbox_acls:aclstandardrule_add",
3635
title="Add",
3736
icon_class="mdi mdi-plus-thick",
38-
color=ButtonColorChoices.GREEN,
3937
permissions=["netbox_acls.add_aclstandardrule"],
4038
),
4139
),
@@ -49,7 +47,6 @@
4947
link="plugins:netbox_acls:aclextendedrule_add",
5048
title="Add",
5149
icon_class="mdi mdi-plus-thick",
52-
color=ButtonColorChoices.GREEN,
5350
permissions=["netbox_acls.add_aclextendedrule"],
5451
),
5552
),
@@ -63,7 +60,6 @@
6360
link="plugins:netbox_acls:aclinterfaceassignment_add",
6461
title="Add",
6562
icon_class="mdi mdi-plus-thick",
66-
color=ButtonColorChoices.GREEN,
6763
permissions=["netbox_acls.add_aclinterfaceassignment"],
6864
),
6965
),

0 commit comments

Comments
 (0)