From ddd29d294d957ce002f111c907585e2fac59c9b3 Mon Sep 17 00:00:00 2001
From: Joe <joe@autistici.org>
Date: Thu, 13 Mar 2014 11:37:04 +0100
Subject: [PATCH] Enabling etcd interface from the app; allowing input of db
 options from the app config file.

---
 configdb/db/interface/sa_interface.py |  4 ++--
 configdb/server/wsgiapp.py            | 12 ++++++++++--
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/configdb/db/interface/sa_interface.py b/configdb/db/interface/sa_interface.py
index 0bb05d4..bbd4ee0 100644
--- a/configdb/db/interface/sa_interface.py
+++ b/configdb/db/interface/sa_interface.py
@@ -44,7 +44,7 @@ class SqlAlchemyDbInterface(base.DbInterface):
 
     AUDIT_SUPPORT = True
 
-    def __init__(self, uri, schema, schema_dir=None):
+    def __init__(self, uri, schema, schema_dir=None, opts={}):
         self.Session = sessionmaker(autocommit=False, autoflush=False)
         Base = declarative_base()
 
@@ -53,7 +53,7 @@ class SqlAlchemyDbInterface(base.DbInterface):
         self._schema_dir = schema_dir # unused, meant for caching
         self._load_schema()
 
-        self.engine = create_engine(uri, pool_recycle=1800)
+        self.engine = create_engine(uri, pool_recycle=1800, **opts)
         self.Session.configure(bind=self.engine)
         Base.metadata.create_all(self.engine)
 
diff --git a/configdb/server/wsgiapp.py b/configdb/server/wsgiapp.py
index 9fd3ffc..8a4a614 100644
--- a/configdb/server/wsgiapp.py
+++ b/configdb/server/wsgiapp.py
@@ -215,11 +215,12 @@ def make_app(config={}):
     # Initialize the database interface.
     schema_obj = schema.Schema(app.config['SCHEMA_JSON'])
     db_driver = app.config.get('DB_DRIVER', 'sqlalchemy')
+    db_opts = app.config.get('DB_OPTIONS', {})
     if db_driver == 'sqlalchemy':
         from configdb.db.interface import sa_interface
         db = sa_interface.SqlAlchemyDbInterface(
             app.config.get('DB_URI', 'sqlite:///:memory:'),
-            schema_obj)
+            schema_obj, opts=db_opts)
     elif db_driver == 'leveldb':
         from configdb.db.interface import leveldb_interface
         db = leveldb_interface.LevelDbInterface(
@@ -230,8 +231,15 @@ def make_app(config={}):
         if not 'ZK_HOSTS' in app.config:
             raise Exception('you need to define ZK_HOSTS list to use zookeeper as db backend')
         db = zookeeper_interface.ZookeeperInterface(app.config['ZK_HOSTS'], schema_obj, app.config['DB_URI'])
+    elif db_driver == 'etcd':
+        from configdb.db.interface import etcd_interface
+        if not 'ETCD_URL' in app.config:
+            raise Exception(
+                'You need to define an ETCD_URL to use the etcd backend')
+        db = etcd_interface.EtcdInterface(
+            app.config['ETCD_URL'], schema_obj, **db_opts)
     else:
-        raise Exception('DB_DRIVER not one of "sqlalchemy" or "leveldb"')
+        raise Exception('DB_DRIVER not supported: %s' % db_driver)
 
     app.api = db_api.AdmDbApi(schema_obj, db)
 
-- 
GitLab