netbox-docker/startup_scripts/120_locations.py
Ryan Merolle 8860d32f97
Cleanup & Reorg startup scripts (#691)
* Cleanup & Reorg startup scripts
2022-02-03 17:10:39 +01:00

24 lines
587 B
Python

import sys
from dcim.models import Location, Site
from startup_script_utils import load_yaml
rack_groups = load_yaml("/opt/netbox/initializers/locations.yml")
if rack_groups is None:
sys.exit()
required_assocs = {"site": (Site, "name")}
for params in rack_groups:
for assoc, details in required_assocs.items():
model, field = details
query = {field: params.pop(assoc)}
params[assoc] = model.objects.get(**query)
location, created = Location.objects.get_or_create(**params)
if created:
print("🎨 Created location", location.name)