From 0777c299b3c87bea75c131657345c8c0378ed949 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 29 Apr 2016 22:26:05 +0300 Subject: [PATCH] started RemActions API --- apier/v1/apier.go | 45 ++++++++++++++++++++++++++++++++++ engine/storage_interface.go | 1 + engine/storage_map.go | 7 ++++++ engine/storage_mongo_datadb.go | 4 +++ engine/storage_redis.go | 5 ++++ 5 files changed, 62 insertions(+) diff --git a/apier/v1/apier.go b/apier/v1/apier.go index 6d489468d..1ca28e9a0 100644 --- a/apier/v1/apier.go +++ b/apier/v1/apier.go @@ -1064,3 +1064,48 @@ func (self *ApierV1) GetLoadHistory(attrs utils.Paginator, reply *[]*engine.Load *reply = loadHist[offset:nrItems] return nil } + +type AttrRemActions struct { + ActionIDs []string +} + +func (self *ApierV1) RemActions(attr AttrRemActions, reply *string) error { + if attr.ActionIDs == nil { + return nil + } + stringMap := utils.NewStringMap(attr.ActionIDs...) + keys, err := self.RatingDb.GetKeysForPrefix(utils.ACTION_TRIGGER_PREFIX, true) + if err != nil { + return err + } + for _, key := range keys { + getAttrs, err := self.RatingDb.GetActionTriggers(key[len(utils.ACTION_TRIGGER_PREFIX):]) + if err != nil { + return err + } + for _, atr := range getAttrs { + if _, found := stringMap[atr.ActionsID]; found { + // found action trigger referencing action; abort + return fmt.Errorf("action %s refenced by action trigger %s", atr.ActionsID, atr.ID) + } + } + } + allAplsMap, err := self.RatingDb.GetAllActionPlans() + if err != nil { + return err + } + for _, apl := range allAplsMap { + for _, aID := range attr.ActionIDs { + if _, found := apl.AccountIDs[aID]; found { + return fmt.Errorf("action %s refenced by action plan %s", aID, apl.Id) + } + } + + } + for _, aID := range attr.ActionIDs { + if err := self.RatingDb.RemoveActions(aID); err != nil { + return err + } + } + return nil +} diff --git a/engine/storage_interface.go b/engine/storage_interface.go index 5fd6657db..327be197f 100644 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -59,6 +59,7 @@ type RatingStorage interface { SetDerivedChargers(string, *utils.DerivedChargers) error GetActions(string, bool) (Actions, error) SetActions(string, Actions) error + RemoveActions(string) error GetSharedGroup(string, bool) (*SharedGroup, error) SetSharedGroup(*SharedGroup) error GetActionTriggers(string) (ActionTriggers, error) diff --git a/engine/storage_map.go b/engine/storage_map.go index afab1a06d..f9a747a5b 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -469,6 +469,13 @@ func (ms *MapStorage) SetActions(key string, as Actions) (err error) { return } +func (ms *MapStorage) RemoveActions(key string) (err error) { + ms.mu.Lock() + defer ms.mu.Unlock() + delete(ms.dict, utils.ACTION_PREFIX+key) + return +} + func (ms *MapStorage) GetSharedGroup(key string, skipCache bool) (sg *SharedGroup, err error) { ms.mu.RLock() defer ms.mu.RUnlock() diff --git a/engine/storage_mongo_datadb.go b/engine/storage_mongo_datadb.go index 0e2e59113..70fc91dc4 100644 --- a/engine/storage_mongo_datadb.go +++ b/engine/storage_mongo_datadb.go @@ -916,6 +916,10 @@ func (ms *MongoStorage) SetActions(key string, as Actions) error { return err } +func (ms *MongoStorage) RemoveActions(key string) error { + return ms.db.C(colAct).Remove(bson.M{"key": key}) +} + func (ms *MongoStorage) GetSharedGroup(key string, skipCache bool) (sg *SharedGroup, err error) { if !skipCache { if x, err := cache2go.Get(utils.SHARED_GROUP_PREFIX + key); err == nil { diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 896181922..be591ff48 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -590,6 +590,11 @@ func (rs *RedisStorage) SetActions(key string, as Actions) (err error) { return } +func (rs *RedisStorage) RemoveActions(key string) (err error) { + err = rs.db.Cmd("DEL", utils.ACTION_PREFIX+key).Err + return +} + func (rs *RedisStorage) GetSharedGroup(key string, skipCache bool) (sg *SharedGroup, err error) { key = utils.SHARED_GROUP_PREFIX + key if !skipCache {