Skip to content

Commit 55dcd11

Browse files
authored
Initial NetBox v3.3 support (#22)
Support the new Cable and CableTermination model from NetBox v3.3 with the initializer.
1 parent a62fd0f commit 55dcd11

File tree

4 files changed

+42
-25
lines changed

4 files changed

+42
-25
lines changed

README.md

Lines changed: 3 additions & 3 deletions
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.2.*"
9+
pip install "netbox-initializers==3.3.*"
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.2
40-
RUN /opt/netbox/venv/bin/pip install "netbox-initializers==3.2.*"
39+
FROM netboxcommunity/netbox:v3.3
40+
RUN /opt/netbox/venv/bin/pip install "netbox-initializers==3.3.*"
4141
```

src/netbox_initializers/__init__.py

Lines changed: 3 additions & 3 deletions
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.2.3"
8+
version = "3.3.0"
99
base_url = "initializers"
10-
min_version = "3.2.0"
11-
max_version = "3.2.99"
10+
min_version = "3.3.0"
11+
max_version = "3.3.99"
1212

1313

1414
config = NetBoxInitializersConfig

src/netbox_initializers/initializers/cables.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from circuits.models import Circuit, CircuitTermination, ProviderNetwork
44
from dcim.models import (
55
Cable,
6+
CableTermination,
67
ConsolePort,
78
ConsoleServerPort,
89
FrontPort,
@@ -93,21 +94,26 @@ def cable_in_cables(term_a: tuple, term_b: tuple) -> bool:
9394
Each tuple should consist termination object and termination type
9495
"""
9596

96-
cable = Cable.objects.filter(
97-
Q(
98-
termination_a_id=term_a[0].id,
99-
termination_a_type=term_a[1],
100-
termination_b_id=term_b[0].id,
101-
termination_b_type=term_b[1],
97+
try:
98+
cable_term_a = CableTermination.objects.get(
99+
Q(
100+
termination_id=term_a[0].id,
101+
termination_type=term_a[1],
102+
)
102103
)
103-
| Q(
104-
termination_a_id=term_b[0].id,
105-
termination_a_type=term_b[1],
106-
termination_b_id=term_a[0].id,
107-
termination_b_type=term_a[1],
104+
cable_term_b = CableTermination.objects.get(
105+
Q(
106+
termination_id=term_b[0].id,
107+
termination_type=term_b[1],
108+
)
108109
)
109-
)
110-
return cable.exists()
110+
except CableTermination.DoesNotExist:
111+
return False
112+
113+
cable_a = Cable.objects.get(Q(terminations=cable_term_a))
114+
cable_b = Cable.objects.get(Q(terminations=cable_term_b))
115+
116+
return cable_a.id == cable_b.id
111117

112118

113119
def check_termination_types(type_a, type_b) -> Tuple[bool, str]:
@@ -223,13 +229,24 @@ def load_data(self):
223229

224230
check_terminations_are_free(term_a, term_b)
225231

226-
params["termination_a_id"] = term_a.id
227-
params["termination_b_id"] = term_b.id
228-
params["termination_a_type"] = term_a_ct
229-
params["termination_b_type"] = term_b_ct
230-
231232
cable = Cable.objects.create(**params)
232233

234+
params_a_term = {
235+
"termination_id": term_a.id,
236+
"termination_type": term_a_ct,
237+
"cable": cable,
238+
"cable_end": "A",
239+
}
240+
CableTermination.objects.create(**params_a_term)
241+
242+
params_b_term = {
243+
"termination_id": term_b.id,
244+
"termination_type": term_b_ct,
245+
"cable": cable,
246+
"cable_end": "B",
247+
}
248+
CableTermination.objects.create(**params_b_term)
249+
233250
print(f"🧷 Created cable {cable} {cable_name}")
234251

235252

test/Dockerfile

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

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

0 commit comments

Comments
 (0)