Ensure model instance have an id

* Register a sqlalchemy event listener on all models that are subclasses
  of HasId to make sure id is set on object creation so the id can be
  referenced prior to commit.
* Add TODO entry to think about refactoring out sqlalchemy from the
  plugin
This commit is contained in:
Jason Kölker 2013-03-13 21:46:27 -05:00
parent 688bbba1a1
commit b18391d5ce
2 changed files with 18 additions and 0 deletions

1
TODO
View File

@ -12,6 +12,7 @@ CRUD IP Allocations
Later:
Maybe implement a straight passthrough driver for testing and debugging purposes
Separate all SQL alchemy out of the plugin
Allocations Controller Extension:

View File

@ -16,10 +16,12 @@
"""
v2 Quantum Plug-in API Quark Implementation
"""
import inspect
import netaddr
from sqlalchemy import func as sql_func
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy import event
from zope.sqlalchemy import ZopeTransactionExtension
from oslo.config import cfg
@ -55,6 +57,13 @@ if 'api_extensions_path' in CONF:
CONF.set_override('api_extensions_path', ":".join(extensions.__path__))
# NOTE(jkoelker) init event listener that will ensure id is filled in
# on object creation (prior to commit).
def perhaps_generate_id(target, args, kwargs):
if hasattr(target, 'id') and target.id is None:
target.id = uuidutils.generate_uuid()
class Plugin(quantum_plugin_base_v2.QuantumPluginBaseV2):
# NOTE(mdietz): I hate this
supported_extension_aliases = ["mac_address_ranges", "routes",
@ -66,6 +75,14 @@ class Plugin(quantum_plugin_base_v2.QuantumPluginBaseV2):
extension=ZopeTransactionExtension()))
def __init__(self):
# NOTE(jkoelker) Register the event on all models that have ids
for _name, klass in inspect.getmembers(models, inspect.isclass):
if klass is models.HasId:
continue
if models.HasId in klass.mro():
event.listen(klass, "init", perhaps_generate_id)
db_api.configure_db()
self._initDBMaker()
self.net_driver = (importutils.import_class(CONF.QUARK.net_driver))()