mirror of
https://github.com/TheNetworkGuy/netbox-zabbix-sync.git
synced 2026-03-21 20:18:38 -06:00
Merge pull request #179 from retigra/bug/fix_site_extension
Site extension improvement
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import ssl
|
import ssl
|
||||||
from os import environ
|
from os import environ
|
||||||
|
from pprint import pformat
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from pynetbox import api as nbapi
|
from pynetbox import api as nbapi
|
||||||
@@ -282,12 +283,15 @@ class Sync:
|
|||||||
continue
|
continue
|
||||||
if self.config["extended_site_properties"] and nb_vm.site:
|
if self.config["extended_site_properties"] and nb_vm.site:
|
||||||
logger.debug("Host %s: extending site information.", vm.name)
|
logger.debug("Host %s: extending site information.", vm.name)
|
||||||
vm.site = convert_recordset(
|
nb_vm.site.full_details()
|
||||||
self.netbox.dcim.sites.filter(id=nb_vm.site.id)
|
|
||||||
)
|
|
||||||
vm.set_inventory(nb_vm)
|
vm.set_inventory(nb_vm)
|
||||||
vm.set_usermacros()
|
vm.set_usermacros()
|
||||||
vm.set_tags()
|
vm.set_tags()
|
||||||
|
logger.debug(
|
||||||
|
"Host %s NetBox data: %s",
|
||||||
|
vm.name,
|
||||||
|
pformat(dict(nb_vm)),
|
||||||
|
)
|
||||||
# Checks if device is in cleanup state
|
# Checks if device is in cleanup state
|
||||||
if vm.status in self.config["zabbix_device_removal"]:
|
if vm.status in self.config["zabbix_device_removal"]:
|
||||||
if vm.zabbix_id:
|
if vm.zabbix_id:
|
||||||
@@ -361,12 +365,14 @@ class Sync:
|
|||||||
continue
|
continue
|
||||||
if self.config["extended_site_properties"] and nb_device.site:
|
if self.config["extended_site_properties"] and nb_device.site:
|
||||||
logger.debug("Host %s: extending site information.", device.name)
|
logger.debug("Host %s: extending site information.", device.name)
|
||||||
device.site = convert_recordset(
|
nb_device.site.full_details()
|
||||||
self.netbox.dcim.sites.filter(id=nb_device.site.id)
|
|
||||||
)
|
|
||||||
device.set_inventory(nb_device)
|
device.set_inventory(nb_device)
|
||||||
device.set_usermacros()
|
device.set_usermacros()
|
||||||
device.set_tags()
|
device.set_tags()
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
"Host %s NetBox data: %s", device.name, pformat(dict(nb_device))
|
||||||
|
)
|
||||||
# Checks if device is part of cluster.
|
# Checks if device is part of cluster.
|
||||||
# Requires clustering variable
|
# Requires clustering variable
|
||||||
if device.is_cluster() and self.config["clustering"]:
|
if device.is_cluster() and self.config["clustering"]:
|
||||||
|
|||||||
@@ -62,7 +62,6 @@ class PhysicalDevice:
|
|||||||
self.hostgroups = []
|
self.hostgroups = []
|
||||||
self.hostgroup_type = "dev"
|
self.hostgroup_type = "dev"
|
||||||
self.tenant = nb.tenant
|
self.tenant = nb.tenant
|
||||||
self.site = nb.site
|
|
||||||
self.config_context = nb.config_context
|
self.config_context = nb.config_context
|
||||||
self.zbxproxy = None
|
self.zbxproxy = None
|
||||||
self.zabbix_state = 0
|
self.zabbix_state = 0
|
||||||
|
|||||||
+78
-2
@@ -10,7 +10,81 @@ from zabbix_utils import APIRequestError
|
|||||||
from netbox_zabbix_sync.modules.core import Sync
|
from netbox_zabbix_sync.modules.core import Sync
|
||||||
|
|
||||||
|
|
||||||
class MockNetboxDevice:
|
class MockListObject:
|
||||||
|
def __repr__(self):
|
||||||
|
return str(self.object)
|
||||||
|
|
||||||
|
def serialize(self):
|
||||||
|
ret = {k: getattr(self, k) for k in ["object_id", "object_type"]}
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def __getattr__(self, k):
|
||||||
|
return getattr(self.object, k)
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
for i in ["object_id", "object_type", "object"]:
|
||||||
|
cur_attr = getattr(self, i)
|
||||||
|
if isinstance(cur_attr, MockRecord):
|
||||||
|
cur_attr = dict(cur_attr)
|
||||||
|
yield i, cur_attr
|
||||||
|
|
||||||
|
|
||||||
|
class MockRecord:
|
||||||
|
def __init__(self, values, api, endpoint):
|
||||||
|
self.has_details = False
|
||||||
|
self._full_cache = []
|
||||||
|
self._init_cache = []
|
||||||
|
self.api = api
|
||||||
|
self.default_ret = MockRecord
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
for i in dict(self._init_cache):
|
||||||
|
cur_attr = getattr(self, i)
|
||||||
|
if isinstance(cur_attr, MockRecord):
|
||||||
|
yield i, dict(cur_attr)
|
||||||
|
elif isinstance(cur_attr, list) and all(
|
||||||
|
isinstance(i, (MockRecord, MockListObject)) for i in cur_attr
|
||||||
|
):
|
||||||
|
yield i, [dict(x) for x in cur_attr]
|
||||||
|
else:
|
||||||
|
yield i, cur_attr
|
||||||
|
|
||||||
|
def __getitem__(self, k):
|
||||||
|
return dict(self)[k]
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return (
|
||||||
|
getattr(self, "name", None)
|
||||||
|
or getattr(self, "label", None)
|
||||||
|
or getattr(self, "display", None)
|
||||||
|
or ""
|
||||||
|
)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return str(self)
|
||||||
|
|
||||||
|
def __getstate__(self):
|
||||||
|
return self.__dict__
|
||||||
|
|
||||||
|
def __setstate__(self, d):
|
||||||
|
self.__dict__.update(d)
|
||||||
|
|
||||||
|
def __key__(self):
|
||||||
|
if hasattr(self, "id"):
|
||||||
|
return ("mock", self.id)
|
||||||
|
else:
|
||||||
|
return "mock"
|
||||||
|
|
||||||
|
def __hash__(self):
|
||||||
|
return hash(self.__key__())
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
if isinstance(other, MockRecord):
|
||||||
|
return self.__key__() == other.__key__()
|
||||||
|
return NotImplemented
|
||||||
|
|
||||||
|
|
||||||
|
class MockNetboxDevice(MockRecord):
|
||||||
"""Mock NetBox device object."""
|
"""Mock NetBox device object."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@@ -31,6 +105,7 @@ class MockNetboxDevice:
|
|||||||
serial="",
|
serial="",
|
||||||
tags=None,
|
tags=None,
|
||||||
):
|
):
|
||||||
|
super().__init__(values={}, api=None, endpoint=None)
|
||||||
self.id = device_id
|
self.id = device_id
|
||||||
self.name = name
|
self.name = name
|
||||||
self.status = MagicMock()
|
self.status = MagicMock()
|
||||||
@@ -108,7 +183,7 @@ class MockNetboxDevice:
|
|||||||
"""Mock save method for NetBox device."""
|
"""Mock save method for NetBox device."""
|
||||||
|
|
||||||
|
|
||||||
class MockNetboxVM:
|
class MockNetboxVM(MockRecord):
|
||||||
"""Mock NetBox virtual machine object.
|
"""Mock NetBox virtual machine object.
|
||||||
|
|
||||||
Mirrors the real NetBox API response structure so the full VirtualMachine
|
Mirrors the real NetBox API response structure so the full VirtualMachine
|
||||||
@@ -130,6 +205,7 @@ class MockNetboxVM:
|
|||||||
platform=None,
|
platform=None,
|
||||||
tags=None,
|
tags=None,
|
||||||
):
|
):
|
||||||
|
super().__init__(values={}, api=None, endpoint=None)
|
||||||
self.id = vm_id
|
self.id = vm_id
|
||||||
self.name = name
|
self.name = name
|
||||||
self.status = MagicMock()
|
self.status = MagicMock()
|
||||||
|
|||||||
Reference in New Issue
Block a user