Add complex query support for alarm history

Change-Id: Ie41d9a0be73a525ae105262fb5859960bf9f9df6
This commit is contained in:
Ildiko Vancsa
2014-03-11 18:23:01 +01:00
parent 10686a6189
commit 298b586a06
5 changed files with 158 additions and 3 deletions

View File

@@ -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))

View File

@@ -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())

View File

@@ -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)

View File

@@ -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'

View File

@@ -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)