From 48edfbb6de74383e8a97d68de3e24cd496f95364 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 28 Jun 2016 12:24:46 +0300 Subject: [PATCH 1/2] 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 { From 14ce5f8e1e917df59673b93e11589bde179b3669 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 28 Jun 2016 12:25:31 +0300 Subject: [PATCH 2/2] triggers_remove console command --- console/trigger_remove.go | 63 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 console/trigger_remove.go diff --git a/console/trigger_remove.go b/console/trigger_remove.go new file mode 100644 index 000000000..eac88f768 --- /dev/null +++ b/console/trigger_remove.go @@ -0,0 +1,63 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2012-2015 ITsysCOM + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package console + +import "github.com/cgrates/cgrates/apier/v1" + +func init() { + c := &CmdRemoveTriggers{ + name: "triggers_remove", + rpcMethod: "ApierV1.RemoveActionTrigger", + rpcParams: &v1.AttrRemoveActionTrigger{}, + } + commands[c.Name()] = c + c.CommandExecuter = &CommandExecuter{c} +} + +// Commander implementation +type CmdRemoveTriggers struct { + name string + rpcMethod string + rpcParams *v1.AttrRemoveActionTrigger + *CommandExecuter +} + +func (self *CmdRemoveTriggers) Name() string { + return self.name +} + +func (self *CmdRemoveTriggers) RpcMethod() string { + return self.rpcMethod +} + +func (self *CmdRemoveTriggers) RpcParams(reset bool) interface{} { + if reset || self.rpcParams == nil { + self.rpcParams = &v1.AttrRemoveActionTrigger{} + } + return self.rpcParams +} + +func (self *CmdRemoveTriggers) PostprocessRpcParams() error { + return nil +} + +func (self *CmdRemoveTriggers) RpcResult() interface{} { + var s string + return &s +}