Skip to content

Commit ca1dc5f

Browse files
authored
Compatibility with Netbox 3.6 (#66)
1 parent a47c8d2 commit ca1dc5f

File tree

11 files changed

+34
-61
lines changed

11 files changed

+34
-61
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Load data from YAML files into Netbox
66

77
First activate your virtual environment where Netbox is installed, the install the plugin version correspondig to your Netbox version.
88
```bash
9-
pip install "netbox-initializers==3.5.*"
9+
pip install "netbox-initializers==3.6.*"
1010
```
1111
Then you need to add the plugin to the `PLUGINS` array in the Netbox configuration.
1212
```python
@@ -36,6 +36,6 @@ The initializers where a part of the Docker image and where then extracted into
3636
To use the new plugin in a the Netbox Docker image, it musst be installad into the image. To this, the following example can be used as a starting point:
3737

3838
```dockerfile
39-
FROM netboxcommunity/netbox:v3.5
40-
RUN /opt/netbox/venv/bin/pip install "netbox-initializers==3.5.*"
39+
FROM netboxcommunity/netbox:v3.6
40+
RUN /opt/netbox/venv/bin/pip install "netbox-initializers==3.6.*"
4141
```

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ license = "Apache-2.0"
1111
name = "netbox-initializers"
1212
readme = "README.md"
1313
repository = "https://github.com/tobiasge/netbox-initializers"
14-
version = "3.5.2"
14+
version = "3.6.0"
1515

1616
[tool.poetry.dependencies]
1717
python = "^3.8"

src/netbox_initializers/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ class NetBoxInitializersConfig(PluginConfig):
55
name = "netbox_initializers"
66
verbose_name = "NetBox Initializers"
77
description = "Load initial data into Netbox"
8-
version = "3.5.2"
8+
version = "3.6.0"
99
base_url = "initializers"
10-
min_version = "3.5.0"
11-
max_version = "3.5.99"
10+
min_version = "3.6.0"
11+
max_version = "3.6.99"
1212

1313

1414
config = NetBoxInitializersConfig

src/netbox_initializers/initializers/custom_fields.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from extras.models import CustomField
1+
from extras.models import CustomField, CustomFieldChoiceSet
22

33
from . import BaseInitializer, register_initializer
44

@@ -113,7 +113,7 @@ def load_data(self):
113113
custom_field.validation_maximum = cf_details["validation_maximum"]
114114

115115
# choices should only be applied when type is select, multiselect
116-
if cf_details.get("choices"):
116+
if choices := cf_details.get("choices"):
117117
if cf_details.get("type") not in (
118118
"select",
119119
"multiselect",
@@ -124,18 +124,12 @@ def load_data(self):
124124
)
125125
custom_field.delete()
126126
continue
127-
custom_field.choices = []
128-
129-
for choice_detail in cf_details.get("choices", []):
130-
if isinstance(choice_detail, dict) and "value" in choice_detail:
131-
# legacy mode
132-
print(
133-
f"⚠️ Please migrate the choice '{choice_detail['value']}' of '{cf_name}'"
134-
+ " to the new format, as 'weight' is no longer supported!"
135-
)
136-
custom_field.choices.append(choice_detail["value"])
137-
else:
138-
custom_field.choices.append(choice_detail)
127+
choice_set, _ = CustomFieldChoiceSet.objects.get_or_create(
128+
name=f"{cf_name}_choices"
129+
)
130+
choice_set.extra_choices = choices
131+
choice_set.save()
132+
custom_field.choice_set = choice_set
139133

140134
custom_field.save()
141135

src/netbox_initializers/initializers/groups.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from users.models import AdminGroup, AdminUser
1+
from users.models import NetBoxGroup, NetBoxUser
22

33
from . import BaseInitializer, register_initializer
44

@@ -12,11 +12,11 @@ def load_data(self):
1212
return
1313

1414
for groupname, group_details in groups.items():
15-
group, created = AdminGroup.objects.get_or_create(name=groupname)
15+
group, created = NetBoxGroup.objects.get_or_create(name=groupname)
1616
if created:
1717
print("👥 Created group", groupname)
1818
for username in group_details.get("users", []):
19-
user = AdminUser.objects.get(username=username)
19+
user = NetBoxUser.objects.get(username=username)
2020
if user:
2121
group.user_set.add(user)
2222
print(" 👤 Assigned user %s to group %s" % (username, group.name))

src/netbox_initializers/initializers/object_permissions.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.contrib.contenttypes.models import ContentType
2-
from users.models import AdminGroup, AdminUser, ObjectPermission
2+
from users.models import NetBoxGroup, NetBoxUser, ObjectPermission
33

44
from . import BaseInitializer, register_initializer
55

@@ -12,7 +12,6 @@ def load_data(self):
1212
if object_permissions is None:
1313
return
1414
for permission_name, permission_details in object_permissions.items():
15-
1615
object_permission, created = ObjectPermission.objects.get_or_create(
1716
name=permission_name,
1817
defaults={
@@ -49,7 +48,7 @@ def load_data(self):
4948

5049
if permission_details.get("groups", 0):
5150
for groupname in permission_details["groups"]:
52-
group = AdminGroup.objects.filter(name=groupname).first()
51+
group = NetBoxGroup.objects.filter(name=groupname).first()
5352

5453
if group:
5554
object_permission.groups.add(group)
@@ -60,7 +59,7 @@ def load_data(self):
6059

6160
if permission_details.get("users", 0):
6261
for username in permission_details["users"]:
63-
user = AdminUser.objects.filter(username=username).first()
62+
user = NetBoxUser.objects.filter(username=username).first()
6463

6564
if user:
6665
object_permission.users.add(user)

src/netbox_initializers/initializers/users.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from django.contrib.auth.models import User
2-
from users.models import Token
1+
from users.models import NetBoxUser, Token
32

43
from . import BaseInitializer, register_initializer
54

@@ -14,8 +13,10 @@ def load_data(self):
1413

1514
for username, user_details in users.items():
1615
api_token = user_details.pop("api_token", Token.generate_key())
17-
password = user_details.pop("password", User.objects.make_random_password())
18-
user, created = User.objects.get_or_create(username=username, defaults=user_details)
16+
password = user_details.pop("password", NetBoxUser.objects.make_random_password())
17+
user, created = NetBoxUser.objects.get_or_create(
18+
username=username, defaults=user_details
19+
)
1920
if created:
2021
user.set_password(password)
2122
user.save()

src/netbox_initializers/initializers/yaml/custom_fields.yml

-15
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,6 @@
6767
# - Third Item
6868
# - Fifth Item
6969
# - Fourth Item
70-
# select_field_legacy_format:
71-
# type: select
72-
# label: Choose between items
73-
# required: false
74-
# filter_logic: loose
75-
# weight: 30
76-
# on_objects:
77-
# - dcim.models.Device
78-
# choices:
79-
# - value: A # this is the deprecated format.
80-
# - value: B # we only use it for the tests.
81-
# - value: C # please see above for the new format.
82-
# - value: "D like deprecated"
83-
# weight: 999
84-
# - value: E
8570
# boolean_field:
8671
# type: boolean
8772
# label: Yes Or No?

src/netbox_initializers/initializers/yaml/device_types.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,27 @@
3131
# - name_template: ttyS[1-48]
3232
# type: rj-45
3333
# power_ports:
34-
# - name_template: psu[0,1]
34+
# - name_template: psu[0-1]
3535
# type: iec-60320-c14
3636
# maximum_draw: 35
3737
# allocated_draw: 35
3838
# front_ports:
39-
# - name_template: front[1,2]
39+
# - name_template: front[1-2]
4040
# type: 8p8c
41-
# rear_port_template: rear[0,1]
42-
# rear_port_position_template: "[1,2]"
41+
# rear_port_template: rear[0-1]
42+
# rear_port_position_template: "[1-2]"
4343
# rear_ports:
44-
# - name_template: rear[0,1]
44+
# - name_template: rear[0-1]
4545
# type: 8p8c
46-
# positions_template: "[3,2]"
46+
# positions_template: "[2-3]"
4747
# device_bays:
4848
# - name: bay0 # both non-template and template field specified; non-template field takes precedence
4949
# name_template: bay[0-9]
5050
# label: test0
5151
# label_template: test[0-5,9,6-8]
5252
# description: Test description
5353
# power_outlets:
54-
# - name_template: outlet[0,1]
54+
# - name_template: outlet[0-1]
5555
# type: iec-60320-c5
5656
# power_port: psu0
5757
# feed_leg: B
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
# - name: Platform 1
22
# slug: platform-1
33
# manufacturer: Manufacturer 1
4-
# napalm_driver: driver1
5-
# napalm_args: "{'arg1': 'value1', 'arg2': 'value2'}"
64
# - name: Platform 2
75
# slug: platform-2
86
# manufacturer: Manufacturer 2
9-
# napalm_driver: driver2
10-
# napalm_args: "{'arg1': 'value1', 'arg2': 'value2'}"
117
# - name: Platform 3
128
# slug: platform-3
139
# manufacturer: No Name
14-
# napalm_driver: driver3
15-
# napalm_args: "{'arg1': 'value1', 'arg2': 'value2'}"

test/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM netboxcommunity/netbox:v3.5
1+
FROM netboxcommunity/netbox:v3.6
22

33
COPY ../ /opt/netbox-initializers/
44
COPY ./test/config/plugins.py /etc/netbox/config/

0 commit comments

Comments
 (0)