netbox-docker/startup_scripts/075_rack_groups.py
Tobias Genannt 50ade7bce1 Add function to load YAML files
This commit starts to remove some code redundancy from the startup
scripts for easier maintenance.
2020-02-10 07:47:17 +01:00

26 lines
578 B
Python

from dcim.models import Site,RackGroup
from startup_script_utils import load_yaml
import sys
rack_groups = load_yaml('/opt/netbox/initializers/rack_groups.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)
rack_group, created = RackGroup.objects.get_or_create(**params)
if created:
print("🎨 Created rack group", rack_group.name)