Enforce unique tenant_id/name for alarm definitions and notifications
Change-Id: Id49c654ea4f9d129438bb30ee7560e634d1d501f
This commit is contained in:
@@ -137,6 +137,24 @@ class NotificationsRepository(mysql_repository.MySQLRepository,
|
||||
else:
|
||||
raise exceptions.DoesNotExistException
|
||||
|
||||
@mysql_repository.mysql_try_catch_block
|
||||
def find_notification_by_name(self, tenant_id, name):
|
||||
cnxn, cursor = self._get_cnxn_cursor_tuple()
|
||||
parms = [tenant_id, name]
|
||||
|
||||
with cnxn:
|
||||
query = """
|
||||
select *
|
||||
from notification_method
|
||||
where tenant_id = %s and name = %s
|
||||
"""
|
||||
rows = self._execute_query(query, parms)
|
||||
|
||||
if rows:
|
||||
return rows[0]
|
||||
else:
|
||||
return None
|
||||
|
||||
@mysql_repository.mysql_try_catch_block
|
||||
def update_notification(
|
||||
self, id, tenant_id, name, type, address):
|
||||
|
||||
@@ -211,6 +211,26 @@ class AlarmDefinitions(alarm_definitions_api_v2.AlarmDefinitionsV2API,
|
||||
self._alarm_definition_delete(tenant_id, alarm_definition_id)
|
||||
res.status = falcon.HTTP_204
|
||||
|
||||
def _validate_name_not_conflicting(self, tenant_id, name, expected_id=None):
|
||||
definitions = self._alarm_definitions_repo.get_alarm_definitions(tenant_id=tenant_id,
|
||||
name=name,
|
||||
dimensions=None,
|
||||
offset=None,
|
||||
limit=0)
|
||||
if definitions:
|
||||
if not expected_id:
|
||||
LOG.warn("Found existing definition for {} with tenant_id {}".format(name, tenant_id))
|
||||
raise exceptions.AlreadyExistsException("An alarm definition with the name {} already exists"
|
||||
.format(name))
|
||||
|
||||
found_definition_id = definitions[0]['id']
|
||||
if found_definition_id != expected_id:
|
||||
LOG.warn("Found existing alarm definition for {} with tenant_id {} with unexpected id {}"
|
||||
.format(name, tenant_id, found_definition_id))
|
||||
raise exceptions.AlreadyExistsException(
|
||||
"An alarm definition with the name {} already exists with id {}"
|
||||
.format(name, found_definition_id))
|
||||
|
||||
@resource.resource_try_catch_block
|
||||
def _alarm_definition_show(self, tenant_id, id):
|
||||
|
||||
@@ -328,7 +348,7 @@ class AlarmDefinitions(alarm_definitions_api_v2.AlarmDefinitionsV2API,
|
||||
|
||||
@resource.resource_try_catch_block
|
||||
def _alarm_definition_update_or_patch(self, tenant_id,
|
||||
id,
|
||||
definition_id,
|
||||
name,
|
||||
expression,
|
||||
actions_enabled,
|
||||
@@ -355,10 +375,12 @@ class AlarmDefinitions(alarm_definitions_api_v2.AlarmDefinitionsV2API,
|
||||
else:
|
||||
sub_expr_list = None
|
||||
|
||||
self._validate_name_not_conflicting(tenant_id, name, expected_id=definition_id)
|
||||
|
||||
alarm_def_row, sub_alarm_def_dicts = (
|
||||
self._alarm_definitions_repo.update_or_patch_alarm_definition(
|
||||
tenant_id,
|
||||
id,
|
||||
definition_id,
|
||||
name,
|
||||
expression,
|
||||
sub_expr_list,
|
||||
@@ -389,7 +411,7 @@ class AlarmDefinitions(alarm_definitions_api_v2.AlarmDefinitionsV2API,
|
||||
|
||||
alarm_def_event_dict = (
|
||||
{u'tenantId': tenant_id,
|
||||
u'alarmDefinitionId': id,
|
||||
u'alarmDefinitionId': definition_id,
|
||||
u'alarmName': name,
|
||||
u'alarmDescription': description,
|
||||
u'alarmExpression': expression,
|
||||
@@ -456,6 +478,8 @@ class AlarmDefinitions(alarm_definitions_api_v2.AlarmDefinitionsV2API,
|
||||
expression.encode('utf8'), str(ex.column).encode('utf8'))
|
||||
raise falcon.HTTPBadRequest(title, msg)
|
||||
|
||||
self._validate_name_not_conflicting(tenant_id, name)
|
||||
|
||||
alarm_definition_id = (
|
||||
self._alarm_definitions_repo.
|
||||
create_alarm_definition(tenant_id,
|
||||
|
||||
@@ -18,6 +18,7 @@ from oslo_log import log
|
||||
import simport
|
||||
|
||||
from monasca_api.api import notifications_api_v2
|
||||
from monasca_api.common.repositories import exceptions
|
||||
from monasca_api.v2.common.schemas import (
|
||||
notifications_request_body_schema as schemas_notifications)
|
||||
from monasca_api.v2.common.schemas import exceptions as schemas_exceptions
|
||||
@@ -50,12 +51,32 @@ class Notifications(notifications_api_v2.NotificationsV2API):
|
||||
LOG.debug(ex)
|
||||
raise falcon.HTTPBadRequest('Bad request', ex.message)
|
||||
|
||||
def _validate_name_not_conflicting(self, tenant_id, name, expected_id=None):
|
||||
notification = self._notifications_repo.find_notification_by_name(tenant_id, name)
|
||||
|
||||
if notification:
|
||||
if not expected_id:
|
||||
LOG.warn("Found existing notification method for {} with tenant_id {}".format(name, tenant_id))
|
||||
raise exceptions.AlreadyExistsException(
|
||||
"A notification method with the name {} already exists".format(name))
|
||||
|
||||
found_notification_id = notification['id']
|
||||
if found_notification_id != expected_id:
|
||||
LOG.warn("Found existing notification method for {} with tenant_id {} with unexpected id {}"
|
||||
.format(name, tenant_id, found_notification_id))
|
||||
raise exceptions.AlreadyExistsException(
|
||||
"A notification method with name {} already exists with id {}"
|
||||
.format(name, found_notification_id))
|
||||
|
||||
@resource.resource_try_catch_block
|
||||
def _create_notification(self, tenant_id, notification, uri):
|
||||
|
||||
name = notification['name']
|
||||
notification_type = notification['type'].upper()
|
||||
address = notification['address']
|
||||
|
||||
self._validate_name_not_conflicting(tenant_id, name)
|
||||
|
||||
notification_id = self._notifications_repo.create_notification(
|
||||
tenant_id,
|
||||
name,
|
||||
@@ -69,16 +90,19 @@ class Notifications(notifications_api_v2.NotificationsV2API):
|
||||
uri)
|
||||
|
||||
@resource.resource_try_catch_block
|
||||
def _update_notification(self, id, tenant_id, notification, uri):
|
||||
def _update_notification(self, notification_id, tenant_id, notification, uri):
|
||||
|
||||
name = notification['name']
|
||||
notification_type = notification['type'].upper()
|
||||
address = notification['address']
|
||||
self._notifications_repo.update_notification(id, tenant_id, name,
|
||||
|
||||
self._validate_name_not_conflicting(tenant_id, name, expected_id=notification_id)
|
||||
|
||||
self._notifications_repo.update_notification(notification_id, tenant_id, name,
|
||||
notification_type,
|
||||
address)
|
||||
|
||||
return self._create_notification_response(id,
|
||||
return self._create_notification_response(notification_id,
|
||||
name,
|
||||
notification_type,
|
||||
address,
|
||||
|
||||
Reference in New Issue
Block a user