diff --git a/apier/v1/attributes.go b/apier/v1/attributes.go index 3d2cdce1e..0fb7a7415 100644 --- a/apier/v1/attributes.go +++ b/apier/v1/attributes.go @@ -62,6 +62,24 @@ func (APIerSv1 *APIerSv1) GetAttributeProfileIDs(args utils.TenantArgWithPaginat return nil } +// GetAttributeProfileIDsCount sets in reply var the total number of AttributeProfileIDs registered for a tenant +// returns ErrNotFound in case of 0 AttributeProfileIDs +func (APIerSv1 *APIerSv1) GetAttributeProfileIDsCount(args *utils.TenantArg, reply *int) (err error) { + if missing := utils.MissingStructFields(&args, []string{utils.Tenant}); len(missing) != 0 { + return utils.NewErrMandatoryIeMissing(missing...) + } + var keys []string + prfx := utils.AttributeProfilePrefix + args.Tenant + ":" + if keys, err = APIerSv1.DataManager.DataDB().GetKeysForPrefix(prfx); err != nil { + return err + } + if len(keys) == 0 { + return utils.ErrNotFound + } + *reply = len(keys) + return +} + type AttributeWithCache struct { *engine.AttributeProfile Cache *string diff --git a/apier/v1/thresholds.go b/apier/v1/thresholds.go index d975143b4..42844f072 100644 --- a/apier/v1/thresholds.go +++ b/apier/v1/thresholds.go @@ -94,6 +94,24 @@ func (APIerSv1 *APIerSv1) GetThresholdProfileIDs(args utils.TenantArgWithPaginat return nil } +// GetThresholdProfileIDsCount sets in reply var the total number of ThresholdProfileIDs registered for the received tenant +// returns ErrNotFound in case of 0 ThresholdProfileIDs +func (APIerSv1 *APIerSv1) GetThresholdProfileIDsCount(args *utils.TenantArg, reply *int) (err error) { + if missing := utils.MissingStructFields(&args, []string{utils.Tenant}); len(missing) != 0 { + return utils.NewErrMandatoryIeMissing(missing...) + } + var keys []string + prfx := utils.ThresholdProfilePrefix + args.Tenant + ":" + if keys, err = APIerSv1.DataManager.DataDB().GetKeysForPrefix(prfx); err != nil { + return err + } + if len(keys) == 0 { + return utils.ErrNotFound + } + *reply = len(keys) + return nil +} + // SetThresholdProfile alters/creates a ThresholdProfile func (APIerSv1 *APIerSv1) SetThresholdProfile(args *engine.ThresholdWithCache, reply *string) error { if missing := utils.MissingStructFields(args.ThresholdProfile, []string{"Tenant", "ID"}); len(missing) != 0 { diff --git a/apier/v2/accounts.go b/apier/v2/accounts.go index cb2df7d33..df8cd96c2 100644 --- a/apier/v2/accounts.go +++ b/apier/v2/accounts.go @@ -80,16 +80,19 @@ func (apiv2 *APIerSv2) GetAccounts(attr utils.AttrGetAccounts, reply *[]*engine. return nil } -// GetAccountsCount returns the total number of accounts -func (apiv2 *APIerSv2) GetAccountsCount(attr utils.AttrGetAccountsCount, reply *int) (err error) { +// GetAccountsCount sets in reply var the total number of accounts registered for the received tenant +// returns ErrNotFound in case of 0 accounts +func (apiv2 *APIerSv2) GetAccountsCount(attr *utils.AttrGetAccountsCount, reply *int) (err error) { if len(attr.Tenant) == 0 { return utils.NewErrMandatoryIeMissing("Tenant") } var accountKeys []string - if accountKeys, err = apiv2.DataManager.DataDB().GetKeysForPrefix(utils.ACCOUNT_PREFIX + attr.Tenant); err != nil { return err } + if len(accountKeys) == 0 { + return utils.ErrNotFound + } *reply = len(accountKeys) return diff --git a/apier/v2/apier.go b/apier/v2/apier.go index 90ed8500a..5222a549a 100644 --- a/apier/v2/apier.go +++ b/apier/v2/apier.go @@ -218,8 +218,9 @@ func (apiv2 *APIerSv2) GetActions(attr AttrGetActions, reply *map[string]engine. type AttrGetActionsCount struct{} -// GetActionsCount get -func (apiv2 *APIerSv2) GetActionsCount(attr AttrGetActionsCount, reply *int) (err error) { +// GetActionsCount sets in reply var the total number of actions registered for the received tenant +// returns ErrNotFound in case of 0 actions +func (apiv2 *APIerSv2) GetActionsCount(attr *AttrGetActionsCount, reply *int) (err error) { var actionKeys []string if actionKeys, err = apiv2.DataManager.DataDB().GetKeysForPrefix(utils.ACTION_PREFIX); err != nil { return err diff --git a/apier/v2/apierv2_it_test.go b/apier/v2/apierv2_it_test.go index b7021d61c..dbe63104d 100644 --- a/apier/v2/apierv2_it_test.go +++ b/apier/v2/apierv2_it_test.go @@ -55,7 +55,9 @@ var ( testAPIerSv2itSetActionWithCategory, testAPIerSv2itSetActionPlanWithWrongTiming, testAPIerSv2itSetActionPlanWithWrongTiming2, - testAPIerSv2BackwardsCompatible, + testAPIerSv2itBackwardsCompatible, + testAPIerSv2itGetAccountsCount, + testAPIerSv2itGetActionsCount, testAPIerSv2itKillEngine, } ) @@ -431,7 +433,7 @@ func testAPIerSv2itSetActionPlanWithWrongTiming2(t *testing.T) { } } -func testAPIerSv2BackwardsCompatible(t *testing.T) { +func testAPIerSv2itBackwardsCompatible(t *testing.T) { var reply string if err := apierRPC.Call("ApierV2.Ping", new(utils.CGREvent), &reply); err != nil { t.Error(err) @@ -440,6 +442,136 @@ func testAPIerSv2BackwardsCompatible(t *testing.T) { } } +func testAPIerSv2itGetAccountsCount(t *testing.T) { + var reply1 int + if err := apierRPC.Call(utils.APIerSv2GetAccountsCount, &utils.AttrGetAccountsCount{ + Tenant: "cgrates.org"}, &reply1); err != nil { + t.Error(err) + } else if reply1 != 3 { + t.Errorf("Expecting: 3, received: %+v", reply1) + } + var reply string + if err := apierRPC.Call(utils.APIerSv1RemoveAccount, &utils.AttrRemoveAccount{ + Account: "dan", Tenant: "cgrates.org"}, &reply); err != nil { + t.Errorf("Unexpected error : %+v", err) + } + if err := apierRPC.Call(utils.APIerSv1RemoveAccount, &utils.AttrRemoveAccount{ + Account: "TestAPIerSv2itSetAccountWithAP1", Tenant: "cgrates.org"}, &reply); err != nil { + t.Errorf("Unexpected error : %+v", err) + } + if err := apierRPC.Call(utils.APIerSv1RemoveAccount, &utils.AttrRemoveAccount{ + Account: "TestAPIerSv2itSetActionWithCategory", Tenant: "cgrates.org"}, &reply); err != nil { + t.Errorf("Unexpected error : %+v", err) + } + if err := apierRPC.Call(utils.APIerSv2GetAccountsCount, &utils.AttrGetAccountsCount{ + Tenant: "cgrates.org"}, &reply1); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expecting %+v, received: %+v", utils.ErrNotFound, err) + } + argSetAccount := AttrSetAccount{ + Tenant: "cgrates.org", + Account: "TestAPIerSv2CountAccounts", + } + if err := apierRPC.Call(utils.APIerSv2SetAccount, argSetAccount, &reply); err != nil { + t.Fatal(err) + } + var acnt engine.Account + if err := apierRPC.Call(utils.APIerSv2GetAccount, &utils.AttrGetAccount{ + Tenant: "cgrates.org", Account: "TestAPIerSv2CountAccounts"}, &acnt); err != nil { + t.Error(err) + } + if err := apierRPC.Call(utils.APIerSv2GetAccountsCount, &utils.AttrGetAccountsCount{Tenant: "cgrates.org"}, &reply1); err != nil { + t.Error(err) + } else if reply1 != 1 { + t.Errorf("Expecting: 1, received: %+v", reply1) + } + argSetAccount = AttrSetAccount{ + Tenant: "cgrates.org", + Account: "TestAPIerSv2CountAccounts2", + } + if err := apierRPC.Call(utils.APIerSv2SetAccount, argSetAccount, &reply); err != nil { + t.Fatal(err) + } + if err := apierRPC.Call(utils.APIerSv2GetAccount, &utils.AttrGetAccount{ + Tenant: "cgrates.org", Account: "TestAPIerSv2CountAccounts2"}, &acnt); err != nil { + t.Error(err) + } + if err := apierRPC.Call(utils.APIerSv2GetAccountsCount, &utils.AttrGetAccountsCount{Tenant: "cgrates.org"}, &reply1); err != nil { + t.Error(err) + } else if reply1 != 2 { + t.Errorf("Expecting: 2, received: %+v", reply1) + } + if err := apierRPC.Call(utils.APIerSv1RemoveAccount, &utils.AttrRemoveAccount{ + Account: "TestAPIerSv2CountAccounts2", Tenant: "cgrates.org"}, &reply); err != nil { + t.Errorf("Unexpected error : %+v", err) + } + if err := apierRPC.Call(utils.APIerSv2GetAccountsCount, &utils.AttrGetAccountsCount{Tenant: "cgrates.org"}, &reply1); err != nil { + t.Error(err) + } else if reply1 != 1 { + t.Errorf("Expecting: 1, received: %+v", reply1) + } + if err := apierRPC.Call(utils.APIerSv1RemoveAccount, &utils.AttrRemoveAccount{ + Account: "TestAPIerSv2CountAccounts", Tenant: "cgrates.org"}, &reply); err != nil { + t.Errorf("Unexpected error : %+v", err) + } + if err := apierRPC.Call(utils.APIerSv2GetAccountsCount, &utils.AttrGetAccountsCount{ + Tenant: "cgrates.org"}, &reply1); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expecting %+v, received: %+v", utils.ErrNotFound, err) + } +} + +func testAPIerSv2itGetActionsCount(t *testing.T) { + var reply1 int + if err := apierRPC.Call(utils.APIerSv2GetActionsCount, &AttrGetActionsCount{}, &reply1); err != nil { + t.Error(err) + } else if reply1 != 3 { + t.Errorf("Expecting: 3, received : %+v", reply1) + } + attrs := utils.AttrSetActions{ActionsId: "DISABLE_ACCOUNT2", Actions: []*utils.TPAction{ + {Identifier: utils.DISABLE_ACCOUNT, Weight: 0.7}, + }} + var reply string + if err := apierRPC.Call(utils.APIerSv2SetActions, attrs, &reply); err != nil { + t.Error(err) + } + if err := apierRPC.Call(utils.APIerSv2GetActionsCount, &AttrGetActionsCount{}, &reply1); err != nil { + t.Error(err) + } else if reply1 != 4 { + t.Errorf("Expecting: 4, received : %+v", reply1) + } + + attrRemoveActions := &v1.AttrRemoveActions{ + ActionIDs: []string{"DISABLE_ACCOUNT", "DISABLE_ACCOUNT2", "TestAPIerSv2itSetAccountWithAP_ACT_1"}, + } + if err := apierRPC.Call(utils.APIerSv2RemoveActions, &attrRemoveActions, &reply); err != nil { + t.Error(err) + } + if err := apierRPC.Call(utils.APIerSv2GetActionsCount, &AttrGetActionsCount{}, &reply1); err != nil { + t.Error(err) + } else if reply1 != 1 { + t.Errorf("Expecting: 1, received : %+v", reply1) + } + attrRemoveActions = &v1.AttrRemoveActions{ + ActionIDs: []string{"TestAPIerSv2itSetActionWithCategory_ACT"}, + } + if err := apierRPC.Call(utils.APIerSv2RemoveActions, &attrRemoveActions, &reply); err != nil { + t.Error(err) + } + if err := apierRPC.Call(utils.APIerSv2GetActionsCount, &AttrGetActionsCount{}, &reply1); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expecting %+v, received: %+v", utils.ErrNotFound, err) + } + attrs = utils.AttrSetActions{ActionsId: "Test", Actions: []*utils.TPAction{ + {Identifier: utils.DISABLE_ACCOUNT, Weight: 0.7}, + }} + if err := apierRPC.Call(utils.APIerSv2SetActions, attrs, &reply); err != nil { + t.Error(err) + } + if err := apierRPC.Call(utils.APIerSv2GetActionsCount, &AttrGetActionsCount{}, &reply1); err != nil { + t.Error(err) + } else if reply1 != 1 { + t.Errorf("Expecting: 1, received : %+v", reply1) + } +} + func testAPIerSv2itKillEngine(t *testing.T) { if err := engine.KillEngine(delay); err != nil { t.Error(err) diff --git a/utils/consts.go b/utils/consts.go index 272b32569..7ff5b3e19 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -1219,6 +1219,7 @@ const ( APIerSv1GetSharedGroup = "APIerSv1.GetSharedGroup" APIerSv1RemoveActionTrigger = "APIerSv1.RemoveActionTrigger" APIerSv1GetAccount = "APIerSv1.GetAccount" + APIerSv1GetAttributeProfileIDsCount = "APIerSv1.GetAttributeProfileIDsCount" ) // APIerSv1 TP APIs @@ -1259,6 +1260,8 @@ const ( APIerSv2ResetAccountActionTriggers = "APIerSv2.ResetAccountActionTriggers" APIerSv2RemoveActions = "APIerSv2.RemoveActions" APIerSv2ExportCdrsToFile = "APIerSv2.ExportCdrsToFile" + APIerSv2GetAccountsCount = "APIerSv2.GetAccountsCount" + APIerSv2GetActionsCount = "APIerSv2.GetActionsCount" ) const (