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

Use Regex for section_config() #60

Open
racsoce opened this issue Oct 6, 2021 · 4 comments
Open

Use Regex for section_config() #60

racsoce opened this issue Oct 6, 2021 · 4 comments

Comments

@racsoce
Copy link

racsoce commented Oct 6, 2021

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,

@itdependsnetworks
Copy link
Contributor

Would supporting full line match suffice? There are implications and I would have to think them through for allowing just a regex.

@racsoce
Copy link
Author

racsoce commented Oct 7, 2021

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,

@itdependsnetworks
Copy link
Contributor

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.

@andreasbourges
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants