Merge "alarm: fix MongoDB alarm id"

This commit is contained in:
Jenkins 2013-05-07 23:37:46 +00:00 committed by Gerrit Code Review
commit 574fd6962e

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