Skip to content

Commit 7bf812f

Browse files
authored
Fix netbox feature tests job failure in Netbox 4.3 (#115)
* Update feature-test.yml * Implement this change from v4.3 The device and virtual_machine foreign keys on the Service model have been replaced with a generic parent relationship to support the assignment of services to FHRP groups as well. * Fix optional association * Fix object check hopefully * Attempt to make services model type lookup more generic * implement changelog: The group foreign key on the Contact model has been replaced with a many-to-many groups field. * Fix contacts * Cleanup services change get to filter test * Change filter back to get * More cleanup * Revert feature test workflow change * Remove quotes from services parent_type yaml * Update netbox 4.2 references to 4.3 * Update readme * Attempt to fix lint errors * Enable lint workflow for test * Disable lint check * Enable manual run of tests * Remove manual run of tests * Reenable manual lint test run * Final cleanup * Fix pylint error
1 parent 87e7d2b commit 7bf812f

File tree

8 files changed

+44
-32
lines changed

8 files changed

+44
-32
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Load data from YAML files into Netbox
77
First activate your virtual environment where Netbox is installed, the install the plugin version correspondig to your Netbox version.
88

99
```bash
10-
pip install "netbox-initializers==4.2.*"
10+
pip install "netbox-initializers==4.3.*"
1111
```
1212

1313
Then you need to add the plugin to the `PLUGINS` array in the Netbox configuration.
@@ -38,6 +38,6 @@ The initializers where a part of the Docker image and where then extracted into
3838
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:
3939

4040
```dockerfile
41-
FROM netboxcommunity/netbox:v4.2
42-
RUN /opt/netbox/venv/bin/pip install "netbox-initializers==4.2.*"
41+
FROM netboxcommunity/netbox:v4.3
42+
RUN /opt/netbox/venv/bin/pip install "netbox-initializers==4.3.*"
4343
```

src/netbox_initializers/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class NetBoxInitializersConfig(PluginConfig):
99
description = "Load initial data into Netbox"
1010
version = VERSION
1111
base_url = "initializers"
12-
min_version = "4.2.0"
13-
max_version = "4.2.99"
12+
min_version = "4.3.0"
13+
max_version = "4.3.99"
1414

1515

1616
config = NetBoxInitializersConfig

src/netbox_initializers/initializers/contacts.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from netbox_initializers.initializers.base import BaseInitializer, register_initializer
44

5-
OPTIONAL_ASSOCS = {"group": (ContactGroup, "name")}
6-
75

86
class ContactInitializer(BaseInitializer):
97
data_file_name = "contacts.yml"
@@ -16,19 +14,26 @@ def load_data(self):
1614
custom_field_data = self.pop_custom_fields(params)
1715
tags = params.pop("tags", None)
1816

19-
for assoc, details in OPTIONAL_ASSOCS.items():
20-
if assoc in params:
21-
model, field = details
22-
query = {field: params.pop(assoc)}
23-
24-
params[assoc] = model.objects.get(**query)
17+
# Group foreign key on the Contact model is a now many-to-many groups field
18+
groups = params.pop("groups", None) # Extract the groups from params if they exist
19+
group_objects = []
20+
if groups:
21+
for group_name in groups: # Iterate through the group names
22+
try:
23+
group_objects.append(ContactGroup.objects.get(name=group_name))
24+
except ContactGroup.DoesNotExist:
25+
raise ValueError(f"ContactGroup with name '{group_name}' does not exist.")
2526

2627
matching_params, defaults = self.split_params(params)
2728
contact, created = Contact.objects.get_or_create(**matching_params, defaults=defaults)
2829

2930
if created:
3031
print("👩‍💻 Created Contact", contact.name)
3132

33+
# Add the groups to the contact if any were found
34+
if group_objects:
35+
contact.groups.set(group_objects)
36+
3237
self.set_custom_fields_values(contact, custom_field_data)
3338
self.set_tags(contact, tags)
3439

src/netbox_initializers/initializers/services.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
from dcim.models import Device
21
from ipam.models import Service
3-
from virtualization.models import VirtualMachine
2+
from django.contrib.contenttypes.models import ContentType
43

54
from netbox_initializers.initializers.base import BaseInitializer, register_initializer
65

7-
MATCH_PARAMS = ["name", "device", "virtual_machine"]
8-
OPTIONAL_ASSOCS = {
9-
"device": (Device, "name"),
10-
"virtual_machine": (VirtualMachine, "name"),
11-
}
6+
MATCH_PARAMS = ["name", "parent_object_type", "parent_object_id"]
127

138

149
class ServiceInitializer(BaseInitializer):
@@ -21,11 +16,18 @@ def load_data(self):
2116
for params in services:
2217
tags = params.pop("tags", None)
2318

24-
for assoc, details in OPTIONAL_ASSOCS.items():
25-
if assoc in params:
26-
model, field = details
27-
query = {field: params.pop(assoc)}
28-
params[assoc] = model.objects.get(**query)
19+
# Get model from Contenttype
20+
scope_type = params.pop("parent_type", None)
21+
if not scope_type:
22+
print(
23+
f"Services '{params['name']}': parent_type is missing from Services"
24+
)
25+
app_label, model = str(scope_type).split(".")
26+
parent_model = ContentType.objects.get(app_label=app_label, model=model).model_class()
27+
parent = parent_model.objects.get(name=params.pop("parent_name"))
28+
29+
params["parent_object_type"] = ContentType.objects.get_for_model(parent)
30+
params["parent_object_id"] = parent.id
2931

3032
matching_params, defaults = self.split_params(params, MATCH_PARAMS)
3133
service, created = Service.objects.get_or_create(**matching_params, defaults=defaults)

src/netbox_initializers/initializers/yaml/contacts.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
# address: 1200 Nowhere Blvd, Scranton NJ, 555111
66
# comments: This is a very important contact
77
# - name: Ali Gator
8-
# group: Network-Team
8+
# groups:
9+
# - Network-Team
910
# title: Consultant for Widget Corp
1011
# phone: 221-555-1213
1112
1213
# address: 1200 Nowhere Blvd, Scranton NJ, 555111
1314
# comments: This is a very important contact
1415
# - name: Karlchen Maier
15-
# group: New Contact Group
16+
# groups:
17+
# - New Contact Group
1618
# title: COO of Widget Corp
1719
# phone: 221-555-1214
1820

src/netbox_initializers/initializers/yaml/services.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
# protocol: TCP
33
# ports:
44
# - 53
5-
# virtual_machine: virtual machine 1
5+
# parent_type: virtualization.virtualmachine
6+
# parent_name: virtual machine 1
67
# - name: DNS
78
# protocol: UDP
89
# ports:
910
# - 53
10-
# virtual_machine: virtual machine 1
11+
# parent_type: virtualization.virtualmachine
12+
# parent_name: virtual machine 1
1113
# - name: MISC
1214
# protocol: UDP
1315
# ports:
1416
# - 4000
15-
# device: server01
17+
# parent_type: dcim.device
18+
# parent_name: server01

src/netbox_initializers/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = "4.2.0"
1+
VERSION = "4.3.0"

test/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM netboxcommunity/netbox:v4.2
1+
FROM netboxcommunity/netbox:v4.3
22

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

0 commit comments

Comments
 (0)