mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
183 lines
6.1 KiB
Go
183 lines
6.1 KiB
Go
/*
|
|
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
|
|
Copyright (C) ITsysCOM GmbH
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>
|
|
*/
|
|
|
|
package admins
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/cgrates/birpc/context"
|
|
"github.com/cgrates/cgrates/utils"
|
|
)
|
|
|
|
// GetAccount returns an Account
|
|
func (admS *AdminS) V1GetAccount(ctx *context.Context, arg *utils.TenantIDWithAPIOpts, reply *utils.Account) error {
|
|
if missing := utils.MissingStructFields(arg, []string{utils.ID}); len(missing) != 0 { //Params missing
|
|
return utils.NewErrMandatoryIeMissing(missing...)
|
|
}
|
|
tnt := arg.Tenant
|
|
if tnt == utils.EmptyString {
|
|
tnt = admS.cfg.GeneralCfg().DefaultTenant
|
|
}
|
|
ap, err := admS.dm.GetAccount(ctx, tnt, arg.ID)
|
|
if err != nil {
|
|
if err.Error() != utils.ErrNotFound.Error() {
|
|
err = utils.NewErrServerError(err)
|
|
}
|
|
return err
|
|
}
|
|
*reply = *ap
|
|
return nil
|
|
}
|
|
|
|
// GetAccountIDs returns list of account profile IDs registered for a tenant
|
|
func (admS *AdminS) V1GetAccountIDs(ctx *context.Context, args *utils.ArgsItemIDs, actPrfIDs *[]string) (err error) {
|
|
tnt := args.Tenant
|
|
if tnt == utils.EmptyString {
|
|
tnt = admS.cfg.GeneralCfg().DefaultTenant
|
|
}
|
|
prfx := utils.AccountPrefix + tnt + utils.ConcatenatedKeySep
|
|
lenPrfx := len(prfx)
|
|
prfx += args.ItemsPrefix
|
|
dataDB, _, err := admS.dm.DBConns().GetConn(utils.MetaAccounts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var keys []string
|
|
if keys, err = dataDB.GetKeysForPrefix(ctx, prfx); err != nil {
|
|
return
|
|
}
|
|
if len(keys) == 0 {
|
|
return utils.ErrNotFound
|
|
}
|
|
retIDs := make([]string, len(keys))
|
|
for i, key := range keys {
|
|
retIDs[i] = key[lenPrfx:]
|
|
}
|
|
var limit, offset, maxItems int
|
|
if limit, offset, maxItems, err = utils.GetPaginateOpts(args.APIOpts); err != nil {
|
|
return
|
|
}
|
|
*actPrfIDs, err = utils.Paginate(retIDs, limit, offset, maxItems)
|
|
return
|
|
}
|
|
|
|
// GetAccounts returns a list of accounts registered for a tenant
|
|
func (admS *AdminS) V1GetAccounts(ctx *context.Context, args *utils.ArgsItemIDs, accs *[]*utils.Account) (err error) {
|
|
tnt := args.Tenant
|
|
if tnt == utils.EmptyString {
|
|
tnt = admS.cfg.GeneralCfg().DefaultTenant
|
|
}
|
|
var accIDs []string
|
|
if err = admS.V1GetAccountIDs(ctx, args, &accIDs); err != nil {
|
|
return
|
|
}
|
|
*accs = make([]*utils.Account, 0, len(accIDs))
|
|
for _, accID := range accIDs {
|
|
var acc *utils.Account
|
|
acc, err = admS.dm.GetAccount(ctx, tnt, accID)
|
|
if err != nil {
|
|
return utils.APIErrorHandler(err)
|
|
}
|
|
*accs = append(*accs, acc)
|
|
}
|
|
return
|
|
}
|
|
|
|
// GetAccountsCount sets in reply var the total number of AccountIDs registered for a tenant
|
|
// returns ErrNotFound in case of 0 AccountIDs
|
|
func (admS *AdminS) V1GetAccountsCount(ctx *context.Context, args *utils.ArgsItemIDs, reply *int) (err error) {
|
|
tnt := args.Tenant
|
|
if tnt == utils.EmptyString {
|
|
tnt = admS.cfg.GeneralCfg().DefaultTenant
|
|
}
|
|
prfx := utils.AccountPrefix + tnt + utils.ConcatenatedKeySep + args.ItemsPrefix
|
|
dataDB, _, err := admS.dm.DBConns().GetConn(utils.MetaAccounts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var keys []string
|
|
if keys, err = dataDB.GetKeysForPrefix(ctx, prfx); err != nil {
|
|
return err
|
|
}
|
|
if len(keys) == 0 {
|
|
return utils.ErrNotFound
|
|
}
|
|
*reply = len(keys)
|
|
return
|
|
}
|
|
|
|
// SetAccount add/update a new Account
|
|
func (admS *AdminS) V1SetAccount(ctx *context.Context, args *utils.AccountWithAPIOpts, reply *string) error {
|
|
if missing := utils.MissingStructFields(args.Account, []string{utils.ID}); len(missing) != 0 {
|
|
return utils.NewErrMandatoryIeMissing(missing...)
|
|
}
|
|
if args.Tenant == utils.EmptyString {
|
|
args.Tenant = admS.cfg.GeneralCfg().DefaultTenant
|
|
}
|
|
if err := admS.dm.SetAccount(ctx, args.Account, true); err != nil {
|
|
return utils.APIErrorHandler(err)
|
|
}
|
|
//generate a loadID for CacheAccountProfiles and store it in database
|
|
if err := admS.dm.SetLoadIDs(ctx, map[string]int64{utils.CacheAccounts: time.Now().UnixNano()}); err != nil {
|
|
return utils.APIErrorHandler(err)
|
|
}
|
|
// delay if needed before cache call
|
|
if admS.cfg.GeneralCfg().CachingDelay != 0 {
|
|
utils.Logger.Info(fmt.Sprintf("<V1SetAccount> Delaying cache call for %v", admS.cfg.GeneralCfg().CachingDelay))
|
|
time.Sleep(admS.cfg.GeneralCfg().CachingDelay)
|
|
}
|
|
if err := admS.CallCache(ctx, utils.IfaceAsString(args.APIOpts[utils.MetaCache]), args.Tenant, utils.CacheAccounts,
|
|
args.TenantID(), utils.EmptyString, &args.FilterIDs, args.APIOpts); err != nil {
|
|
return utils.APIErrorHandler(err)
|
|
}
|
|
*reply = utils.OK
|
|
return nil
|
|
}
|
|
|
|
// RemoveAccount remove a specific Account
|
|
func (admS *AdminS) V1RemoveAccount(ctx *context.Context, arg *utils.TenantIDWithAPIOpts, reply *string) error {
|
|
if missing := utils.MissingStructFields(arg, []string{utils.ID}); len(missing) != 0 { //Params missing
|
|
return utils.NewErrMandatoryIeMissing(missing...)
|
|
}
|
|
tnt := arg.Tenant
|
|
if tnt == utils.EmptyString {
|
|
tnt = admS.cfg.GeneralCfg().DefaultTenant
|
|
}
|
|
if err := admS.dm.RemoveAccount(ctx, tnt, arg.ID,
|
|
true); err != nil {
|
|
return utils.APIErrorHandler(err)
|
|
}
|
|
//generate a loadID for CacheAccountProfiles and store it in database
|
|
if err := admS.dm.SetLoadIDs(ctx, map[string]int64{utils.CacheAccounts: time.Now().UnixNano()}); err != nil {
|
|
return utils.APIErrorHandler(err)
|
|
}
|
|
// delay if needed before cache call
|
|
if admS.cfg.GeneralCfg().CachingDelay != 0 {
|
|
utils.Logger.Info(fmt.Sprintf("<V1RemoveAccount> Delaying cache call for %v", admS.cfg.GeneralCfg().CachingDelay))
|
|
time.Sleep(admS.cfg.GeneralCfg().CachingDelay)
|
|
}
|
|
if err := admS.CallCache(ctx, utils.IfaceAsString(arg.APIOpts[utils.MetaCache]), tnt, utils.CacheAccounts,
|
|
utils.ConcatenatedKey(tnt, arg.ID), utils.EmptyString, nil, arg.APIOpts); err != nil {
|
|
return utils.APIErrorHandler(err)
|
|
}
|
|
*reply = utils.OK
|
|
return nil
|
|
}
|