2021-02-08 04:59:33 -06:00
|
|
|
import sys
|
|
|
|
|
2020-12-30 18:10:46 -06:00
|
|
|
from dcim.models import Device
|
2021-02-08 04:59:33 -06:00
|
|
|
from ipam.models import Service
|
2022-04-05 01:34:08 -05:00
|
|
|
from startup_script_utils import load_yaml, split_params
|
2021-02-08 04:59:33 -06:00
|
|
|
from virtualization.models import VirtualMachine
|
2020-12-30 18:10:46 -06:00
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
services = load_yaml("/opt/netbox/initializers/services.yml")
|
2020-12-30 18:10:46 -06:00
|
|
|
|
|
|
|
if services is None:
|
2021-02-08 04:59:33 -06:00
|
|
|
sys.exit()
|
2020-12-30 18:10:46 -06:00
|
|
|
|
2022-04-05 01:34:08 -05:00
|
|
|
match_params = ["name", "device", "virtual_machine"]
|
2020-12-30 18:10:46 -06:00
|
|
|
optional_assocs = {
|
2021-02-08 04:59:33 -06:00
|
|
|
"device": (Device, "name"),
|
|
|
|
"virtual_machine": (VirtualMachine, "name"),
|
2020-12-30 18:10:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for params in services:
|
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
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
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
params[assoc] = model.objects.get(**query)
|
2020-12-30 18:10:46 -06:00
|
|
|
|
2022-04-05 01:34:08 -05:00
|
|
|
matching_params, defaults = split_params(params, match_params)
|
2021-02-08 04:59:33 -06:00
|
|
|
service, created = Service.objects.get_or_create(**params)
|
2020-12-30 18:10:46 -06:00
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
if created:
|
|
|
|
print("🧰 Created Service", service.name)
|