Skip to content

Commit 4f27964

Browse files
authored
Compatible with Netbox 3.4 (#39)
1 parent c79a796 commit 4f27964

File tree

8 files changed

+32
-16
lines changed

8 files changed

+32
-16
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.3.*"
9+
pip install "netbox-initializers==3.4.*"
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.3
40-
RUN /opt/netbox/venv/bin/pip install "netbox-initializers==3.3.*"
39+
FROM netboxcommunity/netbox:v3.4
40+
RUN /opt/netbox/venv/bin/pip install "netbox-initializers==3.4.*"
4141
```

pyproject.toml

Lines changed: 1 addition & 1 deletion
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.3.3"
14+
version = "3.4.0"
1515

1616
[tool.poetry.dependencies]
1717
python = "^3.8"

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.3.3"
8+
version = "3.4.0"
99
base_url = "initializers"
10-
min_version = "3.3.0"
11-
max_version = "3.3.99"
10+
min_version = "3.4.0"
11+
max_version = "3.4.99"
1212

1313

1414
config = NetBoxInitializersConfig

src/netbox_initializers/initializers/custom_links.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
from . import BaseInitializer, register_initializer
55

66

7-
def get_content_type_id(content_type):
7+
def get_content_type(content_type):
88
try:
9-
return ContentType.objects.get(model=content_type).id
9+
return ContentType.objects.get(model=content_type)
1010
except ContentType.DoesNotExist:
1111
pass
12+
return None
1213

1314

1415
class CustomLinkInitializer(BaseInitializer):
@@ -19,9 +20,9 @@ def load_data(self):
1920
if custom_links is None:
2021
return
2122
for link in custom_links:
22-
content_type = link.pop("content_type")
23-
link["content_type_id"] = get_content_type_id(content_type)
24-
if link["content_type_id"] is None:
23+
content_type_name = link.pop("content_type")
24+
content_type = get_content_type(content_type_name)
25+
if content_type is None:
2526
print(
2627
"⚠️ Unable to create Custom Link '{0}': The content_type '{1}' is unknown".format(
2728
link.get("name"), content_type
@@ -35,6 +36,8 @@ def load_data(self):
3536
)
3637

3738
if created:
39+
custom_link.content_types.add(content_type)
40+
custom_link.save()
3841
print("🔗 Created Custom Link '{0}'".format(custom_link.name))
3942

4043

src/netbox_initializers/initializers/providers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from circuits.models import Provider
2+
from ipam.models import ASN
23

34
from . import BaseInitializer, register_initializer
45

@@ -13,10 +14,22 @@ def load_data(self):
1314
for params in providers:
1415
custom_field_data = self.pop_custom_fields(params)
1516

17+
asn_number = params.pop("asn")
18+
asn = ASN.objects.filter(asn=asn_number).first()
19+
if asn is None:
20+
print(
21+
"⚠️ Unable to create Provider '{0}': The ASN '{1}' is unknown".format(
22+
params.get("name"), asn_number
23+
)
24+
)
25+
continue
26+
1627
matching_params, defaults = self.split_params(params)
1728
provider, created = Provider.objects.get_or_create(**matching_params, defaults=defaults)
1829

1930
if created:
31+
provider.asns.add(asn)
32+
provider.save()
2033
print("📡 Created provider", provider.name)
2134

2235
self.set_custom_fields_values(provider, custom_field_data)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# - name: Provider1
22
# slug: provider1
3-
# asn: 121
3+
# asn: 1
44
# - name: Provider2
55
# slug: provider2
6-
# asn: 122
6+
# asn: 3

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.3
1+
FROM netboxcommunity/netbox:v3.4
22

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

test/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ services:
1515
dockerfile: test/Dockerfile
1616
# postgres
1717
postgres:
18-
image: postgres:14-alpine
18+
image: postgres:15-alpine
1919
env_file: env/postgres.env
2020
volumes:
2121
- netbox-postgres-data:/var/lib/postgresql/data

0 commit comments

Comments
 (0)