76926fa6a5d28034129b6f6a67637c95f3c0cebe
[openstack-build/neutron-build.git] / neutron / db / migration / alembic_migrations / versions / liberty / contract / 4ffceebfada_rbac_network.py
1 # Copyright 2015 OpenStack Foundation
2 #
3 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
4 #    not use this file except in compliance with the License. You may obtain
5 #    a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #    Unless required by applicable law or agreed to in writing, software
10 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 #    License for the specific language governing permissions and limitations
13 #    under the License.
14 #
15
16 """network_rbac
17
18 Revision ID: 4ffceebfada
19 Revises: 30018084ec99
20 Create Date: 2015-06-14 13:12:04.012457
21
22 """
23
24 # revision identifiers, used by Alembic.
25 revision = '4ffceebfada'
26 down_revision = '30018084ec99'
27 depends_on = ('8675309a5c4f',)
28
29 from alembic import op
30 from oslo_utils import uuidutils
31 import sqlalchemy as sa
32
33
34 # A simple model of the networks table with only the fields needed for
35 # the migration.
36 network = sa.Table('networks', sa.MetaData(),
37                    sa.Column('id', sa.String(length=36), nullable=False),
38                    sa.Column('tenant_id', sa.String(length=255)),
39                    sa.Column('shared', sa.Boolean(), nullable=False))
40
41 networkrbacs = sa.Table(
42     'networkrbacs', sa.MetaData(),
43     sa.Column('id', sa.String(length=36), nullable=False),
44     sa.Column('object_id', sa.String(length=36), nullable=False),
45     sa.Column('tenant_id', sa.String(length=255), nullable=True,
46               index=True),
47     sa.Column('target_tenant', sa.String(length=255), nullable=False),
48     sa.Column('action', sa.String(length=255), nullable=False))
49
50
51 def upgrade():
52     op.bulk_insert(networkrbacs, get_values())
53     op.drop_column('networks', 'shared')
54     # the shared column on subnets was just an internal representation of the
55     # shared status of the network it was related to. This is now handled by
56     # other logic so we just drop it.
57     op.drop_column('subnets', 'shared')
58
59
60 def get_values():
61     session = sa.orm.Session(bind=op.get_bind())
62     values = []
63     for row in session.query(network).filter(network.c.shared).all():
64         values.append({'id': uuidutils.generate_uuid(), 'object_id': row[0],
65                        'tenant_id': row[1], 'target_tenant': '*',
66                        'action': 'access_as_shared'})
67     # this commit appears to be necessary to allow further operations
68     session.commit()
69     return values