diff --git a/ceilometer/api/controllers/v2/alarms.py b/ceilometer/api/controllers/v2/alarms.py index a0c6ca2ad..9334cf37e 100644 --- a/ceilometer/api/controllers/v2/alarms.py +++ b/ceilometer/api/controllers/v2/alarms.py @@ -440,11 +440,12 @@ class Alarm(base.Base): ALARMS_RULES[alarm.type].plugin.validate_alarm(alarm) - 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 base.ClientSideError(error) + if alarm.time_constraints: + 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 base.ClientSideError(error) return alarm @@ -513,7 +514,9 @@ class Alarm(base.Base): if k.endswith('_rule'): del d[k] d['rule'] = getattr(self, "%s_rule" % self.type).as_dict() - d['time_constraints'] = [tc.as_dict() for tc in self.time_constraints] + if self.time_constraints: + d['time_constraints'] = [tc.as_dict() + for tc in self.time_constraints] return d Alarm.add_attributes(**{"%s_rule" % ext.name: ext.plugin diff --git a/ceilometer/tests/api/v2/test_alarm_scenarios.py b/ceilometer/tests/api/v2/test_alarm_scenarios.py index 1f8e5dee2..7a46f6752 100644 --- a/ceilometer/tests/api/v2/test_alarm_scenarios.py +++ b/ceilometer/tests/api/v2/test_alarm_scenarios.py @@ -458,6 +458,19 @@ class TestAlarms(v2.FunctionalTest, alarms = list(self.alarm_conn.get_alarms()) self.assertEqual(4, len(alarms)) + def test_post_alarm_null_time_constraint(self): + json = { + 'name': 'added_alarm_invalid_constraint_duration', + 'type': 'threshold', + 'time_constraints': None, + 'threshold_rule': { + 'meter_name': 'ameter', + 'threshold': 300.0 + } + } + self.post_json('/alarms', params=json, status=201, + headers=self.auth_headers) + def test_post_invalid_alarm_time_constraint_duration(self): json = { 'name': 'added_alarm_invalid_constraint_duration',