diff options
author | Jenkins <jenkins@review.openstack.org> | 2014-03-25 23:09:17 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2014-03-25 23:09:17 +0000 |
commit | 53d5a22f30f3e413318033d76627b4f68bd60af2 (patch) | |
tree | 96159437a1e36b170a65a7293fd9298dd9399d85 | |
parent | efb2fc33877ea76313a70f03ee724d11f698ffb4 (diff) | |
parent | 298b586a0669271e5fe228e9ce3882be3b34e1f0 (diff) | |
download | python-ceilometerclient-53d5a22f30f3e413318033d76627b4f68bd60af2.tar.gz |
Merge "Add complex query support for alarm history"
-rw-r--r-- | ceilometerclient/tests/v2/test_query_alarm_history.py | 61 | ||||
-rw-r--r-- | ceilometerclient/tests/v2/test_shell.py | 60 | ||||
-rw-r--r-- | ceilometerclient/v2/client.py | 5 | ||||
-rw-r--r-- | ceilometerclient/v2/query.py | 8 | ||||
-rw-r--r-- | ceilometerclient/v2/shell.py | 27 |
5 files changed, 158 insertions, 3 deletions
diff --git a/ceilometerclient/tests/v2/test_query_alarm_history.py b/ceilometerclient/tests/v2/test_query_alarm_history.py new file mode 100644 index 0000000..2a6cfb4 --- /dev/null +++ b/ceilometerclient/tests/v2/test_query_alarm_history.py @@ -0,0 +1,61 @@ +# Copyright Ericsson AB 2014. All rights reserved +# +# Authors: Ildiko Vancsa <ildiko.vancsa@ericsson.com> +# +# 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.tests import utils +from ceilometerclient.v2 import query + + +ALARMCHANGE = {"alarm_id": "e8ff32f772a44a478182c3fe1f7cad6a", + "event_id": "c74a8611-6553-4764-a860-c15a6aabb5d0", + "detail": "{\"threshold\": 42.0, \"evaluation_periods\": 4}", + "on_behalf_of": "92159030020611e3b26dde429e99ee8c", + "project_id": "b6f16144010811e387e4de429e99ee8c", + "timestamp": "2014-03-11T16:02:58.376261", + "type": "rule change", + "user_id": "3e5d11fda79448ac99ccefb20be187ca" + } + +QUERY = {"filter": {"and": [{">": {"timestamp": "2014-03-11T16:02:58"}}, + {"=": {"type": "rule change"}}]}, + "orderby": [{"timestamp": "desc"}], + "limit": 10} + +base_url = '/v2/query/alarms/history' +fixtures = { + base_url: + { + 'POST': ( + {}, + [ALARMCHANGE], + ), + }, +} + + +class QueryAlarmsManagerTest(utils.BaseTestCase): + + def setUp(self): + super(QueryAlarmsManagerTest, self).setUp() + self.api = utils.FakeAPI(fixtures) + self.mgr = query.QueryAlarmHistoryManager(self.api) + + def test_query(self): + alarm_history = self.mgr.query(**QUERY) + expect = [ + ('POST', '/v2/query/alarms/history', {}, QUERY), + ] + self.assertEqual(expect, self.api.calls) + self.assertEqual(1, len(alarm_history)) diff --git a/ceilometerclient/tests/v2/test_shell.py b/ceilometerclient/tests/v2/test_shell.py index 3d8aca2..f6b59c2 100644 --- a/ceilometerclient/tests/v2/test_shell.py +++ b/ceilometerclient/tests/v2/test_shell.py @@ -1,6 +1,7 @@ # Copyright Ericsson AB 2014. All rights reserved # -# Author: Balazs Gibizer <balazs.gibizer@ericsson.com> +# Authors: Balazs Gibizer <balazs.gibizer@ericsson.com> +# Ildiko Vancsa <ildiko.vancsa@ericsson.com> # # 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 @@ -536,3 +537,60 @@ class ShellQueryAlarmsCommandTest(utils.BaseTestCase): +------------+--------------------------------------------------------------\ ----------------------------------------+ ''', output.getvalue()) + + +class ShellQueryAlarmHistoryCommandTest(utils.BaseTestCase): + + ALARM_HISTORY = [{"alarm_id": "e8ff32f772a44a478182c3fe1f7cad6a", + "event_id": "c74a8611-6553-4764-a860-c15a6aabb5d0", + "detail": + "{\"threshold\": 42.0, \"evaluation_periods\": 4}", + "on_behalf_of": "92159030020611e3b26dde429e99ee8c", + "project_id": "b6f16144010811e387e4de429e99ee8c", + "timestamp": "2014-03-11T16:02:58.376261", + "type": "rule change", + "user_id": "3e5d11fda79448ac99ccefb20be187ca" + }] + + QUERY = {"filter": {"and": [{">": {"timestamp": "2014-03-11T16:02:58"}}, + {"=": {"type": "rule change"}}]}, + "orderby": [{"timestamp": "desc"}], + "limit": 10} + + def setUp(self): + super(ShellQueryAlarmHistoryCommandTest, self).setUp() + self.cc = mock.Mock() + self.args = mock.Mock() + self.args.filter = self.QUERY["filter"] + self.args.orderby = self.QUERY["orderby"] + self.args.limit = self.QUERY["limit"] + + def test_query(self): + + ret_alarm_history = [alarms.AlarmChange(mock.Mock(), alarm_history) + for alarm_history in self.ALARM_HISTORY] + self.cc.query_alarm_history.query.return_value = ret_alarm_history + org_stdout = sys.stdout + try: + sys.stdout = output = six.StringIO() + ceilometer_shell.do_query_alarm_history(self.cc, self.args) + finally: + sys.stdout = org_stdout + + self.assertEqual('''\ ++----------------------------------+--------------------------------------+-\ +------------+----------------------------------------------+----------------\ +------------+ +| Alarm ID | Event ID | \ +Type | Detail | Timestamp \ + | ++----------------------------------+--------------------------------------+-\ +------------+----------------------------------------------+----------------\ +------------+ +| e8ff32f772a44a478182c3fe1f7cad6a | c74a8611-6553-4764-a860-c15a6aabb5d0 | \ +rule change | {"threshold": 42.0, "evaluation_periods": 4} | 2014-03-11T16:0\ +2:58.376261 | ++----------------------------------+--------------------------------------+-\ +------------+----------------------------------------------+----------------\ +------------+ +''', output.getvalue()) diff --git a/ceilometerclient/v2/client.py b/ceilometerclient/v2/client.py index b6aadbf..b9ead19 100644 --- a/ceilometerclient/v2/client.py +++ b/ceilometerclient/v2/client.py @@ -1,6 +1,7 @@ # Copyright Ericsson AB 2014. All rights reserved # -# Author: Balazs Gibizer <balazs.gibizer@ericsson.com> +# Authors: Balazs Gibizer <balazs.gibizer@ericsson.com> +# Ildiko Vancsa <ildiko.vancsa@ericsson.com> # # 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 @@ -54,3 +55,5 @@ class Client(object): self.http_client) self.query_alarms = query.QueryAlarmsManager( self.http_client) + self.query_alarm_history = query.QueryAlarmHistoryManager( + self.http_client) diff --git a/ceilometerclient/v2/query.py b/ceilometerclient/v2/query.py index 97608a7..a615e2f 100644 --- a/ceilometerclient/v2/query.py +++ b/ceilometerclient/v2/query.py @@ -1,6 +1,7 @@ # Copyright Ericsson AB 2014. All rights reserved # -# Author: Balazs Gibizer <balazs.gibizer@ericsson.com> +# Authors: Balazs Gibizer <balazs.gibizer@ericsson.com> +# Ildiko Vancsa <ildiko.vancsa@ericsson.com> # # 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 @@ -49,3 +50,8 @@ class QuerySamplesManager(QueryManager): class QueryAlarmsManager(QueryManager): resource_class = alarms.Alarm path_suffix = '/alarms' + + +class QueryAlarmHistoryManager(QueryManager): + resource_class = alarms.AlarmChange + path_suffix = '/alarms/history' diff --git a/ceilometerclient/v2/shell.py b/ceilometerclient/v2/shell.py index 36873c6..3656d67 100644 --- a/ceilometerclient/v2/shell.py +++ b/ceilometerclient/v2/shell.py @@ -5,6 +5,7 @@ # # Authors: Angus Salkeld <asalkeld@redhat.com> # Balazs Gibizer <balazs.gibizer@ericsson.com> +# Ildiko Vancsa <ildiko.vancsa@ericsson.com> # # 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 @@ -699,3 +700,29 @@ def do_query_alarms(cc, args): utils.print_list(alarms, fields, field_labels, formatters={'rule': alarm_rule_formatter}, sortby=None) + + +@utils.arg('-f', '--filter', metavar='<FILTER>', + help=('{complex_op: [{simple_op: {field_name: value}}]} ' + 'The complex_op is one of: ' + str(COMPLEX_OPERATORS) + ', ' + 'simple_op is one of: ' + str(SIMPLE_OPERATORS) + '.')) +@utils.arg('-o', '--orderby', metavar='<ORDERBY>', + help=('[{field_name: direction}, {field_name: direction}] ' + 'The direction is one of: ' + str(ORDER_DIRECTIONS) + '.')) +@utils.arg('-l', '--limit', metavar='<LIMIT>', + help='Maximum number of alarm history items to return.') +def do_query_alarm_history(cc, args): + '''Query Alarm History.''' + fields = {'filter': args.filter, + 'orderby': args.orderby, + 'limit': args.limit} + try: + alarm_history = cc.query_alarm_history.query(**fields) + except exc.HTTPNotFound: + raise exc.CommandError('Alarm history not found') + else: + field_labels = ['Alarm ID', 'Event ID', 'Type', 'Detail', 'Timestamp'] + fields = ['alarm_id', 'event_id', 'type', 'detail', 'timestamp'] + utils.print_list(alarm_history, fields, field_labels, + formatters={'rule': alarm_change_detail_formatter}, + sortby=None) |