From 6fdd6454b3044288d334489430b515035c65fa53 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Wed, 4 Sep 2013 17:45:21 +0200 Subject: [PATCH] alarm: rename counter_name to meter_name This is a follow-up wrt the recent fix that handled in Ceilometer for the alarming API. Change-Id: Ide73a2e703bf31652b819c6d5170891c73b77ec9 --- ceilometerclient/tests/v2/test_alarms.py | 30 +++++++++++++++++++++++- ceilometerclient/v2/alarms.py | 14 ++++++++++- ceilometerclient/v2/shell.py | 12 +++++----- 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/ceilometerclient/tests/v2/test_alarms.py b/ceilometerclient/tests/v2/test_alarms.py index b0c22f43..727dcc08 100644 --- a/ceilometerclient/tests/v2/test_alarms.py +++ b/ceilometerclient/tests/v2/test_alarms.py @@ -30,7 +30,7 @@ AN_ALARM = {u'alarm_actions': [u'http://site:8000/alarm'], u'evaluation_periods': 2, u'timestamp': u'2013-05-09T13:41:23.085000', u'enabled': True, - u'counter_name': u'storage.objects', + u'meter_name': u'storage.objects', u'period': 240.0, u'alarm_id': u'alarm-id', u'state': u'ok', @@ -49,6 +49,7 @@ del CREATE_ALARM['state_timestamp'] del CREATE_ALARM['alarm_id'] DELTA_ALARM = {u'alarm_actions': ['url1', 'url2'], u'comparison_operator': u'lt', + u'meter_name': u'foobar', u'threshold': 42.1} UPDATED_ALARM = copy.deepcopy(AN_ALARM) UPDATED_ALARM.update(DELTA_ALARM) @@ -144,6 +145,18 @@ class AlarmManagerTest(testtools.TestCase): self.assertEqual(self.api.calls, expect) self.assertTrue(alarm) + def test_create_legacy(self): + create = {} + create.update(CREATE_ALARM) + create['counter_name'] = CREATE_ALARM['meter_name'] + del create['meter_name'] + alarm = self.mgr.create(**create) + expect = [ + ('POST', '/v2/alarms', {}, CREATE_ALARM), + ] + self.assertEqual(self.api.calls, expect) + self.assertTrue(alarm) + def test_update(self): alarm = self.mgr.update(alarm_id='alarm-id', **DELTA_ALARM) expect = [ @@ -155,6 +168,21 @@ class AlarmManagerTest(testtools.TestCase): for (key, value) in DELTA_ALARM.iteritems(): self.assertEqual(getattr(alarm, key), value) + def test_update_legacy(self): + delta = {} + delta.update(DELTA_ALARM) + delta['counter_name'] = DELTA_ALARM['meter_name'] + del delta['meter_name'] + alarm = self.mgr.update(alarm_id='alarm-id', **delta) + expect = [ + ('PUT', '/v2/alarms/alarm-id', {}, DELTA_ALARM), + ] + self.assertEqual(self.api.calls, expect) + self.assertTrue(alarm) + self.assertEqual(alarm.alarm_id, 'alarm-id') + for (key, value) in DELTA_ALARM.iteritems(): + self.assertEqual(getattr(alarm, key), value) + def test_delete(self): deleted = self.mgr.delete(alarm_id='victim-id') expect = [ diff --git a/ceilometerclient/v2/alarms.py b/ceilometerclient/v2/alarms.py index dde7430e..97323066 100644 --- a/ceilometerclient/v2/alarms.py +++ b/ceilometerclient/v2/alarms.py @@ -16,6 +16,8 @@ # License for the specific language governing permissions and limitations # under the License. +import warnings + from ceilometerclient.common import base from ceilometerclient.v2 import options @@ -26,7 +28,7 @@ UPDATABLE_ATTRIBUTES = [ 'evaluation_periods', 'state', 'enabled', - 'counter_name', + 'meter_name', 'statistic', 'comparison_operator', 'threshold', @@ -60,12 +62,22 @@ class AlarmManager(base.Manager): except IndexError: return None + @staticmethod + def _compat_counter_rename_kwargs(kwargs): + # NOTE(jd) Compatibility with Havana-2 API + if 'counter_name' in kwargs: + warnings.warn("counter_name has been renamed to meter_name", + DeprecationWarning) + kwargs['meter_name'] = kwargs['counter_name'] + def create(self, **kwargs): + self._compat_counter_rename_kwargs(kwargs) new = dict((key, value) for (key, value) in kwargs.items() if key in CREATION_ATTRIBUTES) return self._create(self._path(), new) def update(self, alarm_id, **kwargs): + self._compat_counter_rename_kwargs(kwargs) updated = dict((key, value) for (key, value) in kwargs.items() if key in UPDATABLE_ATTRIBUTES) return self._update(self._path(alarm_id), updated) diff --git a/ceilometerclient/v2/shell.py b/ceilometerclient/v2/shell.py index 36599888..4724d0b8 100644 --- a/ceilometerclient/v2/shell.py +++ b/ceilometerclient/v2/shell.py @@ -136,7 +136,7 @@ def do_alarm_list(cc, args={}): field_labels = ['Name', 'Description', 'Metric', 'Period', 'Count', 'Threshold', 'Comparison', 'State', 'Enabled', 'Alarm ID', 'User ID', 'Project ID'] - fields = ['name', 'description', 'counter_name', 'period', + fields = ['name', 'description', 'meter_name', 'period', 'evaluation_periods', 'threshold', 'comparison_operator', 'state', 'enabled', 'alarm_id', 'user_id', 'project_id'] utils.print_list(alarms, fields, field_labels, @@ -144,7 +144,7 @@ def do_alarm_list(cc, args={}): def _display_alarm(alarm): - fields = ['name', 'description', 'counter_name', 'period', + fields = ['name', 'description', 'meter_name', 'period', 'evaluation_periods', 'threshold', 'comparison_operator', 'state', 'enabled', 'alarm_id', 'user_id', 'project_id', 'alarm_actions', 'ok_actions', 'insufficient_data_actions', @@ -185,7 +185,7 @@ def do_alarm_show(cc, args={}): help='State of the alarm, one of: ' + str(ALARM_STATES)) @utils.arg('--enabled', type=utils.string_to_bool, metavar='{True|False}', help='True if alarm evaluation/actioning is enabled') -@utils.arg('--counter-name', metavar='', +@utils.arg('--meter-name', metavar='', help='Metric to evaluate against') @utils.arg('--statistic', metavar='', help='Statistic to evaluate, one of: ' + str(STATISTICS)) @@ -212,7 +212,7 @@ def do_alarm_show(cc, args={}): @utils.arg('--matching-metadata', dest='matching_metadata', metavar='', action='append', default=None, help=('A meter should match this resource metadata (key=value) ' - 'additionally to the counter_name')) + 'additionally to the meter_name')) def do_alarm_create(cc, args={}): '''Create a new alarm.''' fields = dict(filter(lambda x: not (x[1] is None), vars(args).items())) @@ -233,7 +233,7 @@ def do_alarm_create(cc, args={}): help='State of the alarm, one of: ' + str(ALARM_STATES)) @utils.arg('--enabled', type=utils.string_to_bool, metavar='{True|False}', help='True if alarm evaluation/actioning is enabled') -@utils.arg('--counter-name', metavar='', +@utils.arg('--meter-name', metavar='', help='Metric to evaluate against') @utils.arg('--statistic', metavar='', help='Statistic to evaluate, one of: ' + str(STATISTICS)) @@ -260,7 +260,7 @@ def do_alarm_create(cc, args={}): @utils.arg('--matching-metadata', dest='matching_metadata', metavar='', action='append', default=None, help=('A meter should match this resource metadata (key=value) ' - 'additionally to the counter_name')) + 'additionally to the meter_name')) def do_alarm_update(cc, args={}): '''Update an existing alarm.''' fields = dict(filter(lambda x: not (x[1] is None), vars(args).items()))