@@ -645,65 +645,113 @@ def neighbor(ipaddr_or_hostname, verbose):
645
645
#
646
646
647
647
@cli .group ()
648
- def interface ():
649
- """Interface-related configuration tasks"""
650
- pass
651
-
652
- #
653
- # 'shutdown' subcommand
654
- #
655
-
656
- @interface .command ()
657
648
@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
661
656
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 :
664
659
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
668
662
669
663
#
670
664
# 'startup' subcommand
671
665
#
672
666
673
667
@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 ):
677
670
"""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
+ #
682
681
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' ]
683
688
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" })
686
693
687
694
#
688
695
# 'speed' subcommand
689
696
#
690
697
691
698
@interface .command ()
692
- @click .argument ( 'interface_name' , metavar = '<interface_name>' , required = True )
699
+ @click .pass_context
693
700
@click .argument ('interface_speed' , metavar = '<interface_speed>' , required = True )
694
701
@click .option ('-v' , '--verbose' , is_flag = True , help = "Enable verbose output" )
695
- def speed (interface_name , interface_speed , verbose ):
702
+ def speed (ctx , verbose ):
696
703
"""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' ]
701
705
702
706
command = "portconfig -p {} -s {}" .format (interface_name , interface_speed )
703
- if verbose : command += " -vv"
707
+ if verbose :
708
+ command += " -vv"
704
709
run_command (command , display_cmd = verbose )
705
710
706
711
#
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
+ #
707
755
# 'acl' group
708
756
#
709
757
0 commit comments