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 <julien@danjou.info>
This commit is contained in:
Julien Danjou 2013-05-07 14:07:22 +02:00
parent 9dcdb8118a
commit 0fdf53d675

View File

@ -26,6 +26,7 @@ import operator
import os import os
import re import re
import urlparse import urlparse
import uuid
import bson.code import bson.code
import bson.objectid import bson.objectid
@ -521,35 +522,34 @@ class Connection(base.Connection):
if enabled is not None: if enabled is not None:
q['enabled'] = enabled q['enabled'] = enabled
if alarm_id is not None: if alarm_id is not None:
q['_id'] = alarm_id q['alarm_id'] = alarm_id
for alarm in self.db.alarm.find(q): for alarm in self.db.alarm.find(q):
a = {} a = {}
a.update(alarm) a.update(alarm)
a['alarm_id'] = str(a['_id'])
del a['_id'] del a['_id']
yield models.Alarm(**a) yield models.Alarm(**a)
def update_alarm(self, alarm): def update_alarm(self, alarm):
"""update 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() data = alarm.as_dict()
self.db.alarm.update( self.db.alarm.update(
{'_id': aid}, {'alarm_id': alarm.alarm_id},
{'$set': data}, {'$set': data},
upsert=True) upsert=True)
stored_alarm = self.db.alarm.find({'_id': aid})[0] stored_alarm = self.db.alarm.find({'alarm_id': alarm.alarm_id})[0]
stored_alarm['alarm_id'] = str(stored_alarm['_id'])
del stored_alarm['_id'] del stored_alarm['_id']
return models.Alarm(**stored_alarm) return models.Alarm(**stored_alarm)
def delete_alarm(self, alarm_id): def delete_alarm(self, alarm_id):
"""Delete a alarm """Delete a alarm
""" """
aid = bson.objectid.ObjectId(oid=alarm_id) self.db.alarm.remove({'alarm_id': alarm_id})
self.db.alarm.remove({'_id': aid})
def require_map_reduce(conn): def require_map_reduce(conn):