4
4
from swsscommon .swsscommon import ConfigDBConnector
5
5
from .validated_config_db_connector import ValidatedConfigDBConnector
6
6
from jsonpatch import JsonPatchConflict
7
+ from jsonpointer import JsonPointerException
7
8
import utilities_common .cli as clicommon
8
9
9
10
ADHOC_VALIDATION = True
@@ -498,15 +499,16 @@ def statistics(option):
498
499
def add (address , retransmit , timeout , key , auth_type , auth_port , pri , use_mgmt_vrf , source_interface ):
499
500
"""Specify a RADIUS server"""
500
501
501
- if key :
502
- if len (key ) > RADIUS_PASSKEY_MAX_LEN :
503
- click .echo ('--key: Maximum of %d chars can be configured' % RADIUS_PASSKEY_MAX_LEN )
504
- return
505
- elif not is_secret (key ):
506
- click .echo ('--key: ' + VALID_CHARS_MSG )
507
- return
502
+ if ADHOC_VALIDATION :
503
+ if key :
504
+ if len (key ) > RADIUS_PASSKEY_MAX_LEN :
505
+ click .echo ('--key: Maximum of %d chars can be configured' % RADIUS_PASSKEY_MAX_LEN )
506
+ return
507
+ elif not is_secret (key ):
508
+ click .echo ('--key: ' + VALID_CHARS_MSG )
509
+ return
508
510
509
- config_db = ConfigDBConnector ()
511
+ config_db = ValidatedConfigDBConnector ( ConfigDBConnector () )
510
512
config_db .connect ()
511
513
old_data = config_db .get_table ('RADIUS_SERVER' )
512
514
if address in old_data :
@@ -529,16 +531,24 @@ def add(address, retransmit, timeout, key, auth_type, auth_port, pri, use_mgmt_v
529
531
data ['passkey' ] = key
530
532
if use_mgmt_vrf :
531
533
data ['vrf' ] = "mgmt"
532
- if source_interface :
533
- if (source_interface .startswith ("Ethernet" ) or \
534
- source_interface .startswith ("PortChannel" ) or \
535
- source_interface .startswith ("Vlan" ) or \
536
- source_interface .startswith ("Loopback" ) or \
537
- source_interface == "eth0" ):
534
+ if ADHOC_VALIDATION :
535
+ if source_interface :
536
+ if (source_interface .startswith ("Ethernet" ) or \
537
+ source_interface .startswith ("PortChannel" ) or \
538
+ source_interface .startswith ("Vlan" ) or \
539
+ source_interface .startswith ("Loopback" ) or \
540
+ source_interface == "eth0" ):
541
+ data ['src_intf' ] = source_interface
542
+ else :
543
+ click .echo ('Not supported interface name (valid interface name: Etherent<id>/PortChannel<id>/Vlan<id>/Loopback<id>/eth0)' )
544
+ else :
545
+ if source_interface :
538
546
data ['src_intf' ] = source_interface
539
- else :
540
- click .echo ('Not supported interface name (valid interface name: Etherent<id>/PortChannel<id>/Vlan<id>/Loopback<id>/eth0)' )
541
- config_db .set_entry ('RADIUS_SERVER' , address , data )
547
+ try :
548
+ config_db .set_entry ('RADIUS_SERVER' , address , data )
549
+ except ValueError as e :
550
+ ctx = click .get_current_context ()
551
+ ctx .fail ("Invalid ConfigDB. Error: {}" .format (e ))
542
552
radius .add_command (add )
543
553
544
554
@@ -549,7 +559,11 @@ def add(address, retransmit, timeout, key, auth_type, auth_port, pri, use_mgmt_v
549
559
def delete (address ):
550
560
"""Delete a RADIUS server"""
551
561
552
- config_db = ConfigDBConnector ()
562
+ config_db = ValidatedConfigDBConnector ( ConfigDBConnector () )
553
563
config_db .connect ()
554
- config_db .set_entry ('RADIUS_SERVER' , address , None )
564
+ try :
565
+ config_db .set_entry ('RADIUS_SERVER' , address , None )
566
+ except (JsonPointerException , JsonPatchConflict ) as e :
567
+ ctx = click .get_current_context ()
568
+ ctx .fail ("Invalid ConfigDB. Error: {}" .format (e ))
555
569
radius .add_command (delete )
0 commit comments