Skip to content

Commit b12c870

Browse files
author
Shuotian Cheng
authored
[config]: Add IP subgroup commands (sonic-net#308)
config interface <interface_name> add <ip_addr> config interface <interface_name> remove <ip_addr> These two commands are used to configure IP addresses config interface <interface_name> startup config interface <interface_name> shutdown These two commands are moved under interface subgroup, and the order of the arguments are swapped to match the rest of the commands Signed-off-by: Shu0T1an ChenG <[email protected]>
1 parent 9be6e9c commit b12c870

File tree

1 file changed

+81
-33
lines changed

1 file changed

+81
-33
lines changed

config/main.py

+81-33
Original file line numberDiff line numberDiff line change
@@ -645,65 +645,113 @@ def neighbor(ipaddr_or_hostname, verbose):
645645
#
646646

647647
@cli.group()
648-
def interface():
649-
"""Interface-related configuration tasks"""
650-
pass
651-
652-
#
653-
# 'shutdown' subcommand
654-
#
655-
656-
@interface.command()
657648
@click.argument('interface_name', metavar='<interface_name>', required=True)
658-
@click.option('-v', '--verbose', is_flag=True, help="Enable verbose output")
659-
def shutdown(interface_name, verbose):
660-
"""Shut down interface"""
649+
@click.pass_context
650+
def interface(ctx, interface_name):
651+
"""Interface-related configuration tasks"""
652+
config_db = ConfigDBConnector()
653+
config_db.connect()
654+
ctx.obj = {}
655+
ctx.obj['config_db'] = config_db
661656
if get_interface_mode() == "alias":
662-
interface_name = interface_alias_to_name(interface_name)
663-
if interface_name is None:
657+
ctx.obj['interface_name'] = interface_alias_to_name(interface_name)
658+
if ctx.obj['interface_name'] is None:
664659
raise click.Abort()
665-
666-
command = "ip link set {} down".format(interface_name)
667-
run_command(command, display_cmd=verbose)
660+
else:
661+
ctx.obj['interface_name'] = interface_name
668662

669663
#
670664
# 'startup' subcommand
671665
#
672666

673667
@interface.command()
674-
@click.argument('interface_name', metavar='<interface_name>', required=True)
675-
@click.option('-v', '--verbose', is_flag=True, help="Enable verbose output")
676-
def startup(interface_name, verbose):
668+
@click.pass_context
669+
def startup(ctx):
677670
"""Start up interface"""
678-
if get_interface_mode() == "alias":
679-
interface_name = interface_alias_to_name(interface_name)
680-
if interface_name is None:
681-
raise click.Abort()
671+
config_db = ctx.obj['config_db']
672+
interface_name = ctx.obj['interface_name']
673+
674+
if interface_name.startswith("Ethernet"):
675+
config_db.set_entry("PORT", interface_name, {"admin_status": "up"})
676+
elif interface_name.startswith("PortChannel"):
677+
config_db.set_entry("PORTCHANNEL", interface_name, {"admin_status": "up"})
678+
#
679+
# 'shutdown' subcommand
680+
#
682681

682+
@interface.command()
683+
@click.pass_context
684+
def shutdown(ctx):
685+
"""Shut down interface"""
686+
config_db = ctx.obj['config_db']
687+
interface_name = ctx.obj['interface_name']
683688

684-
command = "ip link set {} up".format(interface_name)
685-
run_command(command, display_cmd=verbose)
689+
if interface_name.startswith("Ethernet"):
690+
config_db.set_entry("PORT", interface_name, {"admin_status": "down"})
691+
elif interface_name.startswith("PortChannel"):
692+
config_db.set_entry("PORTCHANNEL", interface_name, {"admin_status": "down"})
686693

687694
#
688695
# 'speed' subcommand
689696
#
690697

691698
@interface.command()
692-
@click.argument('interface_name', metavar='<interface_name>', required=True)
699+
@click.pass_context
693700
@click.argument('interface_speed', metavar='<interface_speed>', required=True)
694701
@click.option('-v', '--verbose', is_flag=True, help="Enable verbose output")
695-
def speed(interface_name, interface_speed, verbose):
702+
def speed(ctx, verbose):
696703
"""Set interface speed"""
697-
if get_interface_mode() == "alias":
698-
interface_name = interface_alias_to_name(interface_name)
699-
if interface_name is None:
700-
raise click.Abort()
704+
interface_name = ctx.obj['interface_name']
701705

702706
command = "portconfig -p {} -s {}".format(interface_name, interface_speed)
703-
if verbose: command += " -vv"
707+
if verbose:
708+
command += " -vv"
704709
run_command(command, display_cmd=verbose)
705710

706711
#
712+
# 'ip' subgroup
713+
#
714+
715+
@interface.group()
716+
@click.pass_context
717+
def ip(ctx):
718+
"""Add or remove IP address"""
719+
pass
720+
721+
#
722+
# 'add' subcommand
723+
#
724+
725+
@ip.command()
726+
@click.argument("ip_addr", metavar="<ip_addr>", required=True)
727+
@click.pass_context
728+
def add(ctx, ip_addr):
729+
"""Add an IP address towards the interface"""
730+
config_db = ctx.obj["config_db"]
731+
interface_name = ctx.obj["interface_name"]
732+
733+
if interface_name.startswith("Ethernet"):
734+
config_db.set_entry("INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
735+
elif interface_name.startswith("PortChannel"):
736+
config_db.set_entry("PORTCHANNEL_INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
737+
738+
#
739+
# 'del' subcommand
740+
#
741+
742+
@ip.command()
743+
@click.argument("ip_addr", metavar="<ip_addr>", required=True)
744+
@click.pass_context
745+
def remove(ctx, ip_addr):
746+
"""Remove an IP address from the interface"""
747+
config_db = ctx.obj["config_db"]
748+
interface_name = ctx.obj["interface_name"]
749+
750+
if interface_name.startswith("Ethernet"):
751+
config_db.set_entry("INTERFACE", (interface_name, ip_addr), None)
752+
elif interface_name.startswith("PortChannel"):
753+
config_db.set_entry("PORTCHANNEL_INTERFACE", (interface_name, ip_addr), None)
754+
#
707755
# 'acl' group
708756
#
709757

0 commit comments

Comments
 (0)