Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[show]: Add 'show interfaces alias' command to display port name/alias mapping #107

Merged
merged 1 commit into from
Sep 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import click
import errno
import getpass
import json
import os
import subprocess
import sys
from click_default_group import DefaultGroup
from natsort import natsorted
from tabulate import tabulate

try:
# noinspection PyPep8Naming
Expand Down Expand Up @@ -144,6 +147,31 @@ def interfaces():
"""Show details of the network interfaces"""
pass

# 'alias' subcommand ("show interfaces alias")
@interfaces.command()
@click.argument('interfacename', required=False)
def alias(interfacename):
"""Show Interface Name/Alias Mapping"""
command = 'sonic-cfggen -d --var-json "PORT"'
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)

port_dict = json.loads(p.stdout.read())

header = ['Name', 'Alias']
body = []

if interfacename is not None:
if interfacename in port_dict:
body.append([interfacename, port_dict[interfacename]['alias']])
else:
click.echo("Invalid interface name, '{0}'".format(interfacename))
return
else:
for port_name in natsorted(port_dict.keys()):
body.append([port_name, port_dict[port_name]['alias']])

click.echo(tabulate(body, header))

# 'summary' subcommand ("show interfaces summary")
@interfaces.command()
@click.argument('interfacename', required=False)
Expand Down