Implemented unique constraint for rules
Change-Id: Ia59e2d350af58a0c85269718c704cee0c50a4422
This commit is contained in:
parent
b5ee1f6d17
commit
5d697ffbab
|
@ -217,9 +217,12 @@ class ChangesWhitelistRuleCollectionHandler(CollectionHandler):
|
||||||
:http: * 200 (OK)
|
:http: * 200 (OK)
|
||||||
* 404 (dashboard entry not found in db)
|
* 404 (dashboard entry not found in db)
|
||||||
"""
|
"""
|
||||||
|
env_id = int(env_id)
|
||||||
self.get_object_or_404(objects.Cluster, env_id)
|
self.get_object_or_404(objects.Cluster, env_id)
|
||||||
rules = self.collection.get_by_env_id(env_id)
|
rules = self.collection.get_by_env_id(env_id)
|
||||||
return self.collection.to_list(rules)
|
if rules:
|
||||||
|
rules = self.collection.to_list(rules)
|
||||||
|
return rules
|
||||||
|
|
||||||
@handle_errors
|
@handle_errors
|
||||||
@serialize
|
@serialize
|
||||||
|
@ -230,12 +233,27 @@ class ChangesWhitelistRuleCollectionHandler(CollectionHandler):
|
||||||
* 400 (invalid object data specified)
|
* 400 (invalid object data specified)
|
||||||
* 409 (object with such parameters already exists)
|
* 409 (object with such parameters already exists)
|
||||||
"""
|
"""
|
||||||
|
env_id = int(env_id)
|
||||||
data = self.checked_data(
|
data = self.checked_data(
|
||||||
validate_method=self.validator.validate_one_or_multiple
|
validate_method=self.validator.validate_one_or_multiple
|
||||||
)
|
)
|
||||||
for item in data:
|
for item in data:
|
||||||
item['env_id'] = env_id
|
item['env_id'] = env_id
|
||||||
|
|
||||||
|
rules = self.collection.get_by_env_id(env_id)
|
||||||
|
existing_rules = []
|
||||||
|
if rules:
|
||||||
|
rules = self.collection.to_list(rules)
|
||||||
|
existing_rules = filter(
|
||||||
|
lambda i: len(filter(lambda o:
|
||||||
|
i['fuel_task'] == o['fuel_task'] and
|
||||||
|
i['rule'] == o['rule'],
|
||||||
|
rules)) > 0,
|
||||||
|
data
|
||||||
|
)
|
||||||
|
if existing_rules:
|
||||||
|
raise self.http(409, existing_rules)
|
||||||
|
|
||||||
new_objs = []
|
new_objs = []
|
||||||
try:
|
try:
|
||||||
for item in data:
|
for item in data:
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
# 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 unique constaint to whitelist rule-task-env
|
||||||
|
|
||||||
|
Revision ID: 954d4c3a76be
|
||||||
|
Revises: fc4f164a7b6c
|
||||||
|
Create Date: 2016-11-15 10:36:06.950577
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '954d4c3a76be'
|
||||||
|
down_revision = 'fc4f164a7b6c'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
from alembic import context
|
||||||
|
from alembic import op
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
table_prefix = context.config.get_main_option('table_prefix')
|
||||||
|
op.create_unique_constraint('_env_id_rule_task_unique',
|
||||||
|
table_prefix + 'changes_whitelist',
|
||||||
|
['env_id', 'rule', 'fuel_task'])
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
pass
|
|
@ -220,9 +220,6 @@ class ChangesWhitelistRuleCollection(NailgunCollection):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_by_env_id(self, env_id):
|
def get_by_env_id(self, env_id):
|
||||||
whitelist = set()
|
whitelist = filter(lambda r: r.env_id == env_id,
|
||||||
for rule in ChangesWhitelistRuleCollection.all():
|
ChangesWhitelistRuleCollection.all())
|
||||||
if env_id == rule.env_id:
|
|
||||||
whitelist.add(rule)
|
|
||||||
|
|
||||||
return whitelist
|
return whitelist
|
||||||
|
|
Loading…
Reference in New Issue