¡@

Home 

OpenStack Study: models.py

OpenStack Index

**** CubicPower OpenStack Study ****

# Copyright 2010 United States Government as represented by the

# Administrator of the National Aeronautics and Space Administration.

# All Rights Reserved.

#

# 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 glance data

"""

import uuid

from sqlalchemy import BigInteger

from sqlalchemy import Boolean

from sqlalchemy import Column

from sqlalchemy import DateTime

from sqlalchemy.ext.compiler import compiles

from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy import ForeignKey

from sqlalchemy import Index

from sqlalchemy import Integer

from sqlalchemy.orm import backref, relationship

from sqlalchemy import String

from sqlalchemy import Text

from sqlalchemy.types import TypeDecorator

from sqlalchemy import UniqueConstraint

from glance.openstack.common.db.sqlalchemy import models

from glance.openstack.common import jsonutils

from glance.openstack.common import timeutils

BASE = declarative_base()

@compiles(BigInteger, 'sqlite')

**** CubicPower OpenStack Study ****

def compile_big_int_sqlite(type_, compiler, **kw):

    return 'INTEGER'

**** CubicPower OpenStack Study ****

class JSONEncodedDict(TypeDecorator):

"""Represents an immutable structure as a json-encoded string"""

impl = Text

**** CubicPower OpenStack Study ****

    def process_bind_param(self, value, dialect):

        if value is not None:

            value = jsonutils.dumps(value)

        return value

**** CubicPower OpenStack Study ****

    def process_result_value(self, value, dialect):

        if value is not None:

            value = jsonutils.loads(value)

        return value

**** CubicPower OpenStack Study ****

class GlanceBase(models.ModelBase, models.TimestampMixin):

"""Base class for Glance Models."""

__table_args__ = {'mysql_engine': 'InnoDB'}

__table_initialized__ = False

__protected_attributes__ = set([

"created_at", "updated_at", "deleted_at", "deleted"])

**** CubicPower OpenStack Study ****

    def save(self, session=None):

        from glance.db.sqlalchemy import api as db_api

        super(GlanceBase, self).save(session or db_api.get_session())

    created_at = Column(DateTime, default=lambda: timeutils.utcnow(),

                        nullable=False)

    # TODO(vsergeyev): Column `updated_at` have no default value in

    #                  openstack common code. We should decide, is this value

    #                  required and make changes in oslo (if required) or

    #                  in glance (if not).

    updated_at = Column(DateTime, default=lambda: timeutils.utcnow(),

                        nullable=False, onupdate=lambda: timeutils.utcnow())

    # TODO(boris-42): Use SoftDeleteMixin instead of deleted Column after

    #                 migration that provides UniqueConstraints and change

    #                 type of this column.

    deleted_at = Column(DateTime)

    deleted = Column(Boolean, nullable=False, default=False)

**** CubicPower OpenStack Study ****

    def delete(self, session=None):

        """Delete this object."""

        self.deleted = True

        self.deleted_at = timeutils.utcnow()

        self.save(session=session)

**** CubicPower OpenStack Study ****

    def keys(self):

        return self.__dict__.keys()

**** CubicPower OpenStack Study ****

    def values(self):

        return self.__dict__.values()

**** CubicPower OpenStack Study ****

    def items(self):

        return self.__dict__.items()

**** CubicPower OpenStack Study ****

    def to_dict(self):

        d = self.__dict__.copy()

        # NOTE(flaper87): Remove

        # private state instance

        # It is not serializable

        # and causes CircularReference

        d.pop("_sa_instance_state")

        return d

**** CubicPower OpenStack Study ****

class Image(BASE, GlanceBase):

"""Represents an image in the datastore."""

__tablename__ = 'images'

__table_args__ = (Index('checksum_image_idx', 'checksum'),

Index('ix_images_is_public', 'is_public'),

Index('ix_images_deleted', 'deleted'),

Index('owner_image_idx', 'owner'),)

id = Column(String(36), primary_key=True,

**** CubicPower OpenStack Study ****

class ImageProperty(BASE, GlanceBase):

"""Represents an image properties in the datastore."""

__tablename__ = 'image_properties'

__table_args__ = (Index('ix_image_properties_image_id', 'image_id'),

Index('ix_image_properties_deleted', 'deleted'),

UniqueConstraint('image_id',

'name',

name='ix_image_properties_'

'image_id_name'),)

id = Column(Integer, primary_key=True)

image_id = Column(String(36), ForeignKey('images.id'),

nullable=False)

image = relationship(Image, backref=backref('properties'))

name = Column(String(255), nullable=False)

value = Column(Text)

**** CubicPower OpenStack Study ****

class ImageTag(BASE, GlanceBase):

"""Represents an image tag in the datastore."""

__tablename__ = 'image_tags'

__table_args__ = (Index('ix_image_tags_image_id', 'image_id'),

Index('ix_image_tags_image_id_tag_value',

'image_id',

'value'),)

id = Column(Integer, primary_key=True, nullable=False)

image_id = Column(String(36), ForeignKey('images.id'), nullable=False)

image = relationship(Image, backref=backref('tags'))

value = Column(String(255), nullable=False)

**** CubicPower OpenStack Study ****

class ImageLocation(BASE, GlanceBase):

"""Represents an image location in the datastore."""

__tablename__ = 'image_locations'

__table_args__ = (Index('ix_image_locations_image_id', 'image_id'),

Index('ix_image_locations_deleted', 'deleted'),)

id = Column(Integer, primary_key=True, nullable=False)

image_id = Column(String(36), ForeignKey('images.id'), nullable=False)

image = relationship(Image, backref=backref('locations'))

value = Column(Text(), nullable=False)

meta_data = Column(JSONEncodedDict(),

**** CubicPower OpenStack Study ****

class ImageMember(BASE, GlanceBase):

"""Represents an image members in the datastore."""

__tablename__ = 'image_members'

unique_constraint_key_name = 'image_members_image_id_member_deleted_at_key'

__table_args__ = (Index('ix_image_members_deleted', 'deleted'),

Index('ix_image_members_image_id', 'image_id'),

Index('ix_image_members_image_id_member',

'image_id',

'member'),

UniqueConstraint('image_id',

'member',

'deleted_at',

name=unique_constraint_key_name),)

id = Column(Integer, primary_key=True)

image_id = Column(String(36), ForeignKey('images.id'),

nullable=False)

image = relationship(Image, backref=backref('members'))

member = Column(String(255), nullable=False)

can_share = Column(Boolean, nullable=False,

**** CubicPower OpenStack Study ****

class Task(BASE, GlanceBase):

"""Represents an task in the datastore"""

__tablename__ = 'tasks'

__table_args__ = (Index('ix_tasks_type', 'type'),

Index('ix_tasks_status', 'status'),

Index('ix_tasks_owner', 'owner'),

Index('ix_tasks_deleted', 'deleted'),

Index('ix_tasks_updated_at', 'updated_at'))

id = Column(String(36), primary_key=True,

**** CubicPower OpenStack Study ****

class TaskInfo(BASE, models.ModelBase):

"""Represents task info in the datastore"""

__tablename__ = 'task_info'

task_id = Column(String(36),

ForeignKey('tasks.id'),

primary_key=True,

nullable=False)

task = relationship(Task, backref=backref('info', uselist=False))

#NOTE(nikhil): input and result are stored as text in the DB.

# SQLAlchemy marshals the data to/from JSON using custom type

# JSONEncodedDict. It uses simplejson underneath.

input = Column(JSONEncodedDict())

result = Column(JSONEncodedDict())

message = Column(Text)

**** CubicPower OpenStack Study ****

def register_models(engine):

    """Create database tables for all models with the given engine."""

    models = (Image, ImageProperty, ImageMember)

    for model in models:

        model.metadata.create_all(engine)

**** CubicPower OpenStack Study ****

def unregister_models(engine):

    """Drop database tables for all models with the given engine."""

    models = (Image, ImageProperty)

    for model in models:

        model.metadata.drop_all(engine)