Skip to content

Commit

Permalink
Merge pull request #23 from angely-dev/master
Browse files Browse the repository at this point in the history
Add contextual diff computation mode
  • Loading branch information
codingnetworksb authored Apr 6, 2024
2 parents 29f33c1 + 4877340 commit 4306aab
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ ignore_missing_imports = True

[mypy-pytest]
ignore_missing_imports = True

[mypy-diffplus]
ignore_missing_imports = True
13 changes: 13 additions & 0 deletions napalm_huawei_vrp/huawei_vrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import napalm.base.constants as C

from datetime import datetime
from diffplus import IndentedConfig, IncrementalDiff
from napalm.base import NetworkDriver
from napalm.base.netmiko_helpers import netmiko_args
from napalm.base.exceptions import (
Expand Down Expand Up @@ -137,6 +138,9 @@ def __init__(self, hostname, username, password, timeout=60, optional_args=None)
# Track whether 'file prompt quiet' is known to be configured
self.prompt_quiet_configured = None

# Contextual diff computation mode
self.contextual_diff = optional_args.get("contextual_diff", False)

# verified
def open(self):
"""Open a connection to the device."""
Expand Down Expand Up @@ -471,6 +475,8 @@ def compare_config(self):
"""Compare candidate config with running."""
if self.loaded:
if not self.replace:
if self.contextual_diff:
return self._get_contextual_diff()
return self._get_merge_diff()
# return self.merge_candidate
diff = self._get_diff(self.replace_file.split("/")[-1])
Expand Down Expand Up @@ -2071,6 +2077,13 @@ def _get_merge_diff(self):
diff.append(line)
return "\n".join(diff)

def _get_contextual_diff(self):
running_config = self.get_config(retrieve="running")["running"]
running_config = IndentedConfig(running_config, sanitize=True)
merge_candidate = IndentedConfig(self.merge_candidate, sanitize=True)
diff = IncrementalDiff(merge_candidate, running_config)
return str(diff)

def _get_diff(self, filename=None):
"""Get a diff between running config and a proposed file."""
if filename is None:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
napalm>=3.0.0
diffplus

0 comments on commit 4306aab

Please sign in to comment.