From 179a93559643f05b068ca72857187983976bc48c Mon Sep 17 00:00:00 2001 From: whoami-rajat Date: Wed, 25 Apr 2018 04:34:52 +0000 Subject: [PATCH] Updating alarm for different alarm types This patch will reflect the value of "type" parameter during parsing of "aodh update" command which add the functionality to update the alarm between different alarm types. Change-Id: I5ca9a9a28c509a6767c7d77796edae9a5e8ce0dc Closes-Bug: #1757994 --- aodhclient/tests/functional/test_alarm.py | 178 ++++++++++++++++++++++ aodhclient/v2/alarm.py | 56 +++++-- aodhclient/v2/alarm_cli.py | 7 +- 3 files changed, 226 insertions(+), 15 deletions(-) diff --git a/aodhclient/tests/functional/test_alarm.py b/aodhclient/tests/functional/test_alarm.py index 4b1349c..4b092c6 100644 --- a/aodhclient/tests/functional/test_alarm.py +++ b/aodhclient/tests/functional/test_alarm.py @@ -336,6 +336,52 @@ class AodhClientTest(base.ClientTestBase): self.assertEqual('ok', state_get['state']) self.aodh('alarm', params='delete %s' % alarm_id) + def test_update_type_event_composite(self): + + res_id = uuidutils.generate_uuid() + # CREATE + result = self.aodh(u'alarm', + params=(u"create --type event --name ev_alarm123")) + alarm = self.details_multiple(result)[0] + ALARM_ID = alarm['alarm_id'] + self.assertEqual('ev_alarm123', alarm['name']) + self.assertEqual('*', alarm['event_type']) + + # UPDATE TYPE TO COMPOSITE + result = self.aodh( + 'alarm', + params=('update %s --type composite --composite-rule ' + '\'{"or":[{"threshold": 0.8, "metric": "cpu_util", ' + '"type": "gnocchi_resources_threshold", "resource_type": ' + '"generic", "resource_id": "%s", ' + '"aggregation_method": "mean"},' + '{"and": [{"threshold": 200, "metric": "disk.iops", ' + '"type": "gnocchi_resources_threshold", "resource_type": ' + '"generic", "resource_id": "%s", ' + '"aggregation_method": "mean"},' + '{"threshold": 1000, "metric": "memory",' + '"type": "gnocchi_resources_threshold", "resource_type": ' + '"generic", "resource_id": "%s", ' + '"aggregation_method": "mean"}]}]}\'' % + (ALARM_ID, res_id, res_id, res_id))) + alarm_updated = self.details_multiple(result)[0] + self.assertEqual(ALARM_ID, alarm_updated["alarm_id"]) + self.assertEqual('composite', alarm_updated['type']) + self.assertIn('composite_rule', alarm_updated) + + # UPDATE TYPE TO EVENT + result = self.aodh( + 'alarm', params=("update %s --type event" + % ALARM_ID)) + alarm_updated = self.details_multiple(result)[0] + self.assertEqual(ALARM_ID, alarm_updated["alarm_id"]) + self.assertEqual('event', alarm_updated['type']) + self.assertEqual('*', alarm_updated['event_type']) + + # DELETE + result = self.aodh('alarm', params="delete %s" % ALARM_ID) + self.assertEqual("", result) + class AodhClientGnocchiRulesTest(base.ClientTestBase): @@ -713,3 +759,135 @@ class AodhClientGnocchiRulesTest(base.ClientTestBase): self.assertEqual(sorted(output_colums), sorted(alarm_list.keys())) self.assertNotIn(ALARM_ID, [r['alarm_id'] for r in self.parser.listing(result)]) + + def test_update_gnresthr_gnaggrresthr(self): + + RESOURCE_ID = uuidutils.generate_uuid() + # CREATE + result = self.aodh(u'alarm', + params=(u"create " + "--type gnocchi_resources_threshold " + "--name alarm_gn123 --metric cpu_util " + "--resource-id %s --threshold 80 " + "--resource-type generic " + "--aggregation-method last " + % RESOURCE_ID)) + alarm = self.details_multiple(result)[0] + ALARM_ID = alarm['alarm_id'] + self.assertEqual('alarm_gn123', alarm['name']) + self.assertEqual('cpu_util', alarm['metric']) + self.assertEqual('80.0', alarm['threshold']) + self.assertEqual('last', alarm['aggregation_method']) + self.assertEqual('generic', alarm['resource_type']) + + # UPDATE TYPE TO GNOCCHI_AGGREGATION_BY_RESOURCES_THRESHOLD + result = self.aodh( + 'alarm', params=("update %s --type " + "gnocchi_aggregation_by_resources_threshold " + "--metric cpu --threshold 90 " + "--query " + '\'{"=": {"creator": "cr3at0r"}}\' ' + "--resource-type generic " + "--aggregation-method last " + % ALARM_ID)) + alarm_updated = self.details_multiple(result)[0] + self.assertEqual(ALARM_ID, alarm_updated["alarm_id"]) + self.assertEqual('cpu', alarm_updated['metric']) + self.assertEqual('90.0', alarm_updated['threshold']) + self.assertEqual('last', alarm_updated['aggregation_method']) + self.assertEqual('generic', alarm_updated['resource_type']) + self.assertEqual('{"=": {"creator": "cr3at0r"}}', + alarm_updated['query']) + self.assertEqual('gnocchi_aggregation_by_resources_threshold', + alarm_updated['type']) + + # UPDATE TYPE TO GNOCCHI_RESOURCES_THRESHOLD + result = self.aodh( + 'alarm', params=("update %s " + "--type gnocchi_resources_threshold " + "--metric cpu_util " + "--resource-id %s --threshold 80 " + "--resource-type generic " + "--aggregation-method last " + % (ALARM_ID, RESOURCE_ID))) + alarm_updated = self.details_multiple(result)[0] + self.assertEqual(ALARM_ID, alarm_updated["alarm_id"]) + self.assertEqual('cpu_util', alarm_updated['metric']) + self.assertEqual('80.0', alarm_updated['threshold']) + self.assertEqual('last', alarm_updated['aggregation_method']) + self.assertEqual('generic', alarm_updated['resource_type']) + self.assertEqual('gnocchi_resources_threshold', + alarm_updated['type']) + + # DELETE + result = self.aodh('alarm', params="delete %s" % ALARM_ID) + self.assertEqual("", result) + + def test_update_gnaggrresthr_gnaggrmetricthr(self): + + METRIC1 = 'cpu' + METRIC2 = 'cpu_util' + + # CREATE + result = self.aodh( + u'alarm', + params=(u"create " + "--type " + "gnocchi_aggregation_by_resources_threshold " + "--name alarm123 --metric cpu --threshold 80 " + "--query " + '\'{"=": {"creator": "cr3at0r"}}\' ' + "--resource-type generic " + "--aggregation-method last ")) + alarm = self.details_multiple(result)[0] + ALARM_ID = alarm['alarm_id'] + self.assertEqual('alarm123', alarm['name']) + self.assertEqual('cpu', alarm['metric']) + self.assertEqual('80.0', alarm['threshold']) + self.assertEqual('last', alarm['aggregation_method']) + self.assertEqual('generic', alarm['resource_type']) + self.assertEqual('{"=": {"creator": "cr3at0r"}}', + alarm['query']) + + # UPDATE TYPE TO GNOCCHI_AGGREGATION_BY_METRICS_THRESHOLD + result = self.aodh( + 'alarm', params=("update %s --type " + "gnocchi_aggregation_by_metrics_threshold " + "--metrics %s " + "--metrics %s " + "--threshold 80 " + "--aggregation-method last" + % (ALARM_ID, METRIC1, METRIC2))) + alarm_updated = self.details_multiple(result)[0] + self.assertEqual(ALARM_ID, alarm_updated["alarm_id"]) + metrics = "[u'cpu', u'cpu_util']" if six.PY2 else "['cpu', 'cpu_util']" + self.assertEqual(metrics, alarm_updated['metrics']) + self.assertEqual('80.0', alarm_updated['threshold']) + self.assertEqual('last', alarm_updated['aggregation_method']) + self.assertEqual('gnocchi_aggregation_by_metrics_threshold', + alarm_updated['type']) + + # UPDATE TYPE TO GNOCCHI_AGGREGATION_BY_RESOURCES_THRESHOLD + result = self.aodh( + 'alarm', params=("update %s --type " + "gnocchi_aggregation_by_resources_threshold " + "--metric cpu --threshold 80 " + "--query " + '\'{"=": {"creator": "cr3at0r"}}\' ' + "--resource-type generic " + "--aggregation-method last " + % ALARM_ID)) + alarm_updated = self.details_multiple(result)[0] + self.assertEqual(ALARM_ID, alarm_updated["alarm_id"]) + self.assertEqual('cpu', alarm_updated['metric']) + self.assertEqual('80.0', alarm_updated['threshold']) + self.assertEqual('last', alarm_updated['aggregation_method']) + self.assertEqual('generic', alarm_updated['resource_type']) + self.assertEqual('{"=": {"creator": "cr3at0r"}}', + alarm_updated['query']) + self.assertEqual('gnocchi_aggregation_by_resources_threshold', + alarm_updated['type']) + + # DELETE + result = self.aodh('alarm', params="delete %s" % ALARM_ID) + self.assertEqual("", result) diff --git a/aodhclient/v2/alarm.py b/aodhclient/v2/alarm.py index 6b2ae20..a3e52da 100644 --- a/aodhclient/v2/alarm.py +++ b/aodhclient/v2/alarm.py @@ -110,29 +110,61 @@ class AlarmManager(base.Manager): :type attributes: dict """ alarm = self._get(self.url + '/' + alarm_id).json() - self._clean_rules(alarm['type'], alarm_update) + if 'type' not in alarm_update: + self._clean_rules(alarm['type'], alarm_update) + else: + self._clean_rules(alarm_update['type'], alarm_update) if 'event_rule' in alarm_update: - alarm['event_rule'].update(alarm_update.get('event_rule')) + if ('type' in alarm_update and + alarm_update['type'] != alarm['type']): + alarm.pop('%s_rule' % alarm['type'], None) + alarm['event_rule'] = alarm_update['event_rule'] + else: + alarm['event_rule'].update(alarm_update.get('event_rule')) alarm_update.pop('event_rule') elif 'gnocchi_resources_threshold_rule' in alarm_update: - alarm['gnocchi_resources_threshold_rule'].update( - alarm_update.get('gnocchi_resources_threshold_rule')) + if ('type' in alarm_update and + alarm_update['type'] != alarm['type']): + alarm.pop('%s_rule' % alarm['type'], None) + alarm['gnocchi_resources_threshold_rule'] = alarm_update[ + 'gnocchi_resources_threshold_rule'] + else: + alarm['gnocchi_resources_threshold_rule'].update( + alarm_update.get('gnocchi_resources_threshold_rule')) alarm_update.pop('gnocchi_resources_threshold_rule') elif 'gnocchi_aggregation_by_metrics_threshold_rule' in alarm_update: - alarm['gnocchi_aggregation_by_metrics_threshold_rule'].update( - alarm_update.get( - 'gnocchi_aggregation_by_metrics_threshold_rule')) + if ('type' in alarm_update and + alarm_update['type'] != alarm['type']): + alarm.pop('%s_rule' % alarm['type'], None) + alarm['gnocchi_aggregation_by_metrics_threshold_rule'] = \ + alarm_update[ + 'gnocchi_aggregation_by_metrics_threshold_rule'] + else: + alarm['gnocchi_aggregation_by_metrics_threshold_rule'].update( + alarm_update.get( + 'gnocchi_aggregation_by_metrics_threshold_rule')) alarm_update.pop('gnocchi_aggregation_by_metrics_threshold_rule') elif 'gnocchi_aggregation_by_resources_threshold_rule' in alarm_update: - alarm['gnocchi_aggregation_by_resources_threshold_rule'].update( - alarm_update.get( - 'gnocchi_aggregation_by_resources_threshold_rule')) + if ('type' in alarm_update and + alarm_update['type'] != alarm['type']): + alarm.pop('%s_rule' % alarm['type'], None) + alarm['gnocchi_aggregation_by_resources_threshold_rule'] = \ + alarm_update[ + 'gnocchi_aggregation_by_resources_threshold_rule'] + else: + alarm['gnocchi_aggregation_by_resources_threshold_rule'].\ + update(alarm_update.get( + 'gnocchi_aggregation_by_resources_threshold_rule')) alarm_update.pop( 'gnocchi_aggregation_by_resources_threshold_rule') elif 'composite_rule' in alarm_update: - if alarm_update['composite_rule']: - alarm['composite_rule'] = alarm_update['composite_rule'] + if ('type' in alarm_update and + alarm_update['type'] != alarm['type']): + alarm.pop('%s_rule' % alarm['type'], None) + if alarm_update['composite_rule'] is not None: + alarm['composite_rule'] = alarm_update[ + 'composite_rule'] alarm_update.pop('composite_rule') alarm.update(alarm_update) diff --git a/aodhclient/v2/alarm_cli.py b/aodhclient/v2/alarm_cli.py index d94fb81..787de5f 100644 --- a/aodhclient/v2/alarm_cli.py +++ b/aodhclient/v2/alarm_cli.py @@ -372,9 +372,10 @@ class CliAlarmCreate(show.ShowOne): def _alarm_from_args(self, parsed_args): alarm = utils.dict_from_parsed_args( - parsed_args, ['name', 'project_id', 'user_id', 'description', - 'state', 'severity', 'enabled', 'alarm_actions', - 'ok_actions', 'insufficient_data_actions', + parsed_args, ['name', 'type', 'project_id', 'user_id', + 'description', 'state', 'severity', 'enabled', + 'alarm_actions', 'ok_actions', + 'insufficient_data_actions', 'time_constraints', 'repeat_actions']) if parsed_args.type == 'event' and parsed_args.query: parsed_args.query = utils.cli_to_array(parsed_args.query)