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:
commit
a6eb4fef00
@ -23,6 +23,6 @@ for params in tenants:
|
|||||||
tenant, created = Tenant.objects.get_or_create(**params)
|
tenant, created = Tenant.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(tenant, custom_field_data)
|
|
||||||
|
|
||||||
print("👩💻 Created Tenant", tenant.name)
|
print("👩💻 Created Tenant", tenant.name)
|
||||||
|
|
||||||
|
set_custom_fields_values(tenant, custom_field_data)
|
||||||
|
@ -24,6 +24,6 @@ for params in sites:
|
|||||||
site, created = Site.objects.get_or_create(**params)
|
site, created = Site.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(site, custom_field_data)
|
|
||||||
|
|
||||||
print("📍 Created site", site.name)
|
print("📍 Created site", site.name)
|
||||||
|
|
||||||
|
set_custom_fields_values(site, custom_field_data)
|
||||||
|
@ -36,6 +36,6 @@ for params in racks:
|
|||||||
rack, created = Rack.objects.get_or_create(**params)
|
rack, created = Rack.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(rack, custom_field_data)
|
|
||||||
|
|
||||||
print("🔳 Created rack", rack.site, rack.name)
|
print("🔳 Created rack", rack.site, rack.name)
|
||||||
|
|
||||||
|
set_custom_fields_values(rack, custom_field_data)
|
||||||
|
@ -31,6 +31,6 @@ for params in power_panels:
|
|||||||
power_panel, created = PowerPanel.objects.get_or_create(**params)
|
power_panel, created = PowerPanel.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(power_panel, custom_field_data)
|
|
||||||
|
|
||||||
print("⚡ Created Power Panel", power_panel.site, power_panel.name)
|
print("⚡ Created Power Panel", power_panel.site, power_panel.name)
|
||||||
|
|
||||||
|
set_custom_fields_values(power_panel, custom_field_data)
|
||||||
|
@ -31,6 +31,6 @@ for params in power_feeds:
|
|||||||
power_feed, created = PowerFeed.objects.get_or_create(**params)
|
power_feed, created = PowerFeed.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(power_feed, custom_field_data)
|
|
||||||
|
|
||||||
print("⚡ Created Power Feed", power_feed.name)
|
print("⚡ Created Power Feed", power_feed.name)
|
||||||
|
|
||||||
|
set_custom_fields_values(power_feed, custom_field_data)
|
||||||
|
@ -32,6 +32,6 @@ for params in device_types:
|
|||||||
device_type, created = DeviceType.objects.get_or_create(**params)
|
device_type, created = DeviceType.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(device_type, custom_field_data)
|
|
||||||
|
|
||||||
print("🔡 Created device type", device_type.manufacturer, device_type.model)
|
print("🔡 Created device type", device_type.manufacturer, device_type.model)
|
||||||
|
|
||||||
|
set_custom_fields_values(device_type, custom_field_data)
|
||||||
|
@ -47,6 +47,6 @@ for params in devices:
|
|||||||
device, created = Device.objects.get_or_create(**params)
|
device, created = Device.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(device, custom_field_data)
|
|
||||||
|
|
||||||
print("🖥️ Created device", device.name)
|
print("🖥️ Created device", device.name)
|
||||||
|
|
||||||
|
set_custom_fields_values(device, custom_field_data)
|
||||||
|
@ -22,6 +22,6 @@ for params in interfaces:
|
|||||||
interface, created = Interface.objects.get_or_create(**params)
|
interface, created = Interface.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(interface, custom_field_data)
|
|
||||||
|
|
||||||
print("🧷 Created interface", interface.name, interface.device.name)
|
print("🧷 Created interface", interface.name, interface.device.name)
|
||||||
|
|
||||||
|
set_custom_fields_values(interface, custom_field_data)
|
||||||
|
@ -24,6 +24,6 @@ for params in route_targets:
|
|||||||
route_target, created = RouteTarget.objects.get_or_create(**params)
|
route_target, created = RouteTarget.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(route_target, custom_field_data)
|
|
||||||
|
|
||||||
print("🎯 Created Route Target", route_target.name)
|
print("🎯 Created Route Target", route_target.name)
|
||||||
|
|
||||||
|
set_custom_fields_values(route_target, custom_field_data)
|
||||||
|
@ -24,6 +24,6 @@ for params in vrfs:
|
|||||||
vrf, created = VRF.objects.get_or_create(**params)
|
vrf, created = VRF.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(vrf, custom_field_data)
|
|
||||||
|
|
||||||
print("📦 Created VRF", vrf.name)
|
print("📦 Created VRF", vrf.name)
|
||||||
|
|
||||||
|
set_custom_fields_values(vrf, custom_field_data)
|
||||||
|
@ -37,6 +37,6 @@ for params in aggregates:
|
|||||||
aggregate, created = Aggregate.objects.get_or_create(**params)
|
aggregate, created = Aggregate.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(aggregate, custom_field_data)
|
|
||||||
|
|
||||||
print("🗞️ Created Aggregate", aggregate.prefix)
|
print("🗞️ Created Aggregate", aggregate.prefix)
|
||||||
|
|
||||||
|
set_custom_fields_values(aggregate, custom_field_data)
|
||||||
|
@ -37,6 +37,6 @@ for params in clusters:
|
|||||||
cluster, created = Cluster.objects.get_or_create(**params)
|
cluster, created = Cluster.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(cluster, custom_field_data)
|
|
||||||
|
|
||||||
print("🗄️ Created cluster", cluster.name)
|
print("🗄️ Created cluster", cluster.name)
|
||||||
|
|
||||||
|
set_custom_fields_values(cluster, custom_field_data)
|
||||||
|
@ -35,6 +35,6 @@ for params in vlan_groups:
|
|||||||
vlan_group, created = VLANGroup.objects.get_or_create(**params)
|
vlan_group, created = VLANGroup.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(vlan_group, custom_field_data)
|
|
||||||
|
|
||||||
print("🏘️ Created VLAN Group", vlan_group.name)
|
print("🏘️ Created VLAN Group", vlan_group.name)
|
||||||
|
|
||||||
|
set_custom_fields_values(vlan_group, custom_field_data)
|
||||||
|
@ -31,6 +31,6 @@ for params in vlans:
|
|||||||
vlan, created = VLAN.objects.get_or_create(**params)
|
vlan, created = VLAN.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(vlan, custom_field_data)
|
|
||||||
|
|
||||||
print("🏠 Created VLAN", vlan.name)
|
print("🏠 Created VLAN", vlan.name)
|
||||||
|
|
||||||
|
set_custom_fields_values(vlan, custom_field_data)
|
||||||
|
@ -41,6 +41,6 @@ for params in virtual_machines:
|
|||||||
virtual_machine, created = VirtualMachine.objects.get_or_create(**params)
|
virtual_machine, created = VirtualMachine.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(virtual_machine, custom_field_data)
|
|
||||||
|
|
||||||
print("🖥️ Created virtual machine", virtual_machine.name)
|
print("🖥️ Created virtual machine", virtual_machine.name)
|
||||||
|
|
||||||
|
set_custom_fields_values(virtual_machine, custom_field_data)
|
||||||
|
@ -22,6 +22,6 @@ for params in interfaces:
|
|||||||
interface, created = VMInterface.objects.get_or_create(**params)
|
interface, created = VMInterface.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(interface, custom_field_data)
|
|
||||||
|
|
||||||
print("🧷 Created interface", interface.name, interface.virtual_machine.name)
|
print("🧷 Created interface", interface.name, interface.virtual_machine.name)
|
||||||
|
|
||||||
|
set_custom_fields_values(interface, custom_field_data)
|
||||||
|
@ -34,6 +34,6 @@ for params in prefixes:
|
|||||||
prefix, created = Prefix.objects.get_or_create(**params)
|
prefix, created = Prefix.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(prefix, custom_field_data)
|
|
||||||
|
|
||||||
print("📌 Created Prefix", prefix.prefix)
|
print("📌 Created Prefix", prefix.prefix)
|
||||||
|
|
||||||
|
set_custom_fields_values(prefix, custom_field_data)
|
||||||
|
@ -17,7 +17,7 @@ if ip_addresses is None:
|
|||||||
optional_assocs = {
|
optional_assocs = {
|
||||||
"tenant": (Tenant, "name"),
|
"tenant": (Tenant, "name"),
|
||||||
"vrf": (VRF, "name"),
|
"vrf": (VRF, "name"),
|
||||||
"interface": (None, None),
|
"interface": (Interface, "name"),
|
||||||
}
|
}
|
||||||
|
|
||||||
vm_interface_ct = ContentType.objects.filter(
|
vm_interface_ct = ContentType.objects.filter(
|
||||||
@ -58,6 +58,6 @@ for params in ip_addresses:
|
|||||||
ip_address, created = IPAddress.objects.get_or_create(**params)
|
ip_address, created = IPAddress.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(ip_address, custom_field_data)
|
|
||||||
|
|
||||||
print("🧬 Created IP Address", ip_address.address)
|
print("🧬 Created IP Address", ip_address.address)
|
||||||
|
|
||||||
|
set_custom_fields_values(ip_address, custom_field_data)
|
||||||
|
@ -14,6 +14,6 @@ for params in providers:
|
|||||||
provider, created = Provider.objects.get_or_create(**params)
|
provider, created = Provider.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(provider, custom_field_data)
|
|
||||||
|
|
||||||
print("📡 Created provider", provider.name)
|
print("📡 Created provider", provider.name)
|
||||||
|
|
||||||
|
set_custom_fields_values(provider, custom_field_data)
|
||||||
|
@ -14,6 +14,6 @@ for params in circuit_types:
|
|||||||
circuit_type, created = CircuitType.objects.get_or_create(**params)
|
circuit_type, created = CircuitType.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(circuit_type, custom_field_data)
|
|
||||||
|
|
||||||
print("⚡ Created Circuit Type", circuit_type.name)
|
print("⚡ Created Circuit Type", circuit_type.name)
|
||||||
|
|
||||||
|
set_custom_fields_values(circuit_type, custom_field_data)
|
||||||
|
@ -32,6 +32,6 @@ for params in circuits:
|
|||||||
circuit, created = Circuit.objects.get_or_create(**params)
|
circuit, created = Circuit.objects.get_or_create(**params)
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
set_custom_fields_values(circuit, custom_field_data)
|
|
||||||
|
|
||||||
print("⚡ Created Circuit", circuit.cid)
|
print("⚡ Created Circuit", circuit.cid)
|
||||||
|
|
||||||
|
set_custom_fields_values(circuit, custom_field_data)
|
||||||
|
@ -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):
|
def set_custom_fields_values(entity, custom_field_data):
|
||||||
if not custom_field_data:
|
if not custom_field_data:
|
||||||
return
|
return
|
||||||
|
|
||||||
entity.custom_field_data = custom_field_data
|
missing_cfs = []
|
||||||
return entity.save()
|
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):
|
def pop_custom_fields(params):
|
||||||
|
Loading…
Reference in New Issue
Block a user