2019-07-04 07:10:56 -05:00
|
|
|
import sys
|
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
from dcim.models import Device, Interface
|
2022-04-05 01:34:08 -05:00
|
|
|
from startup_script_utils import (
|
|
|
|
load_yaml,
|
|
|
|
pop_custom_fields,
|
|
|
|
set_custom_fields_values,
|
|
|
|
split_params,
|
|
|
|
)
|
2020-10-17 14:51:38 -05:00
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
interfaces = load_yaml("/opt/netbox/initializers/dcim_interfaces.yml")
|
2019-07-04 07:10:56 -05:00
|
|
|
|
2020-02-05 08:31:01 -06:00
|
|
|
if interfaces is None:
|
2021-02-08 04:59:33 -06:00
|
|
|
sys.exit()
|
2019-07-04 07:10:56 -05:00
|
|
|
|
2022-04-05 01:34:08 -05:00
|
|
|
match_params = ["device", "name"]
|
2021-02-08 04:59:33 -06:00
|
|
|
required_assocs = {"device": (Device, "name")}
|
2022-04-08 11:25:52 -05:00
|
|
|
related_assocs = {
|
|
|
|
"bridge": (Interface, "name"),
|
|
|
|
"lag": (Interface, "name"),
|
|
|
|
"parent": (Interface, "name"),
|
|
|
|
}
|
2019-07-04 07:10:56 -05:00
|
|
|
|
2020-02-05 08:31:01 -06:00
|
|
|
for params in interfaces:
|
2021-02-08 04:59:33 -06:00
|
|
|
custom_field_data = pop_custom_fields(params)
|
2019-07-04 07:10:56 -05:00
|
|
|
|
2022-04-08 11:25:52 -05:00
|
|
|
related_interfaces = {k: params.pop(k, None) for k in related_assocs}
|
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
for assoc, details in required_assocs.items():
|
|
|
|
model, field = details
|
|
|
|
query = {field: params.pop(assoc)}
|
2019-07-04 07:10:56 -05:00
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
params[assoc] = model.objects.get(**query)
|
2019-10-10 10:35:06 -05:00
|
|
|
|
2022-04-05 01:34:08 -05:00
|
|
|
matching_params, defaults = split_params(params, match_params)
|
|
|
|
interface, created = Interface.objects.get_or_create(**matching_params, defaults=defaults)
|
2019-07-04 07:10:56 -05:00
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
if created:
|
2022-04-08 11:25:52 -05:00
|
|
|
print(f"🧷 Created interface {interface} on {interface.device}")
|
2022-04-05 01:45:22 -05:00
|
|
|
|
|
|
|
set_custom_fields_values(interface, custom_field_data)
|
2022-04-08 11:25:52 -05:00
|
|
|
|
|
|
|
for related_field, related_value in related_interfaces.items():
|
|
|
|
if not related_value:
|
|
|
|
continue
|
|
|
|
|
|
|
|
r_model, r_field = related_assocs[related_field]
|
|
|
|
|
|
|
|
if related_field == "parent" and not interface.parent_id:
|
|
|
|
query = {r_field: related_value, "device": interface.device}
|
|
|
|
try:
|
|
|
|
related_obj = r_model.objects.get(**query)
|
|
|
|
except Interface.DoesNotExist:
|
|
|
|
print(f"⚠️ Could not find parent interface with: {query} for interface {interface}")
|
|
|
|
raise
|
|
|
|
|
|
|
|
interface.parent_id = related_obj.id
|
|
|
|
interface.save()
|
|
|
|
print(
|
|
|
|
f"🧷 Attached interface {interface} on {interface.device} "
|
|
|
|
f"to parent {related_obj}"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
query = {r_field: related_value, "device": interface.device, "type": related_field}
|
|
|
|
related_obj, rel_obj_created = r_model.objects.get_or_create(**query)
|
|
|
|
|
|
|
|
if rel_obj_created:
|
|
|
|
setattr(interface, f"{related_field}_id", related_obj.id)
|
|
|
|
interface.save()
|
|
|
|
print(f"🧷 Created {related_field} interface {interface} on {interface.device}")
|