Skip to content

Commit

Permalink
adds pruning of unused tags #359
Browse files Browse the repository at this point in the history
  • Loading branch information
bb-Ricardo committed Nov 27, 2023
1 parent 0467272 commit d1f52b0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
2 changes: 2 additions & 0 deletions module/netbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@
NBInventoryItem,
NBPowerPort
)

primary_tag_name = "NetBox-synced"
23 changes: 22 additions & 1 deletion module/netbox/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class NetBoxHandler:
minimum_api_version = "2.9"

# This tag gets added to all objects create/updated/inherited by this program
primary_tag = "NetBox-synced"
primary_tag = primary_tag_name

# all objects which have a primary tag but not present in any source anymore will get this tag assigned
orphaned_tag = f"{primary_tag}: Orphaned"
Expand Down Expand Up @@ -869,4 +869,25 @@ def just_delete_all_the_things(self):

return

def delete_unused_tags(self):
"""
deletes all tags with primary tag in description and the attribute 'tagged_items' returned 0
"""

self.query_current_data([NBTag])

for this_tag in self.inventory.get_all_items(NBTag):

tag_description = grab(this_tag, "data.description")
tag_tagged_items = grab(this_tag, "data.tagged_items")

if tag_description is None or not tag_description.startswith(self.primary_tag):
continue

if tag_tagged_items is None or tag_tagged_items != 0:
continue

log.info(f"Deleting unused tag {this_tag.get_display_name()}")
self.request(NBTag, req_type="DELETE", nb_id=this_tag.nb_id)

# EOF
3 changes: 2 additions & 1 deletion module/netbox/object_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,8 @@ def __init__(self, *args, **kwargs):
"name": 100,
"slug": 100,
"color": 6,
"description": 200
"description": 200,
"tagged_items": int
}
super().__init__(*args, **kwargs)

Expand Down
4 changes: 2 additions & 2 deletions module/sources/vmware/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,8 @@ def get_vmware_object_tags(self, obj):
# noinspection PyBroadException
try:
tag_name = self.tag_session.tagging.Tag.get(tag_id).name
tag_prefix_source = f"NetBox-synced {self.name}"
tag_description = f"{tag_prefix_source} {self.tag_session.tagging.Tag.get(tag_id).description}"
tag_description = f"{primary_tag_name} {self.name}: "\
f"{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
Expand Down
3 changes: 3 additions & 0 deletions netbox-sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ def main():
# prune orphaned objects from NetBox
nb_handler.prune_data()

# delete tags which are not used anymore
nb_handler.delete_unused_tags()

# loop over sources and patch netbox data
for source in sources:
# closing all open connections
Expand Down

0 comments on commit d1f52b0

Please sign in to comment.