summaryrefslogtreecommitdiff
path: root/python/vyos
diff options
context:
space:
mode:
authorJernej Jakob <jernej.jakob@gmail.com>2020-05-03 13:46:47 +0200
committerJernej Jakob <jernej.jakob@gmail.com>2020-05-04 20:58:06 +0200
commit900e75e387939a1d1d4d5b0b79809b8bb2305b91 (patch)
tree6575a6da1691f712e88bf6c49bca6b0569a7f672 /python/vyos
parentc1ad2a6461fc2e767d69567be9647150c3310569 (diff)
downloadvyos-1x-900e75e387939a1d1d4d5b0b79809b8bb2305b91.tar.gz
vyos-1x-900e75e387939a1d1d4d5b0b79809b8bb2305b91.zip
validate: T2241: rewrite is_bridge_member to generic is_member
- rewrite the function to support both bridge and bonding interface types, if the type is passed it searches only that type, otherwise it searches both - move is_member check out of the deleted condition - move is_member check to intf_from_dict for interfaces that use it
Diffstat (limited to 'python/vyos')
-rw-r--r--python/vyos/configdict.py3
-rw-r--r--python/vyos/validate.py44
2 files changed, 34 insertions, 13 deletions
diff --git a/python/vyos/configdict.py b/python/vyos/configdict.py
index cd3364c94..ab72aac6c 100644
--- a/python/vyos/configdict.py
+++ b/python/vyos/configdict.py
@@ -23,6 +23,7 @@ from copy import deepcopy
from vyos import ConfigError
from vyos.ifconfig import Interface
+from vyos.validate import is_member
from vyos.util import ifname_from_config
@@ -268,6 +269,8 @@ def intf_to_dict(conf, default):
# Media Access Control (MAC) address
if conf.exists('mac'):
intf['mac'] = conf.return_value('mac')
+ # check if interface is member of a bridge
+ intf['is_bridge_member'] = is_member(conf, intf['intf'], 'bridge')
# IPv6 Duplicate Address Detection (DAD) tries
if conf.exists('ipv6 dup-addr-detect-transmits'):
diff --git a/python/vyos/validate.py b/python/vyos/validate.py
index 446f6e4ca..eb3f8bf52 100644
--- a/python/vyos/validate.py
+++ b/python/vyos/validate.py
@@ -241,25 +241,43 @@ def assert_mac(m):
if octets[:5] == (0, 0, 94, 0, 1):
raise ValueError(f'{m} is a VRRP MAC address')
-def is_bridge_member(conf, interface):
+def is_member(conf, interface, intftype=None):
"""
- Checks if passed interfaces is part of a bridge device or not.
-
- Returns a tuple:
- None -> Interface not a bridge member
- Bridge -> Interface is a member of this bridge
+ Checks if passed interface is member of other interface of specified type.
+ intftype is optional, if not passed it will search all known types
+ (currently bridge and bonding)
+
+ Returns:
+ None -> Interface is not a member
+ interface name -> Interface is a member of this interface
+ False -> interface type cannot have members
"""
ret_val = None
- old_level = conf.get_level()
+
+ if intftype not in ['bonding', 'bridge', None]:
+ raise ValueError((
+ f'unknown interface type "{intftype}" or it cannot '
+ f'have member interfaces'))
+
+ intftype = ['bonding', 'bridge'] if intftype == None else [intftype]
# set config level to root
+ old_level = conf.get_level()
conf.set_level([])
- base = ['interfaces', 'bridge']
- for bridge in conf.list_nodes(base):
- members = conf.list_nodes(base + [bridge, 'member', 'interface'])
- if interface in members:
- ret_val = bridge
- break
+
+ for it in intftype:
+ base = 'interfaces ' + it
+ for intf in conf.list_nodes(base):
+ memberintf = f'{base} {intf} member interface'
+ if conf.is_tag(memberintf):
+ if interface in conf.list_nodes(memberintf):
+ ret_val = intf
+ break
+ elif conf.is_leaf(memberintf):
+ if ( conf.exists(memberintf) and
+ interface in conf.return_values(memberintf) ):
+ ret_val = intf
+ break
old_level = conf.set_level(old_level)
return ret_val