From befdeee0ed5faa70d4e53042056ce3772c24903a Mon Sep 17 00:00:00 2001 From: He Jie Xu Date: Sat, 24 Sep 2016 15:11:47 +0800 Subject: [PATCH] [placement] Add Traits related table to the api database This patch adds DB table `traits` and `resource_provider_traits` into the api database. The traits table is used to represent the trait. The resource_provider_traits table is used to represent the relationship between trait and resource provider. Part of implementation blueprint shared-resources-pike Change-Id: I5c34bdd1423beab53cc4af45e016d9a9bba5ffda --- .../versions/041_resource_provider_traits.py | 66 +++++++++++++++++++ nova/db/sqlalchemy/api_models.py | 29 ++++++++ 2 files changed, 95 insertions(+) create mode 100644 nova/db/sqlalchemy/api_migrations/migrate_repo/versions/041_resource_provider_traits.py diff --git a/nova/db/sqlalchemy/api_migrations/migrate_repo/versions/041_resource_provider_traits.py b/nova/db/sqlalchemy/api_migrations/migrate_repo/versions/041_resource_provider_traits.py new file mode 100644 index 000000000..03c1c311d --- /dev/null +++ b/nova/db/sqlalchemy/api_migrations/migrate_repo/versions/041_resource_provider_traits.py @@ -0,0 +1,66 @@ +# 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 migrations for traits""" + +from migrate.changeset.constraint import ForeignKeyConstraint +from migrate import UniqueConstraint +from sqlalchemy import Column +from sqlalchemy import DateTime +from sqlalchemy import ForeignKey +from sqlalchemy import Index +from sqlalchemy import Integer +from sqlalchemy import MetaData +from sqlalchemy import Table +from sqlalchemy import Unicode + + +def upgrade(migrate_engine): + meta = MetaData() + meta.bind = migrate_engine + + if migrate_engine.name == 'mysql': + nameargs = {'collation': 'utf8_bin'} + else: + nameargs = {} + + resource_providers = Table('resource_providers', meta, autoload=True) + + traits = Table( + 'traits', meta, + Column('created_at', DateTime), + Column('updated_at', DateTime), + Column('id', Integer, primary_key=True, nullable=False, + autoincrement=True), + Column('name', Unicode(255, **nameargs), nullable=False), + UniqueConstraint('name', name='uniq_traits0name'), + mysql_engine='InnoDB', + mysql_charset='latin1' + ) + + resource_provider_traits = Table( + 'resource_provider_traits', meta, + Column('created_at', DateTime), + Column('updated_at', DateTime), + Column('trait_id', Integer, ForeignKey('traits.id'), primary_key=True, + nullable=False), + Column('resource_provider_id', Integer, primary_key=True, + nullable=False), + Index('resource_provider_traits_resource_provider_trait_idx', + 'resource_provider_id', 'trait_id'), + ForeignKeyConstraint(columns=['resource_provider_id'], + refcolumns=[resource_providers.c.id]), + mysql_engine='InnoDB', + mysql_charset='latin1' + ) + + for table in [traits, resource_provider_traits]: + table.create(checkfirst=True) diff --git a/nova/db/sqlalchemy/api_models.py b/nova/db/sqlalchemy/api_models.py index e5c5720dc..cec76f02c 100644 --- a/nova/db/sqlalchemy/api_models.py +++ b/nova/db/sqlalchemy/api_models.py @@ -553,3 +553,32 @@ class Reservation(API_BASE): "QuotaUsage", foreign_keys=usage_id, primaryjoin='Reservation.usage_id == QuotaUsage.id') + + +class Trait(API_BASE): + """Represents a trait.""" + + __tablename__ = "traits" + __table_args__ = ( + schema.UniqueConstraint('name', name='uniq_traits0name'), + ) + + id = Column(Integer, primary_key=True, nullable=False, autoincrement=True) + name = Column(Unicode(255), nullable=False) + + +class ResourceProviderTraits(API_BASE): + """Represents the relationship between traits and resource provider""" + + __tablename__ = "resource_provider_traits" + __table_args__ = ( + Index('resource_provider_traits_resource_provider_trait_idx', + 'resource_provider_id', 'trait_id'), + ) + + trait_id = Column(Integer, ForeignKey('traits.id'), primary_key=True, + nullable=False) + resource_provider_id = Column(Integer, + ForeignKey('resource_providers.id'), + primary_key=True, + nullable=False)