diff --git a/apier/v1/accounts.go b/apier/v1/accounts.go index fa2991869..15f660133 100644 --- a/apier/v1/accounts.go +++ b/apier/v1/accounts.go @@ -21,7 +21,6 @@ package v1 import ( "fmt" "math" - "regexp" "strings" "time" @@ -131,61 +130,6 @@ func (self *ApierV1) RemActionTiming(attrs AttrRemActionTiming, reply *string) e return nil } -// Returns a list of ActionTriggers on an account -func (self *ApierV1) GetAccountActionTriggers(attrs AttrAcntAction, reply *engine.ActionTriggers) error { - if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account"}); len(missing) != 0 { - return utils.NewErrMandatoryIeMissing(missing...) - } - if balance, err := self.AccountDb.GetAccount(utils.AccountKey(attrs.Tenant, attrs.Account)); err != nil { - return utils.NewErrServerError(err) - } else { - *reply = balance.ActionTriggers - } - return nil -} - -type AttrRemAcntActionTriggers struct { - Tenant string // Tenant he account belongs to - Account string // Account name - ActionTriggersId string // Id filtering only specific id to remove (can be regexp pattern) - ActionTriggersUniqueId string -} - -// Returns a list of ActionTriggers on an account -func (self *ApierV1) RemAccountActionTriggers(attrs AttrRemAcntActionTriggers, reply *string) error { - if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account"}); len(missing) != 0 { - return utils.NewErrMandatoryIeMissing(missing...) - } - accID := utils.AccountKey(attrs.Tenant, attrs.Account) - _, err := engine.Guardian.Guard(func() (interface{}, error) { - ub, err := self.AccountDb.GetAccount(accID) - if err != nil { - return 0, err - } - nactrs := make(engine.ActionTriggers, 0) - for _, actr := range ub.ActionTriggers { - match, _ := regexp.MatchString(attrs.ActionTriggersId, actr.ID) - if len(attrs.ActionTriggersId) != 0 && match { - continue - } - if len(attrs.ActionTriggersUniqueId) != 0 && attrs.ActionTriggersUniqueId == actr.UniqueID { - continue - } - nactrs = append(nactrs, actr) - } - ub.ActionTriggers = nactrs - if err := self.AccountDb.SetAccount(ub); err != nil { - return 0, err - } - return 0, nil - }, 0, accID) - if err != nil { - return utils.NewErrServerError(err) - } - *reply = OK - return nil -} - // Ads a new account into dataDb. If already defined, returns success. func (self *ApierV1) SetAccount(attr utils.AttrSetAccount, reply *string) error { if missing := utils.MissingStructFields(&attr, []string{"Tenant", "Account"}); len(missing) != 0 { diff --git a/apier/v1/apier_local_test.go b/apier/v1/apier_local_test.go index 10ced4435..fb28458b8 100644 --- a/apier/v1/apier_local_test.go +++ b/apier/v1/apier_local_test.go @@ -1009,20 +1009,19 @@ func TestApierAddTriggeredAction(t *testing.T) { } reply := "" // Add balance to a previously known account - attrs := &AttrAddActionTrigger{ActionTriggersId: "STTR", ActionTriggersUniqueId: "1", Tenant: "cgrates.org", Account: "dan2", BalanceDirection: "*out", BalanceType: "*monetary", - ThresholdType: "*min_balance", ThresholdValue: 2, BalanceDestinationIds: "*any", Weight: 10, ActionsId: "WARN_VIA_HTTP"} - if err := rater.Call("ApierV1.AddTriggeredAction", attrs, &reply); err != nil { - t.Error("Got error on ApierV1.AddTriggeredAction: ", err.Error()) + attrs := &AttrSetAccountActionTriggers{ActionTriggerIDs: &[]string{"STANDARD_TRIGGERS"}, Tenant: "cgrates.org", Account: "dan2"} + if err := rater.Call("ApierV1.SetAccountActionTriggers", attrs, &reply); err != nil { + t.Error("Got error on ApierV1.SetAccountActionTriggers: ", err.Error()) } else if reply != "OK" { - t.Errorf("Calling ApierV1.AddTriggeredAction received: %s", reply) + t.Errorf("Calling ApierV1.SetAccountActionTriggers received: %s", reply) } reply2 := "" - attrs2 := new(AttrAddActionTrigger) + attrs2 := new(AttrSetAccountActionTriggers) *attrs2 = *attrs attrs2.Account = "dan10" // Does not exist so it should error when adding triggers on it // Add trigger to an account which does n exist - if err := rater.Call("ApierV1.AddTriggeredAction", attrs2, &reply2); err == nil || reply2 == "OK" { - t.Error("Expecting error on ApierV1.AddTriggeredAction.", err, reply2) + if err := rater.Call("ApierV1.SetAccountActionTriggers", attrs2, &reply2); err == nil || reply2 == "OK" { + t.Error("Expecting error on ApierV1.SetAccountActionTriggers.", err, reply2) } } diff --git a/apier/v1/triggers.go b/apier/v1/triggers.go index 59791e49b..ae39f7d13 100644 --- a/apier/v1/triggers.go +++ b/apier/v1/triggers.go @@ -1,14 +1,71 @@ package v1 import ( + "regexp" + "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" ) +// Returns a list of ActionTriggers on an account +func (self *ApierV1) GetAccountActionTriggers(attrs AttrAcntAction, reply *engine.ActionTriggers) error { + if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account"}); len(missing) != 0 { + return utils.NewErrMandatoryIeMissing(missing...) + } + if balance, err := self.AccountDb.GetAccount(utils.AccountKey(attrs.Tenant, attrs.Account)); err != nil { + return utils.NewErrServerError(err) + } else { + *reply = balance.ActionTriggers + } + return nil +} + +type AttrRemAcntActionTriggers struct { + Tenant string // Tenant he account belongs to + Account string // Account name + ActionTriggersId string // Id filtering only specific id to remove (can be regexp pattern) + ActionTriggersUniqueId string +} + +// Returns a list of ActionTriggers on an account +func (self *ApierV1) RemAccountActionTriggers(attrs AttrRemAcntActionTriggers, reply *string) error { + if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account"}); len(missing) != 0 { + return utils.NewErrMandatoryIeMissing(missing...) + } + accID := utils.AccountKey(attrs.Tenant, attrs.Account) + _, err := engine.Guardian.Guard(func() (interface{}, error) { + ub, err := self.AccountDb.GetAccount(accID) + if err != nil { + return 0, err + } + nactrs := make(engine.ActionTriggers, 0) + for _, actr := range ub.ActionTriggers { + match, _ := regexp.MatchString(attrs.ActionTriggersId, actr.ID) + if len(attrs.ActionTriggersId) != 0 && match { + continue + } + if len(attrs.ActionTriggersUniqueId) != 0 && attrs.ActionTriggersUniqueId == actr.UniqueID { + continue + } + nactrs = append(nactrs, actr) + } + ub.ActionTriggers = nactrs + if err := self.AccountDb.SetAccount(ub); err != nil { + return 0, err + } + return 0, nil + }, 0, accID) + if err != nil { + return utils.NewErrServerError(err) + } + *reply = OK + return nil +} + type AttrSetAccountActionTriggers struct { Tenant string Account string - ActionTriggersIDs *[]string + ActionTriggerIDs *[]string ActionTriggerOverwrite bool } @@ -24,11 +81,11 @@ func (self *ApierV1) SetAccountActionTriggers(attr AttrSetAccountActionTriggers, } else { return 0, err } - if attr.ActionTriggersIDs != nil { + if attr.ActionTriggerIDs != nil { if attr.ActionTriggerOverwrite { account.ActionTriggers = make(engine.ActionTriggers, 0) } - for _, actionTriggerID := range *attr.ActionTriggersIDs { + for _, actionTriggerID := range *attr.ActionTriggerIDs { atrs, err := self.RatingDb.GetActionTriggers(actionTriggerID) if err != nil { diff --git a/console/triggeredaction_add.go b/console/triggeredaction_add.go deleted file mode 100644 index 298782b6c..000000000 --- a/console/triggeredaction_add.go +++ /dev/null @@ -1,62 +0,0 @@ -/* -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 := &CmdAddTriggeredAction{ - name: "triggeredaction_add", - rpcMethod: "ApierV1.AddTriggeredAction", - } - commands[c.Name()] = c - c.CommandExecuter = &CommandExecuter{c} -} - -// Commander implementation -type CmdAddTriggeredAction struct { - name string - rpcMethod string - rpcParams *v1.AttrAddActionTrigger - *CommandExecuter -} - -func (self *CmdAddTriggeredAction) Name() string { - return self.name -} - -func (self *CmdAddTriggeredAction) RpcMethod() string { - return self.rpcMethod -} - -func (self *CmdAddTriggeredAction) RpcParams(reset bool) interface{} { - if reset || self.rpcParams == nil { - self.rpcParams = &v1.AttrAddActionTrigger{BalanceDirection: "*out"} - } - return self.rpcParams -} - -func (self *CmdAddTriggeredAction) PostprocessRpcParams() error { - return nil -} - -func (self *CmdAddTriggeredAction) RpcResult() interface{} { - var s string - return &s -}