1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2010 United States Government as represented by the
4
# Administrator of the National Aeronautics and Space Administration.
7
# Licensed under the Apache License, Version 2.0 (the "License"); you may
8
# not use this file except in compliance with the License. You may obtain
9
# a copy of the License at
11
# http://www.apache.org/licenses/LICENSE-2.0
13
# Unless required by applicable law or agreed to in writing, software
14
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
# License for the specific language governing permissions and limitations
20
SQLAlchemy models for glance data
26
from sqlalchemy.orm import relationship, backref, exc, object_mapper, validates
27
from sqlalchemy import Column, Integer, String, BigInteger
28
from sqlalchemy import ForeignKey, DateTime, Boolean, Text
29
from sqlalchemy import UniqueConstraint
30
from sqlalchemy.ext.declarative import declarative_base
32
import glance.registry.db.api
33
from glance.common import exception
35
BASE = declarative_base()
38
class ModelBase(object):
39
"""Base class for Nova and Glance Models"""
40
__table_args__ = {'mysql_engine': 'InnoDB'}
41
__table_initialized__ = False
42
__protected_attributes__ = set([
43
"created_at", "updated_at", "deleted_at", "deleted"])
45
created_at = Column(DateTime, default=datetime.datetime.utcnow,
47
updated_at = Column(DateTime, onupdate=datetime.datetime.utcnow)
48
deleted_at = Column(DateTime)
49
deleted = Column(Boolean, nullable=False, default=False)
51
def save(self, session=None):
52
"""Save this object"""
53
session = session or glance.registry.db.api.get_session()
57
def delete(self, session=None):
58
"""Delete this object"""
60
self.deleted_at = datetime.datetime.utcnow()
61
self.save(session=session)
63
def update(self, values):
64
"""dict.update() behaviour."""
65
for k, v in values.iteritems():
68
def __setitem__(self, key, value):
69
setattr(self, key, value)
71
def __getitem__(self, key):
72
return getattr(self, key)
75
self._i = iter(object_mapper(self).columns)
79
n = self._i.next().name
80
return n, getattr(self, n)
83
return self.__dict__.keys()
86
return self.__dict__.values()
89
return self.__dict__.items()
92
return self.__dict__.copy()
95
class Image(BASE, ModelBase):
96
"""Represents an image in the datastore"""
97
__tablename__ = 'images'
99
id = Column(Integer, primary_key=True)
100
name = Column(String(255))
101
disk_format = Column(String(20))
102
container_format = Column(String(20))
103
size = Column(BigInteger)
104
status = Column(String(30), nullable=False)
105
is_public = Column(Boolean, nullable=False, default=False)
106
location = Column(Text)
107
checksum = Column(String(32))
110
class ImageProperty(BASE, ModelBase):
111
"""Represents an image properties in the datastore"""
112
__tablename__ = 'image_properties'
113
__table_args__ = (UniqueConstraint('image_id', 'name'), {})
115
id = Column(Integer, primary_key=True)
116
image_id = Column(Integer, ForeignKey('images.id'), nullable=False)
117
image = relationship(Image, backref=backref('properties'))
119
name = Column(String(255), index=True, nullable=False)