Updated Apier Get/Set Account

This commit is contained in:
Trial97
2019-11-28 16:16:57 +02:00
parent 644a491852
commit f5c39ae636
9 changed files with 58 additions and 50 deletions

View File

@@ -181,7 +181,7 @@ func (api *ApierV1) SetAccount(attr utils.AttrSetAccount, reply *string) (err er
ID: accID,
}
}
if attr.ActionPlanId != "" {
if attr.ActionPlanID != "" {
_, err := guardian.Guardian.Guard(func() (interface{}, error) {
acntAPids, err := api.DataManager.GetAccountActionPlans(accID, false, utils.NonTransactional)
if err != nil && err != utils.ErrNotFound {
@@ -190,7 +190,7 @@ func (api *ApierV1) SetAccount(attr utils.AttrSetAccount, reply *string) (err er
// clean previous action plans
for i := 0; i < len(acntAPids); {
apID := acntAPids[i]
if apID == attr.ActionPlanId {
if apID == attr.ActionPlanID {
i++ // increase index since we don't remove from slice
continue
}
@@ -202,8 +202,8 @@ func (api *ApierV1) SetAccount(attr utils.AttrSetAccount, reply *string) (err er
dirtyActionPlans[apID] = ap
acntAPids = append(acntAPids[:i], acntAPids[i+1:]...) // remove the item from the list so we can overwrite the real list
}
if !utils.IsSliceMember(acntAPids, attr.ActionPlanId) { // Account not yet attached to action plan, do it here
ap, err := api.DataManager.GetActionPlan(attr.ActionPlanId, false, utils.NonTransactional)
if !utils.IsSliceMember(acntAPids, attr.ActionPlanID) { // Account not yet attached to action plan, do it here
ap, err := api.DataManager.GetActionPlan(attr.ActionPlanID, false, utils.NonTransactional)
if err != nil {
return 0, err
}
@@ -211,8 +211,8 @@ func (api *ApierV1) SetAccount(attr utils.AttrSetAccount, reply *string) (err er
ap.AccountIDs = make(utils.StringMap)
}
ap.AccountIDs[accID] = true
dirtyActionPlans[attr.ActionPlanId] = ap
acntAPids = append(acntAPids, attr.ActionPlanId)
dirtyActionPlans[attr.ActionPlanID] = ap
acntAPids = append(acntAPids, attr.ActionPlanID)
// create tasks
for _, at := range ap.ActionTimings {
if at.IsASAP() {
@@ -252,8 +252,8 @@ func (api *ApierV1) SetAccount(attr utils.AttrSetAccount, reply *string) (err er
}
}
if attr.ActionTriggersId != "" {
atrs, err := api.DataManager.GetActionTriggers(attr.ActionTriggersId, false, utils.NonTransactional)
if attr.ActionTriggersID != "" {
atrs, err := api.DataManager.GetActionTriggers(attr.ActionTriggersID, false, utils.NonTransactional)
if err != nil {
return 0, err
}
@@ -261,11 +261,11 @@ func (api *ApierV1) SetAccount(attr utils.AttrSetAccount, reply *string) (err er
ub.InitCounters()
}
if attr.AllowNegative != nil {
ub.AllowNegative = *attr.AllowNegative
if alNeg, has := attr.ExtraOptions[utils.AllowNegative]; has {
ub.AllowNegative = alNeg
}
if attr.Disabled != nil {
ub.Disabled = *attr.Disabled
if dis, has := attr.ExtraOptions[utils.Disabled]; has {
ub.Disabled = dis
}
// All prepared, save account
if err := api.DataManager.SetAccount(ub); err != nil {
@@ -350,16 +350,16 @@ func (api *ApierV1) GetAccounts(attr utils.AttrGetAccounts, reply *[]interface{}
}
var accountKeys []string
var err error
if len(attr.AccountIds) == 0 {
if len(attr.AccountIDs) == 0 {
if accountKeys, err = api.DataManager.DataDB().GetKeysForPrefix(utils.ACCOUNT_PREFIX + attr.Tenant); err != nil {
return err
}
} else {
for _, acntId := range attr.AccountIds {
if len(acntId) == 0 { // Source of error returned from redis (key not found)
for _, acntID := range attr.AccountIDs {
if len(acntID) == 0 { // Source of error returned from redis (key not found)
continue
}
accountKeys = append(accountKeys, utils.ACCOUNT_PREFIX+utils.ConcatenatedKey(attr.Tenant, acntId))
accountKeys = append(accountKeys, utils.ACCOUNT_PREFIX+utils.ConcatenatedKey(attr.Tenant, acntID))
}
}
if len(accountKeys) == 0 {
@@ -377,7 +377,10 @@ func (api *ApierV1) GetAccounts(attr utils.AttrGetAccounts, reply *[]interface{}
if acnt, err := api.DataManager.GetAccount(acntKey[len(utils.ACCOUNT_PREFIX):]); err != nil && err != utils.ErrNotFound { // Not found is not an error here
return err
} else if acnt != nil {
if attr.Disabled != nil && *attr.Disabled != acnt.Disabled {
if alNeg, has := attr.Filter[utils.AllowNegative]; has && alNeg != acnt.AllowNegative {
continue
}
if dis, has := attr.Filter[utils.Disabled]; has && dis != acnt.Disabled {
continue
}
retAccounts = append(retAccounts, acnt.AsOldStructure())

View File

@@ -460,10 +460,10 @@ func testAccITAddBalanceWithNegative(t *testing.T) {
func testAccITGetDisabledAccounts(t *testing.T) {
var reply string
acnt1 := utils.AttrSetAccount{Tenant: "cgrates.org", Account: "account1", Disabled: utils.BoolPointer(true)}
acnt2 := utils.AttrSetAccount{Tenant: "cgrates.org", Account: "account2", Disabled: utils.BoolPointer(false)}
acnt3 := utils.AttrSetAccount{Tenant: "cgrates.org", Account: "account3", Disabled: utils.BoolPointer(true)}
acnt4 := utils.AttrSetAccount{Tenant: "cgrates.org", Account: "account4", Disabled: utils.BoolPointer(true)}
acnt1 := utils.AttrSetAccount{Tenant: "cgrates.org", Account: "account1", ExtraOptions: map[string]bool{utils.Disabled: true}}
acnt2 := utils.AttrSetAccount{Tenant: "cgrates.org", Account: "account2", ExtraOptions: map[string]bool{utils.Disabled: false}}
acnt3 := utils.AttrSetAccount{Tenant: "cgrates.org", Account: "account3", ExtraOptions: map[string]bool{utils.Disabled: true}}
acnt4 := utils.AttrSetAccount{Tenant: "cgrates.org", Account: "account4", ExtraOptions: map[string]bool{utils.Disabled: true}}
for _, account := range []utils.AttrSetAccount{acnt1, acnt2, acnt3, acnt4} {
if err := accRPC.Call(utils.ApierV1SetAccount, account, &reply); err != nil {
@@ -474,7 +474,7 @@ func testAccITGetDisabledAccounts(t *testing.T) {
}
var acnts []*engine.Account
if err := accRPC.Call(utils.ApierV2GetAccounts, utils.AttrGetAccounts{Tenant: "cgrates.org", Disabled: utils.BoolPointer(true)},
if err := accRPC.Call(utils.ApierV2GetAccounts, utils.AttrGetAccounts{Tenant: "cgrates.org", Filter: map[string]bool{utils.Disabled: true}},
&acnts); err != nil {
t.Error(err)
} else if len(acnts) != 3 {

View File

@@ -85,13 +85,13 @@ func TestGetAccounts(t *testing.T) {
} else if len(accounts) != 2 {
t.Errorf("Accounts returned: %+v", accounts)
}
attrs = utils.AttrGetAccounts{Tenant: "cgrates.org", AccountIds: []string{"account1"}}
attrs = utils.AttrGetAccounts{Tenant: "cgrates.org", AccountIDs: []string{"account1"}}
if err := apierAcnts.GetAccounts(attrs, &accounts); err != nil {
t.Error("Unexpected error", err.Error())
} else if len(accounts) != 1 {
t.Errorf("Accounts returned: %+v", accounts)
}
attrs = utils.AttrGetAccounts{Tenant: "itsyscom.com", AccountIds: []string{"INVALID"}}
attrs = utils.AttrGetAccounts{Tenant: "itsyscom.com", AccountIDs: []string{"INVALID"}}
if err := apierAcnts.GetAccounts(attrs, &accounts); err != nil {
t.Error("Unexpected error", err.Error())
} else if len(accounts) != 0 {

View File

@@ -1181,7 +1181,7 @@ func TestApierRemAccountActionTriggers(t *testing.T) {
// Test here SetAccount
func TestApierSetAccount(t *testing.T) {
reply := ""
attrs := &utils.AttrSetAccount{Tenant: "cgrates.org", Account: "dan7", ActionPlanId: "ATMS_1", ReloadScheduler: true}
attrs := &utils.AttrSetAccount{Tenant: "cgrates.org", Account: "dan7", ActionPlanID: "ATMS_1", ReloadScheduler: true}
if err := rater.Call(utils.ApierV1SetAccount, attrs, &reply); err != nil {
t.Error("Got error on ApierV1.SetAccount: ", err.Error())
} else if reply != "OK" {
@@ -1190,7 +1190,7 @@ func TestApierSetAccount(t *testing.T) {
reply2 := ""
attrs2 := new(utils.AttrSetAccount)
*attrs2 = *attrs
attrs2.ActionPlanId = "DUMMY_DATA" // Does not exist so it should error when adding triggers on it
attrs2.ActionPlanID = "DUMMY_DATA" // Does not exist so it should error when adding triggers on it
// Add account with actions timing which does not exist
if err := rater.Call(utils.ApierV1SetAccount, attrs2, &reply2); err == nil || reply2 == "OK" { // OK is not welcomed
t.Error("Expecting error on ApierV1.SetAccount.", err, reply2)

View File

@@ -893,7 +893,7 @@ func testInternalSetAccount(t *testing.T) {
utils.AttrSetAccount{
Tenant: "cgrates.org",
Account: "testSetAccount",
ActionPlanId: "AP_PACKAGE_10",
ActionPlanID: "AP_PACKAGE_10",
ReloadScheduler: true,
}, &reply); err != nil {
t.Error(err)

View File

@@ -34,16 +34,16 @@ func (apiv2 *ApierV2) GetAccounts(attr utils.AttrGetAccounts, reply *[]*engine.A
}
var accountKeys []string
var err error
if len(attr.AccountIds) == 0 {
if len(attr.AccountIDs) == 0 {
if accountKeys, err = apiv2.DataManager.DataDB().GetKeysForPrefix(utils.ACCOUNT_PREFIX + attr.Tenant); err != nil {
return err
}
} else {
for _, acntId := range attr.AccountIds {
if len(acntId) == 0 { // Source of error returned from redis (key not found)
for _, acntID := range attr.AccountIDs {
if len(acntID) == 0 { // Source of error returned from redis (key not found)
continue
}
accountKeys = append(accountKeys, utils.ACCOUNT_PREFIX+utils.ConcatenatedKey(attr.Tenant, acntId))
accountKeys = append(accountKeys, utils.ACCOUNT_PREFIX+utils.ConcatenatedKey(attr.Tenant, acntID))
}
}
if len(accountKeys) == 0 {
@@ -67,7 +67,10 @@ func (apiv2 *ApierV2) GetAccounts(attr utils.AttrGetAccounts, reply *[]*engine.A
if acnt, err := apiv2.DataManager.GetAccount(acntKey[len(utils.ACCOUNT_PREFIX):]); err != nil && err != utils.ErrNotFound { // Not found is not an error here
return err
} else if acnt != nil {
if attr.Disabled != nil && *attr.Disabled != acnt.Disabled {
if alNeg, has := attr.Filter[utils.AllowNegative]; has && alNeg != acnt.AllowNegative {
continue
}
if dis, has := attr.Filter[utils.Disabled]; has && dis != acnt.Disabled {
continue
}
retAccounts = append(retAccounts, acnt)
@@ -95,8 +98,7 @@ type AttrSetAccount struct {
ActionPlansOverwrite bool
ActionTriggerIDs []string
ActionTriggerOverwrite bool
AllowNegative *bool
Disabled *bool
ExtraOptions map[string]bool
ReloadScheduler bool
}
@@ -219,11 +221,11 @@ func (apiv2 *ApierV2) SetAccount(attr AttrSetAccount, reply *string) error {
}
ub.InitCounters()
if attr.AllowNegative != nil {
ub.AllowNegative = *attr.AllowNegative
if alNeg, has := attr.ExtraOptions[utils.AllowNegative]; has {
ub.AllowNegative = alNeg
}
if attr.Disabled != nil {
ub.Disabled = *attr.Disabled
if dis, has := attr.ExtraOptions[utils.Disabled]; has {
ub.Disabled = dis
}
// All prepared, save account
return 0, apiv2.DataManager.SetAccount(ub)

View File

@@ -203,9 +203,11 @@ func TestApierV2itFraudMitigation(t *testing.T) {
t.Fatalf("Received account: %+v", acnt)
}
attrSetAcnt := AttrSetAccount{
Tenant: "cgrates.org",
Account: "dan",
Disabled: utils.BoolPointer(false),
Tenant: "cgrates.org",
Account: "dan",
ExtraOptions: map[string]bool{
utils.Disabled: false,
},
}
if err := apierRPC.Call(utils.ApierV2SetAccount, attrSetAcnt, &reply); err != nil {
t.Fatal(err)

View File

@@ -299,9 +299,11 @@ func testTutAccounts(t *testing.T) {
// enable the account again
if err := tutRpc.Call(utils.ApierV2SetAccount,
v2.AttrSetAccount{
Tenant: "cgrates.org",
Account: "1001",
Disabled: utils.BoolPointer(false),
Tenant: "cgrates.org",
Account: "1001",
ExtraOptions: map[string]bool{
utils.Disabled: false,
},
}, &reply); err != nil {
t.Error(err)
}

View File

@@ -421,10 +421,10 @@ type AttrGetAccount struct {
type AttrGetAccounts struct {
Tenant string
AccountIds []string
AccountIDs []string
Offset int // Set the item offset
Limit int // Limit number of items retrieved
Disabled *bool
Filter map[string]bool
}
type ArgsCache struct {
@@ -900,10 +900,9 @@ type AttrExecuteAction struct {
type AttrSetAccount struct {
Tenant string
Account string
ActionPlanId string
ActionTriggersId string
AllowNegative *bool
Disabled *bool
ActionPlanID string
ActionTriggersID string
ExtraOptions map[string]bool
ReloadScheduler bool
}