Use Regex for section_config()
Environment
- netutils version: 0.2.2
When using section_config() function passing the sections is using "startswith" and will be convenient to use Regex.
Proposed Functionality
In file:
netutils/netutils/config/compliance.py
import re
line 421
if not match and line.config_line.startswith(line_start):
change to:
if not match and re.search(line_start, line.config_line):
Use Case
looking for specific access list names like:
ip access-list auto_golden_AB
having
ip access-list auto_golden_ABX
ip access-list auto_golden_AB
ip access-list auto_golden_ABC
ip access-list auto_golden_AB1
ip access-list auto_golden_AB2
we can extract the exact match defining the end of line ($):
section_config({'name':'ACL', 'ordered': True, "section":['ip access-list auto_golden_AB$']}, intended, 'cisco_nxos')
or the ones that have a number after (\d+):
section_config({'name':'ACL', 'ordered': True, "section":['ip access-list auto_golden_AB\d+']}, intended, 'cisco_nxos')
Thanks,
Would supporting full line match suffice? There are implications and I would have to think them through for allowing just a regex.
I was thinking in a more flexible way to get desired lines from the "config_parsed" object using the existing method section_config().
We can force start of line using:
if not match and re.search( "^" + line_start, line.config_line):
But it also works supporting full line match, wondering how we tell the method to do full match?
maybe in the features adding "full_match": True,:
features = [{ "name": "BGP",
"ordered": True,
"full_match": True,
"section": [
"router bgp 100"
]
}]
Thanks,
Yes, I was thinking something like if not match and re.search( "^" + line_start, line.config_line): with some checks. The problem is if we left it full regex, there is likely to be other issues for how the rest of the system works.
For full_match: True, there is other issues as well, but I think there is something in the earlier suggestion that should work for this use case.
I have another use-case for the regex feature:
want to match:
interface port-channel100
but not
interface port-channel1001
This is currently only possible when using custom function. I think this would be a good improvement!