diff --git a/module/netbox/connection.py b/module/netbox/connection.py index c6cddb8..433eab8 100644 --- a/module/netbox/connection.py +++ b/module/netbox/connection.py @@ -73,11 +73,11 @@ class NetBoxHandler: # set bogus default version version = "0.0.1" - def __init__(self, settings=None, inventory=None, nb_version=None): + def __init__(self, settings=None, inventory=None, nb_sync_version=None): self.settings = settings self.inventory = inventory - self.version = nb_version + self.version = nb_sync_version self.parse_config_settings(settings) @@ -113,6 +113,8 @@ def __init__(self, settings=None, inventory=None, nb_version=None): do_error_exit(f"NetBox API version '{api_version}' not supported. " f"Minimum API version: {self.minimum_api_version}") + self.inventory.netbox_api_version = api_version + self.setup_caching() def setup_caching(self): diff --git a/module/netbox/inventory.py b/module/netbox/inventory.py index a2d37d3..151cda4 100644 --- a/module/netbox/inventory.py +++ b/module/netbox/inventory.py @@ -24,6 +24,10 @@ class NetBoxInventory: source_tags_of_disabled_sources = list() def __init__(self): + + # track NetBox API version and provided it for all sources + self.netbox_api_version = "0.0.0" + for object_type in NetBoxObject.__subclasses__(): self.base_structure[object_type.name] = list() diff --git a/module/netbox/object_classes.py b/module/netbox/object_classes.py index 1f58a29..a892436 100644 --- a/module/netbox/object_classes.py +++ b/module/netbox/object_classes.py @@ -1536,6 +1536,7 @@ def __init__(self, *args, **kwargs): "comments": str, "primary_ip4": NBIPAddress, "primary_ip6": NBIPAddress, + "site": NBSite, "tags": NBTagList, "tenant": NBTenant, "custom_fields": NBCustomField diff --git a/module/sources/vmware/connection.py b/module/sources/vmware/connection.py index 96112a4..564140b 100644 --- a/module/sources/vmware/connection.py +++ b/module/sources/vmware/connection.py @@ -18,6 +18,8 @@ import urllib3 import requests # noinspection PyUnresolvedReferences +from packaging import version +# noinspection PyUnresolvedReferences from pyVim import connect # noinspection PyUnresolvedReferences from pyVmomi import vim @@ -2290,6 +2292,11 @@ def add_virtual_machine(self, obj): "disk": disk } + # Add adaption for change in NetBox 3.3.0 VM model + # issue: https://github.com/netbox-community/netbox/issues/10131#issuecomment-1225783758 + if version.parse(self.inventory.netbox_api_version) >= version.parse("3.3.0"): + vm_data["site"] = {"name": site_name} + if platform is not None: vm_data["platform"] = {"name": platform} if annotation is not None: diff --git a/netbox-sync.py b/netbox-sync.py index c7df009..d48328c 100755 --- a/netbox-sync.py +++ b/netbox-sync.py @@ -80,7 +80,7 @@ def main(): netbox_settings = get_config(config_handler, section="netbox", valid_settings=NetBoxHandler.settings) # establish NetBox connection - nb_handler = NetBoxHandler(settings=netbox_settings, inventory=inventory, nb_version=__version__) + nb_handler = NetBoxHandler(settings=netbox_settings, inventory=inventory, nb_sync_version=__version__) # if purge was selected we go ahead and remove all items which were managed by this tools if args.purge is True: