]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Move simpledb to db/anydbm just so we are using the new API.
authorAngus Salkeld <asalkeld@redhat.com>
Fri, 30 Mar 2012 10:28:37 +0000 (21:28 +1100)
committerAngus Salkeld <asalkeld@redhat.com>
Fri, 30 Mar 2012 10:28:37 +0000 (21:28 +1100)
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
bin/heat-engine
heat/common/config.py
heat/common/utils.py
heat/db/anydbm/__init__.py [new file with mode: 0644]
heat/db/anydbm/api.py [moved from heat/engine/simpledb.py with 68% similarity]
heat/db/api.py
heat/engine/api/v1/events.py
heat/engine/api/v1/stacks.py
heat/engine/resources.py

index df0176b595893caac32cf53a152bff9e99f34e9b..cb9bb8abcbcb9db1b481e0035ed79cb32895529d 100755 (executable)
@@ -40,7 +40,7 @@ from heat.common import wsgi
 
 if __name__ == '__main__':
     try:
-        conf = config.HeatConfigOpts()
+        conf = config.HeatEngineConfigOpts()
         conf()
 
         app = config.load_paste_app(conf)
index 5f4466375e67554e39c26964a681d692e6e8a13c..0928b4bad9924d074b8d9e5f2a282d175cf73465 100644 (file)
@@ -47,13 +47,17 @@ class HeatConfigOpts(cfg.CommonConfigOpts):
             **kwargs)
 
 
-class HeatCacheConfigOpts(HeatConfigOpts):
+class HeatEngineConfigOpts(HeatConfigOpts):
+
+    db_opts = [
+    cfg.StrOpt('db_backend', default='heat.db.anydbm.api', help='The backend to use for db'),
+    ]
 
     def __init__(self, **kwargs):
         config_files = cfg.find_config_files(project='heat',
-                                             prog='heat-cache')
-        super(HeatCacheConfigOpts, self).__init__(config_files, **kwargs)
-
+                                             prog='heat-engine')
+        super(HeatEngineConfigOpts, self).__init__(config_files, **kwargs)
+        self.register_cli_opts(self.db_opts)
 
 def setup_logging(conf):
     """
index 4bd3973f0b731e5803ae378133aa8d1623a4b377..04344264b485867d276ca18776f848c719a5ecd1 100644 (file)
@@ -25,6 +25,55 @@ import uuid
 
 from heat.common import exception
 
+def import_class(import_str):
+    """Returns a class from a string including module and class."""
+    mod_str, _sep, class_str = import_str.rpartition('.')
+    try:
+        __import__(mod_str)
+        return getattr(sys.modules[mod_str], class_str)
+    except (ImportError, ValueError, AttributeError), exc:
+        #LOG.debug(_('Inner Exception: %s'), exc)
+        raise exception.ClassNotFound(class_name=class_str, exception=exc)
+
+
+def import_object(import_str):
+    """Returns an object including a module or module and class."""
+    try:
+        __import__(import_str)
+        return sys.modules[import_str]
+    except ImportError:
+        cls = import_class(import_str)
+        return cls()
+
+class LazyPluggable(object):
+    """A pluggable backend loaded lazily based on some value."""
+
+    def __init__(self, pivot, **backends):
+        self.__backends = backends
+        self.__pivot = pivot
+        self.__backend = None
+
+    def __get_backend(self):
+        if not self.__backend:
+            backend_name = FLAGS[self.__pivot]
+            if backend_name not in self.__backends:
+                raise exception.Error(_('Invalid backend: %s') % backend_name)
+
+            backend = self.__backends[backend_name]
+            if isinstance(backend, tuple):
+                name = backend[0]
+                fromlist = backend[1]
+            else:
+                name = backend
+                fromlist = backend
+
+            self.__backend = __import__(name, None, None, fromlist)
+            #LOG.debug(_('backend %s'), self.__backend)
+        return self.__backend
+
+    def __getattr__(self, key):
+        backend = self.__get_backend()
+        return getattr(backend, key)
 
 def chunkreadable(iter, chunk_size=65536):
     """
diff --git a/heat/db/anydbm/__init__.py b/heat/db/anydbm/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
similarity index 68%
rename from heat/engine/simpledb.py
rename to heat/db/anydbm/api.py
index a3907991496c62b3a251209e0cb2a263f57ea90d..5f1be480cadc3831898786f8276140e7e68ef629 100644 (file)
 import anydbm
 import json
 
-def event_append(event):
-    '''
-    EventId    The unique ID of this event.
-    Timestamp  Time the status was updated.
-    '''
-    name = event['StackName']
-    d = anydbm.open('/var/lib/heat/%s.events.db' % name, 'c')
-    if d.has_key('lastid'):
-        newid = int(d['lastid']) + 1
-    else:
-        newid = 1
-    event['EventId'] = '%d' % newid
-    d['lastid'] = event['EventId']
-    d[event['EventId']] = json.dumps(event)
+def raw_template_get(context, template_id):
+    return 'test return value'
 
-    d.close()
+def raw_template_get_all(context):
+    pass
+
+def raw_template_create(context, values):
+    pass
+
+
+def parsed_template_get(context, template_id):
+    pass
+
+def parsed_template_get_all(context):
+    pass
+
+def parsed_template_create(context, values):
+    pass
+
+
+def state_get(context, state_id):
+    pass
 
+def state_get_all(context):
+    pass
 
-def events_get(stack_id):
+def state_create(context, values):
+    pass
+
+
+def event_get(context, event_id):
+    pass
+
+def event_get_all(context):
+    pass
+
+def event_get_all_by_stack(context, stack_id):
     events = {'events': []}
     try:
         d = anydbm.open('/var/lib/heat/%s.events.db' % stack_id, 'r')
@@ -48,3 +66,20 @@ def events_get(stack_id):
     d.close()
     return events
 
+def event_create(context, event):
+    '''
+    EventId    The unique ID of this event.
+    Timestamp  Time the status was updated.
+    '''
+    name = event['StackName']
+    d = anydbm.open('/var/lib/heat/%s.events.db' % name, 'c')
+    if d.has_key('lastid'):
+        newid = int(d['lastid']) + 1
+    else:
+        newid = 1
+    event['EventId'] = '%d' % newid
+    d['lastid'] = event['EventId']
+    d[event['EventId']] = json.dumps(event)
+
+    d.close()
+
index 639fae2f64e791ed6e0691c741ee4fbe3cf878df..ad3d7b5643c244d14b8da3958e68d6b888ddd2b6 100644 (file)
@@ -26,20 +26,12 @@ The underlying driver is loaded as a :class:`LazyPluggable`. SQLAlchemy is
 currently the only supported backend.
 '''
 
-from nova import flags
-from nova.openstack.common import cfg
-from nova import utils
-
-
-db_opts = [
-    cfg.StrOpt('db_backend', default='db', help='The backend to use for db'),
-    ]
-
-FLAGS = flags.FLAGS
-FLAGS.register_opts(db_opts)
-
-IMPL = utils.LazyPluggable('db_backend', db='heat.db.sqlalchemy.api')
+from heat.openstack.common import cfg
+from heat.common import utils
 
+def configure(conf):
+    global IMPL
+    IMPL = utils.import_object(conf.db_backend)
 
 def raw_template_get(context, template_id):
     return IMPL.raw_template_get(context, template_id)
index 941fa744ffe8e179dcbdf278277b1fb1a5a71a13..7fc676a41a2613ed07662c88eab18f8303bff16a 100644 (file)
@@ -28,7 +28,7 @@ from heat.common import exception
 from heat.common import wsgi
 
 from heat.engine import parser
-from heat.engine import simpledb
+from heat.db import api as db_api
 
 logger = logging.getLogger('heat.engine.api.v1.events')
 
@@ -43,7 +43,7 @@ class EventsController(object):
         self.conf = conf
 
     def index(self, req, stack_id):
-        return simpledb.events_get(stack_id)
+        return db_api.event_get_all_by_stack(None, stack_id)
 
 def create_resource(conf):
     """Events resource factory method."""
index 99f82da6a809a314c82504978d0e267c34f9a706..684baefa25b639da834d759fb80f5e76eec459a9 100644 (file)
@@ -28,6 +28,7 @@ from heat.common import exception
 from heat.common import wsgi
 
 from heat.engine import parser
+from heat.db import api as db_api
 
 
 logger = logging.getLogger('heat.engine.api.v1.stacks')
@@ -41,6 +42,7 @@ class StacksController(object):
 
     def __init__(self, conf):
         self.conf = conf
+        db_api.configure(conf)
 
     def index(self, req, format='json'):
         logger.info('format is %s' % format)
index 7abc491a83d66a405ee831c0c7ba8a68e63c9b73..1d3f0d28802c1fe181967df3d2be104d42bbd1ef 100644 (file)
@@ -18,7 +18,7 @@ import os
 import time
 from novaclient.v1_1 import client
 
-from heat.engine import simpledb
+from heat.db import api as db_api
 
 logger = logging.getLogger('heat.engine.resources')
 
@@ -72,7 +72,7 @@ class Resource(object):
             ev['ResourceType'] = self.t['Type']
             ev['ResourceProperties'] = self.t['Properties']
 
-            simpledb.event_append(ev)
+            db_api.event_create(None, ev)
             self.state = new_state
 
     def stop(self):