forked from sonic-net/sonic-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ubuntu
committed
Jul 19, 2022
1 parent
eacc138
commit 8829d96
Showing
2 changed files
with
26 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|