From 80a96d620bdf47ba0b3e109f3789a9777a2eb6ba Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Thu, 14 Feb 2019 23:57:59 +0100 Subject: [vyos.configtree] T1248: add a function for node copying Also improve sanity checks in the rename function and add unit tests for copy and rename. --- python/vyos/configtree.py | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'python') diff --git a/python/vyos/configtree.py b/python/vyos/configtree.py index 39fe41669..1f71ddd7c 100644 --- a/python/vyos/configtree.py +++ b/python/vyos/configtree.py @@ -118,6 +118,10 @@ class ConfigTree(object): self.__rename.argtypes = [c_void_p, c_char_p, c_char_p] self.__rename.restype = c_int + self.__copy = self.__lib.copy_node + self.__copy.argtypes = [c_void_p, c_char_p, c_char_p] + self.__copy.restype = c_int + self.__set_replace_value = self.__lib.set_replace_value self.__set_replace_value.argtypes = [c_void_p, c_char_p, c_char_p] self.__set_replace_value.restype = c_int @@ -201,12 +205,31 @@ class ConfigTree(object): self.__delete_value(self.__config, path_str, value.encode()) - def rename(self, path, newname): + def rename(self, path, new_name): check_path(path) path_str = " ".join(map(str, path)).encode() - newname_str = newname.encode() - - self.__rename(self.__config, path_str, newname_str) + newname_str = new_name.encode() + + # Check if a node with intended new name already exists + new_path = path[:-1] + [new_name] + if self.exists(new_path): + raise ConfigTreeError() + res = self.__rename(self.__config, path_str, newname_str) + if (res != 0): + raise ConfigTreeError("Path [{}] doesn't exist".format(oldpath)) + + def copy(self, old_path, new_path): + check_path(old_path) + check_path(new_path) + oldpath_str = " ".join(map(str, old_path)).encode() + newpath_str = " ".join(map(str, new_path)).encode() + + # Check if a node with intended new name already exists + if self.exists(new_path): + raise ConfigTreeError() + res = self.__copy(self.__config, oldpath_str, newpath_str) + if (res != 0): + raise ConfigTreeError("Path [{}] doesn't exist".format(oldpath)) def exists(self, path): check_path(path) -- cgit v1.2.3