Skip to content

Commit

Permalink
improves way to find existing cluster in NetBox #220
Browse files Browse the repository at this point in the history
  • Loading branch information
bb-Ricardo committed Aug 26, 2022
1 parent 0160db3 commit 7ce3a0f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
4 changes: 2 additions & 2 deletions module/netbox/object_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1470,9 +1470,9 @@ class NBCluster(NetBoxObject):
name = "cluster"
api_path = "virtualization/clusters"
primary_key = "name"
secondary_key = "group"
secondary_key = "site"
prune = False
include_secondary_key_if_present = True
#include_secondary_key_if_present = True

def __init__(self, *args, **kwargs):
self.data_model = {
Expand Down
43 changes: 37 additions & 6 deletions module/sources/vmware/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ def get_object_relation(self, name, relation, fallback=None):
match_found = False
if object_regex.match(name):
resolved_name = single_relation.get("assigned_name")
log.debug2(f"Found a matching {relation} '{resolved_name}' ({object_regex.pattern}) for {name}.")
log.debug2(f"Found a matching {relation} '{resolved_name}' ({object_regex.pattern}) for {name}")
resolved_list.append(resolved_name)
match_found = True

Expand All @@ -1094,7 +1094,7 @@ def get_object_relation(self, name, relation, fallback=None):

resolved_name = single_relation.get("assigned_name")
log.debug2(f"Found a matching {relation} '{resolved_name}' ({object_regex.pattern}) "
f"for {stripped_name}.")
f"for {stripped_name}")
resolved_list.append(resolved_name)

if grab(f"{relation}".split("_"), "1") == "tag":
Expand Down Expand Up @@ -1488,11 +1488,42 @@ def add_cluster(self, obj):
data["tags"] = cluster_tags

# try to find cluster including cluster group
cluster_object = self.inventory.get_by_data(NBCluster, data=data)
log.debug2("Trying to find a matching existing cluster")
cluster_object = None
fallback_cluster_object = None
for cluster_candidate in self.inventory.get_all_items(NBCluster):
if grab(cluster_candidate, "data.name") != name:
continue

# if this fails try without cluster group
if cluster_object is None:
cluster_object = self.inventory.get_by_data(NBCluster, data={x: y for x, y in data.items() if x != "group"})
# try to find a cluster with matching site
if cluster_candidate.get_site_name() == site_name:
cluster_object = cluster_candidate
log.debug2("Found an existing cluster where 'name' and 'site' are matching")
break

if grab(cluster_candidate, "data.group") is not None and \
grab(cluster_candidate, "data.group.data.name") == group_name:
cluster_object = cluster_candidate
log.debug2("Found an existing cluster where 'name' and 'cluster group' are matching")
break

if grab(cluster_candidate, "data.tenant") is not None and \
tenant_name is not None and \
grab(cluster_candidate, "data.tenant.data.name") == tenant_name:
cluster_object = cluster_candidate
log.debug2("Found an existing cluster where 'name' and 'tenant' are matching")
break

# if only the name matches and there are multiple cluster with the same name we choose the first
# cluster returned from netbox. This needs to be done to not ignore possible matches in one of
# the next iterations
if fallback_cluster_object is None:
fallback_cluster_object = cluster_candidate

if cluster_object is None and fallback_cluster_object is not None:
log.debug2(f"Found an existing cluster where 'name' "
f"matches (NetBox id: {fallback_cluster_object.get_nb_reference()})")
cluster_object = fallback_cluster_object

if cluster_object is not None:
cluster_object.update(data=data, source=self)
Expand Down

0 comments on commit 7ce3a0f

Please sign in to comment.