Skip to content

Commit 50d077d

Browse files
authored
Merge pull request #99 from tobiasge/prepare-for-nb40
Fix #97: Prepare for Netbox 4.0
2 parents 5d3bc30 + 2067b02 commit 50d077d

16 files changed

+45
-46
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.7.*"
9+
pip install "netbox-initializers==4.0.*"
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.7
40-
RUN /opt/netbox/venv/bin/pip install "netbox-initializers==3.7.*"
39+
FROM netboxcommunity/netbox:v4.0
40+
RUN /opt/netbox/venv/bin/pip install "netbox-initializers==4.0.*"
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.7.2"
14+
version = "4.0.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.7.2"
8+
version = "4.0.0"
99
base_url = "initializers"
10-
min_version = "3.7.0"
11-
max_version = "3.7.99"
10+
min_version = "4.0-beta1"
11+
max_version = "4.0.99"
1212

1313

1414
config = NetBoxInitializersConfig

src/netbox_initializers/initializers/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pathlib import Path
22
from typing import Tuple
33

4-
from django.contrib.contenttypes.models import ContentType
4+
from core.models import ObjectType
55
from django.core.exceptions import ObjectDoesNotExist
66
from extras.models import CustomField, Tag
77
from ruamel.yaml import YAML
@@ -105,8 +105,8 @@ def set_custom_fields_values(self, entity, custom_field_data):
105105
except ObjectDoesNotExist:
106106
missing_cfs.append(key)
107107
else:
108-
ct = ContentType.objects.get_for_model(entity)
109-
if ct not in cf.content_types.all():
108+
ct = ObjectType.objects.get_for_model(entity)
109+
if ct not in cf.object_types.all():
110110
print(
111111
f"⚠️ Custom field {key} is not enabled for {entity}'s model!"
112112
"Please check the 'on_objects' for that custom field in custom_fields.yml"
@@ -131,7 +131,7 @@ def set_tags(self, entity, tags):
131131
if not hasattr(entity, "tags"):
132132
raise Exception(f"⚠️ Tags cannot be applied to {entity}'s model")
133133

134-
ct = ContentType.objects.get_for_model(entity)
134+
ct = ObjectType.objects.get_for_model(entity)
135135

136136
save = False
137137
for tag in Tag.objects.filter(name__in=tags):

src/netbox_initializers/initializers/custom_fields.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
def get_class_for_class_path(class_path):
77
import importlib
88

9-
from django.contrib.contenttypes.models import ContentType
9+
from core.models import ObjectType
1010

1111
module_name, class_name = class_path.rsplit(".", 1)
1212
module = importlib.import_module(module_name)
1313
clazz = getattr(module, class_name)
14-
return ContentType.objects.get_for_model(clazz)
14+
return ObjectType.objects.get_for_model(clazz)
1515

1616

1717
class CustomFieldInitializer(BaseInitializer):
@@ -35,7 +35,7 @@ def load_data(self):
3535
custom_field.label = cf_details["label"]
3636

3737
for object_type in cf_details.get("on_objects", []):
38-
custom_field.content_types.add(get_class_for_class_path(object_type))
38+
custom_field.object_types.add(get_class_for_class_path(object_type))
3939

4040
if cf_details.get("required", False):
4141
custom_field.required = cf_details["required"]

src/netbox_initializers/initializers/custom_links.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
from django.contrib.contenttypes.models import ContentType
1+
from core.models import ObjectType
22
from extras.models import CustomLink
33

44
from . import BaseInitializer, register_initializer
55

66

77
def get_content_type(content_type):
88
try:
9-
return ContentType.objects.get(model=content_type)
10-
except ContentType.DoesNotExist:
9+
return ObjectType.objects.get(model=content_type)
10+
except ObjectType.DoesNotExist:
1111
pass
1212
return None
1313

@@ -36,7 +36,7 @@ def load_data(self):
3636
)
3737

3838
if created:
39-
custom_link.content_types.add(content_type)
39+
custom_link.object_types.add(content_type)
4040
custom_link.save()
4141
print("🔗 Created Custom Link '{0}'".format(custom_link.name))
4242

src/netbox_initializers/initializers/device_roles.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dcim.models import DeviceRole
2-
from utilities.choices import ColorChoices
2+
from netbox.choices import ColorChoices
33

44
from . import BaseInitializer, register_initializer
55

src/netbox_initializers/initializers/devices.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
MATCH_PARAMS = ["device_type", "name", "site"]
99
REQUIRED_ASSOCS = {
10-
"device_role": (DeviceRole, "name"),
10+
"role": (DeviceRole, "name"),
1111
"device_type": (DeviceType, "model"),
1212
"site": (Site, "name"),
1313
}

src/netbox_initializers/initializers/groups.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from users.models import NetBoxGroup, NetBoxUser
1+
from users.models import Group, User
22

33
from . import BaseInitializer, register_initializer
44

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

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

src/netbox_initializers/initializers/object_permissions.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from django.contrib.contenttypes.models import ContentType
2-
from users.models import NetBoxGroup, NetBoxUser, ObjectPermission
1+
from core.models import ObjectType
2+
from users.models import Group, ObjectPermission, User
33

44
from . import BaseInitializer, register_initializer
55

@@ -28,27 +28,27 @@ def load_data(self):
2828
object_types = permission_details["object_types"]
2929

3030
if object_types == "all":
31-
object_permission.object_types.set(ContentType.objects.all())
31+
object_permission.object_types.set(ObjectType.objects.all())
3232

3333
else:
3434
for app_label, models in object_types.items():
3535
if models == "all":
36-
app_models = ContentType.objects.filter(app_label=app_label)
36+
app_models = ObjectType.objects.filter(app_label=app_label)
3737

3838
for app_model in app_models:
3939
object_permission.object_types.add(app_model.id)
4040
else:
4141
# There is
4242
for model in models:
4343
object_permission.object_types.add(
44-
ContentType.objects.get(app_label=app_label, model=model)
44+
ObjectType.objects.get(app_label=app_label, model=model)
4545
)
4646
if created:
4747
print("🔓 Created object permission", object_permission.name)
4848

4949
if permission_details.get("groups", 0):
5050
for groupname in permission_details["groups"]:
51-
group = NetBoxGroup.objects.filter(name=groupname).first()
51+
group = Group.objects.filter(name=groupname).first()
5252

5353
if group:
5454
object_permission.groups.add(group)
@@ -59,7 +59,7 @@ def load_data(self):
5959

6060
if permission_details.get("users", 0):
6161
for username in permission_details["users"]:
62-
user = NetBoxUser.objects.filter(username=username).first()
62+
user = User.objects.filter(username=username).first()
6363

6464
if user:
6565
object_permission.users.add(user)

src/netbox_initializers/initializers/rack_roles.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dcim.models import RackRole
2-
from utilities.choices import ColorChoices
2+
from netbox.choices import ColorChoices
33

44
from . import BaseInitializer, register_initializer
55

src/netbox_initializers/initializers/tags.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from django.contrib.contenttypes.models import ContentType
1+
from core.models import ObjectType
22
from extras.models import Tag
3-
from utilities.choices import ColorChoices
3+
from netbox.choices import ColorChoices
44

55
from . import BaseInitializer, register_initializer
66

@@ -29,7 +29,7 @@ def load_data(self):
2929

3030
if object_types:
3131
for ot in object_types:
32-
ct = ContentType.objects.get(
32+
ct = ObjectType.objects.get(
3333
app_label=ot["app"],
3434
model=ot["model"],
3535
)

src/netbox_initializers/initializers/users.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from users.models import NetBoxUser, Token
1+
from users.models import Token, User
22

33
from . import BaseInitializer, register_initializer
44

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

1414
for username, user_details in users.items():
1515
api_token = user_details.pop("api_token", Token.generate_key())
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-
)
16+
password = user_details.pop("password", User.objects.make_random_password())
17+
user, created = User.objects.get_or_create(username=username, defaults=user_details)
2018
if created:
2119
user.set_password(password)
2220
user.save()

src/netbox_initializers/initializers/yaml/devices.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
## Examples:
1515

1616
# - name: server01
17-
# device_role: server
17+
# role: server
1818
# device_type: Other
1919
# site: AMS 1
2020
# rack: rack-01
@@ -23,7 +23,7 @@
2323
# custom_field_data:
2424
# text_field: Description
2525
# - name: server02
26-
# device_role: server
26+
# role: server
2727
# device_type: Other
2828
# site: AMS 2
2929
# rack: rack-02
@@ -36,7 +36,7 @@
3636
# custom_field_data:
3737
# text_field: Description
3838
# - name: server03
39-
# device_role: server
39+
# role: server
4040
# device_type: Other
4141
# site: SING 1
4242
# rack: rack-03
@@ -45,7 +45,7 @@
4545
# custom_field_data:
4646
# text_field: Description
4747
# - name: server04
48-
# device_role: server
48+
# role: server
4949
# device_type: Other
5050
# site: SING 1
5151
# location: cage 101
@@ -57,7 +57,7 @@
5757
#
5858
## Templated device
5959
# - name: gns3-tor
60-
# device_role: switch
60+
# role: switch
6161
# device_type: TOR-8P
6262
# site: SING 1
6363
# rack: rack-03

test/Dockerfile

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

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

test/env/netbox.env

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ REDIS_INSECURE_SKIP_TLS_VERIFY=false
1414
REDIS_PASSWORD=aC4eic9if9de4eHi@kah
1515
REDIS_SSL=false
1616
SECRET_KEY=yam+ie6Uhou5ciGaez7Psheihae*Nga3wohz9ietsae8Hu:chung:aeGeat9
17+
SKIP_SUPERUSER=true
1718
WEBHOOKS_ENABLED=true

0 commit comments

Comments
 (0)