Add location specific information to image locations db

This patch adds a column to the image_locations table.  The column
will store location specific information.  As a location is added
to an image there may be some meta data about that information that
needs to be stored with it.  For example, if it is a file:// URL,
information about the NFS server (host name, mount point) would be
needed in order for the URL to be useful.

blueprint: direct-url-meta-data
blueprint: multiple-image-locations

Change-Id: I6a96163abc8e4388e84cae2a8952f89d433210ff
This commit is contained in:
John Bresnahan 2013-05-31 10:35:24 -10:00
parent ce7554590d
commit 89e9043fba
4 changed files with 89 additions and 1 deletions

View File

@ -49,6 +49,9 @@ Integer = lambda: sqlalchemy.types.Integer()
BigInteger = lambda: sqlalchemy.types.BigInteger()
PickleType = lambda: sqlalchemy.types.PickleType()
def from_migration_import(module_name, fromlist):
"""
Import a migration file and return the module

View File

@ -0,0 +1,45 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2013 OpenStack Foundation
# 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.
import sqlalchemy
from glance.db.sqlalchemy.migrate_repo import schema
def upgrade(migrate_engine):
meta = sqlalchemy.schema.MetaData()
meta.bind = migrate_engine
image_locations_table = sqlalchemy.Table('image_locations',
meta,
autoload=True)
meta_data = sqlalchemy.Column('meta_data',
schema.PickleType(),
default={})
meta_data.create(image_locations_table)
def downgrade(migrate_engine):
meta = sqlalchemy.schema.MetaData()
meta.bind = migrate_engine
image_locations_table = sqlalchemy.Table('image_locations',
meta,
autoload=True)
image_locations_table.columns['meta_data'].drop()

View File

@ -23,7 +23,7 @@ SQLAlchemy models for glance data
from sqlalchemy import Column, Integer, String, BigInteger
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import ForeignKey, DateTime, Boolean, Text
from sqlalchemy import ForeignKey, DateTime, Boolean, Text, PickleType
from sqlalchemy.orm import relationship, backref, object_mapper
from sqlalchemy import Index, UniqueConstraint
@ -153,6 +153,7 @@ class ImageLocation(BASE, ModelBase):
image_id = Column(String(36), ForeignKey('images.id'), nullable=False)
image = relationship(Image, backref=backref('locations'))
value = Column(Text(), nullable=False)
meta_data = Column(PickleType(), default={})
class ImageMember(BASE, ModelBase):

View File

@ -29,6 +29,7 @@ import commands
import ConfigParser
import datetime
import os
import pickle
import urlparse
from migrate.versioning.repository import Repository
@ -673,3 +674,41 @@ class TestMigrations(utils.BaseTestCase):
def _check_020(self, engine, data):
images = get_table(engine, 'images')
self.assertFalse('location' in images.c)
def _prerun_026(self, engine):
image_locations = get_table(engine, 'image_locations')
now = datetime.datetime.now()
image_id = 'fake_id'
url = 'file:///some/place/onthe/fs'
images = get_table(engine, 'images')
temp = dict(deleted=False,
created_at=now,
updated_at=now,
status='active',
is_public=True,
min_disk=0,
min_ram=0,
id=image_id)
images.insert().values(temp).execute()
temp = dict(deleted=False,
created_at=now,
updated_at=now,
image_id=image_id,
value=url)
image_locations.insert().values(temp).execute()
return image_id
def _check_026(self, engine, data):
image_locations = get_table(engine, 'image_locations')
results = image_locations.select()\
.where(image_locations.c.image_id == data).execute()
r = list(results)
self.assertEquals(len(r), 1)
self.assertEquals(r[0]['value'], 'file:///some/place/onthe/fs')
self.assertTrue('meta_data' in r[0])
x = pickle.loads(r[0]['meta_data'])
self.assertEqual(x, {})