Added verify of vm_hostgroup_format (moved fucntion to tools.py)

This commit is contained in:
Raymond Kuiper 2025-06-05 11:39:42 +02:00
parent 27ee4c341f
commit 77b0798b65
2 changed files with 51 additions and 29 deletions

View File

@ -1,5 +1,5 @@
"""A collection of tools used by several classes"""
from modules.exceptions import HostgroupError
def convert_recordset(recordset):
"""Converts netbox RedcordSet to list of dicts."""
@ -99,3 +99,47 @@ def remove_duplicates(input_list, sortkey=None):
if sortkey and isinstance(sortkey, str):
output_list.sort(key=lambda x: x[sortkey])
return output_list
def verify_hg_format(hg_format, hg_type="dev", logger=None):
"""
Verifies hostgroup field format
"""
allowed_objects = {"dev": ["location",
"rack",
"role",
"manufacturer",
"region",
"site",
"site_group",
"tenant",
"tenant_group",
"platform",
"cluster"]
,"vm": ["location",
"role",
"manufacturer",
"region",
"site",
"site_group",
"tenant",
"tenant_group",
"cluster",
"device",
"platform"]
}
hg_objects = []
if isinstance(hg_format,list):
for f in hg_format:
hg_objects = hg_objects + f.split("/")
else:
hg_objects = hg_format.split("/")
hg_objects = sorted(set(hg_objects))
for hg_object in hg_objects:
if hg_object not in allowed_objects[hg_type]:
e = (
f"Hostgroup item {hg_object} is not valid. Make sure you"
" use valid items and separate them with '/'."
)
logger.error(e)
raise HostgroupError(e)

View File

@ -13,9 +13,9 @@ from requests.exceptions import ConnectionError as RequestsConnectionError
from zabbix_utils import APIRequestError, ProcessingError, ZabbixAPI
from modules.device import PhysicalDevice
from modules.exceptions import EnvironmentVarError, HostgroupError, SyncError
from modules.exceptions import EnvironmentVarError, SyncError
from modules.logging import get_logger, set_log_levels, setup_logger
from modules.tools import convert_recordset, proxy_prepper
from modules.tools import convert_recordset, proxy_prepper, verify_hg_format
from modules.virtual_machine import VirtualMachine
try:
@ -84,24 +84,6 @@ def main(arguments):
netbox_token = environ.get("NETBOX_TOKEN")
# Set NetBox API
netbox = api(netbox_host, token=netbox_token, threading=True)
# Check if the provided Hostgroup layout is valid
hg_objects = []
if isinstance(hostgroup_format,list):
for l in hostgroup_format:
hg_objects = hg_objects + l.split("/")
else:
hg_objects = hostgroup_format.split("/")
hg_objects = sorted(set(hg_objects))
allowed_objects = [
"location",
"role",
"manufacturer",
"region",
"site",
"site_group",
"tenant",
"tenant_group",
]
# Create API call to get all custom fields which are on the device objects
try:
device_cfs = list(
@ -118,14 +100,10 @@ def main(arguments):
sys.exit(1)
for cf in device_cfs:
allowed_objects.append(cf.name)
for hg_object in hg_objects:
if hg_object not in allowed_objects:
e = (
f"Hostgroup item {hg_object} is not valid. Make sure you"
" use valid items and separate them with '/'."
)
logger.error(e)
raise HostgroupError(e)
# Check if the provided Hostgroup layout is valid
verify_hg_format(hostgroup_format, hg_type="dev", logger=logger)
verify_hg_format(vm_hostgroup_format, hg_type="vm", logger=logger)
# Set Zabbix API
try:
ssl_ctx = ssl.create_default_context()