diff options
Diffstat (limited to 'scripts/rpki')
-rw-r--r-- | scripts/rpki/left_right.py | 22 | ||||
-rw-r--r-- | scripts/rpki/sql.py | 6 |
2 files changed, 27 insertions, 1 deletions
diff --git a/scripts/rpki/left_right.py b/scripts/rpki/left_right.py index 62fc69ec..f04deb2e 100644 --- a/scripts/rpki/left_right.py +++ b/scripts/rpki/left_right.py @@ -195,6 +195,28 @@ class repository_elt(base_elt, rpki.sql.sql_persistant): element_name = "repository" attributes = ("action", "type", "self_id", "repository_id", "bsc_link", "peer_contact") + sql_id_name = "repos_id" + sql_select_cmd = """SELECT self_id, bsc_id, repos_id, uri, ta FROM repos WHERE self_id = %(self_id)s AND bsc_id = %(bsc_id)s""" + sql_insert_cmd = """INSERT repos (uri, ta, bsc_id, self_id) VALUES (%(uri)s, %(ta)s, %(bsc_id)s, %(self_id)s)""" + sql_update_cmd = """UPDATE repos SET uri = %(uri)s, ta = %(ta)s, bsc_id = %(bsc_id)s, self_id = %(self_id)s WHERE repos_id = %(repos_id)s""" + sql_delete_cmd = """DELETE FROM repos WHERE repos_id = %(repos_id)s""" + + def sql_decode(self, sql_parent, self_id, bsc_id, repos_id, uri, ta): + self.self_obj = sql_parent + self.bsc_obj = self.self_obj.bscs[bsc_id] + self.self_id = self_id + self.bsc_link = bsc_id + self.repository_id = repos_id + self.peer_contact = uri + self.peer_ta = rpki.x509.X509(DER=ta) + + def sql_encode(self): + return { "self_id" : self.self_obj.self_id, + "bsc_id" : self.bsc_link.bsc_id, + "repos_id" : self.repository_id, + "uri" : self.peer_contact, + "ta" : self.peer_ta.get_DER() } + peer_ta = None def startElement(self, stack, name, attrs): diff --git a/scripts/rpki/sql.py b/scripts/rpki/sql.py index c7c0fe0d..7064bd2b 100644 --- a/scripts/rpki/sql.py +++ b/scripts/rpki/sql.py @@ -64,7 +64,9 @@ class sql_persistant(object): def sql_fetch(cls, db, cur=None, select_dict=None, sql_parent=None): """Fetch rows from SQL based on a canned query and a set of keyword arguments, and instantiate them as objects, returning a - list of the instantiated objects. + list of the instantiated objects. If the object definition + indicates an index field (sql_id_name), this method instead + returns as dictionary using the index field as the key. This is a class method because in general we don't even know how many matches the SQL lookup will return until after we've @@ -83,6 +85,8 @@ class sql_persistant(object): self_dict = self.sql_encode() for k,v in self.sql_children: setattr(self, k, v.sql_fetch(db, cur, self_dict, self)) + if cls.sql_id_name is not None: + result = dict((getattr(i, cls.sql_id_name), i) for i in result) return result def sql_store(self, db, cur=None): |