Skip to content

Commit

Permalink
add loopback interfaces config case
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Jul 19, 2022
1 parent eacc138 commit 8829d96
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
25 changes: 11 additions & 14 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5927,36 +5927,33 @@ def loopback(ctx, redis_unix_socket_path):
@click.argument('loopback_name', metavar='<loopback_name>', 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='<loopback_name>', 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)
Expand Down
18 changes: 15 additions & 3 deletions config/validated_config_db_connector.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
import jsonpatch
from jsonpointer import JsonPointer

from generic_config_updater.generic_updater import GenericUpdater, ConfigFormat

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)
gcu_patch = jsonpatch.JsonPatch(gcu_json_input)
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)

0 comments on commit 8829d96

Please sign in to comment.