From 8f6aa6103d3698df0827b8ecd1398fb7c57f877b Mon Sep 17 00:00:00 2001 From: worldworm <13227454+worldworm@users.noreply.github.com> Date: Thu, 27 Jul 2023 17:05:42 +0200 Subject: [PATCH] adds cluster without site relation config #301 --- module/netbox/object_classes.py | 6 ++++++ module/sources/vmware/config.py | 8 ++++++-- module/sources/vmware/connection.py | 5 +++++ settings-example.ini | 8 ++++++-- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/module/netbox/object_classes.py b/module/netbox/object_classes.py index 2c2ba37..4204906 100644 --- a/module/netbox/object_classes.py +++ b/module/netbox/object_classes.py @@ -539,6 +539,12 @@ def update(self, data=None, read_from_netbox=False, source=None): if type_check_failed is True: continue + # allows an empty site for netbox objects where a site is not mandatory + # required for clusters and sub-objects without site reference + if isinstance(self, (NBCluster, NBVM, NBVLAN)) and key == "site" and "name" in value and value["name"] is None: + parsed_data[key] = None + continue + # this is meant to be reference to a different object if defined_value_type in NetBoxObject.__subclasses__() and defined_value_type != NBCustomField: diff --git a/module/sources/vmware/config.py b/module/sources/vmware/config.py index b2b552b..87d2b0f 100644 --- a/module/sources/vmware/config.py +++ b/module/sources/vmware/config.py @@ -121,9 +121,13 @@ def __init__(self): address it is important to pick the correct site. A VM always depends on the cluster site relation a cluster can be specified as "Cluster-name" or - "Datacenter-name/Cluster-name" if multiple clusters have the same name + "Datacenter-name/Cluster-name" if multiple clusters have the same name. + When a vCenter cluster consists of hosts from multiple NetBox sites, + it is possible to leave the site for a NetBox cluster empty. All VMs from + this cluster will then also have no site reference. + The keyword "" can be used as a value for this. """, - config_example="Cluster_NYC = New York, Cluster_FFM.* = Frankfurt, Datacenter_TOKIO/.* = Tokio"), + config_example="Cluster_NYC = New York, Cluster_FFM.* = Frankfurt, Datacenter_TOKIO/.* = Tokio, Cluster_MultiSite = "), ConfigOption("host_site_relation", str, description="""Same as cluster site but on host level. diff --git a/module/sources/vmware/connection.py b/module/sources/vmware/connection.py index 267f851..f45b30e 100644 --- a/module/sources/vmware/connection.py +++ b/module/sources/vmware/connection.py @@ -478,6 +478,11 @@ def get_site_name(self, object_type, object_name, cluster_name=""): site_name = self.site_name log.debug(f"No site relation for '{object_name}' found, using default site '{site_name}'") + # set the site for cluster to None if None-keyword ("") is set via cluster_site_relation + if object_type == NBCluster and site_name == "": + site_name = None + log.debug2(f"Site relation for '{object_name}' set to None") + return site_name def get_object_based_on_macs(self, object_type, mac_list=None): diff --git a/settings-example.ini b/settings-example.ini index 29618a2..37b0817 100644 --- a/settings-example.ini +++ b/settings-example.ini @@ -183,8 +183,12 @@ password = super-secret ; address it is important to pick the correct site. ; A VM always depends on the cluster site relation ; a cluster can be specified as "Cluster-name" or -; "Datacenter-name/Cluster-name" if multiple clusters have the same name -;cluster_site_relation = Cluster_NYC = New York, Cluster_FFM.* = Frankfurt, Datacenter_TOKIO/.* = Tokio +; "Datacenter-name/Cluster-name" if multiple clusters have the same name. +; When a vCenter cluster consists of hosts from multiple NetBox sites, +; it is possible to leave the site for a NetBox cluster empty. All VMs from +; this cluster will then also have no site reference. +; The keyword "" can be used as a value for this. +;cluster_site_relation = Cluster_NYC = New York, Cluster_FFM.* = Frankfurt, Datacenter_TOKIO/.* = Tokio, Cluster_MultiSite = ; Same as cluster site but on host level. If unset it will fall back to ; cluster_site_relation