Merge pull request #730 from kr3ator/bugfix/cf_creation

Fix setting custom field data if custom field object is missing
This commit is contained in:
Tobias Genannt 2022-04-07 09:01:04 +02:00 committed by GitHub
commit a6eb4fef00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 74 additions and 45 deletions

View File

@ -23,6 +23,6 @@ for params in tenants:
tenant, created = Tenant.objects.get_or_create(**params)
if created:
set_custom_fields_values(tenant, custom_field_data)
print("👩‍💻 Created Tenant", tenant.name)
set_custom_fields_values(tenant, custom_field_data)

View File

@ -24,6 +24,6 @@ for params in sites:
site, created = Site.objects.get_or_create(**params)
if created:
set_custom_fields_values(site, custom_field_data)
print("📍 Created site", site.name)
set_custom_fields_values(site, custom_field_data)

View File

@ -36,6 +36,6 @@ for params in racks:
rack, created = Rack.objects.get_or_create(**params)
if created:
set_custom_fields_values(rack, custom_field_data)
print("🔳 Created rack", rack.site, rack.name)
set_custom_fields_values(rack, custom_field_data)

View File

@ -31,6 +31,6 @@ for params in power_panels:
power_panel, created = PowerPanel.objects.get_or_create(**params)
if created:
set_custom_fields_values(power_panel, custom_field_data)
print("⚡ Created Power Panel", power_panel.site, power_panel.name)
set_custom_fields_values(power_panel, custom_field_data)

View File

@ -31,6 +31,6 @@ for params in power_feeds:
power_feed, created = PowerFeed.objects.get_or_create(**params)
if created:
set_custom_fields_values(power_feed, custom_field_data)
print("⚡ Created Power Feed", power_feed.name)
set_custom_fields_values(power_feed, custom_field_data)

View File

@ -32,6 +32,6 @@ for params in device_types:
device_type, created = DeviceType.objects.get_or_create(**params)
if created:
set_custom_fields_values(device_type, custom_field_data)
print("🔡 Created device type", device_type.manufacturer, device_type.model)
set_custom_fields_values(device_type, custom_field_data)

View File

@ -47,6 +47,6 @@ for params in devices:
device, created = Device.objects.get_or_create(**params)
if created:
set_custom_fields_values(device, custom_field_data)
print("🖥️ Created device", device.name)
set_custom_fields_values(device, custom_field_data)

View File

@ -22,6 +22,6 @@ for params in interfaces:
interface, created = Interface.objects.get_or_create(**params)
if created:
set_custom_fields_values(interface, custom_field_data)
print("🧷 Created interface", interface.name, interface.device.name)
set_custom_fields_values(interface, custom_field_data)

View File

@ -24,6 +24,6 @@ for params in route_targets:
route_target, created = RouteTarget.objects.get_or_create(**params)
if created:
set_custom_fields_values(route_target, custom_field_data)
print("🎯 Created Route Target", route_target.name)
set_custom_fields_values(route_target, custom_field_data)

View File

@ -24,6 +24,6 @@ for params in vrfs:
vrf, created = VRF.objects.get_or_create(**params)
if created:
set_custom_fields_values(vrf, custom_field_data)
print("📦 Created VRF", vrf.name)
set_custom_fields_values(vrf, custom_field_data)

View File

@ -37,6 +37,6 @@ for params in aggregates:
aggregate, created = Aggregate.objects.get_or_create(**params)
if created:
set_custom_fields_values(aggregate, custom_field_data)
print("🗞️ Created Aggregate", aggregate.prefix)
set_custom_fields_values(aggregate, custom_field_data)

View File

@ -37,6 +37,6 @@ for params in clusters:
cluster, created = Cluster.objects.get_or_create(**params)
if created:
set_custom_fields_values(cluster, custom_field_data)
print("🗄️ Created cluster", cluster.name)
set_custom_fields_values(cluster, custom_field_data)

View File

@ -35,6 +35,6 @@ for params in vlan_groups:
vlan_group, created = VLANGroup.objects.get_or_create(**params)
if created:
set_custom_fields_values(vlan_group, custom_field_data)
print("🏘️ Created VLAN Group", vlan_group.name)
set_custom_fields_values(vlan_group, custom_field_data)

View File

@ -31,6 +31,6 @@ for params in vlans:
vlan, created = VLAN.objects.get_or_create(**params)
if created:
set_custom_fields_values(vlan, custom_field_data)
print("🏠 Created VLAN", vlan.name)
set_custom_fields_values(vlan, custom_field_data)

View File

@ -41,6 +41,6 @@ for params in virtual_machines:
virtual_machine, created = VirtualMachine.objects.get_or_create(**params)
if created:
set_custom_fields_values(virtual_machine, custom_field_data)
print("🖥️ Created virtual machine", virtual_machine.name)
set_custom_fields_values(virtual_machine, custom_field_data)

View File

@ -22,6 +22,6 @@ for params in interfaces:
interface, created = VMInterface.objects.get_or_create(**params)
if created:
set_custom_fields_values(interface, custom_field_data)
print("🧷 Created interface", interface.name, interface.virtual_machine.name)
set_custom_fields_values(interface, custom_field_data)

View File

@ -34,6 +34,6 @@ for params in prefixes:
prefix, created = Prefix.objects.get_or_create(**params)
if created:
set_custom_fields_values(prefix, custom_field_data)
print("📌 Created Prefix", prefix.prefix)
set_custom_fields_values(prefix, custom_field_data)

View File

@ -17,7 +17,7 @@ if ip_addresses is None:
optional_assocs = {
"tenant": (Tenant, "name"),
"vrf": (VRF, "name"),
"interface": (None, None),
"interface": (Interface, "name"),
}
vm_interface_ct = ContentType.objects.filter(
@ -58,6 +58,6 @@ for params in ip_addresses:
ip_address, created = IPAddress.objects.get_or_create(**params)
if created:
set_custom_fields_values(ip_address, custom_field_data)
print("🧬 Created IP Address", ip_address.address)
set_custom_fields_values(ip_address, custom_field_data)

View File

@ -14,6 +14,6 @@ for params in providers:
provider, created = Provider.objects.get_or_create(**params)
if created:
set_custom_fields_values(provider, custom_field_data)
print("📡 Created provider", provider.name)
set_custom_fields_values(provider, custom_field_data)

View File

@ -14,6 +14,6 @@ for params in circuit_types:
circuit_type, created = CircuitType.objects.get_or_create(**params)
if created:
set_custom_fields_values(circuit_type, custom_field_data)
print("⚡ Created Circuit Type", circuit_type.name)
set_custom_fields_values(circuit_type, custom_field_data)

View File

@ -32,6 +32,6 @@ for params in circuits:
circuit, created = Circuit.objects.get_or_create(**params)
if created:
set_custom_fields_values(circuit, custom_field_data)
print("⚡ Created Circuit", circuit.cid)
set_custom_fields_values(circuit, custom_field_data)

View File

@ -1,9 +1,38 @@
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist
from extras.models import CustomField
def set_custom_fields_values(entity, custom_field_data):
if not custom_field_data:
return
entity.custom_field_data = custom_field_data
return entity.save()
missing_cfs = []
save = False
for key, value in custom_field_data.items():
try:
cf = CustomField.objects.get(name=key)
except ObjectDoesNotExist:
missing_cfs.append(key)
else:
ct = ContentType.objects.get_for_model(entity)
if ct not in cf.content_types.all():
print(
f"⚠️ Custom field {key} is not enabled for {entity}'s model!"
"Please check the 'on_objects' for that custom field in custom_fields.yml"
)
elif key not in entity.custom_field_data:
entity.custom_field_data[key] = value
save = True
if missing_cfs:
raise Exception(
f"⚠️ Custom field(s) '{missing_cfs}' requested for {entity} but not found in Netbox!"
"Please chceck the custom_fields.yml"
)
if save:
entity.save()
def pop_custom_fields(params):