netbox-docker/startup_scripts/210_dcim_interfaces.py

35 lines
944 B
Python
Raw Normal View History

import sys
from dcim.models import Device, Interface
2022-04-05 08:34:08 +02:00
from startup_script_utils import (
load_yaml,
pop_custom_fields,
set_custom_fields_values,
split_params,
)
interfaces = load_yaml("/opt/netbox/initializers/dcim_interfaces.yml")
if interfaces is None:
sys.exit()
2022-04-05 08:34:08 +02:00
match_params = ["device", "name"]
required_assocs = {"device": (Device, "name")}
for params in interfaces:
custom_field_data = pop_custom_fields(params)
for assoc, details in required_assocs.items():
model, field = details
query = {field: params.pop(assoc)}
params[assoc] = model.objects.get(**query)
2019-10-10 17:35:06 +02:00
2022-04-05 08:34:08 +02:00
matching_params, defaults = split_params(params, match_params)
interface, created = Interface.objects.get_or_create(**matching_params, defaults=defaults)
if created:
print("🧷 Created interface", interface.name, interface.device.name)
set_custom_fields_values(interface, custom_field_data)