@@ -1944,11 +1944,15 @@ def override_config_db(config_db, config_input):
1944
1944
@click .argument ('new_hostname' , metavar = '<new_hostname>' , required = True )
1945
1945
def hostname (new_hostname ):
1946
1946
"""Change device hostname without impacting the traffic."""
1947
-
1948
- config_db = ConfigDBConnector ()
1947
+ config_db = ValidatedConfigDBConnector (ConfigDBConnector ())
1949
1948
config_db .connect ()
1950
- config_db .mod_entry (swsscommon .CFG_DEVICE_METADATA_TABLE_NAME , 'localhost' ,
1951
- {'hostname' : new_hostname })
1949
+ try :
1950
+ config_db .mod_entry (swsscommon .CFG_DEVICE_METADATA_TABLE_NAME , 'localhost' ,
1951
+ {'hostname' : new_hostname })
1952
+ except ValueError as e :
1953
+ ctx = click .get_current_context ()
1954
+ ctx .fail ("Failed to write new hostname to ConfigDB. Error: {}" .format (e ))
1955
+
1952
1956
1953
1957
click .echo ('Please note loaded setting will be lost after system reboot. To'
1954
1958
' preserve setting, run `config save`.' )
@@ -1966,32 +1970,42 @@ def synchronous_mode(sync_mode):
1966
1970
config reload -y \n
1967
1971
2. systemctl restart swss
1968
1972
"""
1969
-
1970
- if sync_mode == 'enable' or sync_mode == 'disable' :
1971
- config_db = ConfigDBConnector ()
1972
- config_db .connect ()
1973
+ if ADHOC_VALIDATION :
1974
+ if sync_mode != 'enable' and sync_mode != 'disable' :
1975
+ raise click .BadParameter ("Error: Invalid argument %s, expect either enable or disable" % sync_mode )
1976
+
1977
+ config_db = ValidatedConfigDBConnector (ConfigDBConnector ())
1978
+ config_db .connect ()
1979
+ try :
1973
1980
config_db .mod_entry ('DEVICE_METADATA' , 'localhost' , {"synchronous_mode" : sync_mode })
1974
- click .echo ("""Wrote %s synchronous mode into CONFIG_DB, swss restart required to apply the configuration: \n
1981
+ except ValueError as e :
1982
+ ctx = click .get_current_context ()
1983
+ ctx .fail ("Error: Invalid argument %s, expect either enable or disable" % sync_mode )
1984
+
1985
+ click .echo ("""Wrote %s synchronous mode into CONFIG_DB, swss restart required to apply the configuration: \n
1975
1986
Option 1. config save -y \n
1976
1987
config reload -y \n
1977
1988
Option 2. systemctl restart swss""" % sync_mode )
1978
- else :
1979
- raise click .BadParameter ("Error: Invalid argument %s, expect either enable or disable" % sync_mode )
1980
1989
1981
1990
#
1982
1991
# 'yang_config_validation' command ('config yang_config_validation ...')
1983
1992
#
1984
1993
@config .command ('yang_config_validation' )
1985
1994
@click .argument ('yang_config_validation' , metavar = '<enable|disable>' , required = True )
1986
1995
def yang_config_validation (yang_config_validation ):
1987
- # Enable or disable YANG validation on updates to ConfigDB
1988
- if yang_config_validation == 'enable' or yang_config_validation == 'disable' :
1989
- config_db = ConfigDBConnector ()
1990
- config_db .connect ()
1996
+ if ADHOC_VALIDATION :
1997
+ if yang_config_validation != 'enable' and yang_config_validation != 'disable' :
1998
+ raise click .BadParameter ("Error: Invalid argument %s, expect either enable or disable" % yang_config_validation )
1999
+
2000
+ config_db = ValidatedConfigDBConnector (ConfigDBConnector ())
2001
+ config_db .connect ()
2002
+ try :
1991
2003
config_db .mod_entry ('DEVICE_METADATA' , 'localhost' , {"yang_config_validation" : yang_config_validation })
1992
- click .echo ("""Wrote %s yang config validation into CONFIG_DB""" % yang_config_validation )
1993
- else :
1994
- raise click .BadParameter ("Error: Invalid argument %s, expect either enable or disable" % yang_config_validation )
2004
+ except ValueError as e :
2005
+ ctx = click .get_current_context ()
2006
+ ctx .fail ("Error: Invalid argument %s, expect either enable or disable" % yang_config_validation )
2007
+
2008
+ click .echo ("""Wrote %s yang config validation into CONFIG_DB""" % yang_config_validation )
1995
2009
1996
2010
#
1997
2011
# 'portchannel' group ('config portchannel ...')
@@ -3193,16 +3207,24 @@ def snmp_user_secret_check(snmp_secret):
3193
3207
def add_community (db , community , string_type ):
3194
3208
""" Add snmp community string"""
3195
3209
string_type = string_type .upper ()
3196
- if not is_valid_community_type (string_type ):
3197
- sys .exit (1 )
3198
- if not snmp_community_secret_check (community ):
3199
- sys .exit (2 )
3200
- snmp_communities = db .cfgdb .get_table ("SNMP_COMMUNITY" )
3201
- if community in snmp_communities :
3202
- click .echo ("SNMP community {} is already configured" .format (community ))
3203
- sys .exit (3 )
3204
- db .cfgdb .set_entry ('SNMP_COMMUNITY' , community , {'TYPE' : string_type })
3205
- click .echo ("SNMP community {} added to configuration" .format (community ))
3210
+ if ADHOC_VALIDATION :
3211
+ if not is_valid_community_type (string_type ):
3212
+ sys .exit (1 )
3213
+ if not snmp_community_secret_check (community ):
3214
+ sys .exit (2 )
3215
+ snmp_communities = db .cfgdb .get_table ("SNMP_COMMUNITY" )
3216
+ if community in snmp_communities :
3217
+ click .echo ("SNMP community {} is already configured" .format (community ))
3218
+ sys .exit (3 )
3219
+
3220
+ config_db = ValidatedConfigDBConnector (db .cfgdb )
3221
+ try :
3222
+ config_db .set_entry ('SNMP_COMMUNITY' , community , {'TYPE' : string_type })
3223
+ click .echo ("SNMP community {} added to configuration" .format (community ))
3224
+ except ValueError as e :
3225
+ ctx = click .get_current_context ()
3226
+ ctx .fail ("SNMP community configuration failed. Error: {}" .format (e ))
3227
+
3206
3228
try :
3207
3229
click .echo ("Restarting SNMP service..." )
3208
3230
clicommon .run_command ("systemctl reset-failed snmp.service" , display_cmd = False )
@@ -3217,20 +3239,27 @@ def add_community(db, community, string_type):
3217
3239
@clicommon .pass_db
3218
3240
def del_community (db , community ):
3219
3241
""" Delete snmp community string"""
3220
- snmp_communities = db .cfgdb .get_table ("SNMP_COMMUNITY" )
3221
- if community not in snmp_communities :
3222
- click .echo ("SNMP community {} is not configured" .format (community ))
3223
- sys .exit (1 )
3224
- else :
3225
- db .cfgdb .set_entry ('SNMP_COMMUNITY' , community , None )
3242
+ if ADHOC_VALIDATION :
3243
+ snmp_communities = db .cfgdb .get_table ("SNMP_COMMUNITY" )
3244
+ if community not in snmp_communities :
3245
+ click .echo ("SNMP community {} is not configured" .format (community ))
3246
+ sys .exit (1 )
3247
+
3248
+ config_db = ValidatedConfigDBConnector (db .cfgdb )
3249
+ try :
3250
+ config_db .set_entry ('SNMP_COMMUNITY' , community , None )
3226
3251
click .echo ("SNMP community {} removed from configuration" .format (community ))
3227
- try :
3228
- click .echo ("Restarting SNMP service..." )
3229
- clicommon .run_command ("systemctl reset-failed snmp.service" , display_cmd = False )
3230
- clicommon .run_command ("systemctl restart snmp.service" , display_cmd = False )
3231
- except SystemExit as e :
3232
- click .echo ("Restart service snmp failed with error {}" .format (e ))
3233
- raise click .Abort ()
3252
+ except JsonPatchConflict as e :
3253
+ ctx = click .get_current_context ()
3254
+ ctx .fail ("SNMP community {} is not configured. Error: {}" .format (community , e ))
3255
+
3256
+ try :
3257
+ click .echo ("Restarting SNMP service..." )
3258
+ clicommon .run_command ("systemctl reset-failed snmp.service" , display_cmd = False )
3259
+ clicommon .run_command ("systemctl restart snmp.service" , display_cmd = False )
3260
+ except SystemExit as e :
3261
+ click .echo ("Restart service snmp failed with error {}" .format (e ))
3262
+ raise click .Abort ()
3234
3263
3235
3264
3236
3265
@community .command ('replace' )
@@ -3286,7 +3315,7 @@ def add_contact(db, contact, contact_email):
3286
3315
click .echo ("Contact already exists. Use sudo config snmp contact modify instead" )
3287
3316
sys .exit (1 )
3288
3317
else :
3289
- db .cfgdb .set_entry ('SNMP' , 'CONTACT' , {contact : contact_email })
3318
+ db .cfgdb .set_entry ('SNMP' , 'CONTACT' , {contact : contact_email }) # TODO: ERROR IN YANG MODEL. Contact name is not defined as key
3290
3319
click .echo ("Contact name {} and contact email {} have been added to "
3291
3320
"configuration" .format (contact , contact_email ))
3292
3321
try :
@@ -3399,19 +3428,24 @@ def location(db):
3399
3428
@clicommon .pass_db
3400
3429
def add_location (db , location ):
3401
3430
""" Add snmp location"""
3431
+ config_db = ValidatedConfigDBConnector (db .cfgdb )
3402
3432
if isinstance (location , tuple ):
3403
3433
location = " " .join (location )
3404
3434
elif isinstance (location , list ):
3405
3435
location = " " .join (location )
3406
- snmp = db . cfgdb .get_table ("SNMP" )
3436
+ snmp = config_db .get_table ("SNMP" )
3407
3437
try :
3408
3438
if snmp ['LOCATION' ]:
3409
3439
click .echo ("Location already exists" )
3410
3440
sys .exit (1 )
3411
3441
except KeyError :
3412
3442
if "LOCATION" not in snmp .keys ():
3413
- db .cfgdb .set_entry ('SNMP' , 'LOCATION' , {'Location' : location })
3414
- click .echo ("SNMP Location {} has been added to configuration" .format (location ))
3443
+ try :
3444
+ config_db .set_entry ('SNMP' , 'LOCATION' , {'Location' : location })
3445
+ click .echo ("SNMP Location {} has been added to configuration" .format (location ))
3446
+ except ValueError :
3447
+ ctx = click .get_current_context ()
3448
+ ctx .fail ("Failed to set SNMP location. Error: {}" .format (e ))
3415
3449
try :
3416
3450
click .echo ("Restarting SNMP service..." )
3417
3451
clicommon .run_command ("systemctl reset-failed snmp.service" , display_cmd = False )
@@ -3426,15 +3460,20 @@ def add_location(db, location):
3426
3460
@clicommon .pass_db
3427
3461
def delete_location (db , location ):
3428
3462
""" Delete snmp location"""
3463
+ config_db = ValidatedConfigDBConnector (db .cfgdb )
3429
3464
if isinstance (location , tuple ):
3430
3465
location = " " .join (location )
3431
3466
elif isinstance (location , list ):
3432
3467
location = " " .join (location )
3433
3468
snmp = db .cfgdb .get_table ("SNMP" )
3434
3469
try :
3435
3470
if location == snmp ['LOCATION' ]['Location' ]:
3436
- db .cfgdb .set_entry ('SNMP' , 'LOCATION' , None )
3437
- click .echo ("SNMP Location {} removed from configuration" .format (location ))
3471
+ try :
3472
+ config_db .set_entry ('SNMP' , 'LOCATION' , None )
3473
+ click .echo ("SNMP Location {} removed from configuration" .format (location ))
3474
+ except (ValueError , JsonPatchConflict ) as e :
3475
+ ctx = click .get_current_context ()
3476
+ ctx .fail ("Failed to remove SNMP location from configuration. Error: {}" .format (e ))
3438
3477
try :
3439
3478
click .echo ("Restarting SNMP service..." )
3440
3479
clicommon .run_command ("systemctl reset-failed snmp.service" , display_cmd = False )
@@ -3456,19 +3495,24 @@ def delete_location(db, location):
3456
3495
@clicommon .pass_db
3457
3496
def modify_location (db , location ):
3458
3497
""" Modify snmp location"""
3498
+ config_db = ValidatedConfigDBConnector (db .cfgdb )
3459
3499
if isinstance (location , tuple ):
3460
3500
location = " " .join (location )
3461
3501
elif isinstance (location , list ):
3462
3502
location = " " .join (location )
3463
- snmp = db . cfgdb .get_table ("SNMP" )
3503
+ snmp = config_db .get_table ("SNMP" )
3464
3504
try :
3465
3505
snmp_location = snmp ['LOCATION' ]['Location' ]
3466
3506
if location in snmp_location :
3467
3507
click .echo ("SNMP location {} already exists" .format (location ))
3468
3508
sys .exit (1 )
3469
3509
else :
3470
- db .cfgdb .mod_entry ('SNMP' , 'LOCATION' , {'Location' : location })
3471
- click .echo ("SNMP location {} modified in configuration" .format (location ))
3510
+ try :
3511
+ config_db .mod_entry ('SNMP' , 'LOCATION' , {'Location' : location })
3512
+ click .echo ("SNMP location {} modified in configuration" .format (location ))
3513
+ except ValueError as e :
3514
+ ctx = click .get_current_context ()
3515
+ ctx .fail ("Failed to modify SNMP location. Error: {}" .format (e ))
3472
3516
try :
3473
3517
click .echo ("Restarting SNMP service..." )
3474
3518
clicommon .run_command ("systemctl reset-failed snmp.service" , display_cmd = False )
0 commit comments