Merge pull request #744 from kr3ator/feature/interfaces_improvements
Add support for bridge, lag, parent in DCIM Interfaces startup script
This commit is contained in:
commit
4c21344e8b
|
@ -8,6 +8,15 @@
|
||||||
##
|
##
|
||||||
## Examples:
|
## Examples:
|
||||||
|
|
||||||
|
# - device: server01
|
||||||
|
# name: ath0
|
||||||
|
# type: 1000base-t
|
||||||
|
# lag: ae0
|
||||||
|
# bridge: br0
|
||||||
|
# - device: server01
|
||||||
|
# name: ath1
|
||||||
|
# type: 1000base-t
|
||||||
|
# parent: ath0
|
||||||
# - device: server01
|
# - device: server01
|
||||||
# enabled: true
|
# enabled: true
|
||||||
# type: virtual
|
# type: virtual
|
||||||
|
|
|
@ -15,10 +15,17 @@ if interfaces is None:
|
||||||
|
|
||||||
match_params = ["device", "name"]
|
match_params = ["device", "name"]
|
||||||
required_assocs = {"device": (Device, "name")}
|
required_assocs = {"device": (Device, "name")}
|
||||||
|
related_assocs = {
|
||||||
|
"bridge": (Interface, "name"),
|
||||||
|
"lag": (Interface, "name"),
|
||||||
|
"parent": (Interface, "name"),
|
||||||
|
}
|
||||||
|
|
||||||
for params in interfaces:
|
for params in interfaces:
|
||||||
custom_field_data = pop_custom_fields(params)
|
custom_field_data = pop_custom_fields(params)
|
||||||
|
|
||||||
|
related_interfaces = {k: params.pop(k, None) for k in related_assocs}
|
||||||
|
|
||||||
for assoc, details in required_assocs.items():
|
for assoc, details in required_assocs.items():
|
||||||
model, field = details
|
model, field = details
|
||||||
query = {field: params.pop(assoc)}
|
query = {field: params.pop(assoc)}
|
||||||
|
@ -29,6 +36,35 @@ for params in interfaces:
|
||||||
interface, created = Interface.objects.get_or_create(**matching_params, defaults=defaults)
|
interface, created = Interface.objects.get_or_create(**matching_params, defaults=defaults)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
print("🧷 Created interface", interface.name, interface.device.name)
|
print(f"🧷 Created interface {interface} on {interface.device}")
|
||||||
|
|
||||||
set_custom_fields_values(interface, custom_field_data)
|
set_custom_fields_values(interface, custom_field_data)
|
||||||
|
|
||||||
|
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}")
|
||||||
|
|
Loading…
Reference in New Issue