diff --git a/startup_scripts/080_tenants.py b/startup_scripts/080_tenants.py index 7b1a629..98e24a6 100644 --- a/startup_scripts/080_tenants.py +++ b/startup_scripts/080_tenants.py @@ -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) diff --git a/startup_scripts/110_sites.py b/startup_scripts/110_sites.py index f785139..736a61c 100644 --- a/startup_scripts/110_sites.py +++ b/startup_scripts/110_sites.py @@ -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) diff --git a/startup_scripts/140_racks.py b/startup_scripts/140_racks.py index b2cfc80..a49ea01 100644 --- a/startup_scripts/140_racks.py +++ b/startup_scripts/140_racks.py @@ -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) diff --git a/startup_scripts/150_power_panels.py b/startup_scripts/150_power_panels.py index 8542435..e0bceca 100644 --- a/startup_scripts/150_power_panels.py +++ b/startup_scripts/150_power_panels.py @@ -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) diff --git a/startup_scripts/160_power_feeds.py b/startup_scripts/160_power_feeds.py index f5aa5b5..7ef55b0 100644 --- a/startup_scripts/160_power_feeds.py +++ b/startup_scripts/160_power_feeds.py @@ -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) diff --git a/startup_scripts/190_device_types.py b/startup_scripts/190_device_types.py index 0d3050b..c43f632 100644 --- a/startup_scripts/190_device_types.py +++ b/startup_scripts/190_device_types.py @@ -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) diff --git a/startup_scripts/200_devices.py b/startup_scripts/200_devices.py index 423b7c9..ae92926 100644 --- a/startup_scripts/200_devices.py +++ b/startup_scripts/200_devices.py @@ -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) diff --git a/startup_scripts/210_dcim_interfaces.py b/startup_scripts/210_dcim_interfaces.py index a802628..880db91 100644 --- a/startup_scripts/210_dcim_interfaces.py +++ b/startup_scripts/210_dcim_interfaces.py @@ -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) diff --git a/startup_scripts/230_route_targets.py b/startup_scripts/230_route_targets.py index e1c8221..cb6721b 100644 --- a/startup_scripts/230_route_targets.py +++ b/startup_scripts/230_route_targets.py @@ -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) diff --git a/startup_scripts/240_vrfs.py b/startup_scripts/240_vrfs.py index a67c8c8..4cf9f16 100644 --- a/startup_scripts/240_vrfs.py +++ b/startup_scripts/240_vrfs.py @@ -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) diff --git a/startup_scripts/270_aggregates.py b/startup_scripts/270_aggregates.py index c638e6f..1cc7b5e 100644 --- a/startup_scripts/270_aggregates.py +++ b/startup_scripts/270_aggregates.py @@ -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) diff --git a/startup_scripts/310_clusters.py b/startup_scripts/310_clusters.py index 2748f20..e492351 100644 --- a/startup_scripts/310_clusters.py +++ b/startup_scripts/310_clusters.py @@ -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) diff --git a/startup_scripts/320_vlan_groups.py b/startup_scripts/320_vlan_groups.py index 2a4a33d..7b5fe6d 100644 --- a/startup_scripts/320_vlan_groups.py +++ b/startup_scripts/320_vlan_groups.py @@ -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) diff --git a/startup_scripts/330_vlans.py b/startup_scripts/330_vlans.py index e8ebb94..8a6b0c8 100644 --- a/startup_scripts/330_vlans.py +++ b/startup_scripts/330_vlans.py @@ -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) diff --git a/startup_scripts/340_virtual_machines.py b/startup_scripts/340_virtual_machines.py index 2e3f428..94d4fef 100644 --- a/startup_scripts/340_virtual_machines.py +++ b/startup_scripts/340_virtual_machines.py @@ -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) diff --git a/startup_scripts/350_virtualization_interfaces.py b/startup_scripts/350_virtualization_interfaces.py index 6ee6347..0ae5c5c 100644 --- a/startup_scripts/350_virtualization_interfaces.py +++ b/startup_scripts/350_virtualization_interfaces.py @@ -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) diff --git a/startup_scripts/360_prefixes.py b/startup_scripts/360_prefixes.py index 4e2b0d0..c4cb936 100644 --- a/startup_scripts/360_prefixes.py +++ b/startup_scripts/360_prefixes.py @@ -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) diff --git a/startup_scripts/370_ip_addresses.py b/startup_scripts/370_ip_addresses.py index 7f166f0..afe7612 100644 --- a/startup_scripts/370_ip_addresses.py +++ b/startup_scripts/370_ip_addresses.py @@ -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) diff --git a/startup_scripts/420_providers.py b/startup_scripts/420_providers.py index 5c4330a..1c652eb 100644 --- a/startup_scripts/420_providers.py +++ b/startup_scripts/420_providers.py @@ -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) diff --git a/startup_scripts/440_circuit_types.py b/startup_scripts/440_circuit_types.py index 071793c..fa49681 100644 --- a/startup_scripts/440_circuit_types.py +++ b/startup_scripts/440_circuit_types.py @@ -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) diff --git a/startup_scripts/450_circuits.py b/startup_scripts/450_circuits.py index f82d3b7..c182203 100644 --- a/startup_scripts/450_circuits.py +++ b/startup_scripts/450_circuits.py @@ -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) diff --git a/startup_scripts/startup_script_utils/custom_fields.py b/startup_scripts/startup_script_utils/custom_fields.py index c000970..74e8a25 100644 --- a/startup_scripts/startup_script_utils/custom_fields.py +++ b/startup_scripts/startup_script_utils/custom_fields.py @@ -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):