Skip to content

Commit

Permalink
Version 0.2.12
Browse files Browse the repository at this point in the history
  • Loading branch information
mihudec committed Sep 7, 2020
1 parent 6d056d4 commit 92689b0
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 3 deletions.
121 changes: 121 additions & 0 deletions ccutils/ccparser/CiscoIosParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,5 +641,126 @@ def section_unprocessed_lines(self, parent, check_patterns):
unprocessed_lines.append(child)
return unprocessed_lines

@property
@functools.lru_cache()
def all_ipv4_physical_addresses(self):
"""
Get all physical IPv4 addresses of device config.
Returns:
list: List IPv4 Addresses
Example::
[
"192.168.100.2",
"192.168.101.2"
]
Returns empty list (``[]``) if absent
"""
addresses = []
for interface_line in self.interface_lines:
if "l3" in interface_line.flags:
addresses.extend([x["ip_address"] for x in interface_line.ip_addresses])
return addresses

@functools.lru_cache()
def vrf_ipv4_physical_addresses(self, vrf="global"):
"""
Get all physical IPv4 addresses in particular VRF. By default returns all IP addresses from global routing table
Args:
vrf: Name of the VRF (default is `"global"`)
Returns:
list: List IPv4 Addresses
Example::
[
"192.168.100.2",
"192.168.101.2"
]
Returns empty list (``[]``) if absent
"""
addresses = []
# Filter L3 Interfaces
interfaces = [x for x in self.interface_lines if "l3" in x.flags]
# Filter VRF interfaces
if vrf == "global":
interfaces = [x for x in interfaces if x.vrf is None]
else:
interfaces = [x for x in interfaces if x.vrf == vrf]
for interface in interfaces:
addresses.extend([x["ip_address"] for x in interface.ip_addresses])
return addresses

@property
@functools.lru_cache()
def all_ipv4_standby_addresses(self):
"""
Returns:
list: List IPv4 Addresses
Example::
[
"192.168.100.1",
"192.168.101.1"
]
Returns empty list (``[]``) if absent
"""
addresses = []
# Filter L3 interfaces
interfaces = [x for x in self.interface_lines if "l3" in x.flags]
# Filter Stadnby Interfaces
interfaces = [x for x in interfaces if x.standby is not None]
for interface in interfaces:
for group in interface.standby["groups"].values():
addresses.extend([x["ip_address"] for x in group["ip_addresses"]])
return addresses

@functools.lru_cache()
def vrf_ipv4_standby_addresses(self, vrf="global"):
"""
Args:
vrf: Name of the VRF (default is `"global"`)
Returns:
list: List IPv4 Addresses
Example::
[
"192.168.100.1",
"192.168.101.1"
]
Returns empty list (``[]``) if absent
"""
addresses = []
# Filter L3 interfaces
interfaces = [x for x in self.interface_lines if "l3" in x.flags]
# Filter Stadnby Interfaces
interfaces = [x for x in interfaces if x.standby is not None]
# Filter VRF Interfaces
if vrf == "global":
interfaces = [x for x in interfaces if x.vrf is None]
else:
interfaces = [x for x in interfaces if x.vrf == vrf]

for interface in interfaces:
for group in interface.standby["groups"].values():
addresses.extend([x["ip_address"] for x in group["ip_addresses"]])
return addresses


4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
add_module_names = False

# The short X.Y version
version = '0.2.11'
version = '0.2.12'
# The full version, including alpha/beta/rc tags
release = '0.2.11-beta'
release = '0.2.12-beta'


# -- General configuration ---------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='ccutils',
version='0.2.11',
version='0.2.12',
packages=find_packages(exclude=["test", "examples"]),
url='https://github.org/mihudec/ccutils',
license='',
Expand Down
9 changes: 9 additions & 0 deletions tests/resources/cisco_ios_L3_interfaces_address_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
interface Vlan100
vrf forwarding TEST
ip address 192.168.100.2 255.255.255.0
standby 100 ip 192.168.100.1
standby 100 version 2
interface Vlan101
vrf forwarding TEST
ip address 192.168.101.2 255.255.255.0
standby 101 ip 192.168.101.1
18 changes: 18 additions & 0 deletions tests/results/cisco_ios_L3_interfaces_address_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"all_ipv4_physical_addresses": [
"192.168.100.2",
"192.168.101.2"
],
"vrf_ipv4_physical_addresses": [
"192.168.100.2",
"192.168.101.2"
],
"all_ipv4_standby_addresses": [
"192.168.100.1",
"192.168.101.1"
],
"vrf_ipv4_standby_addresses": [
"192.168.100.1",
"192.168.101.1"
]
}
40 changes: 40 additions & 0 deletions tests/test_CiscoIosParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,43 @@ def test_vrfs(self):

self.assertEqual(want, have)

def test_all_ipv4_physical_addresses(self):
test_file_base = "cisco_ios_L3_interfaces_address_test"
with self.subTest(msg=test_file_base):
config = self.get_config(test_file_name=test_file_base)
want = self.get_results(results_file_name=test_file_base)["all_ipv4_physical_addresses"]
have = config.all_ipv4_physical_addresses
jprint(have)

self.assertEqual(want, have)

def test_vrf_ipv4_physical_addresses(self):
test_file_base = "cisco_ios_L3_interfaces_address_test"
with self.subTest(msg=test_file_base):
config = self.get_config(test_file_name=test_file_base)
want = self.get_results(results_file_name=test_file_base)["vrf_ipv4_physical_addresses"]
have = config.vrf_ipv4_physical_addresses(vrf="TEST")
jprint(have)

self.assertEqual(want, have)

def test_all_ipv4_standby_addresses(self):
test_file_base = "cisco_ios_L3_interfaces_address_test"
with self.subTest(msg=test_file_base):
config = self.get_config(test_file_name=test_file_base)
want = self.get_results(results_file_name=test_file_base)["all_ipv4_standby_addresses"]
have = config.all_ipv4_standby_addresses
jprint(have)

self.assertEqual(want, have)

def test_vrf_ipv4_standby_addresses(self):
test_file_base = "cisco_ios_L3_interfaces_address_test"
with self.subTest(msg=test_file_base):
config = self.get_config(test_file_name=test_file_base)
want = self.get_results(results_file_name=test_file_base)["vrf_ipv4_standby_addresses"]
have = config.vrf_ipv4_standby_addresses(vrf="TEST")
jprint(have)

self.assertEqual(want, have)

0 comments on commit 92689b0

Please sign in to comment.