Add optional filtering of get_lcps() based on type of interface

This commit is contained in:
Pim van Pelt
2022-03-22 14:17:36 +00:00
parent 907412f49c
commit 5ebaf5e005
2 changed files with 9 additions and 5 deletions

View File

@ -13,11 +13,12 @@
# #
import logging import logging
def get_lcps(yaml): def get_lcps(yaml, interfaces=True, loopbacks=True, bridgedomains=True):
""" Returns a list of all LCPs configured in the system, or an empty list if there are none. """ """ Returns a list of LCPs configured in the system. Optionally (de)select the different
types of LCP. Return an empty list if there are none of the given type(s). """
ret = [] ret = []
if 'interfaces' in yaml: if interfaces and 'interfaces' in yaml:
for ifname, iface in yaml['interfaces'].items(): for ifname, iface in yaml['interfaces'].items():
if 'lcp' in iface: if 'lcp' in iface:
ret.append(iface['lcp']) ret.append(iface['lcp'])
@ -26,11 +27,11 @@ def get_lcps(yaml):
if 'lcp' in sub_iface: if 'lcp' in sub_iface:
ret.append(sub_iface['lcp']) ret.append(sub_iface['lcp'])
if 'loopbacks' in yaml: if loopbacks and 'loopbacks' in yaml:
for ifname, iface in yaml['loopbacks'].items(): for ifname, iface in yaml['loopbacks'].items():
if 'lcp' in iface: if 'lcp' in iface:
ret.append(iface['lcp']) ret.append(iface['lcp'])
if 'bridgedomains' in yaml: if bridgedomains and 'bridgedomains' in yaml:
for ifname, iface in yaml['bridgedomains'].items(): for ifname, iface in yaml['bridgedomains'].items():
if 'lcp' in iface: if 'lcp' in iface:
ret.append(iface['lcp']) ret.append(iface['lcp'])

View File

@ -13,6 +13,9 @@ class TestLCPMethods(unittest.TestCase):
self.assertIn("e1", lcps) self.assertIn("e1", lcps)
self.assertIn("foo", lcps) self.assertIn("foo", lcps)
self.assertIn("e2", lcps) self.assertIn("e2", lcps)
loopback_lcps = lcp.get_lcps(self.cfg, interfaces=False, bridgedomains=False)
self.assertIn("thrice", loopback_lcps)
self.assertNotIn("e1", loopback_lcps)
def test_lcp(self): def test_lcp(self):
self.assertTrue(lcp.is_unique(self.cfg, "e1")) self.assertTrue(lcp.is_unique(self.cfg, "e1"))