Skip to content

Commit

Permalink
Use get_snmp_session_for_profile() in Arnold
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lunkwill42 committed Nov 16, 2023
1 parent b9fa1ce commit 1009841
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions python/nav/arnold.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 1009841

Please sign in to comment.