From 8830bba8f32052da8a2bf01afca273fb000e2f7f Mon Sep 17 00:00:00 2001 From: kr3ator <48438188+kr3ator@users.noreply.github.com> Date: Tue, 5 Apr 2022 08:45:22 +0200 Subject: [PATCH] Fix setting CF data if CF object is missing --- startup_scripts/080_tenants.py | 4 +-- startup_scripts/110_sites.py | 4 +-- startup_scripts/140_racks.py | 4 +-- startup_scripts/150_power_panels.py | 4 +-- startup_scripts/160_power_feeds.py | 4 +-- startup_scripts/190_device_types.py | 4 +-- startup_scripts/200_devices.py | 4 +-- startup_scripts/210_dcim_interfaces.py | 4 +-- startup_scripts/230_route_targets.py | 4 +-- startup_scripts/240_vrfs.py | 4 +-- startup_scripts/270_aggregates.py | 4 +-- startup_scripts/310_clusters.py | 4 +-- startup_scripts/320_vlan_groups.py | 4 +-- startup_scripts/330_vlans.py | 4 +-- startup_scripts/340_virtual_machines.py | 4 +-- .../350_virtualization_interfaces.py | 4 +-- startup_scripts/360_prefixes.py | 4 +-- startup_scripts/370_ip_addresses.py | 4 +-- startup_scripts/420_providers.py | 4 +-- startup_scripts/440_circuit_types.py | 4 +-- startup_scripts/450_circuits.py | 4 +-- .../startup_script_utils/custom_fields.py | 30 +++++++++++++++++-- 22 files changed, 70 insertions(+), 44 deletions(-) diff --git a/startup_scripts/080_tenants.py b/startup_scripts/080_tenants.py index 7b1a629f0..98e24a615 100644 --- a/startup_scripts/080_tenants.py +++ b/startup_scripts/080_tenants.py @@ -23,6 +23,6 @@ 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) diff --git a/startup_scripts/110_sites.py b/startup_scripts/110_sites.py index f7851391a..736a61c7b 100644 --- a/startup_scripts/110_sites.py +++ b/startup_scripts/110_sites.py @@ -24,6 +24,6 @@ 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) diff --git a/startup_scripts/140_racks.py b/startup_scripts/140_racks.py index b2cfc801c..a49ea01ae 100644 --- a/startup_scripts/140_racks.py +++ b/startup_scripts/140_racks.py @@ -36,6 +36,6 @@ 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) diff --git a/startup_scripts/150_power_panels.py b/startup_scripts/150_power_panels.py index 8542435bc..e0bcecac1 100644 --- a/startup_scripts/150_power_panels.py +++ b/startup_scripts/150_power_panels.py @@ -31,6 +31,6 @@ 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) diff --git a/startup_scripts/160_power_feeds.py b/startup_scripts/160_power_feeds.py index f5aa5b570..7ef55b0e2 100644 --- a/startup_scripts/160_power_feeds.py +++ b/startup_scripts/160_power_feeds.py @@ -31,6 +31,6 @@ 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) diff --git a/startup_scripts/190_device_types.py b/startup_scripts/190_device_types.py index 0d3050b45..c43f632e9 100644 --- a/startup_scripts/190_device_types.py +++ b/startup_scripts/190_device_types.py @@ -32,6 +32,6 @@ 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) diff --git a/startup_scripts/200_devices.py b/startup_scripts/200_devices.py index 423b7c9de..ae92926bf 100644 --- a/startup_scripts/200_devices.py +++ b/startup_scripts/200_devices.py @@ -47,6 +47,6 @@ 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) diff --git a/startup_scripts/210_dcim_interfaces.py b/startup_scripts/210_dcim_interfaces.py index a8026282f..880db912e 100644 --- a/startup_scripts/210_dcim_interfaces.py +++ b/startup_scripts/210_dcim_interfaces.py @@ -22,6 +22,6 @@ 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) diff --git a/startup_scripts/230_route_targets.py b/startup_scripts/230_route_targets.py index e1c82217e..cb6721b77 100644 --- a/startup_scripts/230_route_targets.py +++ b/startup_scripts/230_route_targets.py @@ -24,6 +24,6 @@ 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) diff --git a/startup_scripts/240_vrfs.py b/startup_scripts/240_vrfs.py index a67c8c8cb..4cf9f163a 100644 --- a/startup_scripts/240_vrfs.py +++ b/startup_scripts/240_vrfs.py @@ -24,6 +24,6 @@ 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) diff --git a/startup_scripts/270_aggregates.py b/startup_scripts/270_aggregates.py index c638e6f81..1cc7b5e86 100644 --- a/startup_scripts/270_aggregates.py +++ b/startup_scripts/270_aggregates.py @@ -37,6 +37,6 @@ 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) diff --git a/startup_scripts/310_clusters.py b/startup_scripts/310_clusters.py index 2748f2038..e4923518a 100644 --- a/startup_scripts/310_clusters.py +++ b/startup_scripts/310_clusters.py @@ -37,6 +37,6 @@ 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) diff --git a/startup_scripts/320_vlan_groups.py b/startup_scripts/320_vlan_groups.py index 2a4a33dec..7b5fe6db4 100644 --- a/startup_scripts/320_vlan_groups.py +++ b/startup_scripts/320_vlan_groups.py @@ -35,6 +35,6 @@ 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) diff --git a/startup_scripts/330_vlans.py b/startup_scripts/330_vlans.py index e8ebb94f1..8a6b0c84e 100644 --- a/startup_scripts/330_vlans.py +++ b/startup_scripts/330_vlans.py @@ -31,6 +31,6 @@ 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) diff --git a/startup_scripts/340_virtual_machines.py b/startup_scripts/340_virtual_machines.py index 2e3f42885..94d4fefab 100644 --- a/startup_scripts/340_virtual_machines.py +++ b/startup_scripts/340_virtual_machines.py @@ -41,6 +41,6 @@ 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) diff --git a/startup_scripts/350_virtualization_interfaces.py b/startup_scripts/350_virtualization_interfaces.py index 6ee63474c..0ae5c5c8a 100644 --- a/startup_scripts/350_virtualization_interfaces.py +++ b/startup_scripts/350_virtualization_interfaces.py @@ -22,6 +22,6 @@ 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) diff --git a/startup_scripts/360_prefixes.py b/startup_scripts/360_prefixes.py index 4e2b0d007..c4cb93659 100644 --- a/startup_scripts/360_prefixes.py +++ b/startup_scripts/360_prefixes.py @@ -34,6 +34,6 @@ 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) diff --git a/startup_scripts/370_ip_addresses.py b/startup_scripts/370_ip_addresses.py index 7f166f0bb..76a716908 100644 --- a/startup_scripts/370_ip_addresses.py +++ b/startup_scripts/370_ip_addresses.py @@ -58,6 +58,6 @@ 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) diff --git a/startup_scripts/420_providers.py b/startup_scripts/420_providers.py index 5c4330ad3..1c652eb9e 100644 --- a/startup_scripts/420_providers.py +++ b/startup_scripts/420_providers.py @@ -14,6 +14,6 @@ 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) diff --git a/startup_scripts/440_circuit_types.py b/startup_scripts/440_circuit_types.py index 071793c6f..fa49681c3 100644 --- a/startup_scripts/440_circuit_types.py +++ b/startup_scripts/440_circuit_types.py @@ -14,6 +14,6 @@ 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) diff --git a/startup_scripts/450_circuits.py b/startup_scripts/450_circuits.py index f82d3b71d..c1822039e 100644 --- a/startup_scripts/450_circuits.py +++ b/startup_scripts/450_circuits.py @@ -32,6 +32,6 @@ 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) diff --git a/startup_scripts/startup_script_utils/custom_fields.py b/startup_scripts/startup_script_utils/custom_fields.py index c00097079..2e6c4f8e2 100644 --- a/startup_scripts/startup_script_utils/custom_fields.py +++ b/startup_scripts/startup_script_utils/custom_fields.py @@ -1,9 +1,35 @@ +from django.contrib.contenttypes.models import ContentType +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: + missing_cfs.append(key) + else: + ct = ContentType.get_for_model(entity) + if not ct 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):