Skip to content

Commit

Permalink
fixes issue with VM creation in NetBox 3.3.0 bb-Ricardo#220
Browse files Browse the repository at this point in the history
  • Loading branch information
bb-Ricardo committed Aug 29, 2022
1 parent 5a8ce96 commit 6b31cd4
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
6 changes: 4 additions & 2 deletions module/netbox/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions module/netbox/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions module/netbox/object_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions module/sources/vmware/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion netbox-sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 6b31cd4

Please sign in to comment.