Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modules: snmp_config: multiple fixes #13

Merged
merged 2 commits into from
Jul 8, 2024
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
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
Loading