Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jan 8, 2024
1 parent b1c9eb8 commit 273f184
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
4 changes: 3 additions & 1 deletion plugins/module_utils/network/eos/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import difflib


def get_interface_number(name):
digits = ""
for char in name:
Expand Down Expand Up @@ -84,9 +85,10 @@ def numerical_sort(string_int_list):
as_int_list.sort()
return list(set(as_int_list))


def unified_diff(content1, content2, count):
"""
Provide the unified diff in context to number of lines specified with count
"""
unified_diff = difflib.unified_diff(content1, content2, n=count, lineterm='\n')
unified_diff = difflib.unified_diff(content1, content2, n=count, lineterm="\n")
return "\n".join(unified_diff)
18 changes: 12 additions & 6 deletions plugins/modules/eos_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@
NetworkConfig,
dumps,
)
from ansible_collections.arista.eos.plugins.module_utils.network.eos.utils.utils import unified_diff

from ansible_collections.arista.eos.plugins.module_utils.network.eos.eos import (
get_config,
Expand All @@ -357,6 +356,7 @@
load_config,
run_commands,
)
from ansible_collections.arista.eos.plugins.module_utils.network.eos.utils.utils import unified_diff


def get_candidate(module):
Expand Down Expand Up @@ -406,9 +406,13 @@ def main():
parents=dict(type="list", elements="str"),
before=dict(type="list", elements="str"),
after=dict(type="list", elements="str"),
context_diff=dict(type="dict", options=dict(
enable=dict(type="bool"), context_lines=dict(type="int")
)),
context_diff=dict(
type="dict",
options=dict(
enable=dict(type="bool"),
context_lines=dict(type="int"),
),
),
match=dict(
default="line",
choices=["line", "strict", "exact", "none"],
Expand Down Expand Up @@ -523,13 +527,15 @@ def main():
result["changed"] = True
if module.params["diff_against"] == "session":
if "diff" in response:
context_diff = module.params.get("context_diff")
context_diff = module.params.get("context_diff")
if context_diff and context_diff.get("enable"):
if context_diff.get("context_lines"):
count = context_diff.get("context_lines")
else:
count = max(len(candidate), len(running))
result["context_diff"] = unified_diff(candidate.split("\n"), running.split("\n"), count)
result["context_diff"] = unified_diff(
candidate.split("\n"), running.split("\n"), count
)
else:
result["diff"] = {"prepared": response["diff"]}
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
! Command: show running-config
!
interface Ethernet2
description "This is candidate config sample"
description "This is candidate config sample"
shutdown
no switchport
!
end
end
34 changes: 18 additions & 16 deletions tests/unit/modules/network/eos/test_eos_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,15 @@ def test_eos_config_src_replace(self):

def test_eos_config_context_diff(self):
src = load_fixture("eos_candidate.cfg")
args = dict(src=src,
backup=True,
context_diff=dict(enable=True),
backup_options=dict(
filename="backup.cfg",
dir_path="./"
),
)
args = dict(
src=src,
backup=True,
context_diff=dict(enable=True),
backup_options=dict(
filename="backup.cfg",
dir_path="./",
),
)
set_module_args(args)
self.conn.get_diff = MagicMock(
return_value=self.cliconf_obj.get_diff(src, self.running_config),
Expand All @@ -318,14 +319,15 @@ def test_eos_config_context_diff(self):

def test_eos_config_context_diff_lines(self):
src = load_fixture("eos_candidate.cfg")
args = dict(src=src,
backup=True,
context_diff=dict(enable=True, context_lines=10),
backup_options=dict(
filename="backup.cfg",
dir_path="./"
),
)
args = dict(
src=src,
backup=True,
context_diff=dict(enable=True, context_lines=10),
backup_options=dict(
filename="backup.cfg",
dir_path="./",
),
)
set_module_args(args)
self.conn.get_diff = MagicMock(
return_value=self.cliconf_obj.get_diff(src, self.running_config),
Expand Down

0 comments on commit 273f184

Please sign in to comment.