diff --git a/manila/api/views/share_groups.py b/manila/api/views/share_groups.py index 1028e15ab6..1a82a300c0 100644 --- a/manila/api/views/share_groups.py +++ b/manila/api/views/share_groups.py @@ -57,6 +57,7 @@ class ShareGroupViewBuilder(common.ViewBuilder): 'share_types': [st['share_type_id'] for st in share_group.get( 'share_types')], 'links': self._get_links(request, share_group['id']), + # TODO(vponomaryov): add 'consistent_snapshot_support' key in Pike. } if context.is_admin: share_group_dict['share_server_id'] = share_group.get( diff --git a/manila/db/migrations/alembic/versions/d5db24264f5c_add_consistent_snapshot_support_attr_to_share_group_model.py b/manila/db/migrations/alembic/versions/d5db24264f5c_add_consistent_snapshot_support_attr_to_share_group_model.py new file mode 100644 index 0000000000..18567a5ef0 --- /dev/null +++ b/manila/db/migrations/alembic/versions/d5db24264f5c_add_consistent_snapshot_support_attr_to_share_group_model.py @@ -0,0 +1,58 @@ +# 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. + +"""Add enum 'consistent_snapshot_support' attr to 'share_groups' model. + +Revision ID: d5db24264f5c +Revises: 927920b37453 +Create Date: 2017-02-03 15:59:31.134166 + +""" + +# revision identifiers, used by Alembic. +revision = 'd5db24264f5c' +down_revision = '927920b37453' + +from alembic import op +import sqlalchemy as sa + + +SG_TABLE_NAME = 'share_groups' +ATTR_NAME = 'consistent_snapshot_support' +ENUM_POOL_VALUE = 'pool' +ENUM_HOST_VALUE = 'host' + + +def upgrade(): + # Workaround for following alembic bug: + # https://bitbucket.org/zzzeek/alembic/issue/89 + context = op.get_context() + if context.bind.dialect.name == 'postgresql': + op.execute( + "CREATE TYPE %s AS ENUM ('%s', '%s')" % ( + ATTR_NAME, ENUM_POOL_VALUE, ENUM_HOST_VALUE)) + + op.add_column( + SG_TABLE_NAME, + sa.Column( + ATTR_NAME, + sa.Enum(ENUM_POOL_VALUE, ENUM_HOST_VALUE, name=ATTR_NAME), + nullable=True, + ), + ) + + +def downgrade(): + op.drop_column(SG_TABLE_NAME, ATTR_NAME) + context = op.get_context() + if context.bind.dialect.name == 'postgresql': + op.execute('DROP TYPE %s' % ATTR_NAME) diff --git a/manila/db/sqlalchemy/models.py b/manila/db/sqlalchemy/models.py index 7271d32e8f..17c1948dfe 100644 --- a/manila/db/sqlalchemy/models.py +++ b/manila/db/sqlalchemy/models.py @@ -1038,6 +1038,7 @@ class ShareGroup(BASE, ManilaBase): String(36), ForeignKey('share_servers.id'), nullable=True) share_group_type_id = Column( String(36), ForeignKey('share_group_types.id'), nullable=True) + consistent_snapshot_support = Column(Enum('pool', 'host'), default=None) share_group_type = orm.relationship( ShareGroupTypes, backref="share_groups", diff --git a/manila/tests/db/migrations/alembic/migrations_data_checks.py b/manila/tests/db/migrations/alembic/migrations_data_checks.py index 2831999bbd..69c62c8dea 100644 --- a/manila/tests/db/migrations/alembic/migrations_data_checks.py +++ b/manila/tests/db/migrations/alembic/migrations_data_checks.py @@ -36,6 +36,7 @@ See BaseMigrationChecks class for more information. import abc import datetime +from oslo_db import exception as oslo_db_exc from oslo_utils import uuidutils import six from sqlalchemy import exc as sa_exc @@ -2109,3 +2110,62 @@ class ShareGroupSnapshotMemberNewProviderLocationColumnChecks( self.test_case.assertEqual(1, db_result.rowcount) for sgsm in db_result: self.test_case.assertFalse(hasattr(sgsm, 'provider_location')) + + +@map_to_migration('d5db24264f5c') +class ShareGroupNewConsistentSnapshotSupportColumnChecks(BaseMigrationChecks): + table_name = 'share_groups' + new_attr_name = 'consistent_snapshot_support' + share_group_type_id = uuidutils.generate_uuid() + share_group_id = uuidutils.generate_uuid() + + def setup_upgrade_data(self, engine): + # Setup share group type + sgt_data = { + 'id': self.share_group_type_id, + 'name': uuidutils.generate_uuid(), + } + sgt_table = utils.load_table('share_group_types', engine) + engine.execute(sgt_table.insert(sgt_data)) + + # Setup share group + sg_data = { + 'id': self.share_group_id, + 'project_id': 'fake_project_id', + 'user_id': 'fake_user_id', + 'share_group_type_id': self.share_group_type_id, + } + sg_table = utils.load_table('share_groups', engine) + engine.execute(sg_table.insert(sg_data)) + + def check_upgrade(self, engine, data): + sg_table = utils.load_table(self.table_name, engine) + db_result = engine.execute(sg_table.select().where( + sg_table.c.id == self.share_group_id)) + self.test_case.assertEqual(1, db_result.rowcount) + for sg in db_result: + self.test_case.assertTrue(hasattr(sg, self.new_attr_name)) + + # Check that we can write proper enum data to the new field + for value in (None, 'pool', 'host'): + engine.execute(sg_table.update().where( + sg_table.c.id == self.share_group_id, + ).values({self.new_attr_name: value})) + + # Check that we cannot write values that are not allowed by enum. + for value in ('', 'fake', 'pool1', 'host1', '1pool', '1host'): + self.test_case.assertRaises( + oslo_db_exc.DBError, + engine.execute, + sg_table.update().where( + sg_table.c.id == self.share_group_id + ).values({self.new_attr_name: value}) + ) + + def check_downgrade(self, engine): + sg_table = utils.load_table(self.table_name, engine) + db_result = engine.execute(sg_table.select().where( + sg_table.c.id == self.share_group_id)) + self.test_case.assertEqual(1, db_result.rowcount) + for sg in db_result: + self.test_case.assertFalse(hasattr(sg, self.new_attr_name)) diff --git a/releasenotes/notes/bug-1661266-add-consistent-snapshot-support-attr-to-share-groups-DB-model-daa1d05129802796.yaml b/releasenotes/notes/bug-1661266-add-consistent-snapshot-support-attr-to-share-groups-DB-model-daa1d05129802796.yaml new file mode 100644 index 0000000000..3947e19a80 --- /dev/null +++ b/releasenotes/notes/bug-1661266-add-consistent-snapshot-support-attr-to-share-groups-DB-model-daa1d05129802796.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - Added 'consistent_snapshot_support' attribute to 'share_groups' DB model, + to ease possible future backport of bugfixes for 'share groups' feature.