diff options
Diffstat (limited to 'python/vyos/util.py')
-rw-r--r-- | python/vyos/util.py | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py index 17a7dda91..e2f4b8fc4 100644 --- a/python/vyos/util.py +++ b/python/vyos/util.py @@ -630,23 +630,26 @@ def find_device_file(device): return None -def dict_search(path, dict): - """ Traverse Python dictionary (dict) delimited by dot (.). +def dict_search(path, my_dict): + """ Traverse Python dictionary (my_dict) delimited by dot (.). Return value of key if found, None otherwise. - This is faster implementation then jmespath.search('foo.bar', dict)""" + This is faster implementation then jmespath.search('foo.bar', my_dict)""" + if not isinstance(my_dict, dict) or not path: + return None + parts = path.split('.') inside = parts[:-1] if not inside: - if path not in dict: + if path not in my_dict: return None - return dict[path] - c = dict + return my_dict[path] + c = my_dict for p in parts[:-1]: c = c.get(p, {}) return c.get(parts[-1], None) -def get_json_iface_options(interface): +def get_interface_config(interface): """ Returns the used encapsulation protocol for given interface. If interface does not exist, None is returned. """ @@ -655,3 +658,16 @@ def get_json_iface_options(interface): from json import loads tmp = loads(cmd(f'ip -d -j link show {interface}'))[0] return tmp + +def get_all_vrfs(): + """ Return a dictionary of all system wide known VRF instances """ + from json import loads + tmp = loads(cmd('ip -j vrf list')) + # Result is of type [{"name":"red","table":1000},{"name":"blue","table":2000}] + # so we will re-arrange it to a more nicer representation: + # {'red': {'table': 1000}, 'blue': {'table': 2000}} + data = {} + for entry in tmp: + name = entry.pop('name') + data[name] = entry + return data |