From 48edfbb6de74383e8a97d68de3e24cd496f95364 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 28 Jun 2016 12:24:46 +0300 Subject: [PATCH] added RemoveActionTrigger api fixes #431 --- apier/v1/triggers.go | 41 ++++++++++++++++++++++++++++++++++ engine/storage_interface.go | 1 + engine/storage_map.go | 7 ++++++ engine/storage_mongo_datadb.go | 6 +++++ engine/storage_redis.go | 5 +++++ 5 files changed, 60 insertions(+) diff --git a/apier/v1/triggers.go b/apier/v1/triggers.go index eff24c748..ec8645ca5 100644 --- a/apier/v1/triggers.go +++ b/apier/v1/triggers.go @@ -318,6 +318,47 @@ func (self *ApierV1) SetAccountActionTriggers(attr AttrSetAccountActionTriggers, return nil } +type AttrRemoveActionTrigger struct { + GroupID string + UniqueID string +} + +func (self *ApierV1) RemoveActionTrigger(attr AttrRemoveActionTrigger, reply *string) error { + if missing := utils.MissingStructFields(&attr, []string{"GroupID"}); len(missing) != 0 { + return utils.NewErrMandatoryIeMissing(missing...) + } + if attr.UniqueID == "" { + err := self.RatingDb.RemoveActionTriggers(attr.GroupID) + if err != nil { + *reply = err.Error() + } else { + *reply = utils.OK + } + return err + } else { + atrs, err := self.RatingDb.GetActionTriggers(attr.GroupID) + if err != nil { + *reply = err.Error() + return err + } + var remainingAtrs engine.ActionTriggers + for _, atr := range atrs { + if atr.UniqueID == attr.UniqueID { + continue + } + remainingAtrs = append(remainingAtrs, atr) + } + // set the cleared list back + err = self.RatingDb.SetActionTriggers(attr.GroupID, remainingAtrs) + if err != nil { + *reply = err.Error() + } else { + *reply = utils.OK + } + return err + } +} + type AttrSetActionTrigger struct { GroupID string UniqueID string diff --git a/engine/storage_interface.go b/engine/storage_interface.go index 7d88886b0..d5feb90fb 100644 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -65,6 +65,7 @@ type RatingStorage interface { SetSharedGroup(*SharedGroup) error GetActionTriggers(string) (ActionTriggers, error) SetActionTriggers(string, ActionTriggers) error + RemoveActionTriggers(string) error GetActionPlan(string, bool) (*ActionPlan, error) SetActionPlan(string, *ActionPlan, bool) error GetAllActionPlans() (map[string]*ActionPlan, error) diff --git a/engine/storage_map.go b/engine/storage_map.go index dfd961abc..769ce59f5 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -732,6 +732,13 @@ func (ms *MapStorage) SetActionTriggers(key string, atrs ActionTriggers) (err er return } +func (ms *MapStorage) RemoveActionTriggers(key string) (err error) { + ms.mu.Lock() + defer ms.mu.Unlock() + delete(ms.dict, utils.ACTION_TRIGGER_PREFIX+key) + return +} + func (ms *MapStorage) GetActionPlan(key string, skipCache bool) (ats *ActionPlan, err error) { ms.mu.RLock() defer ms.mu.RUnlock() diff --git a/engine/storage_mongo_datadb.go b/engine/storage_mongo_datadb.go index 9d00c226c..4fe320697 100644 --- a/engine/storage_mongo_datadb.go +++ b/engine/storage_mongo_datadb.go @@ -1320,6 +1320,12 @@ func (ms *MongoStorage) SetActionTriggers(key string, atrs ActionTriggers) (err return err } +func (ms *MongoStorage) RemoveActionTriggers(key string) error { + session, col := ms.conn(colAtr) + defer session.Close() + return col.Remove(bson.M{"key": key}) +} + func (ms *MongoStorage) GetActionPlan(key string, skipCache bool) (ats *ActionPlan, err error) { if !skipCache { if x, err := cache2go.Get(utils.ACTION_PLAN_PREFIX + key); err == nil { diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 201a4585a..3ae142b63 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -956,6 +956,11 @@ func (rs *RedisStorage) SetActionTriggers(key string, atrs ActionTriggers) (err return conn.Cmd("SET", utils.ACTION_TRIGGER_PREFIX+key, result).Err } +func (rs *RedisStorage) RemoveActionTriggers(key string) (err error) { + err = rs.db.Cmd("DEL", utils.ACTION_TRIGGER_PREFIX+key).Err + return +} + func (rs *RedisStorage) GetActionPlan(key string, skipCache bool) (ats *ActionPlan, err error) { key = utils.ACTION_PLAN_PREFIX + key if !skipCache {