From 594408643525e1bdaa5e74e5f18959d9890ca2e3 Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Wed, 7 Apr 2021 09:15:21 +0200 Subject: [PATCH 1/8] Rack groups have been renamed to locations --- initializers/{rack_groups.yml => locations.yml} | 0 initializers/power_panels.yml | 2 +- initializers/racks.yml | 2 +- startup_scripts/{075_rack_groups.py => 075_locations.py} | 8 ++++---- startup_scripts/080_racks.py | 4 ++-- startup_scripts/330_power_panels.py | 4 ++-- 6 files changed, 10 insertions(+), 10 deletions(-) rename initializers/{rack_groups.yml => locations.yml} (100%) rename startup_scripts/{075_rack_groups.py => 075_locations.py} (60%) diff --git a/initializers/rack_groups.yml b/initializers/locations.yml similarity index 100% rename from initializers/rack_groups.yml rename to initializers/locations.yml diff --git a/initializers/power_panels.yml b/initializers/power_panels.yml index 1205851ac..a670ba7c8 100644 --- a/initializers/power_panels.yml +++ b/initializers/power_panels.yml @@ -2,4 +2,4 @@ # site: AMS 1 # - name: power panel SING 1 # site: SING 1 -# rack_group: cage 101 +# location: cage 101 diff --git a/initializers/racks.yml b/initializers/racks.yml index 379553df4..9071e190a 100644 --- a/initializers/racks.yml +++ b/initializers/racks.yml @@ -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 diff --git a/startup_scripts/075_rack_groups.py b/startup_scripts/075_locations.py similarity index 60% rename from startup_scripts/075_rack_groups.py rename to startup_scripts/075_locations.py index 3974b56f7..d8a2c5a11 100644 --- a/startup_scripts/075_rack_groups.py +++ b/startup_scripts/075_locations.py @@ -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() @@ -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) diff --git a/startup_scripts/080_racks.py b/startup_scripts/080_racks.py index 087b3f967..eb9bcf967 100644 --- a/startup_scripts/080_racks.py +++ b/startup_scripts/080_racks.py @@ -1,6 +1,6 @@ import sys -from dcim.models import Rack, RackGroup, RackRole, Site +from dcim.models import Rack, Location, RackRole, Site from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values from tenancy.models import Tenant @@ -14,7 +14,7 @@ optional_assocs = { "role": (RackRole, "name"), "tenant": (Tenant, "name"), - "group": (RackGroup, "name"), + "location": (Location, "name"), } for params in racks: diff --git a/startup_scripts/330_power_panels.py b/startup_scripts/330_power_panels.py index bfde18f5d..370a7b651 100644 --- a/startup_scripts/330_power_panels.py +++ b/startup_scripts/330_power_panels.py @@ -1,6 +1,6 @@ import sys -from dcim.models import PowerPanel, RackGroup, Site +from dcim.models import PowerPanel, Location, 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") @@ -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) From 45824ba4f31b1e77ff149a7d3ead84b6bf3a7117 Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Wed, 7 Apr 2021 09:57:59 +0200 Subject: [PATCH 2/8] Devices can be added to locations --- initializers/devices.yml | 9 ++++++ startup_scripts/140_devices.py | 3 +- startup_scripts/145_devices.py | 51 ---------------------------------- 3 files changed, 11 insertions(+), 52 deletions(-) delete mode 100644 startup_scripts/145_devices.py diff --git a/initializers/devices.yml b/initializers/devices.yml index 0de05046e..8324c36d3 100644 --- a/initializers/devices.yml +++ b/initializers/devices.yml @@ -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 diff --git a/startup_scripts/140_devices.py b/startup_scripts/140_devices.py index 597db0cc3..3e26c4178 100644 --- a/startup_scripts/140_devices.py +++ b/startup_scripts/140_devices.py @@ -1,6 +1,6 @@ import sys -from dcim.models import Device, DeviceRole, DeviceType, Platform, Rack, Site +from dcim.models import Device, DeviceRole, DeviceType, Platform, Rack, Site, Location from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values from tenancy.models import Tenant from virtualization.models import Cluster @@ -21,6 +21,7 @@ "platform": (Platform, "name"), "rack": (Rack, "name"), "cluster": (Cluster, "name"), + "location": (Location, "name"), } for params in devices: diff --git a/startup_scripts/145_devices.py b/startup_scripts/145_devices.py deleted file mode 100644 index 597db0cc3..000000000 --- a/startup_scripts/145_devices.py +++ /dev/null @@ -1,51 +0,0 @@ -import sys - -from dcim.models import Device, DeviceRole, DeviceType, 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 - -devices = load_yaml("/opt/netbox/initializers/devices.yml") - -if devices is None: - sys.exit() - -required_assocs = { - "device_role": (DeviceRole, "name"), - "device_type": (DeviceType, "model"), - "site": (Site, "name"), -} - -optional_assocs = { - "tenant": (Tenant, "name"), - "platform": (Platform, "name"), - "rack": (Rack, "name"), - "cluster": (Cluster, "name"), -} - -for params in devices: - custom_field_data = pop_custom_fields(params) - - # primary ips are handled later in `270_primary_ips.py` - params.pop("primary_ip4", None) - params.pop("primary_ip6", None) - - for assoc, details in required_assocs.items(): - model, field = details - query = {field: params.pop(assoc)} - - params[assoc] = model.objects.get(**query) - - for assoc, details in optional_assocs.items(): - if assoc in params: - model, field = details - query = {field: params.pop(assoc)} - - params[assoc] = model.objects.get(**query) - - device, created = Device.objects.get_or_create(**params) - - if created: - set_custom_fields_values(device, custom_field_data) - - print("🖥️ Created device", device.name) From b18e4aaf92b00edfc032acf7fdad1a8009c982ec Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Wed, 7 Apr 2021 10:57:14 +0200 Subject: [PATCH 3/8] Fix super-linter on PRs See: https://github.com/github/super-linter#example-connecting-github-action-workflow --- .github/workflows/push.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 43c648254..3ea6214b8 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -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 From 1c4b6741ac0fe2abd4c2656fcdeb26c2d39b285f Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Wed, 7 Apr 2021 11:01:01 +0200 Subject: [PATCH 4/8] Fixed isort issues --- startup_scripts/080_racks.py | 2 +- startup_scripts/140_devices.py | 2 +- startup_scripts/330_power_panels.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/startup_scripts/080_racks.py b/startup_scripts/080_racks.py index eb9bcf967..b2cfc801c 100644 --- a/startup_scripts/080_racks.py +++ b/startup_scripts/080_racks.py @@ -1,6 +1,6 @@ import sys -from dcim.models import Rack, Location, 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 diff --git a/startup_scripts/140_devices.py b/startup_scripts/140_devices.py index 3e26c4178..423b7c9de 100644 --- a/startup_scripts/140_devices.py +++ b/startup_scripts/140_devices.py @@ -1,6 +1,6 @@ import sys -from dcim.models import Device, DeviceRole, DeviceType, Platform, Rack, Site, Location +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 diff --git a/startup_scripts/330_power_panels.py b/startup_scripts/330_power_panels.py index 370a7b651..8542435bc 100644 --- a/startup_scripts/330_power_panels.py +++ b/startup_scripts/330_power_panels.py @@ -1,6 +1,6 @@ import sys -from dcim.models import PowerPanel, Location, 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") From 9874cef369ba91e26e09b4151b704e20c22480c2 Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Wed, 7 Apr 2021 15:18:33 +0200 Subject: [PATCH 5/8] VLAN Groups can be scoped to multiple types --- initializers/vlan_groups.yml | 22 ++++++++++++++++++++-- startup_scripts/200_vlan_groups.py | 18 +++++++++++++----- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/initializers/vlan_groups.yml b/initializers/vlan_groups.yml index 3e0bb00e3..a40fd0ac9 100644 --- a/initializers/vlan_groups.yml +++ b/initializers/vlan_groups.yml @@ -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 diff --git a/startup_scripts/200_vlan_groups.py b/startup_scripts/200_vlan_groups.py index 7b72b2d3e..6dda2143d 100644 --- a/startup_scripts/200_vlan_groups.py +++ b/startup_scripts/200_vlan_groups.py @@ -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 @@ -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) @@ -18,9 +18,17 @@ 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("scope_type is missing from VLAN Group") + continue + app_label, model = str(scope_type).split(".") + ct = ContentType.objects.get(app_label=app_label, model=model) + if not ct: + print(f"ContentType for 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: From 3673196c16fcbebd0b6b46a61887422ff435d9c8 Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Wed, 7 Apr 2021 15:19:14 +0200 Subject: [PATCH 6/8] Fields in Custom links have been renamed --- initializers/custom_links.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/initializers/custom_links.yml b/initializers/custom_links.yml index f1b275c49..eb733caa3 100644 --- a/initializers/custom_links.yml +++ b/initializers/custom_links.yml @@ -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 From f637de88f06e6ac5bc754a33899afc30eb24f088 Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Tue, 13 Apr 2021 14:31:05 +0200 Subject: [PATCH 7/8] Improved error logging --- initializers/vlan_groups.yml | 12 ++++++------ startup_scripts/200_vlan_groups.py | 9 ++++++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/initializers/vlan_groups.yml b/initializers/vlan_groups.yml index a40fd0ac9..facd34fcb 100644 --- a/initializers/vlan_groups.yml +++ b/initializers/vlan_groups.yml @@ -1,24 +1,24 @@ # - name: VLAN group 1 -# scope_type: "dcim.region" +# scope_type: dcim.region # scope: Amsterdam # slug: vlan-group-1 # - name: VLAN group 2 -# scope_type: "dcim.site" +# scope_type: dcim.site # scope: AMS 1 # slug: vlan-group-2 # - name: VLAN group 3 -# scope_type: "dcim.location" +# scope_type: dcim.location # scope: cage 101 # slug: vlan-group-3 # - name: VLAN group 4 -# scope_type: "dcim.rack" +# scope_type: dcim.rack # scope: rack-01 # slug: vlan-group-4 # - name: VLAN group 5 -# scope_type: "virtualization.cluster" +# scope_type: virtualization.cluster # scope: cluster1 # slug: vlan-group-5 # - name: VLAN group 6 -# scope_type: "virtualization.clustergroup" +# scope_type: virtualization.clustergroup # scope: Group 1 # slug: vlan-group-6 diff --git a/startup_scripts/200_vlan_groups.py b/startup_scripts/200_vlan_groups.py index 6dda2143d..eb6102351 100644 --- a/startup_scripts/200_vlan_groups.py +++ b/startup_scripts/200_vlan_groups.py @@ -21,12 +21,15 @@ # Get model from Contenttype scope_type = params.pop("scope_type", None) if not scope_type: - print("scope_type is missing from VLAN Group") + print(f"VLAN Group '{params['name']}': scope_type is missing from VLAN Group") continue app_label, model = str(scope_type).split(".") - ct = ContentType.objects.get(app_label=app_label, model=model) + ct = ContentType.objects.filter(app_label=app_label, model=model).first() if not ct: - print(f"ContentType for app_label = '{app_label}' and model = '{model}' not found") + print( + f"VLAN Group '{params['name']}': ContentType for " + + "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) From 752f592e5894b8116cdcfac592bef52add674f8f Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Tue, 13 Apr 2021 15:50:23 +0200 Subject: [PATCH 8/8] Fixed Python f-string MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Christian Mäder --- startup_scripts/200_vlan_groups.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/startup_scripts/200_vlan_groups.py b/startup_scripts/200_vlan_groups.py index eb6102351..2a4a33dec 100644 --- a/startup_scripts/200_vlan_groups.py +++ b/startup_scripts/200_vlan_groups.py @@ -28,7 +28,7 @@ if not ct: print( f"VLAN Group '{params['name']}': ContentType for " - + "app_label = '{app_label}' and model = '{model}' not found" + + f"app_label = '{app_label}' and model = '{model}' not found" ) continue params["scope_id"] = ct.model_class().objects.get(**query).id