Skip to content

Commit

Permalink
Update Netbox._get_snmp_config to revised logic
Browse files Browse the repository at this point in the history
Next in our queue is to actually these methods, but for now,
`_get_snmp_config()` was an ugly and unnecessary copy of
`get_preferred_snmp_management_profile`.  This rewrites it to defer to
the latter, while updating the callers to the new "require_write" logic.
  • Loading branch information
lunkwill42 committed Nov 20, 2023
1 parent a74d7bf commit 8690cf6
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions python/nav/models/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def read_only(self):
category=DeprecationWarning,
stacklevel=2,
)
return self._get_snmp_config('community', writeable=False)
return self._get_snmp_config('community')

@property
def read_write(self):
Expand All @@ -321,7 +321,7 @@ def read_write(self):
category=DeprecationWarning,
stacklevel=2,
)
return self._get_snmp_config('community', writeable=True)
return self._get_snmp_config('community', require_write=True)

@property
def snmp_version(self):
Expand All @@ -338,25 +338,16 @@ def snmp_version(self):
return 2
return int(value)

def _get_snmp_config(self, variable='community', writeable=None):
def _get_snmp_config(self, variable='community', require_write=False):
"""Returns SNMP profile configuration variables, preferring the profile
with the highest available SNMP version.
"""
# TODO: This method can be removed when the SNMP properties above are removed
query = Q(protocol=ManagementProfile.PROTOCOL_SNMP)
if writeable:
query = query & Q(configuration__write=True)
elif writeable is not None:
query = query & (
Q(configuration__write=False) | ~Q(configuration__has_key='write')
)
profiles = sorted(
self.profiles.filter(query),
key=lambda p: str(p.configuration.get('version') or 0),
reverse=True,
profile = self.get_preferred_snmp_management_profile(
require_write=require_write
)
if profiles:
return profiles[0].configuration.get(variable)
if profile:
return profile.configuration.get(variable)

def get_preferred_snmp_management_profile(
self, require_write=False
Expand Down

0 comments on commit 8690cf6

Please sign in to comment.