Skip to content

Commit

Permalink
Compatibility with Netbox 3.6 (tobiasge#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasge authored and yanndegat committed Sep 12, 2023
1 parent aae4c76 commit 0be4094
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 61 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.5.*"
pip install "netbox-initializers==3.6.*"
```
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.5
RUN /opt/netbox/venv/bin/pip install "netbox-initializers==3.5.*"
FROM netboxcommunity/netbox:v3.6
RUN /opt/netbox/venv/bin/pip install "netbox-initializers==3.6.*"
```
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.5.2"
version = "3.6.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.5.2"
version = "3.6.0"
base_url = "initializers"
min_version = "3.5.0"
max_version = "3.5.99"
min_version = "3.6.0"
max_version = "3.6.99"


config = NetBoxInitializersConfig
22 changes: 8 additions & 14 deletions src/netbox_initializers/initializers/custom_fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from extras.models import CustomField
from extras.models import CustomField, CustomFieldChoiceSet

from . import BaseInitializer, register_initializer

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

# choices should only be applied when type is select, multiselect
if cf_details.get("choices"):
if choices := cf_details.get("choices"):
if cf_details.get("type") not in (
"select",
"multiselect",
Expand All @@ -124,18 +124,12 @@ def load_data(self):
)
custom_field.delete()
continue
custom_field.choices = []

for choice_detail in cf_details.get("choices", []):
if isinstance(choice_detail, dict) and "value" in choice_detail:
# legacy mode
print(
f"⚠️ Please migrate the choice '{choice_detail['value']}' of '{cf_name}'"
+ " to the new format, as 'weight' is no longer supported!"
)
custom_field.choices.append(choice_detail["value"])
else:
custom_field.choices.append(choice_detail)
choice_set, _ = CustomFieldChoiceSet.objects.get_or_create(
name=f"{cf_name}_choices"
)
choice_set.extra_choices = choices
choice_set.save()
custom_field.choice_set = choice_set

custom_field.save()

Expand Down
6 changes: 3 additions & 3 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 AdminGroup, AdminUser
from users.models import NetBoxGroup, NetBoxUser

from . import BaseInitializer, register_initializer

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

for groupname, group_details in groups.items():
group, created = AdminGroup.objects.get_or_create(name=groupname)
group, created = NetBoxGroup.objects.get_or_create(name=groupname)
if created:
print("👥 Created group", groupname)
for username in group_details.get("users", []):
user = AdminUser.objects.get(username=username)
user = NetBoxUser.objects.get(username=username)
if user:
group.user_set.add(user)
print(" 👤 Assigned user %s to group %s" % (username, group.name))
Expand Down
7 changes: 3 additions & 4 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 AdminGroup, AdminUser, ObjectPermission
from users.models import NetBoxGroup, NetBoxUser, ObjectPermission

from . import BaseInitializer, register_initializer

Expand All @@ -12,7 +12,6 @@ def load_data(self):
if object_permissions is None:
return
for permission_name, permission_details in object_permissions.items():

object_permission, created = ObjectPermission.objects.get_or_create(
name=permission_name,
defaults={
Expand Down Expand Up @@ -49,7 +48,7 @@ def load_data(self):

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

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

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

if user:
object_permission.users.add(user)
Expand Down
9 changes: 5 additions & 4 deletions src/netbox_initializers/initializers/users.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.contrib.auth.models import User
from users.models import Token
from users.models import NetBoxUser, Token

from . import BaseInitializer, register_initializer

Expand All @@ -14,8 +13,10 @@ 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", User.objects.make_random_password())
user, created = User.objects.get_or_create(username=username, defaults=user_details)
password = user_details.pop("password", NetBoxUser.objects.make_random_password())
user, created = NetBoxUser.objects.get_or_create(
username=username, defaults=user_details
)
if created:
user.set_password(password)
user.save()
Expand Down
15 changes: 0 additions & 15 deletions src/netbox_initializers/initializers/yaml/custom_fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,6 @@
# - Third Item
# - Fifth Item
# - Fourth Item
# select_field_legacy_format:
# type: select
# label: Choose between items
# required: false
# filter_logic: loose
# weight: 30
# on_objects:
# - dcim.models.Device
# choices:
# - value: A # this is the deprecated format.
# - value: B # we only use it for the tests.
# - value: C # please see above for the new format.
# - value: "D like deprecated"
# weight: 999
# - value: E
# boolean_field:
# type: boolean
# label: Yes Or No?
Expand Down
14 changes: 7 additions & 7 deletions src/netbox_initializers/initializers/yaml/device_types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,27 @@
# - name_template: ttyS[1-48]
# type: rj-45
# power_ports:
# - name_template: psu[0,1]
# - name_template: psu[0-1]
# type: iec-60320-c14
# maximum_draw: 35
# allocated_draw: 35
# front_ports:
# - name_template: front[1,2]
# - name_template: front[1-2]
# type: 8p8c
# rear_port_template: rear[0,1]
# rear_port_position_template: "[1,2]"
# rear_port_template: rear[0-1]
# rear_port_position_template: "[1-2]"
# rear_ports:
# - name_template: rear[0,1]
# - name_template: rear[0-1]
# type: 8p8c
# positions_template: "[3,2]"
# positions_template: "[2-3]"
# device_bays:
# - name: bay0 # both non-template and template field specified; non-template field takes precedence
# name_template: bay[0-9]
# label: test0
# label_template: test[0-5,9,6-8]
# description: Test description
# power_outlets:
# - name_template: outlet[0,1]
# - name_template: outlet[0-1]
# type: iec-60320-c5
# power_port: psu0
# feed_leg: B
6 changes: 0 additions & 6 deletions src/netbox_initializers/initializers/yaml/platforms.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
# - name: Platform 1
# slug: platform-1
# manufacturer: Manufacturer 1
# napalm_driver: driver1
# napalm_args: "{'arg1': 'value1', 'arg2': 'value2'}"
# - name: Platform 2
# slug: platform-2
# manufacturer: Manufacturer 2
# napalm_driver: driver2
# napalm_args: "{'arg1': 'value1', 'arg2': 'value2'}"
# - name: Platform 3
# slug: platform-3
# manufacturer: No Name
# napalm_driver: driver3
# napalm_args: "{'arg1': 'value1', 'arg2': 'value2'}"
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.5
FROM netboxcommunity/netbox:v3.6

COPY ../ /opt/netbox-initializers/
COPY ./test/config/plugins.py /etc/netbox/config/
Expand Down

0 comments on commit 0be4094

Please sign in to comment.