From 1c7a6b4496440a8a9e7e2ab314564ce8e26aa378 Mon Sep 17 00:00:00 2001 From: lguohan Date: Thu, 7 Dec 2017 12:01:39 -0800 Subject: [PATCH] [configdb]: add mod_entry, rename set_config to mod_config (#25) * [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 | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/swsssdk/configdb.py b/src/swsssdk/configdb.py index 394f43b29132..d0f054ca72fa 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 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. @@ -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,9 @@ 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. + Extra entries/fields in the db which are not in the data are kept. Args: data: config data in a dictionary form { @@ -215,7 +234,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.