From 0fdf53d675fac5946d85ee1ebfc130766d1343a7 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Tue, 7 May 2013 14:07:22 +0200 Subject: [PATCH] alarm: fix MongoDB alarm id The current implementation actually fails unit test because it tries to use alarm id as the internal MongoDB object ID. That doesn't work because MongoDB object ID must have a certain format and length. Rather than using a backend specific alarm id, let's just use an uuid based one that is not tight to the back-end. Change-Id: I9ea79fc56b8beaf1efceddeaa5e9e43b620e5e98 Signed-off-by: Julien Danjou --- ceilometer/storage/impl_mongodb.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ceilometer/storage/impl_mongodb.py b/ceilometer/storage/impl_mongodb.py index d513c0c0d..2546cfcb1 100644 --- a/ceilometer/storage/impl_mongodb.py +++ b/ceilometer/storage/impl_mongodb.py @@ -26,6 +26,7 @@ import operator import os import re import urlparse +import uuid import bson.code import bson.objectid @@ -521,35 +522,34 @@ class Connection(base.Connection): if enabled is not None: q['enabled'] = enabled if alarm_id is not None: - q['_id'] = alarm_id + q['alarm_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 """ - aid = bson.objectid.ObjectId(oid=alarm.alarm_id) + if alarm.alarm_id is None: + # This is an insert, generate an id + alarm.alarm_id = str(uuid.uuid1()) data = alarm.as_dict() self.db.alarm.update( - {'_id': aid}, + {'alarm_id': alarm.alarm_id}, {'$set': data}, upsert=True) - stored_alarm = self.db.alarm.find({'_id': aid})[0] - stored_alarm['alarm_id'] = str(stored_alarm['_id']) + stored_alarm = self.db.alarm.find({'alarm_id': alarm.alarm_id})[0] del stored_alarm['_id'] return models.Alarm(**stored_alarm) def delete_alarm(self, alarm_id): """Delete a alarm """ - aid = bson.objectid.ObjectId(oid=alarm_id) - self.db.alarm.remove({'_id': aid}) + self.db.alarm.remove({'alarm_id': alarm_id}) def require_map_reduce(conn):