Fixed several bugs, set default interface for VM to agent, fixed several linter errors.

This commit is contained in:
TheNetworkGuy
2024-10-30 12:23:15 +01:00
parent bff34a8e38
commit 66f24e6891
6 changed files with 63 additions and 36 deletions

View File

@@ -1,9 +1,12 @@
#!/usr/bin/env python3
# pylint: disable=duplicate-code
"""Module that hosts all functions for virtual machine processing"""
from os import sys
from modules.device import PhysicalDevice
from modules.hostgroups import Hostgroup
from modules.exceptions import TemplateError
from modules.interface import ZabbixInterface
from modules.exceptions import TemplateError, InterfaceConfigError, SyncInventoryError
try:
from config import (
traverse_site_groups,
@@ -16,12 +19,16 @@ except ModuleNotFoundError:
class VirtualMachine(PhysicalDevice):
"""Model for virtual machines"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.hostgroup = None
self.zbx_template_names = None
def set_hostgroup(self, hg_format, nb_site_groups, nb_regions):
"""Set the hostgroup for this device"""
# Create new Hostgroup instance
hg = Hostgroup("vm", self.nb, self.nb_api_version,
traverse_site_groups, traverse_regions,
nb_site_groups, nb_regions)
hg = Hostgroup("vm", self.nb, self.nb_api_version)
hg.set_nesting(traverse_site_groups, traverse_regions, nb_site_groups, nb_regions)
# Generate hostgroup based on hostgroup format
self.hostgroup = hg.generate(hg_format)
@@ -36,11 +43,24 @@ class VirtualMachine(PhysicalDevice):
self.logger.warning(e)
return True
def set_template(self, prefer_config_context=None, overrule_custom=None):
def setInterfaceDetails(self): # pylint: disable=invalid-name
"""
This wrapper takes the original function and
overwrites it with the set_vm_template function.
This function (set_template) is used by several
other functions such as the consistency check in the parent class.
Overwrites device function to select an agent interface type by default
Agent type interfaces are more likely to be used with VMs then SNMP
"""
self.set_vm_template()
try:
# Initiate interface class
interface = ZabbixInterface(self.nb.config_context, self.ip)
# Check if Netbox has device context.
# If not fall back to old config.
if interface.get_context():
# If device is SNMP type, add aditional information.
if interface.interface["type"] == 2:
interface.set_snmp()
else:
interface.set_default_agent()
return [interface.interface]
except InterfaceConfigError as e:
message = f"{self.name}: {e}"
self.logger.warning(message)
raise SyncInventoryError(message) from e