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

add new hier_config option ('no' commands exception) #109

Open
wants to merge 2 commits into
base: 2.3-lts
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions docs/advanced-topics.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ base_options: dict = {
"idempotent_commands": [],
"negation_default_when": [],
"negation_negate_with": [],
"negation_replace": [],
}
```

Expand Down Expand Up @@ -348,6 +349,40 @@ coming soon...

coming soon...

#### negation_replace

Used to apply exceptions on 'no' commands. This option is similar to
per_line_sub but applied in the config removal pipeline.

Mandatory parameters:
```text
search - regex to match and separate by groups
replace - replaced expression used by re.sub. Can be a fixed string or a group
sorted in search
```

e.g. - ipv6 prefix-list commands:
```text
ipv6 prefix-list spine-to-leaf-v6 seq 1 permit 2801:80:3ea1:1::/64 ge 65
no ipv6 prefix-list spine-to-leaf-v6 seq 1
```

```yaml
negation_replace:
- lineage:
- re_search: ipv6 prefix-list \b.* permit
search: (ipv6 prefix-list \b.* seq \b[0-9]+).(permit\b.*)
replace: 'no \1 '
```

```text
original:
no ipv6 prefix-list spine-to-leaf-v6 seq 1 permit 2801:80:3ea1:1::/64 ge 65

result:
no ipv6 prefix-list spine-to-leaf-v6 seq 1
```

## Custom hier_config Workflows

Coming soon...
8 changes: 8 additions & 0 deletions hier_config/child.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from .base import HConfigBase
from . import text_match
import re

if TYPE_CHECKING:
from .root import HConfig
Expand Down Expand Up @@ -219,6 +220,13 @@ def negate(self) -> HConfigChild:
if self.lineage_test(rule):
return self._default()

if "negation_replace" in self.options.keys():
for rule in self.options["negation_replace"]:
if self.lineage_test(rule):
line = re.sub(rule["search"], rule["replace"], self.text)
self.text = line
return self

return self._swap_negation()

@property
Expand Down
1 change: 1 addition & 0 deletions hier_config/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"idempotent_commands": [],
"negation_default_when": [],
"negation_negate_with": [],
"negation_replace": [],
}
ios_options: dict = {
"style": "ios",
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/options_ios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,7 @@ negation_default_when: []
# - startswith: description
# use: no description
negation_negate_with: []

# Negate expressions with replace: lineage expression -> negate replace
# Use search and replace options to find and replace some line expression
negation_replace: []
Loading