From c167b8508b1f6b38f447b80667e7e70122481f60 Mon Sep 17 00:00:00 2001 From: tyderian1978 Date: Thu, 19 Nov 2026 11:21:26 +1100 Subject: [PATCH] vmware: keep syncing when a vCenter tag lookup fails get_vmware_object_tags() returned None when listing the tags attached to an object failed, and for no object at all. collect_object_tags() extends its tag list with every result, so a single failed tag request ended the whole run with "TypeError: 'NoneType' object is not iterable", right after logging "Unable to retrieve vCenter tags for ...". Both functions now always return a list. The object only loses the tags of the lookup which failed: its other tag sources (parent folders, cluster, datacenter) still count and the sync goes on. The error is logged as before. --- module/sources/vmware/connection.py | 7 +- tests/test_vmware_tag_errors.py | 110 ++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 tests/test_vmware_tag_errors.py diff --git a/module/sources/vmware/connection.py b/module/sources/vmware/connection.py index 123260cb..5ef31fa3 100644 --- a/module/sources/vmware/connection.py +++ b/module/sources/vmware/connection.py @@ -1313,7 +1313,7 @@ def get_vmware_object_tags(self, obj): list of NBTag objets retrieved from vCenter for this object """ if obj is None: - return + return list() tag_list = list() if self.tag_session is not None: @@ -1324,7 +1324,8 @@ def get_vmware_object_tags(self, obj): DynamicID(type=grab(obj, "_wsdlName"), id=grab(obj, "_moId"))) except Exception as e: log.error(f"Unable to retrieve vCenter tags for '{obj.name}': {e}") - return + # callers extend their tag list with the result, the object's other tag sources still count + return tag_list # vCenter tags and categories by id: most objects share a few hundred tags, so each # is fetched once per run (per source) instead of once per object it's attached to @@ -1388,7 +1389,7 @@ def collect_object_tags(self, obj): """ if obj is None: - return + return list() tag_list = list() diff --git a/tests/test_vmware_tag_errors.py b/tests/test_vmware_tag_errors.py new file mode 100644 index 00000000..41298255 --- /dev/null +++ b/tests/test_vmware_tag_errors.py @@ -0,0 +1,110 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2020 - 2026 netbox-sync team. All rights reserved. +# +# netbox-sync.py +# +# This work is licensed under the terms of the MIT license. +# For a copy, see file LICENSE.txt included in this +# repository or visit: . + +""" +A failed vCenter tag lookup must cost the object those tags, not the whole sync: +get_vmware_object_tags() and collect_object_tags() always return a list, which +their callers extend their tag lists with. + +vcsim serves no tag API usable by the vcf-sdk, so the tag session is faked. +""" +from types import SimpleNamespace + +import pytest +from pyVmomi import vim + +from module.sources.vmware import connection as vmware_connection +from module.sources.vmware.connection import VMWareHandler + + +@pytest.fixture(autouse=True) +def dynamic_id(monkeypatch): + """The vSphere automation SDK is optional, the tag call only needs type and id.""" + monkeypatch.setattr(vmware_connection, "DynamicID", + lambda **kwargs: SimpleNamespace(**kwargs), raising=False) + + +class _Folder(vim.Folder): + """vim.Folder with a plain name and no vCenter connection behind it.""" + + def __init__(self, mo_id, name): + super().__init__(mo_id) + object.__setattr__(self, "_name", name) + + name = property(lambda self: self._name) + parent = property(lambda self: None) + + +class _VirtualMachine(vim.VirtualMachine): + """vim.VirtualMachine with a plain name and folder.""" + + def __init__(self, mo_id, name, parent): + super().__init__(mo_id) + object.__setattr__(self, "_name", name) + object.__setattr__(self, "_parent", parent) + + name = property(lambda self: self._name) + parent = property(lambda self: self._parent) + + +class _Tagging: + """The parts of the vSphere tagging API get_vmware_object_tags() uses. Listing the tags + attached to one of the 'failing' objects raises, as a vCenter error or timeout would.""" + + def __init__(self, failing): + tags = {"tag-1": SimpleNamespace(name="prod", description="", category_id="cat-1")} + + class _Tag: + @staticmethod + def get(tag_id): + return tags[tag_id] + + class _TagAssociation: + @staticmethod + def list_attached_tags(dynamic_id): + if dynamic_id.id in failing: + raise RuntimeError("simulated vCenter error") + return ["tag-1"] + + self.Tag, self.TagAssociation = _Tag, _TagAssociation + + +def _handler(inventory, failing): + handler = object.__new__(VMWareHandler) + handler.inventory = inventory + handler.name = "test" + handler.source_tag = "Source: test" + handler.tag_session = SimpleNamespace(tagging=_Tagging(failing)) + handler.settings = SimpleNamespace(tag_name_include_category=False, + vm_tag_source=["object", "parent_folder_1"]) + return handler + + +FOLDER = _Folder("group-v1", "Prod") +VM = _VirtualMachine("vm-1", "web01", FOLDER) + + +def _names(tags): + return [tag.get_display_name() for tag in tags] + + +def test_a_failed_tag_lookup_keeps_the_other_tag_sources(inventory): + # the VM's own tags can't be listed, its folder's can + handler = _handler(inventory, failing={"vm-1"}) + + assert _names(handler.collect_object_tags(VM)) == ["prod"] + + +def test_tag_lookups_return_a_list_when_nothing_can_be_read(inventory): + handler = _handler(inventory, failing={"vm-1", "group-v1"}) + + assert handler.get_vmware_object_tags(VM) == [] + assert handler.get_vmware_object_tags(None) == [] + assert handler.collect_object_tags(VM) == [] + assert handler.collect_object_tags(None) == []