From 896015ccae49d70d1c5801d54aa08e95c0418d20 Mon Sep 17 00:00:00 2001 From: Angus Salkeld Date: Thu, 2 May 2013 13:11:11 +1000 Subject: [PATCH] Add the mongo implementation of alarms collection blueprint alarm-api Change-Id: I2c0691a788a0ca81d231802cc44a40641672b23e --- ceilometer/storage/impl_mongodb.py | 35 +++++++++++++++++++++++++++--- tests/storage/base.py | 12 ++++++++++ tests/storage/test_impl_mongodb.py | 4 ++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/ceilometer/storage/impl_mongodb.py b/ceilometer/storage/impl_mongodb.py index cfd4edcab..d513c0c0d 100644 --- a/ceilometer/storage/impl_mongodb.py +++ b/ceilometer/storage/impl_mongodb.py @@ -28,6 +28,7 @@ import re import urlparse import bson.code +import bson.objectid import pymongo from ceilometer.openstack.common import log @@ -510,17 +511,45 @@ class Connection(base.Connection): project=None, enabled=True, alarm_id=None): """Yields a lists of alarms that match filters """ - raise NotImplementedError('Alarms not implemented') + q = {} + if user is not None: + q['user_id'] = user + if project is not None: + q['project_id'] = project + if name is not None: + q['name'] = name + if enabled is not None: + q['enabled'] = enabled + if alarm_id is not None: + q['_id'] = alarm_id + + for alarm in self.db.alarm.find(q): + a = {} + a.update(alarm) + a['alarm_id'] = str(a['_id']) + del a['_id'] + yield models.Alarm(**a) def update_alarm(self, alarm): """update alarm """ - raise NotImplementedError('Alarms not implemented') + aid = bson.objectid.ObjectId(oid=alarm.alarm_id) + data = alarm.as_dict() + self.db.alarm.update( + {'_id': aid}, + {'$set': data}, + upsert=True) + + stored_alarm = self.db.alarm.find({'_id': aid})[0] + stored_alarm['alarm_id'] = str(stored_alarm['_id']) + del stored_alarm['_id'] + return models.Alarm(**stored_alarm) def delete_alarm(self, alarm_id): """Delete a alarm """ - raise NotImplementedError('Alarms not implemented') + aid = bson.objectid.ObjectId(oid=alarm_id) + self.db.alarm.remove({'_id': aid}) def require_map_reduce(conn): diff --git a/tests/storage/base.py b/tests/storage/base.py index 574a9bb8c..f0beda031 100644 --- a/tests/storage/base.py +++ b/tests/storage/base.py @@ -638,6 +638,18 @@ class AlarmTest(DBTestBase): self.assertEquals(updated.enabled, False) self.assertEquals(updated.state, models.Alarm.ALARM_INSUFFICIENT_DATA) + def test_update_llu(self): + llu = models.Alarm('llu', + 'counter_name', 'lt', 34, 'max', + 'bla', 'ffo') + updated = self.conn.update_alarm(llu) + updated.state = models.Alarm.ALARM_OK + updated.description = ':)' + self.conn.update_alarm(updated) + + all = list(self.conn.get_alarms()) + self.assertEquals(len(all), 1) + def test_delete(self): self.add_some_alarms() victim = list(self.conn.get_alarms(name='orange-alert'))[0] diff --git a/tests/storage/test_impl_mongodb.py b/tests/storage/test_impl_mongodb.py index 6b853cb0f..37833868d 100644 --- a/tests/storage/test_impl_mongodb.py +++ b/tests/storage/test_impl_mongodb.py @@ -97,6 +97,10 @@ class StatisticsTest(base.StatisticsTest, MongoDBEngineTestBase): require_map_reduce(self.conn) +class AlarmTest(base.AlarmTest, MongoDBEngineTestBase): + pass + + class CompatibilityTest(MongoDBEngineTestBase): def prepare_data(self):