diff options
author | Christian Poessinger <christian@poessinger.com> | 2022-07-29 18:14:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-29 18:14:01 +0200 |
commit | 0d7ad932d2842edb8862c9f88454fa2e668871b6 (patch) | |
tree | b3a6506c22eeb1843be0068f5bb1bedaeeb87155 | |
parent | fafd18c41e3333ef3d97dbf0b50e493f40aad688 (diff) | |
parent | 5e7120a00278671a73e9762a5da5e6f6961a387d (diff) | |
download | vyos-1x-0d7ad932d2842edb8862c9f88454fa2e668871b6.tar.gz vyos-1x-0d7ad932d2842edb8862c9f88454fa2e668871b6.zip |
Merge pull request #1443 from sever-sever/T4562
vrf: T4562: Rewrite show vrf name xxx delete old script op-mode
-rw-r--r-- | op-mode-definitions/show-vrf.xml.in | 2 | ||||
-rwxr-xr-x | src/op_mode/show_vrf.py | 66 | ||||
-rwxr-xr-x | src/op_mode/vrf.py | 20 |
3 files changed, 17 insertions, 71 deletions
diff --git a/op-mode-definitions/show-vrf.xml.in b/op-mode-definitions/show-vrf.xml.in index d8d5284d7..0e0370445 100644 --- a/op-mode-definitions/show-vrf.xml.in +++ b/op-mode-definitions/show-vrf.xml.in @@ -15,7 +15,7 @@ <path>vrf name</path> </completionHelp> </properties> - <command>${vyos_op_scripts_dir}/show_vrf.py -e "$3"</command> + <command>${vyos_op_scripts_dir}/vrf.py show --name="$3"</command> <children> <leafNode name="processes"> <properties> diff --git a/src/op_mode/show_vrf.py b/src/op_mode/show_vrf.py deleted file mode 100755 index 3c7a90205..000000000 --- a/src/op_mode/show_vrf.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env python3 -# -# Copyright (C) 2020 VyOS maintainers and contributors -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License version 2 or later as -# published by the Free Software Foundation. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -import argparse -import jinja2 -from json import loads - -from vyos.util import cmd - -vrf_out_tmpl = """VRF name state mac address flags interfaces --------- ----- ----------- ----- ---------- -{%- for v in vrf %} -{{"%-16s"|format(v.ifname)}} {{ "%-8s"|format(v.operstate | lower())}} {{"%-17s"|format(v.address | lower())}} {{ v.flags|join(',')|lower()}} {{v.members|join(',')|lower()}} -{%- endfor %} - -""" - -def list_vrfs(): - command = 'ip -j -br link show type vrf' - answer = loads(cmd(command)) - return [_ for _ in answer if _] - -def list_vrf_members(vrf): - command = f'ip -j -br link show master {vrf}' - answer = loads(cmd(command)) - return [_ for _ in answer if _] - -parser = argparse.ArgumentParser() -group = parser.add_mutually_exclusive_group() -group.add_argument("-e", "--extensive", action="store_true", - help="provide detailed vrf informatio") -parser.add_argument('interface', metavar='I', type=str, nargs='?', - help='interface to display') - -args = parser.parse_args() - -if args.extensive: - data = { 'vrf': [] } - for vrf in list_vrfs(): - name = vrf['ifname'] - if args.interface and name != args.interface: - continue - - vrf['members'] = [] - for member in list_vrf_members(name): - vrf['members'].append(member['ifname']) - data['vrf'].append(vrf) - - tmpl = jinja2.Template(vrf_out_tmpl) - print(tmpl.render(data)) - -else: - print(" ".join([vrf['ifname'] for vrf in list_vrfs()])) diff --git a/src/op_mode/vrf.py b/src/op_mode/vrf.py index 63d9b5ee5..f86516786 100755 --- a/src/op_mode/vrf.py +++ b/src/op_mode/vrf.py @@ -16,6 +16,7 @@ import json import sys +import typing from tabulate import tabulate from vyos.util import cmd @@ -23,12 +24,23 @@ from vyos.util import cmd import vyos.opmode -def _get_raw_data(): +def _get_raw_data(name=None): """ - :return: list + If vrf name is not set - get all VRFs + If vrf name is set - get only this name data + If vrf name set and not found - return [] """ output = cmd('sudo ip --json --brief link show type vrf') data = json.loads(output) + if not data: + return [] + if name: + is_vrf_exists = True if [vrf for vrf in data if vrf.get('ifname') == name] else False + if is_vrf_exists: + output = cmd(f'sudo ip --json --brief link show dev {name}') + data = json.loads(output) + return data + return [] return data @@ -62,8 +74,8 @@ def _get_formatted_output(raw_data): return output -def show(raw: bool): - vrf_data = _get_raw_data() +def show(raw: bool, name: typing.Optional[str]): + vrf_data = _get_raw_data(name=name) if raw: return vrf_data else: |