Add prefixlist.get_network_list() + tests

This commit is contained in:
Pim van Pelt
2023-01-16 12:01:29 +00:00
parent 597981e79b
commit a274fdc2af
2 changed files with 23 additions and 0 deletions

View File

@ -36,6 +36,20 @@ def get_by_name(yaml, plname):
return None, None
def get_network_list(yaml, plname):
"""Returns a list of 0 or more ip_network elements, that represent the members
in a prefixlist of given name. Return the empty list if the prefixlist doesn't
exist"""
ret = []
plname, pl = get_by_name(yaml, plname)
if not pl:
return ret
for m in pl["members"]:
ipn = ipaddress.ip_network(m, strict=False)
ret.append(ipn)
return ret
def count(yaml, plname):
"""Return the number of IPv4 and IPv6 entries in the prefixlist.
Returns 0, 0 if it doesn't exist"""

View File

@ -75,3 +75,12 @@ class TestACLMethods(unittest.TestCase):
self.assertFalse(prefixlist.is_empty(self.cfg, "trusted"))
self.assertTrue(prefixlist.is_empty(self.cfg, "empty"))
self.assertTrue(prefixlist.is_empty(self.cfg, "pl-noexist"))
def test_get_network_list(self):
l = prefixlist.get_network_list(self.cfg, "trusted")
self.assertIsInstance(l, list)
self.assertEquals(4, len(l))
l = prefixlist.get_network_list(self.cfg, "pl-notexist")
self.assertIsInstance(l, list)
self.assertEquals(0, len(l))