Skip to content

Commit

Permalink
Merge pull request #13 from cpaillet/snmp
Browse files Browse the repository at this point in the history
modules: snmp_config: multiple fixes
  • Loading branch information
cpaillet authored Jul 8, 2024
2 parents f2c59ec + fc80456 commit 9150238
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
40 changes: 25 additions & 15 deletions _modules/sonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Please keep in mind that in some cases, changing the configuration needs a complete reload of the
configuration, stopping the service during few seconds.
"""

# pylint: disable=C0302

import difflib
Expand Down Expand Up @@ -443,27 +444,36 @@ def snmp_config(template_name, context=None, saltenv="base", test=False):
remote_tmpfile = "/etc/sonic/tmp/snmp.yml"
__salt__["file.write"](remote_tmpfile, rendered)

if not test:
current = str(get_snmp_config())
out = _apply_snmp_config(remote_tmpfile)
if __context__["retcode"] != 0:
__salt__["file.remove"](remote_tmpfile)
__context__["retcode"] = 1
raise CommandExecutionError("Unable to push snmp configuration: {}".format(out))
new_config = str(get_snmp_config())
result = None

changes = _diff(current, new_config)
changed = bool(changes)
comment = "- Configuration pushed and loaded"
else:
current = str(get_snmp_config())
expected = str(yaml.safe_load(rendered))
changes = _diff(current, expected)

if test:
result = None if changes else True
comment = "- Configuration expected:\n{}\n- Changes needed:\n{}".format(rendered, changes)
changes = None
changed = None
comment = "- Configuration discarded:\n{}".format(rendered)

else:
if changes:
out = _apply_snmp_config(remote_tmpfile)
if __context__["retcode"] != 0:
__salt__["file.remove"](remote_tmpfile)
__context__["retcode"] = 1
raise CommandExecutionError(
"Unable to push snmp configuration: {}, change {}".format(out, changes)
)
comment = "- Configuration pushed and loaded"
else:
comment = "- No change detected"

result = True

__salt__["file.remove"](remote_tmpfile)

return {
"result": changed,
"result": result,
"dry_run": test,
"changes": changes,
"comment": comment,
Expand Down
1 change: 1 addition & 0 deletions _states/sonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Except for BGP, a change can cause to a service unavailability (docker containers need to be
rebooted when config is changed)
"""

from salt.exceptions import CommandExecutionError

__virtualname__ = "sonic"
Expand Down

0 comments on commit 9150238

Please sign in to comment.