From 08ee911b05da8e6771e4f9a7cbead0defc12e0b2 Mon Sep 17 00:00:00 2001 From: isabelmsft Date: Tue, 1 Nov 2022 07:43:01 +0000 Subject: [PATCH] Add DEVICE_METADATA table --- config/main.py | 54 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/config/main.py b/config/main.py index 59875dfcb9..95d68090e0 100644 --- a/config/main.py +++ b/config/main.py @@ -1971,10 +1971,14 @@ def override_config_db(config_db, config_input): def hostname(new_hostname): """Change device hostname without impacting the traffic.""" - config_db = ConfigDBConnector() + config_db = ValidatedConfigDBConnector(ConfigDBConnector()) config_db.connect() - config_db.mod_entry(swsscommon.CFG_DEVICE_METADATA_TABLE_NAME, 'localhost', - {'hostname': new_hostname}) + try: + config_db.mod_entry(swsscommon.CFG_DEVICE_METADATA_TABLE_NAME, 'localhost', + {'hostname': new_hostname}) + except Exception as e: + ctx = click.get_current_context() + ctx.fail("Failed to write new hostname to ConfigDB. Error: {}".format(e)) click.echo('Please note loaded setting will be lost after system reboot. To' ' preserve setting, run `config save`.') @@ -1993,16 +1997,22 @@ def synchronous_mode(sync_mode): 2. systemctl restart swss """ - if sync_mode == 'enable' or sync_mode == 'disable': - config_db = ConfigDBConnector() - config_db.connect() + if ADHOC_VALIDATION: + if sync_mode != 'enable' and sync_mode != 'disable': + raise click.BadParameter("Error: Invalid argument %s, expect either enable or disable" % sync_mode) + + config_db = ValidatedConfigDBConnector(ConfigDBConnector()) + config_db.connect() + try: config_db.mod_entry('DEVICE_METADATA' , 'localhost', {"synchronous_mode" : sync_mode}) - click.echo("""Wrote %s synchronous mode into CONFIG_DB, swss restart required to apply the configuration: \n - Option 1. config save -y \n - config reload -y \n - Option 2. systemctl restart swss""" % sync_mode) - else: - raise click.BadParameter("Error: Invalid argument %s, expect either enable or disable" % sync_mode) + except Exception as e: + ctx = click.get_current_context() + ctx.fail("Error: Invalid argument %s, expect either enable or disable" % sync_mode) + + click.echo("""Wrote %s synchronous mode into CONFIG_DB, swss restart required to apply the configuration: \n + Option 1. config save -y \n + config reload -y \n + Option 2. systemctl restart swss""" % sync_mode) # # 'yang_config_validation' command ('config yang_config_validation ...') @@ -2011,13 +2021,21 @@ def synchronous_mode(sync_mode): @click.argument('yang_config_validation', metavar='', required=True) def yang_config_validation(yang_config_validation): # Enable or disable YANG validation on updates to ConfigDB - if yang_config_validation == 'enable' or yang_config_validation == 'disable': - config_db = ConfigDBConnector() - config_db.connect() + + if ADHOC_VALIDATION: + if yang_config_validation != 'enable' and yang_config_validation != 'disable': + raise click.BadParameter("Error: Invalid argument %s, expect either enable or disable" % yang_config_validation) + + config_db = ValidatedConfigDBConnector(ConfigDBConnector()) + config_db.connect() + try: config_db.mod_entry('DEVICE_METADATA', 'localhost', {"yang_config_validation": yang_config_validation}) - click.echo("""Wrote %s yang config validation into CONFIG_DB""" % yang_config_validation) - else: - raise click.BadParameter("Error: Invalid argument %s, expect either enable or disable" % yang_config_validation) + except Exception as e: + ctx = click.get_current_context() + print(e) + ctx.fail("Error: Invalid argument %s, expect either enable or disable" % yang_config_validation) + + click.echo("""Wrote %s yang config validation into CONFIG_DB""" % yang_config_validation) # # 'portchannel' group ('config portchannel ...')