From 8829d96e4569920d19eec6259aaa119a8ec413c2 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Tue, 19 Jul 2022 09:27:19 +0000 Subject: [PATCH] add loopback interfaces config case --- config/main.py | 25 +++++++++++-------------- config/validated_config_db_connector.py | 18 +++++++++++++++--- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/config/main.py b/config/main.py index 701f4cc88c..920affee75 100644 --- a/config/main.py +++ b/config/main.py @@ -5927,36 +5927,33 @@ def loopback(ctx, redis_unix_socket_path): @click.argument('loopback_name', metavar='', required=True) @click.pass_context def add_loopback(ctx, loopback_name): - config_db = ctx.obj['db'] - if is_loopback_name_valid(loopback_name) is False: - ctx.fail("{} is invalid, name should have prefix '{}' and suffix '{}' " - .format(loopback_name, CFG_LOOPBACK_PREFIX, CFG_LOOPBACK_NO)) + config_db = ValidatedConfigDBConnector(ctx.obj['db']) lo_intfs = [k for k, v in config_db.get_table('LOOPBACK_INTERFACE').items() if type(k) != tuple] if loopback_name in lo_intfs: ctx.fail("{} already exists".format(loopback_name)) - - config_db.set_entry('LOOPBACK_INTERFACE', loopback_name, {"NULL" : "NULL"}) + + try: + config_db.set_entry('LOOPBACK_INTERFACE', loopback_name, {"NULL" : "NULL"}) + except ValueError: + ctx.fail("{} is invalid, name should have prefix '{}' and suffix'{}' ".format(loopback_name, CFG_LOOPBACK_PREFIX, CFG_LOOPBACK_NO)) @loopback.command('del') @click.argument('loopback_name', metavar='', required=True) @click.pass_context def del_loopback(ctx, loopback_name): - config_db = ctx.obj['db'] - if is_loopback_name_valid(loopback_name) is False: - ctx.fail("{} is invalid, name should have prefix '{}' and suffix '{}' " - .format(loopback_name, CFG_LOOPBACK_PREFIX, CFG_LOOPBACK_NO)) + config_db = ValidatedConfigDBConnector(ctx.obj['db']) lo_config_db = config_db.get_table('LOOPBACK_INTERFACE') - lo_intfs = [k for k, v in lo_config_db.items() if type(k) != tuple] - if loopback_name not in lo_intfs: - ctx.fail("{} does not exists".format(loopback_name)) ips = [ k[1] for k in lo_config_db if type(k) == tuple and k[0] == loopback_name ] for ip in ips: config_db.set_entry('LOOPBACK_INTERFACE', (loopback_name, ip), None) - config_db.set_entry('LOOPBACK_INTERFACE', loopback_name, None) + try: + config_db.set_entry('LOOPBACK_INTERFACE', loopback_name, None) + except JsonPatchConflict: + ctx.fail("{} does not exist".format(loopback_name)) @config.group(cls=clicommon.AbbreviationGroup) diff --git a/config/validated_config_db_connector.py b/config/validated_config_db_connector.py index d6006c8d65..4394ad40c6 100644 --- a/config/validated_config_db_connector.py +++ b/config/validated_config_db_connector.py @@ -1,4 +1,5 @@ import jsonpatch +from jsonpointer import JsonPointer from generic_config_updater.generic_updater import GenericUpdater, ConfigFormat @@ -6,16 +7,26 @@ def ValidatedConfigDBConnector(config_db_connector): config_db_connector.set_entry = validated_set_entry return config_db_connector +def make_path_value_jsonpatch_compatible(table, key, value): + if type(key) == tuple: + path = JsonPointer.from_parts([table, '|'.join(key)]).path + else: + path = "/{}/{}".format(table, key) + if value == {"NULL" : "NULL"}: + value = {} + return path, value + def validated_set_entry(table, key, value): - if value: + if value is not None: op = "add" else: op = "remove" - path = "/{}/{}".format(table, key) + path, value = make_path_value_jsonpatch_compatible(table, key, value) + gcu_json_input = [] gcu_json = {"op": "{}".format(op), "path": "{}".format(path)} - if value: + if op is "add": gcu_json["value"] = value gcu_json_input.append(gcu_json) @@ -23,3 +34,4 @@ def validated_set_entry(table, key, value): format = ConfigFormat.CONFIGDB.name config_format = ConfigFormat[format.upper()] GenericUpdater().apply_patch(patch=gcu_patch, config_format=config_format, verbose=False, dry_run=False, ignore_non_yang_tables=False, ignore_paths=None) +