From 7951f67538a38ee15721af808719558c4e63167c Mon Sep 17 00:00:00 2001 From: Chris Carter Date: Sat, 13 Jan 2024 08:29:00 +1100 Subject: [PATCH] Support Tagging Objects Support for tagging is added to all types that are initialized after tags. If a type does not include tags support in netbox, an exception will be raised alerting the user to the fact. Tags must be pre-existing. Example: ```yaml - address: 10.1.1.1 status: active tags: - MyTag ``` --- .../initializers/__init__.py | 23 ++++++++++++++++++- .../initializers/aggregates.py | 2 ++ src/netbox_initializers/initializers/asns.py | 3 +++ .../initializers/cables.py | 3 +++ .../initializers/circuit_types.py | 2 ++ .../initializers/circuits.py | 2 ++ .../initializers/cluster_groups.py | 2 ++ .../initializers/cluster_types.py | 2 ++ .../initializers/clusters.py | 2 ++ .../initializers/config_templates.py | 2 ++ .../initializers/contact_groups.py | 2 ++ .../initializers/contact_roles.py | 2 ++ .../initializers/contacts.py | 2 ++ .../initializers/device_roles.py | 3 +++ .../initializers/device_types.py | 2 ++ .../initializers/devices.py | 2 ++ .../initializers/interfaces.py | 2 ++ .../initializers/ip_addresses.py | 2 ++ .../initializers/locations.py | 3 +++ .../initializers/manufacturers.py | 3 +++ .../initializers/platforms.py | 3 +++ .../initializers/power_feeds.py | 2 ++ .../initializers/power_panels.py | 2 ++ .../initializers/prefix_vlan_roles.py | 3 +++ .../initializers/prefixes.py | 2 ++ .../initializers/providers.py | 2 ++ .../initializers/rack_roles.py | 3 +++ src/netbox_initializers/initializers/racks.py | 2 ++ .../initializers/regions.py | 3 +++ src/netbox_initializers/initializers/rirs.py | 3 +++ .../initializers/route_targets.py | 2 ++ .../initializers/service_templates.py | 3 +++ .../initializers/services.py | 3 +++ src/netbox_initializers/initializers/sites.py | 2 ++ .../initializers/tenant_groups.py | 3 +++ .../initializers/tenants.py | 2 ++ .../initializers/virtual_machines.py | 2 ++ .../initializers/virtualization_interfaces.py | 2 ++ .../initializers/vlan_groups.py | 2 ++ src/netbox_initializers/initializers/vlans.py | 2 ++ src/netbox_initializers/initializers/vrfs.py | 2 ++ .../initializers/webhooks.py | 3 +++ 42 files changed, 118 insertions(+), 1 deletion(-) diff --git a/src/netbox_initializers/initializers/__init__.py b/src/netbox_initializers/initializers/__init__.py index f65beab..17b13cd 100644 --- a/src/netbox_initializers/initializers/__init__.py +++ b/src/netbox_initializers/initializers/__init__.py @@ -3,7 +3,7 @@ from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ObjectDoesNotExist -from extras.models import CustomField +from extras.models import CustomField, Tag from ruamel.yaml import YAML INITIALIZER_ORDER = ( @@ -122,6 +122,27 @@ def set_custom_fields_values(self, entity, custom_field_data): if save: entity.save() + def set_tags(self, entity, tags): + if not tags: + return + + if not hasattr(entity, "tags"): + raise Exception(f"⚠️ Tags cannot be applied to {entity}'s model") + + ct = ContentType.objects.get_for_model(entity) + + save = False + for tag in Tag.objects.filter(name__in=tags): + restricted_cts = tag.object_types.all() + if restricted_cts and ct not in restricted_cts: + raise Exception(f"⚠️ Tag {tag} cannot be applied to {entity}'s model") + + entity.tags.add(tag) + save = True + + if save: + entity.save() + def split_params(self, params: dict, unique_params: list = None) -> Tuple[dict, dict]: """Split params dict into dict with matching params and a dict with default values""" diff --git a/src/netbox_initializers/initializers/aggregates.py b/src/netbox_initializers/initializers/aggregates.py index fd76a9f..76d33c3 100644 --- a/src/netbox_initializers/initializers/aggregates.py +++ b/src/netbox_initializers/initializers/aggregates.py @@ -20,6 +20,7 @@ def load_data(self): return for params in aggregates: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) params["prefix"] = IPNetwork(params["prefix"]) @@ -45,6 +46,7 @@ def load_data(self): print("πŸ—žοΈ Created Aggregate", aggregate.prefix) self.set_custom_fields_values(aggregate, custom_field_data) + self.set_tags(aggregate, tags) register_initializer("aggregates", AggregateInitializer) diff --git a/src/netbox_initializers/initializers/asns.py b/src/netbox_initializers/initializers/asns.py index 99d75df..02350e3 100644 --- a/src/netbox_initializers/initializers/asns.py +++ b/src/netbox_initializers/initializers/asns.py @@ -16,6 +16,7 @@ def load_data(self): if asns is None: return for params in asns: + tags = params.pop("tags", None) for assoc, details in REQUIRED_ASSOCS.items(): model, field = details query = {field: params.pop(assoc)} @@ -35,5 +36,7 @@ def load_data(self): if created: print(f"πŸ”‘ Created ASN {asn.asn}") + self.set_tags(asn, tags) + register_initializer("asns", ASNInitializer) diff --git a/src/netbox_initializers/initializers/cables.py b/src/netbox_initializers/initializers/cables.py index e36e084..ef4f32e 100644 --- a/src/netbox_initializers/initializers/cables.py +++ b/src/netbox_initializers/initializers/cables.py @@ -202,6 +202,8 @@ def load_data(self): if cables is None: return for params in cables: + tags = params.pop("tags", None) + params["termination_a_class"] = get_termination_class_by_name( params.get("termination_a_class") ) @@ -248,6 +250,7 @@ def load_data(self): CableTermination.objects.create(**params_b_term) print(f"🧷 Created cable {cable} {cable_name}") + self.set_tags(cable, tags) register_initializer("cables", CableInitializer) diff --git a/src/netbox_initializers/initializers/circuit_types.py b/src/netbox_initializers/initializers/circuit_types.py index f5f1416..2e1aa3b 100644 --- a/src/netbox_initializers/initializers/circuit_types.py +++ b/src/netbox_initializers/initializers/circuit_types.py @@ -11,6 +11,7 @@ def load_data(self): if circuit_types is None: return for params in circuit_types: + tags = params.pop("tags", None) custom_field_data = self.pop_custom_fields(params) matching_params, defaults = self.split_params(params) @@ -22,6 +23,7 @@ def load_data(self): print("⚑ Created Circuit Type", circuit_type.name) self.set_custom_fields_values(circuit_type, custom_field_data) + self.set_tags(circuit_type, tags) register_initializer("circuit_types", CircuitTypeInitializer) diff --git a/src/netbox_initializers/initializers/circuits.py b/src/netbox_initializers/initializers/circuits.py index e687096..125f17c 100644 --- a/src/netbox_initializers/initializers/circuits.py +++ b/src/netbox_initializers/initializers/circuits.py @@ -17,6 +17,7 @@ def load_data(self): return for params in circuits: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) for assoc, details in REQUIRED_ASSOCS.items(): model, field = details @@ -38,6 +39,7 @@ def load_data(self): print("⚑ Created Circuit", circuit.cid) self.set_custom_fields_values(circuit, custom_field_data) + self.set_tags(circuit, tags) register_initializer("circuits", CircuitInitializer) diff --git a/src/netbox_initializers/initializers/cluster_groups.py b/src/netbox_initializers/initializers/cluster_groups.py index 2109b2b..f56490a 100644 --- a/src/netbox_initializers/initializers/cluster_groups.py +++ b/src/netbox_initializers/initializers/cluster_groups.py @@ -11,6 +11,7 @@ def load_data(self): if cluster_groups is None: return for params in cluster_groups: + tags = params.pop("tags", None) matching_params, defaults = self.split_params(params) cluster_group, created = ClusterGroup.objects.get_or_create( **matching_params, defaults=defaults @@ -18,6 +19,7 @@ def load_data(self): if created: print("πŸ—„οΈ Created Cluster Group", cluster_group.name) + self.set_tags(cluster_group, tags) register_initializer("cluster_groups", ClusterGroupInitializer) diff --git a/src/netbox_initializers/initializers/cluster_types.py b/src/netbox_initializers/initializers/cluster_types.py index a9ddfb4..cd9d811 100644 --- a/src/netbox_initializers/initializers/cluster_types.py +++ b/src/netbox_initializers/initializers/cluster_types.py @@ -11,6 +11,7 @@ def load_data(self): if cluster_types is None: return for params in cluster_types: + tags = params.pop("tags", None) matching_params, defaults = self.split_params(params) cluster_type, created = ClusterType.objects.get_or_create( **matching_params, defaults=defaults @@ -18,6 +19,7 @@ def load_data(self): if created: print("🧰 Created Cluster Type", cluster_type.name) + self.set_tags(cluster_type, tags) register_initializer("cluster_types", ClusterTypesInitializer) diff --git a/src/netbox_initializers/initializers/clusters.py b/src/netbox_initializers/initializers/clusters.py index 21f65e0..46d9df9 100644 --- a/src/netbox_initializers/initializers/clusters.py +++ b/src/netbox_initializers/initializers/clusters.py @@ -22,6 +22,7 @@ def load_data(self): return for params in clusters: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) for assoc, details in REQUIRED_ASSOCS.items(): model, field = details @@ -43,6 +44,7 @@ def load_data(self): print("πŸ—„οΈ Created cluster", cluster.name) self.set_custom_fields_values(cluster, custom_field_data) + self.set_tags(cluster, tags) register_initializer("clusters", ClusterInitializer) diff --git a/src/netbox_initializers/initializers/config_templates.py b/src/netbox_initializers/initializers/config_templates.py index 992daf4..b82c237 100644 --- a/src/netbox_initializers/initializers/config_templates.py +++ b/src/netbox_initializers/initializers/config_templates.py @@ -22,6 +22,7 @@ def load_data(self): if config_templates is None: return for template in config_templates: + tags = template.pop("tags", None) matching_params, defaults = self.split_params(template) config_template, created = ConfigTemplate.objects.get_or_create( **matching_params, defaults=defaults @@ -30,6 +31,7 @@ def load_data(self): if created: config_template.save() print("πŸͺ Created Config Template {0}".format(config_template.name)) + self.set_tags(config_template, tags) register_initializer("config_templates", ConfigTemplateInitializer) diff --git a/src/netbox_initializers/initializers/contact_groups.py b/src/netbox_initializers/initializers/contact_groups.py index 7ad2841..fc0a88a 100644 --- a/src/netbox_initializers/initializers/contact_groups.py +++ b/src/netbox_initializers/initializers/contact_groups.py @@ -14,6 +14,7 @@ def load_data(self): return for params in contact_groups: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) for assoc, details in OPTIONAL_ASSOCS.items(): if assoc in params: @@ -31,6 +32,7 @@ def load_data(self): print("πŸ”³ Created Contact Group", contact_group.name) self.set_custom_fields_values(contact_group, custom_field_data) + self.set_tags(contact_group, tags) register_initializer("contact_groups", ContactGroupInitializer) diff --git a/src/netbox_initializers/initializers/contact_roles.py b/src/netbox_initializers/initializers/contact_roles.py index 61e8dae..a43316f 100644 --- a/src/netbox_initializers/initializers/contact_roles.py +++ b/src/netbox_initializers/initializers/contact_roles.py @@ -12,6 +12,7 @@ def load_data(self): return for params in contact_roles: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) matching_params, defaults = self.split_params(params) contact_role, created = ContactRole.objects.get_or_create( @@ -22,6 +23,7 @@ def load_data(self): print("πŸ”³ Created Contact Role", contact_role.name) self.set_custom_fields_values(contact_role, custom_field_data) + self.set_tags(contact_role, tags) register_initializer("contact_roles", ContactRoleInitializer) diff --git a/src/netbox_initializers/initializers/contacts.py b/src/netbox_initializers/initializers/contacts.py index 8017461..64ce5c1 100644 --- a/src/netbox_initializers/initializers/contacts.py +++ b/src/netbox_initializers/initializers/contacts.py @@ -14,6 +14,7 @@ def load_data(self): return for params in contacts: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) for assoc, details in OPTIONAL_ASSOCS.items(): if assoc in params: @@ -29,6 +30,7 @@ def load_data(self): print("πŸ‘©β€πŸ’» Created Contact", contact.name) self.set_custom_fields_values(contact, custom_field_data) + self.set_tags(contact, tags) register_initializer("contacts", ContactInitializer) diff --git a/src/netbox_initializers/initializers/device_roles.py b/src/netbox_initializers/initializers/device_roles.py index c6e2486..c6cc1d1 100644 --- a/src/netbox_initializers/initializers/device_roles.py +++ b/src/netbox_initializers/initializers/device_roles.py @@ -12,6 +12,7 @@ def load_data(self): if device_roles is None: return for params in device_roles: + tags = params.pop("tags", None) if "color" in params: color = params.pop("color") @@ -28,5 +29,7 @@ def load_data(self): if created: print("🎨 Created device role", device_role.name) + self.set_tags(device_role, tags) + register_initializer("device_roles", DeviceRoleInitializer) diff --git a/src/netbox_initializers/initializers/device_types.py b/src/netbox_initializers/initializers/device_types.py index 97b0672..7ea87d5 100644 --- a/src/netbox_initializers/initializers/device_types.py +++ b/src/netbox_initializers/initializers/device_types.py @@ -85,6 +85,7 @@ def load_data(self): return for params in device_types: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) components = [(v[0], v[1], params.pop(k, [])) for k, v in SUPPORTED_COMPONENTS.items()] for assoc, details in REQUIRED_ASSOCS.items(): @@ -109,6 +110,7 @@ def load_data(self): print("πŸ”‘ Created device type", device_type.manufacturer, device_type.model) self.set_custom_fields_values(device_type, custom_field_data) + self.set_tags(device_type, tags) for component in components: c_model, c_match_params, c_params = component diff --git a/src/netbox_initializers/initializers/devices.py b/src/netbox_initializers/initializers/devices.py index cde86d9..c549b42 100644 --- a/src/netbox_initializers/initializers/devices.py +++ b/src/netbox_initializers/initializers/devices.py @@ -30,6 +30,7 @@ def load_data(self): return for params in devices: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) # primary ips are handled later in `380_primary_ips.py` params.pop("primary_ip4", None) @@ -57,6 +58,7 @@ def load_data(self): print("πŸ–₯️ Created device", device.name) self.set_custom_fields_values(device, custom_field_data) + self.set_tags(device, tags) register_initializer("devices", DeviceInitializer) diff --git a/src/netbox_initializers/initializers/interfaces.py b/src/netbox_initializers/initializers/interfaces.py index 6ab89c1..9bf8bdc 100644 --- a/src/netbox_initializers/initializers/interfaces.py +++ b/src/netbox_initializers/initializers/interfaces.py @@ -24,6 +24,7 @@ def load_data(self): return for params in interfaces: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) related_interfaces = {k: params.pop(k, None) for k in RELATED_ASSOCS} @@ -49,6 +50,7 @@ def load_data(self): print(f"🧷 Created interface {interface} on {interface.device}") self.set_custom_fields_values(interface, custom_field_data) + self.set_tags(interface, tags) for related_field, related_value in related_interfaces.items(): if not related_value: diff --git a/src/netbox_initializers/initializers/ip_addresses.py b/src/netbox_initializers/initializers/ip_addresses.py index 4310216..db67b8f 100644 --- a/src/netbox_initializers/initializers/ip_addresses.py +++ b/src/netbox_initializers/initializers/ip_addresses.py @@ -30,6 +30,7 @@ def load_data(self): return for params in ip_addresses: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) vm = params.pop("virtual_machine", None) device = params.pop("device", None) @@ -70,6 +71,7 @@ def load_data(self): print("🧬 Created IP Address", ip_address.address) self.set_custom_fields_values(ip_address, custom_field_data) + self.set_tags(ip_address, tags) register_initializer("ip_addresses", IPAddressInitializer) diff --git a/src/netbox_initializers/initializers/locations.py b/src/netbox_initializers/initializers/locations.py index a363660..30ffce6 100644 --- a/src/netbox_initializers/initializers/locations.py +++ b/src/netbox_initializers/initializers/locations.py @@ -13,6 +13,7 @@ def load_data(self): if locations is None: return for params in locations: + tags = params.pop("tags", None) for assoc, details in OPTIONAL_ASSOCS.items(): if assoc in params: @@ -26,5 +27,7 @@ def load_data(self): if created: print("🎨 Created location", location.name) + self.set_tags(location, tags) + register_initializer("locations", LocationInitializer) diff --git a/src/netbox_initializers/initializers/manufacturers.py b/src/netbox_initializers/initializers/manufacturers.py index 270f4a4..820ee9f 100644 --- a/src/netbox_initializers/initializers/manufacturers.py +++ b/src/netbox_initializers/initializers/manufacturers.py @@ -11,6 +11,7 @@ def load_data(self): if manufacturers is None: return for params in manufacturers: + tags = params.pop("tags", None) matching_params, defaults = self.split_params(params) manufacturer, created = Manufacturer.objects.get_or_create( **matching_params, defaults=defaults @@ -19,5 +20,7 @@ def load_data(self): if created: print("🏭 Created Manufacturer", manufacturer.name) + self.set_tags(manufacturer, tags) + register_initializer("manufacturers", ManufacturerInitializer) diff --git a/src/netbox_initializers/initializers/platforms.py b/src/netbox_initializers/initializers/platforms.py index c571227..4b73f46 100644 --- a/src/netbox_initializers/initializers/platforms.py +++ b/src/netbox_initializers/initializers/platforms.py @@ -15,6 +15,7 @@ def load_data(self): if platforms is None: return for params in platforms: + tags = params.pop("tags", None) for assoc, details in OPTIONAL_ASSOCS.items(): if assoc in params: @@ -29,5 +30,7 @@ def load_data(self): if created: print("πŸ’Ύ Created platform", platform.name) + self.set_tags(platform, tags) + register_initializer("platforms", PlatformInitializer) diff --git a/src/netbox_initializers/initializers/power_feeds.py b/src/netbox_initializers/initializers/power_feeds.py index 1c9a35e..6badd0e 100644 --- a/src/netbox_initializers/initializers/power_feeds.py +++ b/src/netbox_initializers/initializers/power_feeds.py @@ -16,6 +16,7 @@ def load_data(self): return for params in power_feeds: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) for assoc, details in REQUIRED_ASSOCS.items(): model, field = details @@ -39,6 +40,7 @@ def load_data(self): print("⚑ Created Power Feed", power_feed.name) self.set_custom_fields_values(power_feed, custom_field_data) + self.set_tags(power_feed, tags) register_initializer("power_feeds", PowerFeedInitializer) diff --git a/src/netbox_initializers/initializers/power_panels.py b/src/netbox_initializers/initializers/power_panels.py index 7031aef..89b92e7 100644 --- a/src/netbox_initializers/initializers/power_panels.py +++ b/src/netbox_initializers/initializers/power_panels.py @@ -16,6 +16,7 @@ def load_data(self): return for params in power_panels: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) for assoc, details in REQUIRED_ASSOCS.items(): model, field = details @@ -39,6 +40,7 @@ def load_data(self): print("⚑ Created Power Panel", power_panel.site, power_panel.name) self.set_custom_fields_values(power_panel, custom_field_data) + self.set_tags(power_panel, tags) register_initializer("power_panels", PowerPanelInitializer) diff --git a/src/netbox_initializers/initializers/prefix_vlan_roles.py b/src/netbox_initializers/initializers/prefix_vlan_roles.py index f1bdc55..9bf0470 100644 --- a/src/netbox_initializers/initializers/prefix_vlan_roles.py +++ b/src/netbox_initializers/initializers/prefix_vlan_roles.py @@ -11,11 +11,14 @@ def load_data(self): if roles is None: return for params in roles: + tags = params.pop("tags", None) matching_params, defaults = self.split_params(params) role, created = Role.objects.get_or_create(**matching_params, defaults=defaults) if created: print("⛹️‍ Created Prefix/VLAN Role", role.name) + self.set_tags(role, tags) + register_initializer("prefix_vlan_roles", RoleInitializer) diff --git a/src/netbox_initializers/initializers/prefixes.py b/src/netbox_initializers/initializers/prefixes.py index e6bdba8..b4a435f 100644 --- a/src/netbox_initializers/initializers/prefixes.py +++ b/src/netbox_initializers/initializers/prefixes.py @@ -25,6 +25,7 @@ def load_data(self): return for params in prefixes: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) params["prefix"] = IPNetwork(params["prefix"]) @@ -41,6 +42,7 @@ def load_data(self): print("πŸ“Œ Created Prefix", prefix.prefix) self.set_custom_fields_values(prefix, custom_field_data) + self.set_tags(prefix, tags) register_initializer("prefixes", PrefixInitializer) diff --git a/src/netbox_initializers/initializers/providers.py b/src/netbox_initializers/initializers/providers.py index 9801fb2..31245f9 100644 --- a/src/netbox_initializers/initializers/providers.py +++ b/src/netbox_initializers/initializers/providers.py @@ -13,6 +13,7 @@ def load_data(self): return for params in providers: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) asn_number = params.pop("asn") asn = ASN.objects.filter(asn=asn_number).first() @@ -33,6 +34,7 @@ def load_data(self): print("πŸ“‘ Created provider", provider.name) self.set_custom_fields_values(provider, custom_field_data) + self.set_tags(provider, tags) register_initializer("providers", ProviderInitializer) diff --git a/src/netbox_initializers/initializers/rack_roles.py b/src/netbox_initializers/initializers/rack_roles.py index fbd2d48..ab4d9f2 100644 --- a/src/netbox_initializers/initializers/rack_roles.py +++ b/src/netbox_initializers/initializers/rack_roles.py @@ -12,6 +12,7 @@ def load_data(self): if rack_roles is None: return for params in rack_roles: + tags = params.pop("tags", None) if "color" in params: color = params.pop("color") @@ -27,5 +28,7 @@ def load_data(self): if created: print("🎨 Created rack role", rack_role.name) + self.set_tags(rack_role, tags) + register_initializer("rack_roles", RackRoleInitializer) diff --git a/src/netbox_initializers/initializers/racks.py b/src/netbox_initializers/initializers/racks.py index 285cdd8..3cb3ae3 100644 --- a/src/netbox_initializers/initializers/racks.py +++ b/src/netbox_initializers/initializers/racks.py @@ -21,6 +21,7 @@ def load_data(self): return for params in racks: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) for assoc, details in REQUIRED_ASSOCS.items(): model, field = details @@ -42,6 +43,7 @@ def load_data(self): print("πŸ”³ Created rack", rack.site, rack.name) self.set_custom_fields_values(rack, custom_field_data) + self.set_tags(rack, tags) register_initializer("racks", RackInitializer) diff --git a/src/netbox_initializers/initializers/regions.py b/src/netbox_initializers/initializers/regions.py index 5f70698..d6c8e28 100644 --- a/src/netbox_initializers/initializers/regions.py +++ b/src/netbox_initializers/initializers/regions.py @@ -13,6 +13,7 @@ def load_data(self): if regions is None: return for params in regions: + tags = params.pop("tags", None) for assoc, details in OPTIONAL_ASSOCS.items(): if assoc in params: @@ -27,5 +28,7 @@ def load_data(self): if created: print("🌐 Created region", region.name) + self.set_tags(region, tags) + register_initializer("regions", RegionInitializer) diff --git a/src/netbox_initializers/initializers/rirs.py b/src/netbox_initializers/initializers/rirs.py index 7e86b6b..69532f3 100644 --- a/src/netbox_initializers/initializers/rirs.py +++ b/src/netbox_initializers/initializers/rirs.py @@ -12,11 +12,14 @@ def load_data(self): return for params in rirs: + tags = params.pop("tags", None) matching_params, defaults = self.split_params(params) rir, created = RIR.objects.get_or_create(**matching_params, defaults=defaults) if created: print("πŸ—ΊοΈ Created RIR", rir.name) + self.set_tags(rir, tags) + register_initializer("rirs", RIRInitializer) diff --git a/src/netbox_initializers/initializers/route_targets.py b/src/netbox_initializers/initializers/route_targets.py index e099305..fdb0caa 100644 --- a/src/netbox_initializers/initializers/route_targets.py +++ b/src/netbox_initializers/initializers/route_targets.py @@ -15,6 +15,7 @@ def load_data(self): return for params in route_targets: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) for assoc, details in OPTIONAL_ASSOCS.items(): if assoc in params: @@ -32,6 +33,7 @@ def load_data(self): print("🎯 Created Route Target", route_target.name) self.set_custom_fields_values(route_target, custom_field_data) + self.set_tags(route_target, tags) register_initializer("route_targets", RouteTargetInitializer) diff --git a/src/netbox_initializers/initializers/service_templates.py b/src/netbox_initializers/initializers/service_templates.py index 434dcbf..beba486 100644 --- a/src/netbox_initializers/initializers/service_templates.py +++ b/src/netbox_initializers/initializers/service_templates.py @@ -13,6 +13,7 @@ def load_data(self): if service_templates is None: return for params in service_templates: + tags = params.pop("tags", None) matching_params, defaults = self.split_params(params, MATCH_PARAMS) service_template, created = ServiceTemplate.objects.get_or_create( **matching_params, defaults=defaults @@ -21,5 +22,7 @@ def load_data(self): if created: print("🧰 Created Service Template", service_template.name) + self.set_tags(service_template, tags) + register_initializer("service_templates", ServiceTemplateInitializer) diff --git a/src/netbox_initializers/initializers/services.py b/src/netbox_initializers/initializers/services.py index 276400d..38138fb 100644 --- a/src/netbox_initializers/initializers/services.py +++ b/src/netbox_initializers/initializers/services.py @@ -19,6 +19,7 @@ def load_data(self): if services is None: return for params in services: + tags = params.pop("tags", None) for assoc, details in OPTIONAL_ASSOCS.items(): if assoc in params: @@ -32,5 +33,7 @@ def load_data(self): if created: print("🧰 Created Service", service.name) + self.set_tags(service, tags) + register_initializer("services", ServiceInitializer) diff --git a/src/netbox_initializers/initializers/sites.py b/src/netbox_initializers/initializers/sites.py index b2e832e..09a4386 100644 --- a/src/netbox_initializers/initializers/sites.py +++ b/src/netbox_initializers/initializers/sites.py @@ -16,6 +16,7 @@ def load_data(self): return for params in sites: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) for assoc, details in OPTIONAL_ASSOCS.items(): if assoc in params: @@ -49,6 +50,7 @@ def load_data(self): print("πŸ“ Created site", site.name) self.set_custom_fields_values(site, custom_field_data) + self.set_tags(site, tags) for asn in asnFounds: site.asns.add(asn) diff --git a/src/netbox_initializers/initializers/tenant_groups.py b/src/netbox_initializers/initializers/tenant_groups.py index 802717e..4718542 100644 --- a/src/netbox_initializers/initializers/tenant_groups.py +++ b/src/netbox_initializers/initializers/tenant_groups.py @@ -11,6 +11,7 @@ def load_data(self): if tenant_groups is None: return for params in tenant_groups: + tags = params.pop("tags", None) matching_params, defaults = self.split_params(params) tenant_group, created = TenantGroup.objects.get_or_create( **matching_params, defaults=defaults @@ -19,5 +20,7 @@ def load_data(self): if created: print("πŸ”³ Created Tenant Group", tenant_group.name) + self.set_tags(tenant_group, tags) + register_initializer("tenant_groups", TenantGroupInitializer) diff --git a/src/netbox_initializers/initializers/tenants.py b/src/netbox_initializers/initializers/tenants.py index a2b59fe..ec6668b 100644 --- a/src/netbox_initializers/initializers/tenants.py +++ b/src/netbox_initializers/initializers/tenants.py @@ -14,6 +14,7 @@ def load_data(self): return for params in tenants: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) for assoc, details in OPTIONAL_ASSOCS.items(): if assoc in params: @@ -29,6 +30,7 @@ def load_data(self): print("πŸ‘©β€πŸ’» Created Tenant", tenant.name) self.set_custom_fields_values(tenant, custom_field_data) + self.set_tags(tenant, tags) register_initializer("tenants", TenantInitializer) diff --git a/src/netbox_initializers/initializers/virtual_machines.py b/src/netbox_initializers/initializers/virtual_machines.py index 195cf9d..77191dd 100644 --- a/src/netbox_initializers/initializers/virtual_machines.py +++ b/src/netbox_initializers/initializers/virtual_machines.py @@ -23,6 +23,7 @@ def load_data(self): return for params in virtual_machines: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) # primary ips are handled later in `270_primary_ips.py` params.pop("primary_ip4", None) @@ -52,6 +53,7 @@ def load_data(self): print("πŸ–₯️ Created virtual machine", virtual_machine.name) self.set_custom_fields_values(virtual_machine, custom_field_data) + self.set_tags(virtual_machine, tags) register_initializer("virtual_machines", VirtualMachineInitializer) diff --git a/src/netbox_initializers/initializers/virtualization_interfaces.py b/src/netbox_initializers/initializers/virtualization_interfaces.py index 3057fa6..0fe915f 100644 --- a/src/netbox_initializers/initializers/virtualization_interfaces.py +++ b/src/netbox_initializers/initializers/virtualization_interfaces.py @@ -15,6 +15,7 @@ def load_data(self): return for params in interfaces: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) for assoc, details in REQUIRED_ASSOCS.items(): model, field = details @@ -31,6 +32,7 @@ def load_data(self): print("🧷 Created interface", interface.name, interface.virtual_machine.name) self.set_custom_fields_values(interface, custom_field_data) + self.set_tags(interface, tags) register_initializer("virtualization_interfaces", VMInterfaceInitializer) diff --git a/src/netbox_initializers/initializers/vlan_groups.py b/src/netbox_initializers/initializers/vlan_groups.py index aa0d331..5997ba3 100644 --- a/src/netbox_initializers/initializers/vlan_groups.py +++ b/src/netbox_initializers/initializers/vlan_groups.py @@ -15,6 +15,7 @@ def load_data(self): return for params in vlan_groups: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) for assoc, details in OPTIONAL_ASSOCS.items(): if assoc in params: @@ -47,6 +48,7 @@ def load_data(self): print("🏘️ Created VLAN Group", vlan_group.name) self.set_custom_fields_values(vlan_group, custom_field_data) + self.set_tags(vlan_group, tags) register_initializer("vlan_groups", VLANGroupInitializer) diff --git a/src/netbox_initializers/initializers/vlans.py b/src/netbox_initializers/initializers/vlans.py index b522514..690d44d 100644 --- a/src/netbox_initializers/initializers/vlans.py +++ b/src/netbox_initializers/initializers/vlans.py @@ -23,6 +23,7 @@ def load_data(self): return for params in vlans: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) for assoc, details in OPTIONAL_ASSOCS.items(): if assoc in params: @@ -38,6 +39,7 @@ def load_data(self): print("🏠 Created VLAN", vlan.name) self.set_custom_fields_values(vlan, custom_field_data) + self.set_tags(vlan, tags) register_initializer("vlans", VLANInitializer) diff --git a/src/netbox_initializers/initializers/vrfs.py b/src/netbox_initializers/initializers/vrfs.py index c0fe18c..4379e9d 100644 --- a/src/netbox_initializers/initializers/vrfs.py +++ b/src/netbox_initializers/initializers/vrfs.py @@ -16,6 +16,7 @@ def load_data(self): return for params in vrfs: custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) for assoc, details in OPTIONAL_ASSOCS.items(): if assoc in params: @@ -31,6 +32,7 @@ def load_data(self): print("πŸ“¦ Created VRF", vrf.name) self.set_custom_fields_values(vrf, custom_field_data) + self.set_tags(vrf, tags) register_initializer("vrfs", VRFInitializer) diff --git a/src/netbox_initializers/initializers/webhooks.py b/src/netbox_initializers/initializers/webhooks.py index 1a3447d..26625f9 100644 --- a/src/netbox_initializers/initializers/webhooks.py +++ b/src/netbox_initializers/initializers/webhooks.py @@ -20,11 +20,14 @@ def load_data(self): if webhooks is None: return for hook in webhooks: + tags = hook.pop("tags", None) matching_params, defaults = self.split_params(hook) webhook, created = Webhook.objects.get_or_create(**matching_params, defaults=defaults) if created: print("πŸͺ Created Webhook {0}".format(webhook.name)) + self.set_tags(webhook, tags) + register_initializer("webhooks", WebhookInitializer)