Merge "Base DB: rehome model_base"
This commit is contained in:
commit
8426b8e12e
@ -32,6 +32,12 @@ def _create_facade_lazily():
|
||||
return _FACADE
|
||||
|
||||
|
||||
def get_engine():
|
||||
"""Helper method to grab engine."""
|
||||
facade = _create_facade_lazily()
|
||||
return facade.get_engine()
|
||||
|
||||
|
||||
def get_session(autocommit=True, expire_on_commit=False, use_slave=False):
|
||||
"""Helper method to grab session."""
|
||||
facade = _create_facade_lazily()
|
||||
|
24
neutron_lib/db/constants.py
Normal file
24
neutron_lib/db/constants.py
Normal file
@ -0,0 +1,24 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
# Database field sizes
|
||||
NAME_FIELD_SIZE = 255
|
||||
LONG_DESCRIPTION_FIELD_SIZE = 1024
|
||||
DESCRIPTION_FIELD_SIZE = 255
|
||||
PROJECT_ID_FIELD_SIZE = 255
|
||||
DEVICE_ID_FIELD_SIZE = 255
|
||||
DEVICE_OWNER_FIELD_SIZE = 255
|
||||
UUID_FIELD_SIZE = 36
|
||||
STATUS_FIELD_SIZE = 16
|
||||
IP_ADDR_FIELD_SIZE = 64 # large enough to hold a v4 or v6 address
|
||||
MAC_ADDR_FIELD_SIZE = 32
|
||||
RESOURCE_TYPE_FIELD_SIZE = 255
|
113
neutron_lib/db/model_base.py
Normal file
113
neutron_lib/db/model_base.py
Normal file
@ -0,0 +1,113 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from oslo_db.sqlalchemy import models
|
||||
from oslo_utils import uuidutils
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.ext import declarative
|
||||
from sqlalchemy import orm
|
||||
|
||||
from neutron_lib.db import constants as db_const
|
||||
|
||||
|
||||
class HasProject(object):
|
||||
"""Project mixin, add to subclasses that have a user."""
|
||||
|
||||
# NOTE: project_id is just a free form string
|
||||
project_id = sa.Column(sa.String(db_const.PROJECT_ID_FIELD_SIZE),
|
||||
index=True)
|
||||
|
||||
def get_tenant_id(self):
|
||||
return self.project_id
|
||||
|
||||
def set_tenant_id(self, value):
|
||||
self.project_id = value
|
||||
|
||||
@declarative.declared_attr
|
||||
def tenant_id(cls):
|
||||
return orm.synonym(
|
||||
'project_id',
|
||||
descriptor=property(cls.get_tenant_id, cls.set_tenant_id))
|
||||
|
||||
|
||||
class HasProjectNoIndex(HasProject):
|
||||
"""Project mixin, add to subclasses that have a user."""
|
||||
|
||||
# NOTE: project_id is just a free form string
|
||||
project_id = sa.Column(sa.String(db_const.PROJECT_ID_FIELD_SIZE))
|
||||
|
||||
|
||||
class HasProjectPrimaryKeyIndex(HasProject):
|
||||
"""Project mixin, add to subclasses that have a user."""
|
||||
|
||||
# NOTE: project_id is just a free form string
|
||||
project_id = sa.Column(sa.String(db_const.PROJECT_ID_FIELD_SIZE),
|
||||
nullable=False, primary_key=True, index=True)
|
||||
|
||||
|
||||
class HasProjectPrimaryKey(HasProject):
|
||||
"""Project mixin, add to subclasses that have a user."""
|
||||
|
||||
# NOTE: project_id is just a free form string
|
||||
project_id = sa.Column(sa.String(db_const.PROJECT_ID_FIELD_SIZE),
|
||||
nullable=False, primary_key=True)
|
||||
|
||||
|
||||
class HasId(object):
|
||||
"""id mixin, add to subclasses that have an id."""
|
||||
|
||||
id = sa.Column(sa.String(db_const.UUID_FIELD_SIZE),
|
||||
primary_key=True,
|
||||
default=uuidutils.generate_uuid)
|
||||
|
||||
|
||||
class HasStatusDescription(object):
|
||||
"""Status with description mixin."""
|
||||
|
||||
status = sa.Column(sa.String(db_const.STATUS_FIELD_SIZE),
|
||||
nullable=False)
|
||||
status_description = sa.Column(sa.String(db_const.DESCRIPTION_FIELD_SIZE))
|
||||
|
||||
|
||||
class _NeutronBase(models.ModelBase):
|
||||
"""Base class for Neutron Models."""
|
||||
|
||||
__table_args__ = {'mysql_engine': 'InnoDB'}
|
||||
|
||||
def __iter__(self):
|
||||
self._i = iter(orm.object_mapper(self).columns)
|
||||
return self
|
||||
|
||||
def next(self):
|
||||
n = next(self._i).name
|
||||
return n, getattr(self, n)
|
||||
|
||||
__next__ = next
|
||||
|
||||
def __repr__(self):
|
||||
"""sqlalchemy based automatic __repr__ method."""
|
||||
items = ['%s=%r' % (col.name, getattr(self, col.name))
|
||||
for col in self.__table__.columns]
|
||||
return "<%s.%s[object at %x] {%s}>" % (self.__class__.__module__,
|
||||
self.__class__.__name__,
|
||||
id(self), ', '.join(items))
|
||||
|
||||
|
||||
class NeutronBaseV2(_NeutronBase):
|
||||
|
||||
@declarative.declared_attr
|
||||
def __tablename__(cls):
|
||||
# Use the pluralized name of the class as the table name.
|
||||
return cls.__name__.lower() + 's'
|
||||
|
||||
|
||||
BASEV2 = declarative.declarative_base(cls=NeutronBaseV2)
|
51
neutron_lib/tests/unit/db/_base.py
Normal file
51
neutron_lib/tests/unit/db/_base.py
Normal file
@ -0,0 +1,51 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
"""
|
||||
Base classes for unit tests needing DB backend.
|
||||
Only sqlite is supported in neutron-lib.
|
||||
"""
|
||||
|
||||
import fixtures
|
||||
|
||||
from neutron_lib.db import _api as db_api
|
||||
from neutron_lib.db import model_base
|
||||
|
||||
from neutron_lib.tests import _base as base
|
||||
|
||||
|
||||
class SqlFixture(fixtures.Fixture):
|
||||
|
||||
# flag to indicate that the models have been loaded
|
||||
_TABLES_ESTABLISHED = False
|
||||
|
||||
def _setUp(self):
|
||||
# Register all data models
|
||||
engine = db_api.get_engine()
|
||||
if not SqlFixture._TABLES_ESTABLISHED:
|
||||
model_base.BASEV2.metadata.create_all(engine)
|
||||
SqlFixture._TABLES_ESTABLISHED = True
|
||||
|
||||
def clear_tables():
|
||||
with engine.begin() as conn:
|
||||
for table in reversed(
|
||||
model_base.BASEV2.metadata.sorted_tables):
|
||||
conn.execute(table.delete())
|
||||
|
||||
self.addCleanup(clear_tables)
|
||||
|
||||
|
||||
class SqlTestCase(base.BaseTestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(SqlTestCase, self).setUp()
|
||||
self.useFixture(SqlFixture())
|
61
neutron_lib/tests/unit/db/test_model_base.py
Normal file
61
neutron_lib/tests/unit/db/test_model_base.py
Normal file
@ -0,0 +1,61 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from neutron_lib import _context as context
|
||||
from neutron_lib.db import model_base
|
||||
|
||||
from neutron_lib.tests.unit.db import _base as db_base
|
||||
|
||||
|
||||
class TestTable(model_base.BASEV2, model_base.HasProject,
|
||||
model_base.HasId, model_base.HasStatusDescription):
|
||||
|
||||
name = sa.Column(sa.String(8), primary_key=True)
|
||||
|
||||
|
||||
class TestModelBase(db_base.SqlTestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestModelBase, self).setUp()
|
||||
self.ctx = context.Context('user', 'project')
|
||||
self.session = self.ctx.session
|
||||
|
||||
def test_model_base(self):
|
||||
foo = TestTable(name='meh')
|
||||
self.assertEqual('meh', foo.name)
|
||||
self.assertIn('meh', str(foo)) # test foo.__repr__
|
||||
cols = [k for k, _v in foo] # test foo.__iter__ and foo.next
|
||||
self.assertIn('name', cols)
|
||||
|
||||
def test_get_set_tenant_id_tenant(self):
|
||||
foo = TestTable(tenant_id='tenant')
|
||||
self.assertEqual('tenant', foo.get_tenant_id())
|
||||
foo.set_tenant_id('project')
|
||||
self.assertEqual('project', foo.get_tenant_id())
|
||||
|
||||
def test_get_set_tenant_id_project(self):
|
||||
foo = TestTable(project_id='project')
|
||||
self.assertEqual('project', foo.get_tenant_id())
|
||||
foo.set_tenant_id('tenant')
|
||||
self.assertEqual('tenant', foo.get_tenant_id())
|
||||
|
||||
def test_project_id_attribute(self):
|
||||
foo = TestTable(project_id='project')
|
||||
self.assertEqual('project', foo.project_id)
|
||||
self.assertEqual('project', foo.tenant_id)
|
||||
|
||||
def test_tenant_id_attribute(self):
|
||||
foo = TestTable(project_id='project')
|
||||
self.assertEqual('project', foo.project_id)
|
||||
self.assertEqual('project', foo.tenant_id)
|
@ -5,6 +5,7 @@
|
||||
pbr>=1.6 # Apache-2.0
|
||||
Babel>=2.3.4 # BSD
|
||||
|
||||
SQLAlchemy>=1.0.10,<1.1.0 # MIT
|
||||
debtcollector>=1.2.0 # Apache-2.0
|
||||
oslo.config>=3.14.0 # Apache-2.0
|
||||
oslo.context>=2.6.0 # Apache-2.0
|
||||
|
@ -5,6 +5,8 @@
|
||||
hacking<0.12,>=0.11.0 # Apache-2.0
|
||||
|
||||
coverage>=3.6 # Apache-2.0
|
||||
discover # BSD
|
||||
fixtures>=3.0.0 # Apache-2.0/BSD
|
||||
python-subunit>=0.0.18 # Apache-2.0/BSD
|
||||
sphinx!=1.3b1,<1.3,>=1.2.1 # BSD
|
||||
oslosphinx!=3.4.0,>=2.5.0 # Apache-2.0
|
||||
@ -12,6 +14,7 @@ oslotest>=1.10.0 # Apache-2.0
|
||||
os-api-ref>=0.4.0 # Apache-2.0
|
||||
os-testr>=0.7.0 # Apache-2.0
|
||||
testrepository>=0.0.18 # Apache-2.0/BSD
|
||||
testresources>=0.2.4 # Apache-2.0/BSD
|
||||
testscenarios>=0.4 # Apache-2.0/BSD
|
||||
testtools>=1.4.0 # MIT
|
||||
pep8==1.5.7 # MIT
|
||||
|
Loading…
Reference in New Issue
Block a user