netbox-docker/startup_scripts/120_locations.py

26 lines
736 B
Python
Raw Normal View History

import sys
from dcim.models import Location, Site
2022-04-05 01:34:08 -05:00
from startup_script_utils import load_yaml, split_params
rack_groups = load_yaml("/opt/netbox/initializers/locations.yml")
if rack_groups is None:
sys.exit()
2022-04-05 01:34:08 -05:00
match_params = ["name", "slug", "site"]
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)
2022-04-05 01:34:08 -05:00
matching_params, defaults = split_params(params, match_params)
location, created = Location.objects.get_or_create(**matching_params, defaults=defaults)
if created:
print("🎨 Created location", location.name)