netbox-docker/startup_scripts/320_services.py

31 lines
747 B
Python
Raw Normal View History

import sys
2020-12-30 18:10:46 -06:00
from dcim.models import Device
from ipam.models import Service
2020-12-30 18:10:46 -06:00
from startup_script_utils import load_yaml
from virtualization.models import VirtualMachine
2020-12-30 18:10:46 -06:00
services = load_yaml("/opt/netbox/initializers/services.yml")
2020-12-30 18:10:46 -06:00
if services is None:
sys.exit()
2020-12-30 18:10:46 -06:00
optional_assocs = {
"device": (Device, "name"),
"virtual_machine": (VirtualMachine, "name"),
2020-12-30 18:10:46 -06:00
}
for params in services:
for assoc, details in optional_assocs.items():
if assoc in params:
model, field = details
query = {field: params.pop(assoc)}
2020-12-30 18:10:46 -06:00
params[assoc] = model.objects.get(**query)
2020-12-30 18:10:46 -06:00
service, created = Service.objects.get_or_create(**params)
2020-12-30 18:10:46 -06:00
if created:
print("🧰 Created Service", service.name)