Skip to content

Commit

Permalink
Closes networktocode#288 - added hostname_regex function and jinja fi…
Browse files Browse the repository at this point in the history
…lter
  • Loading branch information
John Anderson authored and itdependsnetworks committed Jul 22, 2023
1 parent d95593b commit 1f0e72e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/dev/code_reference/hostname.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Hostname

::: netutils.hostname
options:
show_submodules: True
1 change: 1 addition & 0 deletions docs/user/include_jinja_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
| paloalto_panos_brace_to_set | netutils.config.conversion.paloalto_panos_brace_to_set |
| fqdn_to_ip | netutils.dns.fqdn_to_ip |
| is_fqdn_resolvable | netutils.dns.is_fqdn_resolvable |
| hostname_regex | netutils.hostname.hostname_regex |
| abbreviated_interface_name | netutils.interface.abbreviated_interface_name |
| abbreviated_interface_name_list | netutils.interface.abbreviated_interface_name_list |
| canonical_interface_name | netutils.interface.canonical_interface_name |
Expand Down
35 changes: 35 additions & 0 deletions netutils/hostname.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import re


def hostname_regex(hostname, match_regex_string):
"""Given a hostname string and regex string, return the regex match object or `None` if no match found
This is useful in two primary use cases:
1. Truthy conditional check that a hostname matches a given regex
2. Returning regex capture groups from the hostname string
Args:
hostname: String representation of the device hostname
match_regex_string: Regex string to match against
Returns:
Regex match object or None if no match found
Extamples:
>>> from netutils.hostname import hostname_regex
>>> print("South Carolina" if hostname_regex("USSCAMS07", ".+SC.+\d\d") else "Not South Carolina")
South Carolina
>>>
>>> match = hostname_regex("USSCAMS07", "([A-Z]{2})([A-Z]{2})([A-Z]{3})(\d*)")
>>> match[1]
'US'
>>> match[2]
'SC'
>>> match[3]
'AMS'
>>> match[4]
'07'
"""
return re.match(match_regex_string, hostname)
1 change: 1 addition & 0 deletions netutils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"section_config": "config.compliance.section_config",
"fqdn_to_ip": "dns.fqdn_to_ip",
"is_fqdn_resolvable": "dns.is_fqdn_resolvable",
"hostname_regex": "hostname.hostname_regex",
"interface_range_expansion": "interface.interface_range_expansion",
"interface_range_compress": "interface.interface_range_compress",
"split_interface": "interface.split_interface",
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/test_hostname.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Test for the Hostname based functions."""

from netutils import hostname


def test_truthy_happy_path_case():
match = hostname.hostname_regex("USSCAMS07", ".+SC.+\d\d")
assert bool(match) is True


def test_truthy_sad_path_case():
match = hostname.hostname_regex("USSCAMS07", "foobar")
assert bool(match) is False


def test_capture_group_happy_path_case():
match = hostname.hostname_regex("USSCAMS07", "([A-Z]{2})([A-Z]{2})([A-Z]{3})(\d*)")
assert match[1] == "US"


def test_capture_group_sad_path_case():
match = hostname.hostname_regex("USSCAMS07", "(foobar)")
assert match is None

0 comments on commit 1f0e72e

Please sign in to comment.