diff --git a/ceilometerclient/v2/alarms.py b/ceilometerclient/v2/alarms.py new file mode 100644 index 0000000..2a1f8b7 --- /dev/null +++ b/ceilometerclient/v2/alarms.py @@ -0,0 +1,33 @@ +# -*- encoding: utf-8 -*- +# +# Copyright © 2013 Red Hat, Inc +# +# Author: Eoghan Glynn +# +# 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. + +from ceilometerclient.common import base +from ceilometerclient.v2 import options + + +class Alarm(base.Resource): + def __repr__(self): + return "" % self._info + + +class AlarmManager(base.Manager): + resource_class = Alarm + + def list(self, q=None): + path = '/v2/alarms' + return self._list(options.build_url(path, q)) diff --git a/ceilometerclient/v2/client.py b/ceilometerclient/v2/client.py index 71622a6..d3c64e1 100644 --- a/ceilometerclient/v2/client.py +++ b/ceilometerclient/v2/client.py @@ -14,6 +14,7 @@ # under the License. from ceilometerclient.common import http +from ceilometerclient.v2 import alarms from ceilometerclient.v2 import meters from ceilometerclient.v2 import resources from ceilometerclient.v2 import samples @@ -37,3 +38,4 @@ class Client(http.HTTPClient): self.samples = samples.SampleManager(self) self.statistics = statistics.StatisticsManager(self) self.resources = resources.ResourceManager(self) + self.alarms = alarms.AlarmManager(self) diff --git a/ceilometerclient/v2/shell.py b/ceilometerclient/v2/shell.py index f577229..946dabc 100644 --- a/ceilometerclient/v2/shell.py +++ b/ceilometerclient/v2/shell.py @@ -71,7 +71,7 @@ def do_sample_list(cc, args): @utils.arg('-q', '--query', metavar='', help='key[op]value; list.') def do_meter_list(cc, args={}): - '''List the user's meter''' + '''List the user's meters''' meters = cc.meters.list(q=options.cli_to_array(args.query)) field_labels = ['Name', 'Type', 'Unit', 'Resource ID', 'User ID', 'Project ID'] @@ -81,6 +81,23 @@ def do_meter_list(cc, args={}): sortby=0) +@utils.arg('-q', '--query', metavar='', + help='key[op]value; list.') +def do_alarm_list(cc, args={}): + '''List the user's alarms''' + alarms = cc.alarms.list(q=options.cli_to_array(args.query)) + # omit action initially to keep output width sane + # (can switch over to vertical formatting when available from CLIFF) + field_labels = ['Name', 'Description', 'Metric', 'Period', 'Count', + 'Threshold', 'Comparison', 'State', 'Enabled', 'Alarm ID', + 'User ID', 'Project ID'] + fields = ['name', 'description', 'counter_name', 'period', + 'evaluation_periods', 'threshold', 'comparison_operator', + 'state', 'enabled', 'alarm_id', 'user_id', 'project_id'] + utils.print_list(alarms, fields, field_labels, + sortby=0) + + @utils.arg('-q', '--query', metavar='', help='key[op]value; list.') def do_resource_list(cc, args={}): diff --git a/tests/v2/test_alarms.py b/tests/v2/test_alarms.py new file mode 100644 index 0000000..25dae46 --- /dev/null +++ b/tests/v2/test_alarms.py @@ -0,0 +1,94 @@ +# -*- encoding: utf-8 -*- +# +# Copyright © 2013 Red Hat, Inc +# +# Author: Eoghan Glynn +# +# 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 unittest + +import ceilometerclient.v2.alarms +from tests import utils + + +AN_ALARM = {u'alarm_actions': [u'http://site:8000/alarm'], + u'ok_actions': [u'http://site:8000/ok'], + u'description': u'An alarm', + u'matching_metadata': {u'key_name': u'key_value'}, + u'evaluation_periods': 2, + u'timestamp': u'2013-05-09T13:41:23.085000', + u'enabled': True, + u'counter_name': u'storage.objects', + u'period': 240.0, + u'alarm_id': u'alarm-id', + u'state': u'ok', + u'insufficient_data_actions': [u'http://site:8000/nodata'], + u'statistic': u'avg', + u'threshold': 200.0, + u'user_id': u'user-id', + u'project_id': u'project-id', + u'state_timestamp': u'2013-05-09T13:41:23.085000', + u'comparison_operator': 'gt', + u'name': 'SwiftObjectAlarm'} + +fixtures = { + '/v2/alarms': + { + 'GET': ( + {}, + [AN_ALARM], + ), + }, + '/v2/alarms?q.op=&q.op=&q.value=project-id&q.value=SwiftObjectAlarm' + '&q.field=project_id&q.field=name': + { + 'GET': ( + {}, + [AN_ALARM], + ), + } +} + + +class AlarmManagerTest(unittest.TestCase): + + def setUp(self): + self.api = utils.FakeAPI(fixtures) + self.mgr = ceilometerclient.v2.alarms.AlarmManager(self.api) + + def test_list_all(self): + alarms = list(self.mgr.list()) + expect = [ + ('GET', '/v2/alarms', {}, None), + ] + self.assertEqual(self.api.calls, expect) + self.assertEqual(len(alarms), 1) + self.assertEqual(alarms[0].alarm_id, 'alarm-id') + + def test_list_with_query(self): + alarms = list(self.mgr.list(q=[ + {"field": "project_id", + "value": "project-id"}, + {"field": "name", + "value": "SwiftObjectAlarm"}, + ])) + expect = [ + ('GET', + '/v2/alarms?q.op=&q.op=&q.value=project-id&q.value=' + 'SwiftObjectAlarm&q.field=project_id&q.field=name', + {}, None), + ] + self.assertEqual(self.api.calls, expect) + self.assertEqual(len(alarms), 1) + self.assertEqual(alarms[0].alarm_id, 'alarm-id')