Adding alarm list filtering by state and meter

Adding alarm list filtering by state for SQLAlchemy, PyMongo and
HBase drivers
Adding alarm list filtering by meter only for SQLAlchemy and PyMongo
drivers.

Closes-Bug: #1224298

Change-Id: I393e2988142878d33363438671c240199ede65a4
This commit is contained in:
Christian Martinez 2014-05-29 20:40:11 +00:00
parent 97748958b6
commit ab82354b5b
6 changed files with 67 additions and 7 deletions

View File

@ -231,7 +231,7 @@ class Connection(object):
raise NotImplementedError('Statistics not implemented')
@staticmethod
def get_alarms(name=None, user=None,
def get_alarms(name=None, user=None, state=None, meter=None,
project=None, enabled=None, alarm_id=None, pagination=None):
"""Yields a lists of alarms that match filters."""
raise NotImplementedError('Alarms not implemented')

View File

@ -248,14 +248,16 @@ class Connection(base.Connection):
alarm_table = conn.table(self.ALARM_TABLE)
alarm_table.delete(alarm_id)
def get_alarms(self, name=None, user=None,
def get_alarms(self, name=None, user=None, state=None, meter=None,
project=None, enabled=None, alarm_id=None, pagination=None):
if pagination:
raise NotImplementedError('Pagination not implemented')
if meter:
raise NotImplementedError('Filter by meter not implemented')
q = make_query(alarm_id=alarm_id, name=name, enabled=enabled,
user_id=user, project_id=project)
user_id=user, project_id=project, state=state)
with self.conn_pool.connection() as conn:
alarm_table = conn.table(self.ALARM_TABLE)

View File

@ -132,7 +132,7 @@ class Connection(base.Connection):
"""
return []
def get_alarms(self, name=None, user=None,
def get_alarms(self, name=None, user=None, state=None, meter=None,
project=None, enabled=None, alarm_id=None, pagination=None):
"""Yields a lists of alarms that match filters
"""

View File

@ -685,10 +685,12 @@ class Connection(base.Connection):
def _retrieve_alarms(self, query):
return (self._row_to_alarm_model(x) for x in query.all())
def get_alarms(self, name=None, user=None,
def get_alarms(self, name=None, user=None, state=None, meter=None,
project=None, enabled=None, alarm_id=None, pagination=None):
"""Yields a lists of alarms that match filters
:param user: Optional ID for user that owns the resource.
:param state: Optional string for alarm state.
:param meter: Optional string for alarms associated with meter.
:param project: Optional ID for project that owns the resource.
:param enabled: Optional boolean to list disable alarm.
:param alarm_id: Optional alarm_id to return one alarm.
@ -710,8 +712,18 @@ class Connection(base.Connection):
query = query.filter(models.Alarm.project_id == project)
if alarm_id is not None:
query = query.filter(models.Alarm.alarm_id == alarm_id)
if state is not None:
query = query.filter(models.Alarm.state == state)
return self._retrieve_alarms(query)
alarms = self._retrieve_alarms(query)
# TODO(cmart): improve this by using sqlalchemy.func factory
if meter is not None:
alarms = filter(lambda row:
row.rule.get('meter_name', None) == meter,
alarms)
return alarms
def create_alarm(self, alarm):
"""Create an alarm.

View File

@ -269,11 +269,13 @@ class Connection(base.Connection):
[("timestamp", pymongo.DESCENDING)],
limit)
def get_alarms(self, name=None, user=None,
def get_alarms(self, name=None, user=None, state=None, meter=None,
project=None, enabled=None, alarm_id=None, pagination=None):
"""Yields a lists of alarms that match filters
:param name: The Alarm name.
:param user: Optional ID for user that owns the resource.
:param state: Optional string for alarm state.
:param meter: Optional string for alarms associated with meter.
:param project: Optional ID for project that owns the resource.
:param enabled: Optional boolean to list disable alarm.
:param alarm_id: Optional alarm_id to return one alarm.
@ -293,6 +295,10 @@ class Connection(base.Connection):
q['enabled'] = enabled
if alarm_id is not None:
q['alarm_id'] = alarm_id
if state is not None:
q['state'] = state
if meter is not None:
q['rule.meter_name'] = meter
return self._retrieve_alarms(q, [], None)

View File

@ -195,6 +195,46 @@ class TestAlarms(FunctionalTest,
'Unknown argument: "timestamp": '
'not valid for this resource')
def test_alarms_query_with_meter(self):
resp = self.get_json('/alarms',
q=[{'field': 'meter',
'op': 'eq',
'value': 'meter.mine'}],
)
self.assertEqual(1, len(resp))
self.assertEqual('c',
resp[0]['alarm_id'])
self.assertEqual('meter.mine',
resp[0]
['threshold_rule']
['meter_name'])
def test_alarms_query_with_state(self):
alarm = models.Alarm(name='disabled',
type='combination',
enabled=False,
alarm_id='d',
description='d',
state='ok',
state_timestamp=None,
timestamp=None,
ok_actions=[],
insufficient_data_actions=[],
alarm_actions=[],
repeat_actions=False,
user_id=self.auth_headers['X-User-Id'],
project_id=self.auth_headers['X-Project-Id'],
time_constraints=[],
rule=dict(alarm_ids=['a', 'b'], operator='or'))
self.conn.update_alarm(alarm)
resp = self.get_json('/alarms',
q=[{'field': 'state',
'op': 'eq',
'value': 'ok'}],
)
self.assertEqual(1, len(resp))
self.assertEqual('ok', resp[0]['state'])
def test_get_not_existing_alarm(self):
resp = self.get_json('/alarms/alarm-id-3', expect_errors=True)
self.assertEqual(404, resp.status_code)