Skip to content

Commit b7388d9

Browse files
committed
fixes issue if deleted vmware tags not removed from objects #359
1 parent fd68deb commit b7388d9

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

module/netbox/object_classes.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ def get_dependencies(self):
791791

792792
return r
793793

794-
def get_tags(self):
794+
def get_tags(self) -> list:
795795
"""
796796
returns a list of strings of tag names
797797
@@ -801,6 +801,7 @@ def get_tags(self):
801801
"""
802802

803803
tag_list = list()
804+
804805
if "tags" not in self.data_model.keys():
805806
return tag_list
806807

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

819+
@classmethod
820+
def extract_tag_name(cls, this_tag):
821+
822+
if isinstance(this_tag, NBTag):
823+
return this_tag.get_display_name()
824+
elif isinstance(this_tag, str):
825+
return this_tag
826+
elif isinstance(this_tag, dict) and this_tag.get("name") is not None:
827+
return this_tag.get("name")
828+
817829
def compile_tags(self, tags, remove=False):
818830
"""
819831
@@ -842,20 +854,13 @@ def compile_tags(self, tags, remove=False):
842854

843855
new_tag_list = NBTagList()
844856

845-
def extract_tags(this_tags):
846-
if isinstance(this_tags, NBTag):
847-
sanitized_tag_strings.append(this_tags.get_display_name())
848-
elif isinstance(this_tags, str):
849-
sanitized_tag_strings.append(this_tags)
850-
elif isinstance(this_tags, dict) and this_tags.get("name") is not None:
851-
sanitized_tag_strings.append(this_tags.get("name"))
852-
853857
if isinstance(tags, list):
854858
for tag in tags:
855-
extract_tags(tag)
859+
sanitized_tag_strings.append(self.extract_tag_name(tag))
860+
856861
else:
857862
# noinspection PyTypeChecker
858-
extract_tags(tags)
863+
sanitized_tag_strings.append(self.extract_tag_name(tags))
859864

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

866871
for tag_name in sanitized_tag_strings:
867872

873+
if tag_name is None:
874+
continue
875+
868876
# add tag
869877
if tag_name not in current_tag_strings and remove is False:
870878

module/sources/vmware/connection.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -679,13 +679,18 @@ def get_vmware_object_tags(self, obj):
679679
# noinspection PyBroadException
680680
try:
681681
tag_name = self.tag_session.tagging.Tag.get(tag_id).name
682-
tag_description = f"{primary_tag_name}: "\
683-
f"{self.tag_session.tagging.Tag.get(tag_id).description}"
682+
tag_description = self.tag_session.tagging.Tag.get(tag_id).description
684683
except Exception as e:
685684
log.error(f"Unable to retrieve vCenter tag '{tag_id}' for '{obj.name}': {e}")
686685
continue
687686

688687
if tag_name is not None:
688+
689+
if tag_description is not None and len(f"{tag_description}") > 0:
690+
tag_description = f"{primary_tag_name}: {tag_description}"
691+
else:
692+
tag_description = primary_tag_name
693+
689694
tag_list.append(self.inventory.add_update_object(NBTag, data={
690695
"name": tag_name,
691696
"description": tag_description
@@ -1092,13 +1097,25 @@ def add_device_vm_to_inventory(self, object_type, object_data, pnic_data=None, v
10921097
role_name = self.get_object_relation(object_name,
10931098
"host_role_relation" if object_type == NBDevice else "vm_role_relation")
10941099

1100+
# take care of object role in NetBox
10951101
if object_type == NBDevice:
10961102
if role_name is None:
10971103
role_name = "Server"
10981104
device_vm_object.update(data={"device_role": {"name": role_name}})
10991105
if object_type == NBVM and role_name is not None:
11001106
device_vm_object.update(data={"role": {"name": role_name}})
11011107

1108+
# verify if source tags have been removed from object.
1109+
new_object_tags = list(map(NetBoxObject.extract_tag_name, object_data.get("tags", list())))
1110+
1111+
for object_tag in device_vm_object.data.get("tags", list()):
1112+
1113+
if not f'{object_tag.data.get("description")}'.startswith(primary_tag_name):
1114+
continue
1115+
1116+
if NetBoxObject.extract_tag_name(object_tag) not in new_object_tags:
1117+
device_vm_object.remove_tags(object_tag)
1118+
11021119
# update VM disk data information
11031120
if version.parse(self.inventory.netbox_api_version) >= version.parse("3.7.0") and \
11041121
object_type == NBVM and disk_data is not None and len(disk_data) > 0:
@@ -2167,8 +2184,8 @@ def add_virtual_machine(self, obj):
21672184
# Add adaption for added virtual disks in NetBox 3.7.0
21682185
if version.parse(self.inventory.netbox_api_version) < version.parse("3.7.0"):
21692186
vm_data["disk"] = int(sum([getattr(comp, "capacityInKB", 0) for comp in hardware_devices
2170-
if isinstance(comp, vim.vm.device.VirtualDisk)
2171-
]) / 1024 / 1024)
2187+
if isinstance(comp, vim.vm.device.VirtualDisk)
2188+
]) / 1024 / 1024)
21722189

21732190
if platform is not None:
21742191
vm_data["platform"] = {"name": platform}

0 commit comments

Comments
 (0)