From 0674cf8f130fa6bbf85eceb99954d6aefde28ba2 Mon Sep 17 00:00:00 2001 From: "ricardo.bartels@telekom.de" Date: Thu, 30 Sep 2021 23:52:03 +0200 Subject: [PATCH] adds host/vm tag_relation config option to assign tags via config refs: #98 --- module/sources/vmware/connection.py | 28 +++++++++++++++++++++++++--- settings-example.ini | 7 +++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/module/sources/vmware/connection.py b/module/sources/vmware/connection.py index 9cfd60c..4ac45f4 100644 --- a/module/sources/vmware/connection.py +++ b/module/sources/vmware/connection.py @@ -97,6 +97,8 @@ class VMWareHandler(SourceBase): "vm_platform_relation": None, "host_role_relation": None, "vm_role_relation": None, + "host_tag_relation": None, + "vm_tag_relation": None, "dns_name_lookup": False, "custom_dns_servers": None, "set_primary_ip": "when-undefined", @@ -217,9 +219,7 @@ class VMWareHandler(SourceBase): config_settings[setting] = re_compiled - for relation_option in ["cluster_site_relation", "host_site_relation", "host_tenant_relation", - "vm_tenant_relation", "vm_platform_relation", - "host_role_relation", "vm_role_relation"]: + for relation_option in [x for x in self.settings.keys() if "relation" in x]: if config_settings.get(relation_option) is None: continue @@ -1261,6 +1261,15 @@ class VMWareHandler(SourceBase): log.debug2(f"Found a match ({object_regex.pattern}) for {name}, using tenant '{tenant_name}'") break + # assign host_tag_relation + host_tags = list() + for tag_relation in grab(self, "host_tag_relation", fallback=list()): + object_regex = tag_relation.get("object_regex") + if object_regex.match(name): + tag_name = tag_relation.get("tag_name") + log.debug2(f"Found a match ({object_regex.pattern}) for {name}, using tag '{tag_name}'") + host_tags.append(tag_name) + # prepare host data model host_data = { "name": name, @@ -1284,6 +1293,8 @@ class VMWareHandler(SourceBase): host_data["platform"] = {"name": platform} if tenant_name is not None: host_data["tenant"] = {"name": tenant_name} + if len(host_tags) > 0: + host_data["tags"] = host_tags # iterate over hosts virtual switches, needed to enrich data on physical interfaces self.network_data["vswitch"][name] = dict() @@ -1734,6 +1745,15 @@ class VMWareHandler(SourceBase): log.debug2(f"Found a match ({object_regex.pattern}) for {name}, using tenant '{tenant_name}'") break + # assign vm_tag_relation + vm_tags = list() + for tag_relation in grab(self, "vm_tag_relation", fallback=list()): + object_regex = tag_relation.get("object_regex") + if object_regex.match(name): + tag_name = tag_relation.get("tag_name") + log.debug2(f"Found a match ({object_regex.pattern}) for {name}, using tag '{tag_name}'") + vm_tags.append(tag_name) + vm_data = { "name": name, "cluster": {"name": cluster_name}, @@ -1749,6 +1769,8 @@ class VMWareHandler(SourceBase): vm_data["comments"] = annotation if tenant_name is not None: vm_data["tenant"] = {"name": tenant_name} + if len(vm_tags) > 0: + vm_data["tags"] = vm_tags vm_primary_ip4 = None vm_primary_ip6 = None diff --git a/settings-example.ini b/settings-example.ini index 683af60..649d3ef 100644 --- a/settings-example.ini +++ b/settings-example.ini @@ -176,6 +176,13 @@ permitted_subnets = 172.16.0.0/12, 10.0.0.0/8, 192.168.0.0/16, fd00::/8 #host_role_relation = .* = Server #vm_role_relation = .* = Server +# Define NetBox tags which are assigned to a host or VM. This is done +# with a comma separated key = value list. +# key: defines a hosts/VM name as regex +# value: defines the NetBox tag (use quotes if name contains commas) +#host_tag_relation = esxi300.* = Infrastructure +#vm_tag_relation = grafana.* = Infrastructure + # Try to find existing host based on serial number. This can cause issues with blade centers if VMWare does not # report the blades serial number properly. #match_host_by_serial = True