Skip to content

Commit

Permalink
fixes issue if deleted vmware tags not removed from objects #359
Browse files Browse the repository at this point in the history
  • Loading branch information
bb-Ricardo committed Feb 14, 2024
1 parent fd68deb commit b7388d9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
30 changes: 19 additions & 11 deletions module/netbox/object_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ def get_dependencies(self):

return r

def get_tags(self):
def get_tags(self) -> list:
"""
returns a list of strings of tag names
Expand All @@ -801,6 +801,7 @@ def get_tags(self):
"""

tag_list = list()

if "tags" not in self.data_model.keys():
return tag_list

Expand All @@ -812,8 +813,19 @@ def get_tags(self):
else:
log.error(f"This tag is not an NetBox object: {tag}")
log.error(f"Please report this here: https://github.com/bb-Ricardo/netbox-sync/issues/120")

return tag_list

@classmethod
def extract_tag_name(cls, this_tag):

if isinstance(this_tag, NBTag):
return this_tag.get_display_name()
elif isinstance(this_tag, str):
return this_tag
elif isinstance(this_tag, dict) and this_tag.get("name") is not None:
return this_tag.get("name")

def compile_tags(self, tags, remove=False):
"""
Expand Down Expand Up @@ -842,20 +854,13 @@ def compile_tags(self, tags, remove=False):

new_tag_list = NBTagList()

def extract_tags(this_tags):
if isinstance(this_tags, NBTag):
sanitized_tag_strings.append(this_tags.get_display_name())
elif isinstance(this_tags, str):
sanitized_tag_strings.append(this_tags)
elif isinstance(this_tags, dict) and this_tags.get("name") is not None:
sanitized_tag_strings.append(this_tags.get("name"))

if isinstance(tags, list):
for tag in tags:
extract_tags(tag)
sanitized_tag_strings.append(self.extract_tag_name(tag))

else:
# noinspection PyTypeChecker
extract_tags(tags)
sanitized_tag_strings.append(self.extract_tag_name(tags))

# current list of tag strings
current_tag_strings = self.get_tags()
Expand All @@ -865,6 +870,9 @@ def extract_tags(this_tags):

for tag_name in sanitized_tag_strings:

if tag_name is None:
continue

# add tag
if tag_name not in current_tag_strings and remove is False:

Expand Down
25 changes: 21 additions & 4 deletions module/sources/vmware/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,13 +679,18 @@ def get_vmware_object_tags(self, obj):
# noinspection PyBroadException
try:
tag_name = self.tag_session.tagging.Tag.get(tag_id).name
tag_description = f"{primary_tag_name}: "\
f"{self.tag_session.tagging.Tag.get(tag_id).description}"
tag_description = self.tag_session.tagging.Tag.get(tag_id).description
except Exception as e:
log.error(f"Unable to retrieve vCenter tag '{tag_id}' for '{obj.name}': {e}")
continue

if tag_name is not None:

if tag_description is not None and len(f"{tag_description}") > 0:
tag_description = f"{primary_tag_name}: {tag_description}"
else:
tag_description = primary_tag_name

tag_list.append(self.inventory.add_update_object(NBTag, data={
"name": tag_name,
"description": tag_description
Expand Down Expand Up @@ -1092,13 +1097,25 @@ def add_device_vm_to_inventory(self, object_type, object_data, pnic_data=None, v
role_name = self.get_object_relation(object_name,
"host_role_relation" if object_type == NBDevice else "vm_role_relation")

# take care of object role in NetBox
if object_type == NBDevice:
if role_name is None:
role_name = "Server"
device_vm_object.update(data={"device_role": {"name": role_name}})
if object_type == NBVM and role_name is not None:
device_vm_object.update(data={"role": {"name": role_name}})

# verify if source tags have been removed from object.
new_object_tags = list(map(NetBoxObject.extract_tag_name, object_data.get("tags", list())))

for object_tag in device_vm_object.data.get("tags", list()):

if not f'{object_tag.data.get("description")}'.startswith(primary_tag_name):
continue

if NetBoxObject.extract_tag_name(object_tag) not in new_object_tags:
device_vm_object.remove_tags(object_tag)

# update VM disk data information
if version.parse(self.inventory.netbox_api_version) >= version.parse("3.7.0") and \
object_type == NBVM and disk_data is not None and len(disk_data) > 0:
Expand Down Expand Up @@ -2167,8 +2184,8 @@ def add_virtual_machine(self, obj):
# Add adaption for added virtual disks in NetBox 3.7.0
if version.parse(self.inventory.netbox_api_version) < version.parse("3.7.0"):
vm_data["disk"] = int(sum([getattr(comp, "capacityInKB", 0) for comp in hardware_devices
if isinstance(comp, vim.vm.device.VirtualDisk)
]) / 1024 / 1024)
if isinstance(comp, vim.vm.device.VirtualDisk)
]) / 1024 / 1024)

if platform is not None:
vm_data["platform"] = {"name": platform}
Expand Down

0 comments on commit b7388d9

Please sign in to comment.