diff --git a/apier/v1/accounts.go b/apier/v1/accounts.go index f007f1537..4331f4c24 100644 --- a/apier/v1/accounts.go +++ b/apier/v1/accounts.go @@ -242,7 +242,7 @@ func (self *ApierV1) RemoveAccount(attr utils.AttrRemoveAccount, reply *string) return nil } -func (self *ApierV1) GetAccounts(attr utils.AttrGetAccounts, reply *[]*engine.Account) error { +func (self *ApierV1) GetAccounts(attr utils.AttrGetAccounts, reply *[]interface{}) error { if len(attr.Tenant) == 0 { return utils.NewErrMandatoryIeMissing("Tenanat") } @@ -270,12 +270,12 @@ func (self *ApierV1) GetAccounts(attr utils.AttrGetAccounts, reply *[]*engine.Ac } else { limitedAccounts = accountKeys[attr.Offset:] } - retAccounts := make([]*engine.Account, 0) + retAccounts := make([]interface{}, 0) for _, acntKey := range limitedAccounts { if acnt, err := self.AccountDb.GetAccount(acntKey[len(utils.ACCOUNT_PREFIX):]); err != nil && err != utils.ErrNotFound { // Not found is not an error here return err } else if acnt != nil { - retAccounts = append(retAccounts, acnt) + retAccounts = append(retAccounts, acnt.AsOldStructure()) } } *reply = retAccounts diff --git a/apier/v1/accounts_test.go b/apier/v1/accounts_test.go index 5c6a8604a..51a5ce573 100644 --- a/apier/v1/accounts_test.go +++ b/apier/v1/accounts_test.go @@ -72,7 +72,7 @@ func TestGetAccountIds(t *testing.T) { */ func TestGetAccounts(t *testing.T) { - var accounts []*engine.Account + var accounts []interface{} var attrs utils.AttrGetAccounts if err := apierAcnts.GetAccounts(utils.AttrGetAccounts{Tenant: "cgrates.org"}, &accounts); err != nil { t.Error("Unexpected error", err.Error()) diff --git a/apier/v2/accounts.go b/apier/v2/accounts.go new file mode 100644 index 000000000..72cebd7c6 --- /dev/null +++ b/apier/v2/accounts.go @@ -0,0 +1,66 @@ +/* +Real-time Charging System for Telecom & ISP environments +Copyright (C) 2012-2015 ITsysCOM GmbH + +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 v2 + +import ( + "math" + + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +func (self *ApierV2) GetAccounts(attr utils.AttrGetAccounts, reply *[]*engine.Account) error { + if len(attr.Tenant) == 0 { + return utils.NewErrMandatoryIeMissing("Tenanat") + } + var accountKeys []string + var err error + if len(attr.AccountIds) == 0 { + if accountKeys, err = self.AccountDb.GetKeysForPrefix(utils.ACCOUNT_PREFIX + utils.ConcatenatedKey(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) + continue + } + accountKeys = append(accountKeys, utils.ACCOUNT_PREFIX+utils.ConcatenatedKey(attr.Tenant, acntId)) + } + } + if len(accountKeys) == 0 { + return nil + } + var limitedAccounts []string + if attr.Limit != 0 { + max := math.Min(float64(attr.Offset+attr.Limit), float64(len(accountKeys))) + limitedAccounts = accountKeys[attr.Offset:int(max)] + } else { + limitedAccounts = accountKeys[attr.Offset:] + } + retAccounts := make([]*engine.Account, 0) + for _, acntKey := range limitedAccounts { + if acnt, err := self.AccountDb.GetAccount(acntKey[len(utils.ACCOUNT_PREFIX):]); err != nil && err != utils.ErrNotFound { // Not found is not an error here + return err + } else if acnt != nil { + retAccounts = append(retAccounts, acnt) + } + } + *reply = retAccounts + return nil +} diff --git a/console/accounts.go b/console/accounts.go index f1d760fc3..c9030290e 100644 --- a/console/accounts.go +++ b/console/accounts.go @@ -26,7 +26,7 @@ import ( func init() { c := &CmdGetAccounts{ name: "accounts", - rpcMethod: "ApierV1.GetAccounts", + rpcMethod: "ApierV2.GetAccounts", rpcParams: &utils.AttrGetAccounts{}, } commands[c.Name()] = c