Fixes duplicated names in alarm time constraints

Currently, an alarm can have multiple time constraints
with the same name. Consequently the constraints aren't
uniquely identifiable and cannot be edited correctly.

This patch adds duplicate checking.

Change-Id: I6654e13b249db53c2107b43fe36ddacedf67be67
Closes-bug: #1293994
This commit is contained in:
Nejc Saje 2014-03-18 13:38:04 +00:00
parent 79bb83f340
commit 9cb360866b
2 changed files with 35 additions and 0 deletions

View File

@ -1737,6 +1737,12 @@ class Alarm(_Base):
if not alarms:
raise EntityNotFound(_('Alarm'), id)
tc_names = [tc.name for tc in alarm.time_constraints]
if len(tc_names) > len(set(tc_names)):
error = _("Time constraint names must be "
"unique for a given alarm.")
raise ClientSideError(error)
return alarm
@classmethod

View File

@ -338,6 +338,35 @@ class TestAlarms(FunctionalTest,
alarms = list(self.conn.get_alarms())
self.assertEqual(4, len(alarms))
def test_post_duplicate_time_constraint_name(self):
json = {
'name': 'added_alarm_duplicate_constraint_name',
'type': 'threshold',
'time_constraints': [
{
'name': 'testcons',
'start': '* 11 * * *',
'duration': 10
},
{
'name': 'testcons',
'start': '* * * * *',
'duration': 20
}
],
'threshold_rule': {
'meter_name': 'ameter',
'threshold': 300.0
}
}
resp = self.post_json('/alarms', params=json, expect_errors=True,
status=400, headers=self.auth_headers)
self.assertEqual(
"Time constraint names must be unique for a given alarm.",
resp.json['error_message']['faultstring'])
alarms = list(self.conn.get_alarms())
self.assertEqual(4, len(alarms))
def test_post_invalid_alarm_time_constraint_duration(self):
json = {
'name': 'added_alarm_invalid_constraint_duration',