extract resources apis to different file

This commit is contained in:
ionutboangiu
2025-05-28 16:25:02 +03:00
committed by Dan Christian Bogos
parent 2f7da3702a
commit 180eb6cbfd
5 changed files with 3191 additions and 3756 deletions

View File

@@ -1,628 +0,0 @@
{
// Sample CGRateS Configuration file for EEs
//
// Copyright (C) ITsysCOM GmbH
"logger": {
"level": 7
},
"listen": {
"rpc_json": ":2012",
"rpc_gob": ":2013",
"http": ":2080",
},
"data_db": {
"db_type": "redis",
"db_port": 6379,
"db_name": "10",
},
"stor_db": {
"db_password": "CGRateS.org",
},
"rates": {
"enabled": true,
},
"cdrs": {
"enabled": true,
"chargers_conns": ["*localhost"],
"rates_conns": ["*internal"],
"session_cost_retries": 0,
},
"chargers": {
"enabled": true,
"attributes_conns": ["*internal"],
},
"attributes": {
"enabled": true,
},
"ees": {
"attributes_conns": [],
"cache": {
"*file_csv": {
"limit": -1,
"static_ttl": false,
"ttl": "5s"
},
"*sql": {
"limit": -1,
"static_ttl": false,
"ttl": "1h"
}
},
"enabled": true,
"exporters": [
{
"attempts": 2,
"attribute_context": "",
"attribute_ids": [],
"export_path": "mysql://cgrates:CGRateS.org@127.0.0.1:3306",
"fields": [
{
"tag": "RequiredTemplate",
"type": "*template",
"value": "requiredFields"
}
],
"filters": [
"*prefix:~*req.SetupTime:2022-02"
],
"flags": [],
"id": "monthly_cdrs_1",
"opts": {
"sqlConnMaxLifetime": "0",
"sqlDBName": "cgrates2",
"sqlMaxIdleConns": 10,
"sqlMaxOpenConns": 100,
"sqlTableName": "cdrs",
"pgSSLMode": "disable"
},
"tenant": "",
"timezone": "",
"type": "*sql"
},
{
"attempts": 2,
"attribute_context": "",
"attribute_ids": [],
"export_path": "mysql://cgrates:CGRateS.org@127.0.0.1:3306",
"fields": [
{
"tag": "RequiredTemplate",
"type": "*template",
"value": "requiredFields"
}
],
"filters": [
"*prefix:~*req.SetupTime:2022-03"
],
"flags": [],
"id": "monthly_cdrs_2",
"opts": {
"sqlConnMaxLifetime": "0",
"sqlDBName": "cgrates2",
"sqlMaxIdleConns": 10,
"sqlMaxOpenConns": 100,
"sqlTableName": "cdrs",
"pgSSLMode": "disable"
},
"tenant": "",
"timezone": "",
"type": "*sql"
},
{
"attempts": 2,
"attribute_context": "",
"attribute_ids": [],
"export_path": "mysql://cgrates:CGRateS.org@127.0.0.1:3306",
"fields": [
{
"tag": "RequiredTemplate",
"type": "*template",
"value": "requiredFields"
}
],
"filters": [
"*prefix:~*req.SetupTime:2022-04"
],
"flags": [],
"id": "monthly_cdrs_3",
"opts": {
"sqlConnMaxLifetime": "0",
"sqlDBName": "cgrates2",
"sqlMaxIdleConns": 10,
"sqlMaxOpenConns": 100,
"sqlTableName": "cdrs",
"pgSSLMode": "disable"
},
"tenant": "",
"timezone": "",
"type": "*sql"
},
{
"attempts": 2,
"attribute_context": "",
"attribute_ids": [],
"export_path": "mysql://cgrates:CGRateS.org@127.0.0.1:3306",
"fields": [
{
"tag": "RequiredTemplate",
"type": "*template",
"value": "requiredFields"
}
],
"filters": [
"*prefix:~*req.SetupTime:2022-05"
],
"flags": [],
"id": "monthly_cdrs_4",
"opts": {
"sqlConnMaxLifetime": "0",
"sqlDBName": "cgrates2",
"sqlMaxIdleConns": 10,
"sqlMaxOpenConns": 100,
"sqlTableName": "cdrs",
"pgSSLMode": "disable"
},
"tenant": "",
"timezone": "",
"type": "*sql"
},
{
"attempts": 2,
"attribute_context": "",
"attribute_ids": [],
"export_path": "mysql://cgrates:CGRateS.org@127.0.0.1:3306",
"fields": [
{
"tag": "RequiredTemplate",
"type": "*template",
"value": "requiredFields"
}
],
"filters": [
"*prefix:~*req.SetupTime:2022-06"
],
"flags": [],
"id": "monthly_cdrs_5",
"opts": {
"sqlConnMaxLifetime": "0",
"sqlDBName": "cgrates2",
"sqlMaxIdleConns": 10,
"sqlMaxOpenConns": 100,
"sqlTableName": "cdrs",
"pgSSLMode": "disable"
},
"tenant": "",
"timezone": "",
"type": "*sql"
},
{
"attempts": 2,
"attribute_context": "",
"attribute_ids": [],
"export_path": "mysql://cgrates:CGRateS.org@127.0.0.1:3306",
"fields": [
{
"tag": "RequiredTemplate",
"type": "*template",
"value": "requiredFields"
}
],
"filters": [
"*prefix:~*req.SetupTime:2022-07"
],
"flags": [],
"id": "monthly_cdrs_6",
"opts": {
"sqlConnMaxLifetime": "0",
"sqlDBName": "cgrates2",
"sqlMaxIdleConns": 10,
"sqlMaxOpenConns": 100,
"sqlTableName": "cdrs",
"pgSSLMode": "disable"
},
"tenant": "",
"timezone": "",
"type": "*sql"
},
{
"attempts": 2,
"attribute_context": "",
"attribute_ids": [],
"export_path": "mysql://cgrates:CGRateS.org@127.0.0.1:3306",
"fields": [
{
"tag": "RequiredTemplate",
"type": "*template",
"value": "requiredFields"
}
],
"filters": [
"*prefix:~*req.SetupTime:2022-08"
],
"flags": [],
"id": "monthly_cdrs_7",
"opts": {
"sqlConnMaxLifetime": "0",
"sqlDBName": "cgrates2",
"sqlMaxIdleConns": 10,
"sqlMaxOpenConns": 100,
"sqlTableName": "cdrs",
"pgSSLMode": "disable"
},
"tenant": "",
"timezone": "",
"type": "*sql"
},
{
"attempts": 2,
"attribute_context": "",
"attribute_ids": [],
"export_path": "mysql://cgrates:CGRateS.org@127.0.0.1:3306",
"fields": [
{
"tag": "RequiredTemplate",
"type": "*template",
"value": "requiredFields"
}
],
"filters": [
"*prefix:~*req.SetupTime:2022-09"
],
"flags": [],
"id": "monthly_cdrs_8",
"opts": {
"sqlConnMaxLifetime": "0",
"sqlDBName": "cgrates2",
"sqlMaxIdleConns": 10,
"sqlMaxOpenConns": 100,
"sqlTableName": "cdrs",
"pgSSLMode": "disable"
},
"tenant": "",
"timezone": "",
"type": "*sql"
},
{
"attempts": 2,
"attribute_context": "",
"attribute_ids": [],
"export_path": "mysql://cgrates:CGRateS.org@127.0.0.1:3306",
"fields": [
{
"tag": "RequiredTemplate",
"type": "*template",
"value": "requiredFields"
}
],
"filters": [
"*prefix:~*req.SetupTime:2022-10"
],
"flags": [],
"id": "monthly_cdrs_9",
"opts": {
"sqlConnMaxLifetime": "0",
"sqlDBName": "cgrates2",
"sqlMaxIdleConns": 10,
"sqlMaxOpenConns": 100,
"sqlTableName": "cdrs",
"pgSSLMode": "disable"
},
"tenant": "",
"timezone": "",
"type": "*sql"
},
{
"attempts": 2,
"attribute_context": "",
"attribute_ids": [],
"export_path": "mysql://cgrates:CGRateS.org@127.0.0.1:3306",
"fields": [
{
"tag": "RequiredTemplate",
"type": "*template",
"value": "requiredFields"
}
],
"filters": [
"*prefix:~*req.SetupTime:2022-11"
],
"flags": [],
"id": "monthly_cdrs_10",
"opts": {
"sqlConnMaxLifetime": "0",
"sqlDBName": "cgrates2",
"sqlMaxIdleConns": 10,
"sqlMaxOpenConns": 100,
"sqlTableName": "cdrs",
"pgSSLMode": "disable"
},
"tenant": "",
"timezone": "",
"type": "*sql"
},
{
"attempts": 2,
"attribute_context": "",
"attribute_ids": [],
"export_path": "mysql://cgrates:CGRateS.org@127.0.0.1:3306",
"fields": [
{
"tag": "RequiredTemplate",
"type": "*template",
"value": "requiredFields"
}
],
"filters": [
"*prefix:~*req.SetupTime:2022-12"
],
"flags": [],
"id": "monthly_cdrs_11",
"opts": {
"sqlConnMaxLifetime": "0",
"sqlDBName": "cgrates2",
"sqlMaxIdleConns": 10,
"sqlMaxOpenConns": 100,
"sqlTableName": "cdrs",
"pgSSLMode": "disable"
},
"tenant": "",
"timezone": "",
"type": "*sql"
},
{
"attempts": 2,
"attribute_context": "",
"attribute_ids": [],
"export_path": "mysql://cgrates:CGRateS.org@127.0.0.1:3306",
"fields": [
{
"tag": "RequiredTemplate",
"type": "*template",
"value": "requiredFields"
}
],
"filters": [
"*prefix:~*req.SetupTime:2023-01"
],
"flags": [],
"id": "monthly_cdrs_12",
"opts": {
"sqlConnMaxLifetime": "0",
"sqlDBName": "cgrates2",
"sqlMaxIdleConns": 10,
"sqlMaxOpenConns": 100,
"sqlTableName": "cdrs",
"pgSSLMode": "disable"
},
"tenant": "",
"timezone": "",
"type": "*sql"
},
{
"attempts": 2,
"attribute_context": "",
"attribute_ids": [],
"export_path": "mysql://cgrates:CGRateS.org@127.0.0.1:3306",
"fields": [
{
"tag": "RequiredTemplate",
"type": "*template",
"value": "requiredFields"
}
],
"filters": [
"*prefix:~*req.SetupTime:2023-02"
],
"flags": [],
"id": "monthly_cdrs_13",
"opts": {
"sqlConnMaxLifetime": "0",
"sqlDBName": "cgrates2",
"sqlMaxIdleConns": 10,
"sqlMaxOpenConns": 100,
"sqlTableName": "cdrs",
"pgSSLMode": "disable"
},
"tenant": "",
"timezone": "",
"type": "*sql"
}
]
},
"apiers": {
"enabled": true,
},
"templates": {
"requiredFields": [
{
"path": "*exp.cgrid",
"tag": "cgrid",
"type": "*variable",
"value": "~*req.CGRID"
},
{
"path": "*exp.run_id",
"tag": "run_id",
"type": "*variable",
"value": "~*req.RunID"
},
{
"path": "*exp.origin_host",
"tag": "origin_host",
"type": "*variable",
"value": "~*req.OriginHost"
},
{
"path": "*exp.source",
"tag": "source",
"type": "*variable",
"value": "~*req.Source"
},
{
"path": "*exp.origin_id",
"tag": "origin_id",
"type": "*variable",
"value": "~*req.OriginID"
},
{
"path": "*exp.tor",
"tag": "tor",
"type": "*variable",
"value": "~*req.ToR"
},
{
"path": "*exp.request_type",
"tag": "request_type",
"type": "*variable",
"value": "~*req.RequestType"
},
{
"path": "*exp.tenant",
"tag": "tenant",
"type": "*variable",
"value": "~*req.Tenant"
},
{
"path": "*exp.category",
"tag": "category",
"type": "*variable",
"value": "~*req.Category"
},
{
"path": "*exp.account",
"tag": "account",
"type": "*variable",
"value": "~*req.Account"
},
{
"path": "*exp.subject",
"tag": "subject",
"type": "*variable",
"value": "~*req.Subject"
},
{
"path": "*exp.destination",
"tag": "destination",
"type": "*variable",
"value": "~*req.Destination"
},
{
"path": "*exp.setup_time",
"tag": "setup_time",
"type": "*variable",
"value": "~*req.SetupTime"
},
{
"path": "*exp.answer_time",
"tag": "answer_time",
"type": "*variable",
"value": "~*req.AnswerTime"
},
{
"path": "*exp.`usage`",
"tag": "usage",
"type": "*variable",
"value": "~*req.Usage{*duration_nanoseconds}"
},
{
"path": "*exp.cost_source",
"tag": "cost_source",
"type": "*variable",
"value": "~*req.CostSource"
},
{
"path": "*exp.cost",
"tag": "cost",
"type": "*variable",
"value": "~*req.Cost"
},
{
"path": "*exp.cost_details",
"tag": "cost_details",
"type": "*variable",
"value": "~*req.CostDetails"
},
{
"path": "*exp.extra_info",
"tag": "extra_info",
"type": "*variable",
"value": "~*req.ExtraInfo"
},
{
"path": "*exp.disconnect_cause",
"tag": "disconnect_cause",
"type": "*variable",
"value": "~*req.DisconnectCause"
},
{
"path": "*exp.route",
"tag": "route",
"type": "*variable",
"value": "~*req.Route"
},
{
"path": "*exp.caller_is_determined_in_areas",
"tag": "caller_is_determined_in_areas",
"type": "*variable",
"value": "~*req.caller_is_determined_in_areas"
},
{
"path": "*exp.caller_is_determined",
"tag": "caller_is_determined",
"type": "*variable",
"value": "~*req.variable_caller_is_determined"
},
{
"path": "*exp.origination",
"tag": "origination",
"type": "*variable",
"value": "~*req.Account"
},
{
"path": "*exp.e164_destination",
"tag": "e164_destination",
"type": "*variable",
"value": "~*req.variable_e164_destination"
},
{
"path": "*exp.rn_destination",
"tag": "rn_destination",
"type": "*variable",
"value": "~*req.variable_rn_destination"
},
{
"path": "*exp.original_destination",
"tag": "original_destination",
"type": "*variable",
"value": "~*req.variable_original_destination"
},
{
"path": "*exp.original_account",
"tag": "original_account",
"type": "*variable",
"value": "~*req.variable_original_account"
}
]
},
}

381
resources/apis.go Normal file
View File

@@ -0,0 +1,381 @@
/*
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 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 <http://www.gnu.org/licenses/>
*/
package resources
import (
"time"
"github.com/cgrates/birpc/context"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
"github.com/cgrates/guardian"
)
// V1GetResourcesForEvent returns active resource configs matching the event
func (rS *ResourceS) V1GetResourcesForEvent(ctx *context.Context, args *utils.CGREvent, reply *Resources) (err error) {
if args == nil {
return utils.NewErrMandatoryIeMissing(utils.Event)
}
if missing := utils.MissingStructFields(args, []string{utils.ID, utils.Event}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
var usageID string
if usageID, err = engine.GetStringOpts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.UsageID,
utils.OptsResourcesUsageID); err != nil {
return
}
var ttl time.Duration
if ttl, err = engine.GetDurationOpts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.UsageTTL,
utils.OptsResourcesUsageTTL); err != nil {
return
}
usageTTL := utils.DurationPointer(ttl)
if usageID == utils.EmptyString {
return utils.NewErrMandatoryIeMissing(utils.UsageID)
}
tnt := args.Tenant
if tnt == utils.EmptyString {
tnt = rS.cfg.GeneralCfg().DefaultTenant
}
// RPC caching
if config.CgrConfig().CacheCfg().Partitions[utils.CacheRPCResponses].Limit != 0 {
cacheKey := utils.ConcatenatedKey(utils.ResourceSv1GetResourcesForEvent, utils.ConcatenatedKey(tnt, args.ID))
refID := guardian.Guardian.GuardIDs("",
config.CgrConfig().GeneralCfg().LockingTimeout, cacheKey) // RPC caching needs to be atomic
defer guardian.Guardian.UnguardIDs(refID)
if itm, has := engine.Cache.Get(utils.CacheRPCResponses, cacheKey); has {
cachedResp := itm.(*utils.CachedRPCResponse)
if cachedResp.Error == nil {
*reply = *cachedResp.Result.(*Resources)
}
return cachedResp.Error
}
defer engine.Cache.Set(ctx, utils.CacheRPCResponses, cacheKey,
&utils.CachedRPCResponse{Result: reply, Error: err},
nil, true, utils.NonTransactional)
}
// end of RPC caching
var mtcRLs Resources
if mtcRLs, err = rS.matchingResourcesForEvent(ctx, tnt, args, usageID, usageTTL); err != nil {
return err
}
*reply = mtcRLs
mtcRLs.unlock()
return
}
// V1AuthorizeResources queries service to find if an Usage is allowed
func (rS *ResourceS) V1AuthorizeResources(ctx *context.Context, args *utils.CGREvent, reply *string) (err error) {
if args == nil {
return utils.NewErrMandatoryIeMissing(utils.Event)
}
if missing := utils.MissingStructFields(args, []string{utils.ID, utils.Event}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
var usageID string
if usageID, err = engine.GetStringOpts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.UsageID,
utils.OptsResourcesUsageID); err != nil {
return
}
var units float64
if units, err = engine.GetFloat64Opts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.Units,
utils.OptsResourcesUnits); err != nil {
return
}
var ttl time.Duration
if ttl, err = engine.GetDurationOpts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.UsageTTL,
utils.OptsResourcesUsageTTL); err != nil {
return
}
usageTTL := utils.DurationPointer(ttl)
if usageID == utils.EmptyString {
return utils.NewErrMandatoryIeMissing(utils.UsageID)
}
tnt := args.Tenant
if tnt == utils.EmptyString {
tnt = rS.cfg.GeneralCfg().DefaultTenant
}
// RPC caching
if config.CgrConfig().CacheCfg().Partitions[utils.CacheRPCResponses].Limit != 0 {
cacheKey := utils.ConcatenatedKey(utils.ResourceSv1AuthorizeResources, utils.ConcatenatedKey(tnt, args.ID))
refID := guardian.Guardian.GuardIDs("",
config.CgrConfig().GeneralCfg().LockingTimeout, cacheKey) // RPC caching needs to be atomic
defer guardian.Guardian.UnguardIDs(refID)
if itm, has := engine.Cache.Get(utils.CacheRPCResponses, cacheKey); has {
cachedResp := itm.(*utils.CachedRPCResponse)
if cachedResp.Error == nil {
*reply = *cachedResp.Result.(*string)
}
return cachedResp.Error
}
defer engine.Cache.Set(ctx, utils.CacheRPCResponses, cacheKey,
&utils.CachedRPCResponse{Result: reply, Error: err},
nil, true, utils.NonTransactional)
}
// end of RPC caching
var mtcRLs Resources
if mtcRLs, err = rS.matchingResourcesForEvent(ctx, tnt, args, usageID, usageTTL); err != nil {
return err
}
defer mtcRLs.unlock()
var alcMessage string
if alcMessage, err = mtcRLs.allocateResource(&utils.ResourceUsage{
Tenant: tnt,
ID: usageID,
Units: units}, true); err != nil {
if err == utils.ErrResourceUnavailable {
err = utils.ErrResourceUnauthorized
}
return
}
*reply = alcMessage
return
}
// V1AllocateResources is called when a resource requires allocation
func (rS *ResourceS) V1AllocateResources(ctx *context.Context, args *utils.CGREvent, reply *string) (err error) {
if args == nil {
return utils.NewErrMandatoryIeMissing(utils.Event)
}
if missing := utils.MissingStructFields(args, []string{utils.ID, utils.Event}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
var usageID string
if usageID, err = engine.GetStringOpts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.UsageID,
utils.OptsResourcesUsageID); err != nil {
return
}
var units float64
if units, err = engine.GetFloat64Opts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.Units,
utils.OptsResourcesUnits); err != nil {
return
}
var ttl time.Duration
if ttl, err = engine.GetDurationOpts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.UsageTTL,
utils.OptsResourcesUsageTTL); err != nil {
return
}
usageTTL := utils.DurationPointer(ttl)
if usageID == utils.EmptyString {
return utils.NewErrMandatoryIeMissing(utils.UsageID)
}
tnt := args.Tenant
if tnt == utils.EmptyString {
tnt = rS.cfg.GeneralCfg().DefaultTenant
}
// RPC caching
if config.CgrConfig().CacheCfg().Partitions[utils.CacheRPCResponses].Limit != 0 {
cacheKey := utils.ConcatenatedKey(utils.ResourceSv1AllocateResources, utils.ConcatenatedKey(tnt, args.ID))
refID := guardian.Guardian.GuardIDs("",
config.CgrConfig().GeneralCfg().LockingTimeout, cacheKey) // RPC caching needs to be atomic
defer guardian.Guardian.UnguardIDs(refID)
if itm, has := engine.Cache.Get(utils.CacheRPCResponses, cacheKey); has {
cachedResp := itm.(*utils.CachedRPCResponse)
if cachedResp.Error == nil {
*reply = *cachedResp.Result.(*string)
}
return cachedResp.Error
}
defer engine.Cache.Set(ctx, utils.CacheRPCResponses, cacheKey,
&utils.CachedRPCResponse{Result: reply, Error: err},
nil, true, utils.NonTransactional)
}
// end of RPC caching
var mtcRLs Resources
if mtcRLs, err = rS.matchingResourcesForEvent(ctx, tnt, args, usageID,
usageTTL); err != nil {
return err
}
defer mtcRLs.unlock()
var alcMsg string
if alcMsg, err = mtcRLs.allocateResource(&utils.ResourceUsage{Tenant: tnt, ID: usageID,
Units: units}, false); err != nil {
return
}
// index it for storing
if err = rS.storeMatchedResources(ctx, mtcRLs); err != nil {
return
}
if err = rS.processThresholds(ctx, mtcRLs, args.APIOpts); err != nil {
return
}
*reply = alcMsg
return
}
// V1ReleaseResources is called when we need to clear an allocation
func (rS *ResourceS) V1ReleaseResources(ctx *context.Context, args *utils.CGREvent, reply *string) (err error) {
if args == nil {
return utils.NewErrMandatoryIeMissing(utils.Event)
}
if missing := utils.MissingStructFields(args, []string{utils.ID, utils.Event}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
var usageID string
if usageID, err = engine.GetStringOpts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.UsageID,
utils.OptsResourcesUsageID); err != nil {
return
}
var ttl time.Duration
if ttl, err = engine.GetDurationOpts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.UsageTTL,
utils.OptsResourcesUsageTTL); err != nil {
return
}
usageTTL := utils.DurationPointer(ttl)
if usageID == utils.EmptyString {
return utils.NewErrMandatoryIeMissing(utils.UsageID)
}
tnt := args.Tenant
if tnt == utils.EmptyString {
tnt = rS.cfg.GeneralCfg().DefaultTenant
}
// RPC caching
if config.CgrConfig().CacheCfg().Partitions[utils.CacheRPCResponses].Limit != 0 {
cacheKey := utils.ConcatenatedKey(utils.ResourceSv1ReleaseResources, utils.ConcatenatedKey(tnt, args.ID))
refID := guardian.Guardian.GuardIDs("",
config.CgrConfig().GeneralCfg().LockingTimeout, cacheKey) // RPC caching needs to be atomic
defer guardian.Guardian.UnguardIDs(refID)
if itm, has := engine.Cache.Get(utils.CacheRPCResponses, cacheKey); has {
cachedResp := itm.(*utils.CachedRPCResponse)
if cachedResp.Error == nil {
*reply = *cachedResp.Result.(*string)
}
return cachedResp.Error
}
defer engine.Cache.Set(ctx, utils.CacheRPCResponses, cacheKey,
&utils.CachedRPCResponse{Result: reply, Error: err},
nil, true, utils.NonTransactional)
}
// end of RPC caching
var mtcRLs Resources
if mtcRLs, err = rS.matchingResourcesForEvent(ctx, tnt, args, usageID,
usageTTL); err != nil {
return
}
defer mtcRLs.unlock()
if err = mtcRLs.clearUsage(usageID); err != nil {
return
}
// Handle storing
if err = rS.storeMatchedResources(ctx, mtcRLs); err != nil {
return
}
if err = rS.processThresholds(ctx, mtcRLs, args.APIOpts); err != nil {
return
}
*reply = utils.OK
return
}
// V1GetResource returns a resource configuration
func (rS *ResourceS) V1GetResource(ctx *context.Context, arg *utils.TenantIDWithAPIOpts, reply *utils.Resource) 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 = rS.cfg.GeneralCfg().DefaultTenant
}
// make sure resource is locked at process level
lkID := guardian.Guardian.GuardIDs(utils.EmptyString,
config.CgrConfig().GeneralCfg().LockingTimeout,
utils.ResourceLockKey(tnt, arg.ID))
defer guardian.Guardian.UnguardIDs(lkID)
res, err := rS.dm.GetResource(ctx, tnt, arg.ID, true, true, utils.NonTransactional)
if err != nil {
return err
}
*reply = *res
return nil
}
func (rS *ResourceS) V1GetResourceWithConfig(ctx *context.Context, arg *utils.TenantIDWithAPIOpts, reply *utils.ResourceWithConfig) (err 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 = rS.cfg.GeneralCfg().DefaultTenant
}
// make sure resource is locked at process level
lkID := guardian.Guardian.GuardIDs(utils.EmptyString,
config.CgrConfig().GeneralCfg().LockingTimeout,
utils.ResourceLockKey(tnt, arg.ID))
defer guardian.Guardian.UnguardIDs(lkID)
var res *utils.Resource
res, err = rS.dm.GetResource(ctx, tnt, arg.ID, true, true, utils.NonTransactional)
if err != nil {
return
}
// make sure resourceProfile is locked at process level
lkPrflID := guardian.Guardian.GuardIDs(utils.EmptyString,
config.CgrConfig().GeneralCfg().LockingTimeout,
utils.ResourceProfileLockKey(tnt, arg.ID))
defer guardian.Guardian.UnguardIDs(lkPrflID)
var cfg *utils.ResourceProfile
cfg, err = rS.dm.GetResourceProfile(ctx, tnt, arg.ID, true, true, utils.NonTransactional)
if err != nil {
return
}
*reply = utils.ResourceWithConfig{
Resource: res,
Config: cfg,
}
return
}

2809
resources/apis_test.go Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -589,355 +589,3 @@ func (rS *ResourceS) matchingResourcesForEvent(ctx *context.Context, tnt string,
}
return
}
// V1GetResourcesForEvent returns active resource configs matching the event
func (rS *ResourceS) V1GetResourcesForEvent(ctx *context.Context, args *utils.CGREvent, reply *Resources) (err error) {
if args == nil {
return utils.NewErrMandatoryIeMissing(utils.Event)
}
if missing := utils.MissingStructFields(args, []string{utils.ID, utils.Event}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
var usageID string
if usageID, err = engine.GetStringOpts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.UsageID,
utils.OptsResourcesUsageID); err != nil {
return
}
var ttl time.Duration
if ttl, err = engine.GetDurationOpts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.UsageTTL,
utils.OptsResourcesUsageTTL); err != nil {
return
}
usageTTL := utils.DurationPointer(ttl)
if usageID == utils.EmptyString {
return utils.NewErrMandatoryIeMissing(utils.UsageID)
}
tnt := args.Tenant
if tnt == utils.EmptyString {
tnt = rS.cfg.GeneralCfg().DefaultTenant
}
// RPC caching
if config.CgrConfig().CacheCfg().Partitions[utils.CacheRPCResponses].Limit != 0 {
cacheKey := utils.ConcatenatedKey(utils.ResourceSv1GetResourcesForEvent, utils.ConcatenatedKey(tnt, args.ID))
refID := guardian.Guardian.GuardIDs("",
config.CgrConfig().GeneralCfg().LockingTimeout, cacheKey) // RPC caching needs to be atomic
defer guardian.Guardian.UnguardIDs(refID)
if itm, has := engine.Cache.Get(utils.CacheRPCResponses, cacheKey); has {
cachedResp := itm.(*utils.CachedRPCResponse)
if cachedResp.Error == nil {
*reply = *cachedResp.Result.(*Resources)
}
return cachedResp.Error
}
defer engine.Cache.Set(ctx, utils.CacheRPCResponses, cacheKey,
&utils.CachedRPCResponse{Result: reply, Error: err},
nil, true, utils.NonTransactional)
}
// end of RPC caching
var mtcRLs Resources
if mtcRLs, err = rS.matchingResourcesForEvent(ctx, tnt, args, usageID, usageTTL); err != nil {
return err
}
*reply = mtcRLs
mtcRLs.unlock()
return
}
// V1AuthorizeResources queries service to find if an Usage is allowed
func (rS *ResourceS) V1AuthorizeResources(ctx *context.Context, args *utils.CGREvent, reply *string) (err error) {
if args == nil {
return utils.NewErrMandatoryIeMissing(utils.Event)
}
if missing := utils.MissingStructFields(args, []string{utils.ID, utils.Event}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
var usageID string
if usageID, err = engine.GetStringOpts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.UsageID,
utils.OptsResourcesUsageID); err != nil {
return
}
var units float64
if units, err = engine.GetFloat64Opts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.Units,
utils.OptsResourcesUnits); err != nil {
return
}
var ttl time.Duration
if ttl, err = engine.GetDurationOpts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.UsageTTL,
utils.OptsResourcesUsageTTL); err != nil {
return
}
usageTTL := utils.DurationPointer(ttl)
if usageID == utils.EmptyString {
return utils.NewErrMandatoryIeMissing(utils.UsageID)
}
tnt := args.Tenant
if tnt == utils.EmptyString {
tnt = rS.cfg.GeneralCfg().DefaultTenant
}
// RPC caching
if config.CgrConfig().CacheCfg().Partitions[utils.CacheRPCResponses].Limit != 0 {
cacheKey := utils.ConcatenatedKey(utils.ResourceSv1AuthorizeResources, utils.ConcatenatedKey(tnt, args.ID))
refID := guardian.Guardian.GuardIDs("",
config.CgrConfig().GeneralCfg().LockingTimeout, cacheKey) // RPC caching needs to be atomic
defer guardian.Guardian.UnguardIDs(refID)
if itm, has := engine.Cache.Get(utils.CacheRPCResponses, cacheKey); has {
cachedResp := itm.(*utils.CachedRPCResponse)
if cachedResp.Error == nil {
*reply = *cachedResp.Result.(*string)
}
return cachedResp.Error
}
defer engine.Cache.Set(ctx, utils.CacheRPCResponses, cacheKey,
&utils.CachedRPCResponse{Result: reply, Error: err},
nil, true, utils.NonTransactional)
}
// end of RPC caching
var mtcRLs Resources
if mtcRLs, err = rS.matchingResourcesForEvent(ctx, tnt, args, usageID, usageTTL); err != nil {
return err
}
defer mtcRLs.unlock()
var alcMessage string
if alcMessage, err = mtcRLs.allocateResource(&utils.ResourceUsage{
Tenant: tnt,
ID: usageID,
Units: units}, true); err != nil {
if err == utils.ErrResourceUnavailable {
err = utils.ErrResourceUnauthorized
}
return
}
*reply = alcMessage
return
}
// V1AllocateResources is called when a resource requires allocation
func (rS *ResourceS) V1AllocateResources(ctx *context.Context, args *utils.CGREvent, reply *string) (err error) {
if args == nil {
return utils.NewErrMandatoryIeMissing(utils.Event)
}
if missing := utils.MissingStructFields(args, []string{utils.ID, utils.Event}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
var usageID string
if usageID, err = engine.GetStringOpts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.UsageID,
utils.OptsResourcesUsageID); err != nil {
return
}
var units float64
if units, err = engine.GetFloat64Opts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.Units,
utils.OptsResourcesUnits); err != nil {
return
}
var ttl time.Duration
if ttl, err = engine.GetDurationOpts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.UsageTTL,
utils.OptsResourcesUsageTTL); err != nil {
return
}
usageTTL := utils.DurationPointer(ttl)
if usageID == utils.EmptyString {
return utils.NewErrMandatoryIeMissing(utils.UsageID)
}
tnt := args.Tenant
if tnt == utils.EmptyString {
tnt = rS.cfg.GeneralCfg().DefaultTenant
}
// RPC caching
if config.CgrConfig().CacheCfg().Partitions[utils.CacheRPCResponses].Limit != 0 {
cacheKey := utils.ConcatenatedKey(utils.ResourceSv1AllocateResources, utils.ConcatenatedKey(tnt, args.ID))
refID := guardian.Guardian.GuardIDs("",
config.CgrConfig().GeneralCfg().LockingTimeout, cacheKey) // RPC caching needs to be atomic
defer guardian.Guardian.UnguardIDs(refID)
if itm, has := engine.Cache.Get(utils.CacheRPCResponses, cacheKey); has {
cachedResp := itm.(*utils.CachedRPCResponse)
if cachedResp.Error == nil {
*reply = *cachedResp.Result.(*string)
}
return cachedResp.Error
}
defer engine.Cache.Set(ctx, utils.CacheRPCResponses, cacheKey,
&utils.CachedRPCResponse{Result: reply, Error: err},
nil, true, utils.NonTransactional)
}
// end of RPC caching
var mtcRLs Resources
if mtcRLs, err = rS.matchingResourcesForEvent(ctx, tnt, args, usageID,
usageTTL); err != nil {
return err
}
defer mtcRLs.unlock()
var alcMsg string
if alcMsg, err = mtcRLs.allocateResource(&utils.ResourceUsage{Tenant: tnt, ID: usageID,
Units: units}, false); err != nil {
return
}
// index it for storing
if err = rS.storeMatchedResources(ctx, mtcRLs); err != nil {
return
}
if err = rS.processThresholds(ctx, mtcRLs, args.APIOpts); err != nil {
return
}
*reply = alcMsg
return
}
// V1ReleaseResources is called when we need to clear an allocation
func (rS *ResourceS) V1ReleaseResources(ctx *context.Context, args *utils.CGREvent, reply *string) (err error) {
if args == nil {
return utils.NewErrMandatoryIeMissing(utils.Event)
}
if missing := utils.MissingStructFields(args, []string{utils.ID, utils.Event}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
var usageID string
if usageID, err = engine.GetStringOpts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.UsageID,
utils.OptsResourcesUsageID); err != nil {
return
}
var ttl time.Duration
if ttl, err = engine.GetDurationOpts(ctx, args.Tenant, args.AsDataProvider(), nil, rS.fltrS, rS.cfg.ResourceSCfg().Opts.UsageTTL,
utils.OptsResourcesUsageTTL); err != nil {
return
}
usageTTL := utils.DurationPointer(ttl)
if usageID == utils.EmptyString {
return utils.NewErrMandatoryIeMissing(utils.UsageID)
}
tnt := args.Tenant
if tnt == utils.EmptyString {
tnt = rS.cfg.GeneralCfg().DefaultTenant
}
// RPC caching
if config.CgrConfig().CacheCfg().Partitions[utils.CacheRPCResponses].Limit != 0 {
cacheKey := utils.ConcatenatedKey(utils.ResourceSv1ReleaseResources, utils.ConcatenatedKey(tnt, args.ID))
refID := guardian.Guardian.GuardIDs("",
config.CgrConfig().GeneralCfg().LockingTimeout, cacheKey) // RPC caching needs to be atomic
defer guardian.Guardian.UnguardIDs(refID)
if itm, has := engine.Cache.Get(utils.CacheRPCResponses, cacheKey); has {
cachedResp := itm.(*utils.CachedRPCResponse)
if cachedResp.Error == nil {
*reply = *cachedResp.Result.(*string)
}
return cachedResp.Error
}
defer engine.Cache.Set(ctx, utils.CacheRPCResponses, cacheKey,
&utils.CachedRPCResponse{Result: reply, Error: err},
nil, true, utils.NonTransactional)
}
// end of RPC caching
var mtcRLs Resources
if mtcRLs, err = rS.matchingResourcesForEvent(ctx, tnt, args, usageID,
usageTTL); err != nil {
return
}
defer mtcRLs.unlock()
if err = mtcRLs.clearUsage(usageID); err != nil {
return
}
// Handle storing
if err = rS.storeMatchedResources(ctx, mtcRLs); err != nil {
return
}
if err = rS.processThresholds(ctx, mtcRLs, args.APIOpts); err != nil {
return
}
*reply = utils.OK
return
}
// V1GetResource returns a resource configuration
func (rS *ResourceS) V1GetResource(ctx *context.Context, arg *utils.TenantIDWithAPIOpts, reply *utils.Resource) 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 = rS.cfg.GeneralCfg().DefaultTenant
}
// make sure resource is locked at process level
lkID := guardian.Guardian.GuardIDs(utils.EmptyString,
config.CgrConfig().GeneralCfg().LockingTimeout,
utils.ResourceLockKey(tnt, arg.ID))
defer guardian.Guardian.UnguardIDs(lkID)
res, err := rS.dm.GetResource(ctx, tnt, arg.ID, true, true, utils.NonTransactional)
if err != nil {
return err
}
*reply = *res
return nil
}
func (rS *ResourceS) V1GetResourceWithConfig(ctx *context.Context, arg *utils.TenantIDWithAPIOpts, reply *utils.ResourceWithConfig) (err 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 = rS.cfg.GeneralCfg().DefaultTenant
}
// make sure resource is locked at process level
lkID := guardian.Guardian.GuardIDs(utils.EmptyString,
config.CgrConfig().GeneralCfg().LockingTimeout,
utils.ResourceLockKey(tnt, arg.ID))
defer guardian.Guardian.UnguardIDs(lkID)
var res *utils.Resource
res, err = rS.dm.GetResource(ctx, tnt, arg.ID, true, true, utils.NonTransactional)
if err != nil {
return
}
// make sure resourceProfile is locked at process level
lkPrflID := guardian.Guardian.GuardIDs(utils.EmptyString,
config.CgrConfig().GeneralCfg().LockingTimeout,
utils.ResourceProfileLockKey(tnt, arg.ID))
defer guardian.Guardian.UnguardIDs(lkPrflID)
var cfg *utils.ResourceProfile
cfg, err = rS.dm.GetResourceProfile(ctx, tnt, arg.ID, true, true, utils.NonTransactional)
if err != nil {
return
}
*reply = utils.ResourceWithConfig{
Resource: res,
Config: cfg,
}
return
}

File diff suppressed because it is too large Load Diff