Skip to content

Commit

Permalink
Use get_snmp_session_for_profile() in SeedDB
Browse files Browse the repository at this point in the history
This replaces built-in SNMP session establishment from profiles in
SeedDB with the new utility function from nav.Snmp.profile, thereby
making it work for SNMP v1/v2c *and* v3 profiles.
  • Loading branch information
lunkwill42 committed Nov 13, 2023
1 parent af860f1 commit 3e242e8
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions python/nav/web/seeddb/page/netbox/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from nav.models.manage import NetboxInfo, ManagementProfile
from nav.Snmp import Snmp, safestring
from nav.Snmp.errors import SnmpError
from nav.Snmp.profile import get_snmp_session_for_profile
from nav import napalm
from nav.util import is_valid_ip
from nav.web.seeddb import reverse_lazy
Expand Down Expand Up @@ -223,11 +224,7 @@ def check_snmp_version(ip, profile):
"""Check if version of snmp is supported by device"""
sysobjectid = '1.3.6.1.2.1.1.2.0'
try:
snmp = Snmp(
ip,
profile.configuration.get("community"),
profile.configuration.get("version"),
)
snmp = get_snmp_session_for_profile(profile)(ip)

Check warning on line 227 in python/nav/web/seeddb/page/netbox/edit.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/seeddb/page/netbox/edit.py#L227

Added line #L227 was not covered by tests
snmp.get(sysobjectid)
except Exception: # pylint: disable=W0703
return False
Expand Down Expand Up @@ -256,16 +253,14 @@ def get_sysname(ip_address):

def get_type_id(ip_addr, profile):
"""Gets the id of the type of the ip_addr"""
netbox_type = snmp_type(
ip_addr, profile.configuration.get("community"), profile.snmp_version
)
netbox_type = snmp_type(ip_addr, profile)

Check warning on line 256 in python/nav/web/seeddb/page/netbox/edit.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/seeddb/page/netbox/edit.py#L256

Added line #L256 was not covered by tests
if netbox_type:
return netbox_type.id


def snmp_type(ip_addr, snmp_ro, snmp_version):
def snmp_type(ip_addr, profile: ManagementProfile):
"""Query ip for sysobjectid using form data"""
snmp = Snmp(ip_addr, snmp_ro, snmp_version)
snmp = get_snmp_session_for_profile(profile)(ip_addr)

Check warning on line 263 in python/nav/web/seeddb/page/netbox/edit.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/seeddb/page/netbox/edit.py#L263

Added line #L263 was not covered by tests
try:
sysobjectid = snmp.get('.1.3.6.1.2.1.1.2.0')
except SnmpError:
Expand Down

0 comments on commit 3e242e8

Please sign in to comment.