From 45824ba4f31b1e77ff149a7d3ead84b6bf3a7117 Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Wed, 7 Apr 2021 09:57:59 +0200 Subject: [PATCH] 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 0de0504..8324c36 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 597db0c..3e26c41 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 @@ optional_assocs = { "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 597db0c..0000000 --- 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)