From 1009841ba1d03088106a3cc786d3b10f106cd36f Mon Sep 17 00:00:00 2001 From: Morten Brekkevold Date: Mon, 13 Nov 2023 11:51:18 +0100 Subject: [PATCH] Use get_snmp_session_for_profile() in Arnold Arnold mostly uses PortAdmin management handlers, except for when changing the ifAdminStatus value of a port. This lets the get_snmp_session_for_profile() utility function set up these SNMP sessions instead, so that even SNMPv3 sessions will be supported. Additionally, to avoid mocking hell when updating the corresponding unit tests, this inserts an optional argument for dependency injection, so a caller (i.e. the test suite) can specify which utility function to use to fetch an Snmp class. --- python/nav/arnold.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/python/nav/arnold.py b/python/nav/arnold.py index 58eb59bbc5..53f9ab8e6b 100644 --- a/python/nav/arnold.py +++ b/python/nav/arnold.py @@ -31,6 +31,7 @@ from subprocess import Popen, PIPE from collections import namedtuple from smtplib import SMTPException +from typing import Callable from IPy import IP from django.db import connection @@ -40,6 +41,7 @@ from nav.Snmp.errors import AgentError import nav.bitvector import nav.buildconf +from nav.Snmp.profile import get_snmp_session_for_profile from nav.config import find_config_file from nav.errors import GeneralException from nav.models.arnold import Identity, Event @@ -443,7 +445,11 @@ def open_port(identity, username, eventcomment=""): _logger.info("openPort: Port successfully opened") -def change_port_status(action, identity): +def change_port_status( + action, + identity, + agent_getter: Callable = get_snmp_session_for_profile, +): """Use SNMP to change status on an interface. We use ifadminstatus to enable and disable ports @@ -464,11 +470,7 @@ def change_port_status(action, identity): if not profile: raise NoReadWriteManagementProfileError - agent = nav.Snmp.Snmp( - host=netbox.ip, - community=profile.snmp_community, - version=profile.snmp_version, - ) + agent = agent_getter(profile)(host=netbox.ip) # Disable or enable based on input try: