summaryrefslogtreecommitdiff
path: root/python/vyos/util.py
diff options
context:
space:
mode:
authorJernej Jakob <jernej.jakob@gmail.com>2020-05-03 13:25:56 +0200
committerJernej Jakob <jernej.jakob@gmail.com>2020-05-04 20:58:06 +0200
commit9bfbb24ae649d7cedc2002f7df95a0aa0fececdc (patch)
treec4dd6c19910fc5f5b8a434e42e4864b6d1f6f873 /python/vyos/util.py
parent208414293f341923990ff3a30f1d0eddbf94b14f (diff)
downloadvyos-1x-9bfbb24ae649d7cedc2002f7df95a0aa0fececdc.tar.gz
vyos-1x-9bfbb24ae649d7cedc2002f7df95a0aa0fececdc.zip
util: T2241: add func that gets interface name from current config level
Diffstat (limited to 'python/vyos/util.py')
-rw-r--r--python/vyos/util.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py
index 92b6f7992..c708c1eb4 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -498,3 +498,27 @@ def get_half_cpus():
if cpu > 1:
cpu /= 2
return int(cpu)
+
+def ifname_from_config(conf):
+ """
+ Gets interface name with VLANs from current config level.
+ Level must be at the interface whose name we want.
+
+ Example:
+ >>> from vyos.util import ifname_from_config
+ >>> from vyos.config import Config
+ >>> conf = Config()
+ >>> conf.set_level('interfaces ethernet eth0 vif-s 1 vif-c 2')
+ >>> ifname_from_config(conf)
+ 'eth0.1.2'
+ """
+ level = conf.get_level()
+
+ # vlans
+ if level[-2] == 'vif' or level[-2] == 'vif-s':
+ return level[-3] + '.' + level[-1]
+ if level[-2] == 'vif-c':
+ return level[-5] + '.' + level[-3] + '.' + level[-1]
+
+ # no vlans
+ return level[-1]