diff --git a/aodh/api/controllers/v2/alarm_rules/event.py b/aodh/api/controllers/v2/alarm_rules/event.py new file mode 100644 index 000000000..60d0fbe86 --- /dev/null +++ b/aodh/api/controllers/v2/alarm_rules/event.py @@ -0,0 +1,61 @@ +# +# Copyright 2015 NEC Corporation. +# +# 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. + +import wsme +from wsme import types as wtypes + +from aodh.api.controllers.v2 import base +from aodh.i18n import _ + + +class AlarmEventRule(base.AlarmRule): + """Alarm Event Rule. + + Describe when to trigger the alarm based on an event + """ + + event_type = wsme.wsattr(wtypes.text) + "The type of event (default is '*')" + + query = wsme.wsattr([base.Query]) + "The query to find the event (default is [])" + + def __init__(self, event_type=None, query=None): + event_type = event_type or '*' + query = [base.Query(**q) for q in query or []] + super(AlarmEventRule, self).__init__(event_type=event_type, + query=query) + + @classmethod + def validate_alarm(cls, alarm): + for i in alarm.event_rule.query: + i._get_value_as_type() + + @property + def default_description(self): + return _('Alarm when %s event occurred.') % self.event_type + + def as_dict(self): + rule = self.as_dict_from_keys(['event_type']) + rule['query'] = [q.as_dict() for q in self.query] + return rule + + @classmethod + def sample(cls): + return cls(event_type='compute.instance.update', + query=[{'field': 'traits.instance_id"', + 'value': '153462d0-a9b8-4b5b-8175-9e4b05e9b856', + 'op': 'eq', + 'type': 'string'}]) diff --git a/aodh/tests/api/v2/test_alarm_scenarios.py b/aodh/tests/api/v2/test_alarm_scenarios.py index 55a0f89a3..1951e9ab5 100644 --- a/aodh/tests/api/v2/test_alarm_scenarios.py +++ b/aodh/tests/api/v2/test_alarm_scenarios.py @@ -2959,3 +2959,73 @@ class TestAlarmsRuleGnocchi(TestAlarmsBase): json['gnocchi_aggregation_by_resources_threshold_rule']['query'] = ( expected_query) self._verify_alarm(json, alarms[0]) + + +class TestAlarmsEvent(TestAlarmsBase): + + def test_list_alarms(self): + alarm = models.Alarm(name='event.alarm.1', + type='event', + enabled=True, + alarm_id='h', + description='h', + state='insufficient data', + severity='moderate', + state_timestamp=constants.MIN_DATETIME, + timestamp=constants.MIN_DATETIME, + ok_actions=[], + insufficient_data_actions=[], + alarm_actions=[], + repeat_actions=False, + user_id=self.auth_headers['X-User-Id'], + project_id=self.auth_headers['X-Project-Id'], + time_constraints=[], + rule=dict(event_type='event.test', + query=[]), + ) + self.alarm_conn.update_alarm(alarm) + + data = self.get_json('/alarms') + self.assertEqual(1, len(data)) + self.assertEqual(set(['event.alarm.1']), + set(r['name'] for r in data)) + self.assertEqual(set(['event.test']), + set(r['event_rule']['event_type'] + for r in data if 'event_rule' in r)) + + def test_post_event_alarm_defaults(self): + to_check = { + 'enabled': True, + 'name': 'added_alarm_defaults', + 'state': 'insufficient data', + 'description': 'Alarm when * event occurred.', + 'type': 'event', + 'ok_actions': [], + 'alarm_actions': [], + 'insufficient_data_actions': [], + 'repeat_actions': False, + 'rule': { + 'event_type': '*', + 'query': [], + } + } + + json = { + 'name': 'added_alarm_defaults', + 'type': 'event', + 'event_rule': { + 'event_type': '*', + 'query': [] + } + } + self.post_json('/alarms', params=json, status=201, + headers=self.auth_headers) + alarms = list(self.alarm_conn.get_alarms()) + self.assertEqual(1, len(alarms)) + for alarm in alarms: + if alarm.name == 'added_alarm_defaults': + for key in to_check: + self.assertEqual(to_check[key], getattr(alarm, key)) + break + else: + self.fail("Alarm not found") diff --git a/setup.cfg b/setup.cfg index 345916143..bdb177a4f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -41,6 +41,7 @@ aodh.alarm.rule = gnocchi_resources_threshold = aodh.api.controllers.v2.alarm_rules.gnocchi:MetricOfResourceRule gnocchi_aggregation_by_metrics_threshold = aodh.api.controllers.v2.alarm_rules.gnocchi:AggregationMetricsByIdLookupRule gnocchi_aggregation_by_resources_threshold = aodh.api.controllers.v2.alarm_rules.gnocchi:AggregationMetricByResourcesLookupRule + event = aodh.api.controllers.v2.alarm_rules.event:AlarmEventRule aodh.evaluator = threshold = aodh.evaluator.threshold:ThresholdEvaluator