@@ -320,6 +320,16 @@ def _restart_services():
320
320
log_error ("Restart {} failed with error {}" .format (service , e ))
321
321
raise
322
322
323
+ def is_ipaddress (val ):
324
+ """ Validate if an entry is a valid IP """
325
+ if not val :
326
+ return False
327
+ try :
328
+ netaddr .IPAddress (str (val ))
329
+ except :
330
+ return False
331
+ return True
332
+
323
333
# This is our main entrypoint - the main 'config' command
324
334
@click .group ()
325
335
def config ():
@@ -782,6 +792,65 @@ def del_vlan_member(ctx, vid, interface_name):
782
792
db .set_entry ('VLAN' , vlan_name , vlan )
783
793
db .set_entry ('VLAN_MEMBER' , (vlan_name , interface_name ), None )
784
794
795
+ @vlan .group ('dhcp_relay' )
796
+ @click .pass_context
797
+ def vlan_dhcp_relay (ctx ):
798
+ pass
799
+
800
+ @vlan_dhcp_relay .command ('add' )
801
+ @click .argument ('vid' , metavar = '<vid>' , required = True , type = int )
802
+ @click .argument ('dhcp_relay_destination_ip' , metavar = '<dhcp_relay_destination_ip>' , required = True )
803
+ @click .pass_context
804
+ def add_vlan_dhcp_relay_destination (ctx , vid , dhcp_relay_destination_ip ):
805
+ """ Add a destination IP address to the VLAN's DHCP relay """
806
+ if not is_ipaddress (dhcp_relay_destination_ip ):
807
+ ctx .fail ('Invalid IP address' )
808
+ db = ctx .obj ['db' ]
809
+ vlan_name = 'Vlan{}' .format (vid )
810
+ vlan = db .get_entry ('VLAN' , vlan_name )
811
+
812
+ if len (vlan ) == 0 :
813
+ ctx .fail ("{} doesn't exist" .format (vlan_name ))
814
+ dhcp_relay_dests = vlan .get ('dhcp_servers' , [])
815
+ if dhcp_relay_destination_ip in dhcp_relay_dests :
816
+ click .echo ("{} is already a DHCP relay destination for {}" .format (dhcp_relay_destination_ip , vlan_name ))
817
+ return
818
+ else :
819
+ dhcp_relay_dests .append (dhcp_relay_destination_ip )
820
+ db .set_entry ('VLAN' , vlan_name , {"dhcp_servers" :dhcp_relay_dests })
821
+ click .echo ("Added DHCP relay destination address {} to {}" .format (dhcp_relay_destination_ip , vlan_name ))
822
+ try :
823
+ click .echo ("Restarting DHCP relay service..." )
824
+ run_command ("systemctl restart dhcp_relay" , display_cmd = False )
825
+ except SystemExit as e :
826
+ ctx .fail ("Restart service dhcp_relay failed with error {}" .format (e ))
827
+
828
+ @vlan_dhcp_relay .command ('del' )
829
+ @click .argument ('vid' , metavar = '<vid>' , required = True , type = int )
830
+ @click .argument ('dhcp_relay_destination_ip' , metavar = '<dhcp_relay_destination_ip>' , required = True )
831
+ @click .pass_context
832
+ def del_vlan_dhcp_relay_destination (ctx , vid , dhcp_relay_destination_ip ):
833
+ """ Remove a destination IP address from the VLAN's DHCP relay """
834
+ if not is_ipaddress (dhcp_relay_destination_ip ):
835
+ ctx .fail ('Invalid IP address' )
836
+ db = ctx .obj ['db' ]
837
+ vlan_name = 'Vlan{}' .format (vid )
838
+ vlan = db .get_entry ('VLAN' , vlan_name )
839
+
840
+ if len (vlan ) == 0 :
841
+ ctx .fail ("{} doesn't exist" .format (vlan_name ))
842
+ dhcp_relay_dests = vlan .get ('dhcp_servers' , [])
843
+ if dhcp_relay_destination_ip in dhcp_relay_dests :
844
+ dhcp_relay_dests .remove (dhcp_relay_destination_ip )
845
+ db .set_entry ('VLAN' , vlan_name , {"dhcp_servers" :dhcp_relay_dests })
846
+ click .echo ("Removed DHCP relay destination address {} from {}" .format (dhcp_relay_destination_ip , vlan_name ))
847
+ try :
848
+ click .echo ("Restarting DHCP relay service..." )
849
+ run_command ("systemctl restart dhcp_relay" , display_cmd = False )
850
+ except SystemExit as e :
851
+ ctx .fail ("Restart service dhcp_relay failed with error {}" .format (e ))
852
+ else :
853
+ ctx .fail ("{} is not a DHCP relay destination for {}" .format (dhcp_relay_destination_ip , vlan_name ))
785
854
786
855
#
787
856
# 'bgp' group ('config bgp ...')
0 commit comments