From 205351e93de80dc283f5fda97dbf400f35af2dea Mon Sep 17 00:00:00 2001 From: Guohan Lu Date: Thu, 7 Dec 2017 19:30:27 +0000 Subject: [PATCH 1/3] [configdb]: add mod_entry, rename set_config to mod_config mod_entry will not delete extra data in the db. change the set_config to mod_config so that mod_config does not delete extra data in the db --- src/swsssdk/configdb.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/swsssdk/configdb.py b/src/swsssdk/configdb.py index 394f43b29..5fbfaa65a 100644 --- a/src/swsssdk/configdb.py +++ b/src/swsssdk/configdb.py @@ -5,7 +5,7 @@ # Write to config DB config_db = ConfigDBConnector() config_db.connect() - config_db.set_entry('BGP_NEIGHBOR', '10.0.0.1', { + config_db.mod_entry('BGP_NEIGHBOR', '10.0.0.1', { 'admin_status': state }) @@ -144,6 +144,7 @@ def deserialize_key(key): def set_entry(self, table, key, data): """Write a table entry to config db. + Remove all fields in the db which are not in the data. Args: table: Table name. key: Key of table entry, or a tuple of keys if it is a multi-key table. @@ -164,6 +165,23 @@ def set_entry(self, table, key, data): k = k + '@' client.hdel(_hash, self.serialize_key(k)) + def mod_entry(self, table, key, data): + """Modify a table entry to config db. + Args: + table: Table name. + key: Key of table entry, or a tuple of keys if it is a multi-key table. + data: Table row data in a form of dictionary {'column_key': 'value', ...}. + Pass {} as data will create an entry with no column if not already existed. + Pass None as data will delete the entry. + """ + key = self.serialize_key(key) + client = self.redis_clients[self.CONFIG_DB] + _hash = '{}{}{}'.format(table.upper(), self.TABLE_NAME_SEPARATOR, key) + if data == None: + client.delete(_hash) + else: + client.hmset(_hash, self.__typed_to_raw(data)) + def get_entry(self, table, key): """Read a table entry from config db. Args: @@ -202,8 +220,8 @@ def get_table(self, table): pass #Ignore non table-formated redis entries return data - def set_config(self, data): - """Write multiple tables into config db. + def mod_config(self, data): + """Write multiple tables into config db. Existing entries in the db are kept. Args: data: config data in a dictionary form { @@ -215,7 +233,7 @@ def set_config(self, data): for table_name in data: table_data = data[table_name] for key in table_data: - self.set_entry(table_name, key, table_data[key]) + self.mod_entry(table_name, key, table_data[key]) def get_config(self): """Read all config data. From f03d58c80dafc87e971430466c7e9eb89f2bb3f5 Mon Sep 17 00:00:00 2001 From: Guohan Lu Date: Thu, 7 Dec 2017 19:44:51 +0000 Subject: [PATCH 2/3] update function description --- src/swsssdk/configdb.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/swsssdk/configdb.py b/src/swsssdk/configdb.py index 5fbfaa65a..4b441eadb 100644 --- a/src/swsssdk/configdb.py +++ b/src/swsssdk/configdb.py @@ -144,7 +144,7 @@ def deserialize_key(key): def set_entry(self, table, key, data): """Write a table entry to config db. - Remove all fields in the db which are not in the data. + Remove extra fields in the db which are not in the data. Args: table: Table name. key: Key of table entry, or a tuple of keys if it is a multi-key table. @@ -221,7 +221,8 @@ def get_table(self, table): return data def mod_config(self, data): - """Write multiple tables into config db. Existing entries in the db are kept. + """Write multiple tables into config db. + Extra entries/attributes in the db which are not in the data are kept. Args: data: config data in a dictionary form { From 1b08b27b922eda974281efc46eae7b57cac78351 Mon Sep 17 00:00:00 2001 From: Guohan Lu Date: Thu, 7 Dec 2017 19:49:59 +0000 Subject: [PATCH 3/3] rename to attributes to fields --- src/swsssdk/configdb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/swsssdk/configdb.py b/src/swsssdk/configdb.py index 4b441eadb..d0f054ca7 100644 --- a/src/swsssdk/configdb.py +++ b/src/swsssdk/configdb.py @@ -222,7 +222,7 @@ def get_table(self, table): def mod_config(self, data): """Write multiple tables into config db. - Extra entries/attributes in the db which are not in the data are kept. + Extra entries/fields in the db which are not in the data are kept. Args: data: config data in a dictionary form {