aboutsummaryrefslogtreecommitdiffstats
path: root/python_moondb/python_moondb/migrate_repo/versions/001_moon.py
blob: 13670b91b18113860b8d7f64ef51961c5f733b4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the 'Apache-2.0'
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.

import json
import sqlalchemy as sql
from sqlalchemy import types as sql_types


class JsonBlob(sql_types.TypeDecorator):
    impl = sql.Text

    def process_bind_param(self, value, dialect):
        return json.dumps(value)

    def process_result_value(self, value, dialect):
        return json.loads(value)


def upgrade(migrate_engine):
    meta = sql.MetaData()
    meta.bind = migrate_engine

    table = sql.Table(
        'pdp',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('name', sql.String(256), nullable=False),
        sql.Column('keystone_project_id', sql.String(64), nullable=True, default=""),
        sql.Column('value', JsonBlob(), nullable=True),
        sql.UniqueConstraint('name', 'keystone_project_id', name='unique_constraint_models'),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    table.create(migrate_engine, checkfirst=True)

    table = sql.Table(
        'policies',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('name', sql.String(256), nullable=False),
        sql.Column('model_id', sql.String(64), nullable=True, default=""),
        sql.Column('value', JsonBlob(), nullable=True),
        sql.UniqueConstraint('name', 'model_id', name='unique_constraint_models'),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    table.create(migrate_engine, checkfirst=True)

    table = sql.Table(
        'models',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('name', sql.String(256), nullable=False),
        sql.Column('value', JsonBlob(), nullable=True),
        sql.UniqueConstraint('name', name='unique_constraint_models'),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    table.create(migrate_engine, checkfirst=True)

    subject_categories_table = sql.Table(
        'subject_categories',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('name', sql.String(256), nullable=False),
        sql.Column('description', sql.String(256), nullable=True),

        sql.UniqueConstraint('name', name='unique_constraint_subject_categories'),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    subject_categories_table.create(migrate_engine, checkfirst=True)

    object_categories_table = sql.Table(
        'object_categories',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('name', sql.String(256), nullable=False),
        sql.Column('description', sql.String(256), nullable=True),

        sql.UniqueConstraint('name', name='unique_constraint_object_categories'),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    object_categories_table.create(migrate_engine, checkfirst=True)

    action_categories_table = sql.Table(
        'action_categories',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('name', sql.String(256), nullable=False),
        sql.Column('description', sql.String(256), nullable=True),

        sql.UniqueConstraint('name', name='unique_constraint_action_categories'),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    action_categories_table.create(migrate_engine, checkfirst=True)

    subjects_table = sql.Table(
        'subjects',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('name', sql.String(256), nullable=False),
        sql.Column('value', JsonBlob(), nullable=True),
        sql.UniqueConstraint('name', name='unique_constraint_subjects'),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    subjects_table.create(migrate_engine, checkfirst=True)

    objects_table = sql.Table(
        'objects',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('name', sql.String(256), nullable=False),
        sql.Column('value', JsonBlob(), nullable=True),
        sql.UniqueConstraint('name', name='unique_constraint_objects'),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    objects_table.create(migrate_engine, checkfirst=True)

    actions_table = sql.Table(
        'actions',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('name', sql.String(256), nullable=False),
        sql.Column('value', JsonBlob(), nullable=True),
        sql.UniqueConstraint('name', name='unique_constraint_actions'),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    actions_table.create(migrate_engine, checkfirst=True)

    subject_data_table = sql.Table(
        'subject_data',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('name', sql.String(256), nullable=False),
        sql.Column('value', JsonBlob(), nullable=True),
        sql.Column('category_id', sql.ForeignKey("subject_categories.id"), nullable=False),
        sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
        sql.UniqueConstraint('name', 'category_id', 'policy_id',
                             name='unique_constraint_subject_data'),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    subject_data_table.create(migrate_engine, checkfirst=True)

    object_data_table = sql.Table(
        'object_data',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('name', sql.String(256), nullable=False),
        sql.Column('value', JsonBlob(), nullable=True),
        sql.Column('category_id', sql.ForeignKey("object_categories.id"), nullable=False),
        sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
        sql.UniqueConstraint('name', 'category_id', 'policy_id',
                             name='unique_constraint_object_data'),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    object_data_table.create(migrate_engine, checkfirst=True)

    action_data_table = sql.Table(
        'action_data',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('name', sql.String(256), nullable=False),
        sql.Column('value', JsonBlob(), nullable=True),
        sql.Column('category_id', sql.ForeignKey("action_categories.id"), nullable=False),
        sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
        sql.UniqueConstraint('name', 'category_id', 'policy_id',
                             name='unique_constraint_action_data'),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    action_data_table.create(migrate_engine, checkfirst=True)

    subject_assignments_table = sql.Table(
        'subject_assignments',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('assignments', sql.String(256), nullable=True),
        sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
        sql.Column('subject_id', sql.ForeignKey("subjects.id"), nullable=False),
        sql.Column('category_id', sql.ForeignKey("subject_categories.id"), nullable=False),
        sql.UniqueConstraint('policy_id', 'subject_id', 'category_id',
                             name='unique_constraint_subject_assignment'),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    subject_assignments_table.create(migrate_engine, checkfirst=True)

    object_assignments_table = sql.Table(
        'object_assignments',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('assignments', sql.String(256), nullable=True),
        sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
        sql.Column('object_id', sql.ForeignKey("objects.id"), nullable=False),
        sql.Column('category_id', sql.ForeignKey("object_categories.id"), nullable=False),
        sql.UniqueConstraint('policy_id', 'object_id', 'category_id',
                             name='unique_constraint_object_assignment'),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    object_assignments_table.create(migrate_engine, checkfirst=True)

    action_assignments_table = sql.Table(
        'action_assignments',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('assignments', sql.String(256), nullable=True),
        sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
        sql.Column('action_id', sql.ForeignKey("actions.id"), nullable=False),
        sql.Column('category_id', sql.ForeignKey("action_categories.id"), nullable=False),
        sql.UniqueConstraint('policy_id', 'action_id', 'category_id',
                             name='unique_constraint_action_assignment'),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    action_assignments_table.create(migrate_engine, checkfirst=True)

    meta_rules_table = sql.Table(
        'meta_rules',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('name', sql.String(256), nullable=False),
        sql.Column('subject_categories', JsonBlob(), nullable=False),
        sql.Column('object_categories', JsonBlob(), nullable=False),
        sql.Column('action_categories', JsonBlob(), nullable=False),
        sql.Column('value', JsonBlob(), nullable=True),
        sql.UniqueConstraint('name', name='unique_constraint_meta_rule_name'),
        # sql.UniqueConstraint('subject_categories', 'object_categories', 'action_categories', name='unique_constraint_meta_rule_def'),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    meta_rules_table.create(migrate_engine, checkfirst=True)

    rules_table = sql.Table(
        'rules',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('rule', JsonBlob(), nullable=True),
        sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
        sql.Column('meta_rule_id', sql.ForeignKey("meta_rules.id"), nullable=False),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    rules_table.create(migrate_engine, checkfirst=True)


def downgrade(migrate_engine):
    meta = sql.MetaData()
    meta.bind = migrate_engine

    for _table in (
            'rules',
            'meta_rules',
            'action_assignments',
            'object_assignments',
            'subject_assignments',
            'action_data',
            'object_data',
            'subject_data',
            'actions',
            'objects',
            'subjects',
            'action_categories',
            'object_categories',
            'subject_categories',
            'models',
            'policies',
            'pdp'
    ):
        try:
            table = sql.Table(_table, meta, autoload=True)
            table.drop(migrate_engine, checkfirst=True)
        except Exception as e:
            print(e)