Skip to content

Commit

Permalink
Merge pull request #99 from tobiasge/prepare-for-nb40
Browse files Browse the repository at this point in the history
Fix #97: Prepare for Netbox 4.0
  • Loading branch information
tobiasge authored May 7, 2024
2 parents 5d3bc30 + 2067b02 commit 50d077d
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 46 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Load data from YAML files into Netbox

First activate your virtual environment where Netbox is installed, the install the plugin version correspondig to your Netbox version.
```bash
pip install "netbox-initializers==3.7.*"
pip install "netbox-initializers==4.0.*"
```
Then you need to add the plugin to the `PLUGINS` array in the Netbox configuration.
```python
Expand Down Expand Up @@ -36,6 +36,6 @@ The initializers where a part of the Docker image and where then extracted into
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:

```dockerfile
FROM netboxcommunity/netbox:v3.7
RUN /opt/netbox/venv/bin/pip install "netbox-initializers==3.7.*"
FROM netboxcommunity/netbox:v4.0
RUN /opt/netbox/venv/bin/pip install "netbox-initializers==4.0.*"
```
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license = "Apache-2.0"
name = "netbox-initializers"
readme = "README.md"
repository = "https://github.com/tobiasge/netbox-initializers"
version = "3.7.2"
version = "4.0.0"

[tool.poetry.dependencies]
python = "^3.8"
Expand Down
6 changes: 3 additions & 3 deletions src/netbox_initializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ class NetBoxInitializersConfig(PluginConfig):
name = "netbox_initializers"
verbose_name = "NetBox Initializers"
description = "Load initial data into Netbox"
version = "3.7.2"
version = "4.0.0"
base_url = "initializers"
min_version = "3.7.0"
max_version = "3.7.99"
min_version = "4.0-beta1"
max_version = "4.0.99"


config = NetBoxInitializersConfig
8 changes: 4 additions & 4 deletions src/netbox_initializers/initializers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path
from typing import Tuple

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

ct = ContentType.objects.get_for_model(entity)
ct = ObjectType.objects.get_for_model(entity)

save = False
for tag in Tag.objects.filter(name__in=tags):
Expand Down
6 changes: 3 additions & 3 deletions src/netbox_initializers/initializers/custom_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
def get_class_for_class_path(class_path):
import importlib

from django.contrib.contenttypes.models import ContentType
from core.models import ObjectType

module_name, class_name = class_path.rsplit(".", 1)
module = importlib.import_module(module_name)
clazz = getattr(module, class_name)
return ContentType.objects.get_for_model(clazz)
return ObjectType.objects.get_for_model(clazz)


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

for object_type in cf_details.get("on_objects", []):
custom_field.content_types.add(get_class_for_class_path(object_type))
custom_field.object_types.add(get_class_for_class_path(object_type))

if cf_details.get("required", False):
custom_field.required = cf_details["required"]
Expand Down
8 changes: 4 additions & 4 deletions src/netbox_initializers/initializers/custom_links.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from django.contrib.contenttypes.models import ContentType
from core.models import ObjectType
from extras.models import CustomLink

from . import BaseInitializer, register_initializer


def get_content_type(content_type):
try:
return ContentType.objects.get(model=content_type)
except ContentType.DoesNotExist:
return ObjectType.objects.get(model=content_type)
except ObjectType.DoesNotExist:
pass
return None

Expand Down Expand Up @@ -36,7 +36,7 @@ def load_data(self):
)

if created:
custom_link.content_types.add(content_type)
custom_link.object_types.add(content_type)
custom_link.save()
print("🔗 Created Custom Link '{0}'".format(custom_link.name))

Expand Down
2 changes: 1 addition & 1 deletion src/netbox_initializers/initializers/device_roles.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dcim.models import DeviceRole
from utilities.choices import ColorChoices
from netbox.choices import ColorChoices

from . import BaseInitializer, register_initializer

Expand Down
2 changes: 1 addition & 1 deletion src/netbox_initializers/initializers/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

MATCH_PARAMS = ["device_type", "name", "site"]
REQUIRED_ASSOCS = {
"device_role": (DeviceRole, "name"),
"role": (DeviceRole, "name"),
"device_type": (DeviceType, "model"),
"site": (Site, "name"),
}
Expand Down
8 changes: 4 additions & 4 deletions src/netbox_initializers/initializers/groups.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from users.models import NetBoxGroup, NetBoxUser
from users.models import Group, User

from . import BaseInitializer, register_initializer

Expand All @@ -12,13 +12,13 @@ def load_data(self):
return

for groupname, group_details in groups.items():
group, created = NetBoxGroup.objects.get_or_create(name=groupname)
group, created = Group.objects.get_or_create(name=groupname)
if created:
print("👥 Created group", groupname)
for username in group_details.get("users", []):
user = NetBoxUser.objects.get(username=username)
user = User.objects.get(username=username)
if user:
group.user_set.add(user)
group.users.add(user)
print(" 👤 Assigned user %s to group %s" % (username, group.name))
group.save()

Expand Down
14 changes: 7 additions & 7 deletions src/netbox_initializers/initializers/object_permissions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib.contenttypes.models import ContentType
from users.models import NetBoxGroup, NetBoxUser, ObjectPermission
from core.models import ObjectType
from users.models import Group, ObjectPermission, User

from . import BaseInitializer, register_initializer

Expand Down Expand Up @@ -28,27 +28,27 @@ def load_data(self):
object_types = permission_details["object_types"]

if object_types == "all":
object_permission.object_types.set(ContentType.objects.all())
object_permission.object_types.set(ObjectType.objects.all())

else:
for app_label, models in object_types.items():
if models == "all":
app_models = ContentType.objects.filter(app_label=app_label)
app_models = ObjectType.objects.filter(app_label=app_label)

for app_model in app_models:
object_permission.object_types.add(app_model.id)
else:
# There is
for model in models:
object_permission.object_types.add(
ContentType.objects.get(app_label=app_label, model=model)
ObjectType.objects.get(app_label=app_label, model=model)
)
if created:
print("🔓 Created object permission", object_permission.name)

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

if group:
object_permission.groups.add(group)
Expand All @@ -59,7 +59,7 @@ def load_data(self):

if permission_details.get("users", 0):
for username in permission_details["users"]:
user = NetBoxUser.objects.filter(username=username).first()
user = User.objects.filter(username=username).first()

if user:
object_permission.users.add(user)
Expand Down
2 changes: 1 addition & 1 deletion src/netbox_initializers/initializers/rack_roles.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dcim.models import RackRole
from utilities.choices import ColorChoices
from netbox.choices import ColorChoices

from . import BaseInitializer, register_initializer

Expand Down
6 changes: 3 additions & 3 deletions src/netbox_initializers/initializers/tags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib.contenttypes.models import ContentType
from core.models import ObjectType
from extras.models import Tag
from utilities.choices import ColorChoices
from netbox.choices import ColorChoices

from . import BaseInitializer, register_initializer

Expand Down Expand Up @@ -29,7 +29,7 @@ def load_data(self):

if object_types:
for ot in object_types:
ct = ContentType.objects.get(
ct = ObjectType.objects.get(
app_label=ot["app"],
model=ot["model"],
)
Expand Down
8 changes: 3 additions & 5 deletions src/netbox_initializers/initializers/users.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from users.models import NetBoxUser, Token
from users.models import Token, User

from . import BaseInitializer, register_initializer

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

for username, user_details in users.items():
api_token = user_details.pop("api_token", Token.generate_key())
password = user_details.pop("password", NetBoxUser.objects.make_random_password())
user, created = NetBoxUser.objects.get_or_create(
username=username, defaults=user_details
)
password = user_details.pop("password", User.objects.make_random_password())
user, created = User.objects.get_or_create(username=username, defaults=user_details)
if created:
user.set_password(password)
user.save()
Expand Down
10 changes: 5 additions & 5 deletions src/netbox_initializers/initializers/yaml/devices.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
## Examples:

# - name: server01
# device_role: server
# role: server
# device_type: Other
# site: AMS 1
# rack: rack-01
Expand All @@ -23,7 +23,7 @@
# custom_field_data:
# text_field: Description
# - name: server02
# device_role: server
# role: server
# device_type: Other
# site: AMS 2
# rack: rack-02
Expand All @@ -36,7 +36,7 @@
# custom_field_data:
# text_field: Description
# - name: server03
# device_role: server
# role: server
# device_type: Other
# site: SING 1
# rack: rack-03
Expand All @@ -45,7 +45,7 @@
# custom_field_data:
# text_field: Description
# - name: server04
# device_role: server
# role: server
# device_type: Other
# site: SING 1
# location: cage 101
Expand All @@ -57,7 +57,7 @@
#
## Templated device
# - name: gns3-tor
# device_role: switch
# role: switch
# device_type: TOR-8P
# site: SING 1
# rack: rack-03
2 changes: 1 addition & 1 deletion test/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM netboxcommunity/netbox:v3.7
FROM netboxcommunity/netbox:feature

COPY ../ /opt/netbox-initializers/
COPY ./test/config/plugins.py /etc/netbox/config/
Expand Down
1 change: 1 addition & 0 deletions test/env/netbox.env
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ REDIS_INSECURE_SKIP_TLS_VERIFY=false
REDIS_PASSWORD=aC4eic9if9de4eHi@kah
REDIS_SSL=false
SECRET_KEY=yam+ie6Uhou5ciGaez7Psheihae*Nga3wohz9ietsae8Hu:chung:aeGeat9
SKIP_SUPERUSER=true
WEBHOOKS_ENABLED=true

0 comments on commit 50d077d

Please sign in to comment.