Moves the uuid generation event listener from the plugin to the DB API
to decouple test expectations from the plugin.
This commit is contained in:
Matt Dietz
2013-07-19 19:20:58 +00:00
parent 78223887fe
commit e9ea926465
3 changed files with 18 additions and 22 deletions

View File

@@ -14,9 +14,12 @@
# under the License.
import datetime
import inspect
from neutron.openstack.common import log as logging
from neutron.openstack.common import timeutils
from neutron.openstack.common import uuidutils
from sqlalchemy import event
from sqlalchemy import func as sql_func
from sqlalchemy import orm, or_
@@ -32,6 +35,21 @@ ONE = "one"
ALL = "all"
# 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()
# 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)
def _listify(filters):
for key in ["name", "network_id", "id", "device_id", "tenant_id",
"mac_address", "shared"]:

View File

@@ -16,13 +16,10 @@
"""
v2 Neutron Plug-in API Quark Implementation
"""
import inspect
import netaddr
from oslo.config import cfg
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy import event
from zope import sqlalchemy as zsa
from neutron.api.v2 import attributes
@@ -124,20 +121,6 @@ class Plugin(neutron_plugin_base_v2.NeutronPluginBaseV2,
def __init__(self):
# 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()
# 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)
neutron_db_api.configure_db()
self._initDBMaker()
self.net_driver = (importutils.import_class(CONF.QUARK.net_driver))()

View File

@@ -22,7 +22,6 @@ from oslo.config import cfg
from quark.db import models
import quark.ipam
import quark.plugin
from quark.tests import test_base
@@ -36,10 +35,6 @@ class QuarkIpamBaseTest(test_base.TestBase):
models.BASEV2.metadata.create_all(neutron_session._ENGINE)
self.ipam = quark.ipam.QuarkIpam()
# FIXME(mdietz): refactor around issue #130 and remove this
# Ensures that the perhaps_generate_uuid event handler is initialized
self.plugin = quark.plugin.Plugin()
def tearDown(self):
neutron_db_api.clear_db()