mirror of
https://github.com/netbox-community/netbox-docker.git
synced 2024-12-02 04:43:33 +01:00
30 lines
709 B
Python
30 lines
709 B
Python
from ipam.models import Service
|
|
from dcim.models import Device
|
|
from virtualization.models import VirtualMachine
|
|
from startup_script_utils import load_yaml
|
|
import sys
|
|
|
|
services = load_yaml('/opt/netbox/initializers/services.yml')
|
|
|
|
if services is None:
|
|
sys.exit()
|
|
|
|
optional_assocs = {
|
|
'device': (Device, 'name'),
|
|
'virtual_machine': (VirtualMachine, 'name')
|
|
}
|
|
|
|
for params in services:
|
|
|
|
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)
|
|
|
|
service, created = Service.objects.get_or_create(**params)
|
|
|
|
if created:
|
|
print("🧰 Created Service", service.name)
|