Skip to content

Commit

Permalink
Merge pull request #485 from tobiasge/prepare-2.11
Browse files Browse the repository at this point in the history
Initializer updates for Netbox 2.11
  • Loading branch information
tobiasge authored Apr 16, 2021
2 parents eee07f7 + 752f592 commit c7df608
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 73 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ jobs:
name: Checks syntax of our code
steps:
- uses: actions/checkout@v2
with:
# Full git history is needed to get a proper list of changed files within `super-linter`
fetch-depth: 0
- uses: actions/setup-python@v2
- name: Lint Code Base
uses: github/super-linter@v3
Expand Down
8 changes: 4 additions & 4 deletions initializers/custom_links.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
## Examples:

# - name: link_to_repo
# text: 'Link to Netbox Docker'
# url: 'https://github.com/netbox-community/netbox-docker'
# link_text: 'Link to Netbox Docker'
# link_url: 'https://github.com/netbox-community/netbox-docker'
# new_window: False
# content_type: device
# - name: link_to_localhost
# text: 'Link to localhost'
# url: 'http://localhost'
# link_text: 'Link to localhost'
# link_url: 'http://localhost'
# new_window: True
# content_type: device
9 changes: 9 additions & 0 deletions initializers/devices.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,12 @@
# position: 3
# custom_field_data:
# text_field: Description
# - name: server04
# device_role: server
# device_type: Other
# site: SING 1
# location: cage 101
# face: front
# position: 3
# custom_field_data:
# text_field: Description
File renamed without changes.
2 changes: 1 addition & 1 deletion initializers/power_panels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# site: AMS 1
# - name: power panel SING 1
# site: SING 1
# rack_group: cage 101
# location: cage 101
2 changes: 1 addition & 1 deletion initializers/racks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
# text_field: Description
# - site: SING 1
# name: rack-03
# group: cage 101
# location: cage 101
# role: Role 3
# type: 4-post-cabinet
# width: 19
Expand Down
22 changes: 20 additions & 2 deletions initializers/vlan_groups.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
# - name: VLAN group 1
# site: AMS 1
# scope_type: dcim.region
# scope: Amsterdam
# slug: vlan-group-1
# - name: VLAN group 2
# site: AMS 1
# scope_type: dcim.site
# scope: AMS 1
# slug: vlan-group-2
# - name: VLAN group 3
# scope_type: dcim.location
# scope: cage 101
# slug: vlan-group-3
# - name: VLAN group 4
# scope_type: dcim.rack
# scope: rack-01
# slug: vlan-group-4
# - name: VLAN group 5
# scope_type: virtualization.cluster
# scope: cluster1
# slug: vlan-group-5
# - name: VLAN group 6
# scope_type: virtualization.clustergroup
# scope: Group 1
# slug: vlan-group-6
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import sys

from dcim.models import RackGroup, Site
from dcim.models import Location, Site
from startup_script_utils import load_yaml

rack_groups = load_yaml("/opt/netbox/initializers/rack_groups.yml")
rack_groups = load_yaml("/opt/netbox/initializers/locations.yml")

if rack_groups is None:
sys.exit()
Expand All @@ -17,7 +17,7 @@
query = {field: params.pop(assoc)}
params[assoc] = model.objects.get(**query)

rack_group, created = RackGroup.objects.get_or_create(**params)
location, created = Location.objects.get_or_create(**params)

if created:
print("🎨 Created rack group", rack_group.name)
print("🎨 Created location", location.name)
4 changes: 2 additions & 2 deletions startup_scripts/080_racks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys

from dcim.models import Rack, RackGroup, RackRole, Site
from dcim.models import Location, Rack, RackRole, Site
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values
from tenancy.models import Tenant

Expand All @@ -14,7 +14,7 @@
optional_assocs = {
"role": (RackRole, "name"),
"tenant": (Tenant, "name"),
"group": (RackGroup, "name"),
"location": (Location, "name"),
}

for params in racks:
Expand Down
3 changes: 2 additions & 1 deletion startup_scripts/140_devices.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys

from dcim.models import Device, DeviceRole, DeviceType, Platform, Rack, Site
from dcim.models import Device, DeviceRole, DeviceType, Location, Platform, Rack, Site
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values
from tenancy.models import Tenant
from virtualization.models import Cluster
Expand All @@ -21,6 +21,7 @@
"platform": (Platform, "name"),
"rack": (Rack, "name"),
"cluster": (Cluster, "name"),
"location": (Location, "name"),
}

for params in devices:
Expand Down
51 changes: 0 additions & 51 deletions startup_scripts/145_devices.py

This file was deleted.

21 changes: 16 additions & 5 deletions startup_scripts/200_vlan_groups.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys

from dcim.models import Site
from django.contrib.contenttypes.models import ContentType
from ipam.models import VLANGroup
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values

Expand All @@ -9,7 +9,7 @@
if vlan_groups is None:
sys.exit()

optional_assocs = {"site": (Site, "name")}
optional_assocs = {"scope": (None, "name")}

for params in vlan_groups:
custom_field_data = pop_custom_fields(params)
Expand All @@ -18,9 +18,20 @@
if assoc in params:
model, field = details
query = {field: params.pop(assoc)}

params[assoc] = model.objects.get(**query)

# Get model from Contenttype
scope_type = params.pop("scope_type", None)
if not scope_type:
print(f"VLAN Group '{params['name']}': scope_type is missing from VLAN Group")
continue
app_label, model = str(scope_type).split(".")
ct = ContentType.objects.filter(app_label=app_label, model=model).first()
if not ct:
print(
f"VLAN Group '{params['name']}': ContentType for "
+ f"app_label = '{app_label}' and model = '{model}' not found"
)
continue
params["scope_id"] = ct.model_class().objects.get(**query).id
vlan_group, created = VLANGroup.objects.get_or_create(**params)

if created:
Expand Down
4 changes: 2 additions & 2 deletions startup_scripts/330_power_panels.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys

from dcim.models import PowerPanel, RackGroup, Site
from dcim.models import Location, PowerPanel, Site
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values

power_panels = load_yaml("/opt/netbox/initializers/power_panels.yml")
Expand All @@ -10,7 +10,7 @@

required_assocs = {"site": (Site, "name")}

optional_assocs = {"rack_group": (RackGroup, "name")}
optional_assocs = {"location": (Location, "name")}

for params in power_panels:
custom_field_data = pop_custom_fields(params)
Expand Down

0 comments on commit c7df608

Please sign in to comment.