Update Custom Field logic for Netbox v2.10.x

This commit is contained in:
Christian Mäder 2020-10-17 21:51:38 +02:00 committed by Christian Mäder
parent 036f94a450
commit 5c9bea8b50
24 changed files with 168 additions and 268 deletions

View file

@ -1,9 +1,9 @@
from dcim.models import Interface, Device
from extras.models import CustomField, CustomFieldValue
from startup_script_utils import load_yaml
import sys
interfaces= load_yaml('/opt/netbox/initializers/dcim_interfaces.yml')
from dcim.models import Interface, Device
from startup_script_utils import *
interfaces = load_yaml('/opt/netbox/initializers/dcim_interfaces.yml')
if interfaces is None:
sys.exit()
@ -13,7 +13,7 @@ required_assocs = {
}
for params in interfaces:
custom_fields = params.pop('custom_fields', None)
custom_field_data = pop_custom_fields(params)
for assoc, details in required_assocs.items():
model, field = details
@ -24,15 +24,6 @@ for params in interfaces:
interface, created = Interface.objects.get_or_create(**params)
if created:
if custom_fields is not None:
for cf_name, cf_value in custom_fields.items():
custom_field = CustomField.objects.get(name=cf_name)
custom_field_value = CustomFieldValue.objects.create(
field=custom_field,
obj=interface,
value=cf_value
)
interface.custom_field_values.add(custom_field_value)
set_custom_fields_values(interface, custom_field_data)
print("🧷 Created interface", interface.name, interface.device.name)