@@ -15,6 +15,41 @@
|
||||
|
||||
'''Implementation of SQLAlchemy backend.'''
|
||||
|
||||
from nova.db.sqlalchemy.session import get_session
|
||||
from nova import flags
|
||||
from nova import utils
|
||||
|
||||
FLAGS = flags.FLAGS
|
||||
|
||||
def model_query(context, *args, **kwargs):
|
||||
"""Query helper that accounts for context's `read_deleted` field.
|
||||
|
||||
:param context: context to query under
|
||||
:param session: if present, the session to use
|
||||
:param read_deleted: if present, overrides context's read_deleted field.
|
||||
:param project_only: if present and context is user-type, then restrict
|
||||
query to match the context's project_id.
|
||||
"""
|
||||
session = kwargs.get('session') or get_session()
|
||||
read_deleted = kwargs.get('read_deleted') or context.read_deleted
|
||||
project_only = kwargs.get('project_only')
|
||||
|
||||
query = session.query(*args)
|
||||
|
||||
if read_deleted == 'no':
|
||||
query = query.filter_by(deleted=False)
|
||||
elif read_deleted == 'yes':
|
||||
pass # omit the filter to include deleted and active
|
||||
elif read_deleted == 'only':
|
||||
query = query.filter_by(deleted=True)
|
||||
else:
|
||||
raise Exception(
|
||||
_("Unrecognized read_deleted value '%s'") % read_deleted)
|
||||
|
||||
if project_only and is_user_context(context):
|
||||
query = query.filter_by(project_id=context.project_id)
|
||||
|
||||
return query
|
||||
|
||||
# a big TODO
|
||||
def raw_template_get(context, template_id):
|
||||
|
||||
5
heat/db/sqlalchemy/manage.py
Normal file
5
heat/db/sqlalchemy/manage.py
Normal file
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from migrate.versioning.shell import main
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(url='mysql://heat:heat@localhost/heat', debug='False', repository='migrate_repo')
|
||||
4
heat/db/sqlalchemy/migrate_repo/README
Normal file
4
heat/db/sqlalchemy/migrate_repo/README
Normal file
@@ -0,0 +1,4 @@
|
||||
This is a database migration repository.
|
||||
|
||||
More information at
|
||||
http://code.google.com/p/sqlalchemy-migrate/
|
||||
0
heat/db/sqlalchemy/migrate_repo/__init__.py
Normal file
0
heat/db/sqlalchemy/migrate_repo/__init__.py
Normal file
5
heat/db/sqlalchemy/migrate_repo/manage.py
Normal file
5
heat/db/sqlalchemy/migrate_repo/manage.py
Normal file
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from migrate.versioning.shell import main
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(debug='False')
|
||||
25
heat/db/sqlalchemy/migrate_repo/migrate.cfg
Normal file
25
heat/db/sqlalchemy/migrate_repo/migrate.cfg
Normal file
@@ -0,0 +1,25 @@
|
||||
[db_settings]
|
||||
# Used to identify which repository this database is versioned under.
|
||||
# You can use the name of your project.
|
||||
repository_id=heat
|
||||
|
||||
# The name of the database table used to track the schema version.
|
||||
# This name shouldn't already be used by your project.
|
||||
# If this is changed once a database is under version control, you'll need to
|
||||
# change the table name in each database too.
|
||||
version_table=migrate_version
|
||||
|
||||
# When committing a change script, Migrate will attempt to generate the
|
||||
# sql for all supported databases; normally, if one of them fails - probably
|
||||
# because you don't have that database installed - it is ignored and the
|
||||
# commit continues, perhaps ending successfully.
|
||||
# Databases in this list MUST compile successfully during a commit, or the
|
||||
# entire commit will fail. List the databases your application will actually
|
||||
# be using to ensure your updates to that database work properly.
|
||||
# This must be a list; example: ['postgres','sqlite']
|
||||
required_dbs=[]
|
||||
|
||||
# When creating new change scripts, Migrate will stamp the new script with
|
||||
# a version number. By default this is latest_version + 1. You can set this
|
||||
# to 'true' to tell Migrate to use the UTC timestamp instead.
|
||||
use_timestamp_numbering=False
|
||||
66
heat/db/sqlalchemy/migrate_repo/versions/001_norwhal.py
Normal file
66
heat/db/sqlalchemy/migrate_repo/versions/001_norwhal.py
Normal file
@@ -0,0 +1,66 @@
|
||||
from sqlalchemy import *
|
||||
from migrate import *
|
||||
|
||||
def upgrade(migrate_engine):
|
||||
meta = MetaData()
|
||||
meta.bind = migrate_engine
|
||||
|
||||
rawtemplate = Table(
|
||||
'raw_template', meta,
|
||||
Column('id', Integer, primary_key=True),
|
||||
Column('created_at', DateTime(timezone=False)),
|
||||
Column('updated_at', DateTime(timezone=False)),
|
||||
Column('template', Text()),
|
||||
)
|
||||
|
||||
event = Table(
|
||||
'event', meta,
|
||||
Column('id', Integer, primary_key=True),
|
||||
Column('created_at', DateTime(timezone=False)),
|
||||
Column('updated_at', DateTime(timezone=False)),
|
||||
Column('name', String(length=255, convert_unicode=False,
|
||||
assert_unicode=None,
|
||||
unicode_error=None, _warn_on_bytestring=False)),
|
||||
)
|
||||
|
||||
resource = Table(
|
||||
'resource', meta,
|
||||
Column('id', Integer, primary_key=True),
|
||||
Column('instance_id', String(length=255, convert_unicode=False,
|
||||
assert_unicode=None,
|
||||
unicode_error=None, _warn_on_bytestring=False)),
|
||||
Column('created_at', DateTime(timezone=False)),
|
||||
Column('updated_at', DateTime(timezone=False)),
|
||||
Column('state', Integer()),
|
||||
Column('state_description', String(length=255, convert_unicode=False,
|
||||
assert_unicode=None,
|
||||
unicode_error=None,
|
||||
_warn_on_bytestring=False)),
|
||||
)
|
||||
|
||||
parsedtemplate = Table(
|
||||
'parsed_template', meta,
|
||||
Column('id', Integer, primary_key=True),
|
||||
Column('resource_id', Integer()),
|
||||
Column('template', Text()),
|
||||
)
|
||||
|
||||
tables = [rawtemplate, event, resource, parsedtemplate]
|
||||
for table in tables:
|
||||
try:
|
||||
table.create()
|
||||
except Exception:
|
||||
meta.drop_all(tables=tables)
|
||||
raise
|
||||
|
||||
def downgrade(migrate_engine):
|
||||
meta = MetaData()
|
||||
meta.bind = migrate_engine
|
||||
|
||||
rawtemplate = Table('raw_template', meta, autoload=True)
|
||||
event = Table('event', meta, autoload=True)
|
||||
resource = Table('resource', meta, autoload=True)
|
||||
parsedtemplate = Table('parsed_template', meta, autoload=True)
|
||||
|
||||
for table in (rawtemplate, event, resource, parsedtemplate):
|
||||
table.drop()
|
||||
117
heat/db/sqlalchemy/models.py
Normal file
117
heat/db/sqlalchemy/models.py
Normal file
@@ -0,0 +1,117 @@
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
#
|
||||
# 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.
|
||||
"""
|
||||
SQLAlchemy models for heat data.
|
||||
"""
|
||||
|
||||
from sqlalchemy import *
|
||||
from sqlalchemy.orm import relationship, backref, object_mapper
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.schema import ForeignKeyConstraint
|
||||
|
||||
from nova import flags
|
||||
|
||||
FLAGS = flags.FLAGS
|
||||
BASE = declarative_base()
|
||||
meta = MetaData()
|
||||
|
||||
class HeatBase(object):
|
||||
"""Base class for Heat Models."""
|
||||
__table_args__ = {'mysql_engine': 'InnoDB'}
|
||||
__table_initialized__ = False
|
||||
created_at = Column(DateTime, default=utils.utcnow)
|
||||
updated_at = Column(DateTime, onupdate=utils.utcnow)
|
||||
|
||||
def save(self, session=None):
|
||||
"""Save this object."""
|
||||
if not session:
|
||||
session = get_session()
|
||||
session.add(self)
|
||||
try:
|
||||
session.flush()
|
||||
except IntegrityError, e:
|
||||
if str(e).endswith('is not unique'):
|
||||
raise exception.Duplicate(str(e))
|
||||
else:
|
||||
raise
|
||||
|
||||
def delete(self, session=None):
|
||||
"""Delete this object."""
|
||||
self.deleted = True
|
||||
self.deleted_at = utils.utcnow()
|
||||
self.save(session=session)
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
setattr(self, key, value)
|
||||
|
||||
def __getitem__(self, key):
|
||||
return getattr(self, key)
|
||||
|
||||
def get(self, key, default=None):
|
||||
return getattr(self, key, default)
|
||||
|
||||
def __iter__(self):
|
||||
self._i = iter(object_mapper(self).columns)
|
||||
return self
|
||||
|
||||
def next(self):
|
||||
n = self._i.next().name
|
||||
return n, getattr(self, n)
|
||||
|
||||
def update(self, values):
|
||||
"""Make the model object behave like a dict"""
|
||||
for k, v in values.iteritems():
|
||||
setattr(self, k, v)
|
||||
|
||||
def iteritems(self):
|
||||
"""Make the model object behave like a dict.
|
||||
|
||||
Includes attributes from joins."""
|
||||
local = dict(self)
|
||||
joined = dict([(k, v) for k, v in self.__dict__.iteritems()
|
||||
if not k[0] == '_'])
|
||||
local.update(joined)
|
||||
return local.iteritems()
|
||||
|
||||
class RawTemplate(Base, HeatBase):
|
||||
"""Represents an unparsed template which should be in JSON format."""
|
||||
|
||||
__tablename__ = 'raw_template'
|
||||
id = Column(Integer, primary_key=True)
|
||||
template = Text()
|
||||
|
||||
class ParsedTemplate(Base, HeatBase):
|
||||
"""Represents a parsed template."""
|
||||
|
||||
__tablename__ = 'parsed_template'
|
||||
id = Column(Integer, primary_key=True)
|
||||
resource_id = Column('resource_id', Integer)
|
||||
|
||||
class Event(Base, HeatBase):
|
||||
"""Represents an event generated by the heat engine."""
|
||||
|
||||
__tablename__ = 'event'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
name = Column(String)
|
||||
|
||||
class Resource(Base, HeatBase):
|
||||
"""Represents a resource created by the heat engine."""
|
||||
|
||||
__tablename__ = 'resource'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
state = Column(String)
|
||||
state_description = Column('state_description', String)
|
||||
Reference in New Issue
Block a user