Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/11148-snmp-facts-improve.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- snmp_facts - simplify and improve code using standard Ansible validations (https://github.com/ansible-collections/community.general/pull/11148).
21 changes: 7 additions & 14 deletions plugins/modules/snmp_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,7 @@ def decode_mac(hexstring):

def lookup_adminstatus(int_adminstatus):
adminstatus_options = {1: "up", 2: "down", 3: "testing"}
if int_adminstatus in adminstatus_options:
return adminstatus_options[int_adminstatus]
return ""
return adminstatus_options.get(int_adminstatus, "")


def lookup_operstatus(int_operstatus):
Expand All @@ -264,9 +262,7 @@ def lookup_operstatus(int_operstatus):
6: "notPresent",
7: "lowerLayerDown",
}
if int_operstatus in operstatus_options:
return operstatus_options[int_operstatus]
return ""
return operstatus_options.get(int_operstatus, "")


def main():
Expand All @@ -288,6 +284,11 @@ def main():
["username", "level", "integrity", "authkey"],
["privacy", "privkey"],
),
required_if=[
("version", "v2", ["community"]),
("version", "v2c", ["community"]),
("version", "v3", ["username", "authkey", "level"]),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding level because if it is not passed:

  • in line 305 the validation of param privacy when level=authPriv is not enforced, but ...
  • ... in line 327, because it is a plain else, it creates a silent, undocumented default level=authPriv when level is not passed

This could lead to a blatantly wrong situation. I suppose this still configures a breaking change, but in this case it's one that should be performed, IMO.

],
supports_check_mode=True,
)

Expand All @@ -298,17 +299,9 @@ def main():
cmdGen = cmdgen.CommandGenerator()
transport_opts = {k: m_args[k] for k in ("timeout", "retries") if m_args[k] is not None}

# Verify that we receive a community when using snmp v2
if m_args["version"] in ("v2", "v2c"):
if m_args["community"] is None:
module.fail_json(msg="Community not set when using snmp version 2")

integrity_proto = None
privacy_proto = None
if m_args["version"] == "v3":
if m_args["username"] is None:
module.fail_json(msg="Username not set when using snmp version 3")

if m_args["level"] == "authPriv" and m_args["privacy"] is None:
module.fail_json(msg="Privacy algorithm not set when using authPriv")

Expand Down