Updating IPs dynamic options

This commit is contained in:
DanB
2025-06-03 17:52:01 +02:00
parent 0d11ac9d8a
commit 86347c2741
5 changed files with 51 additions and 87 deletions

View File

@@ -206,9 +206,8 @@ func newCGRConfig(config []byte) (cfg *CGRConfig, err error) {
Units: []*DynamicFloat64Opt{{value: ResourcesUnitsDftOpt}},
}},
ipsCfg: &IPsCfg{Opts: &IPsOpts{
UsageID: []*DynamicStringOpt{{value: IPsUsageIDDftOpt}},
TTL: []*DynamicDurationOpt{{value: IPsTTLDftOpt}},
Units: []*DynamicFloat64Opt{{value: IPsUnitsDftOpt}},
AllocationID: []*DynamicStringOpt{{value: IPsAllocationIDDftOpt}},
TTL: []*DynamicDurationOpt{{value: IPsTTLDftOpt}},
}},
trendSCfg: new(TrendSCfg),
rankingSCfg: new(RankingSCfg),

View File

@@ -27,9 +27,9 @@ import (
)
const (
IPsUsageIDDftOpt = utils.EmptyString
IPsTTLDftOpt = 72 * time.Hour
IPsUnitsDftOpt = 1
IPsAllocationIDDftOpt = utils.EmptyString
IPsTTLDftOpt = 72 * time.Hour
IPsUnitsDftOpt = 1
)
// IPsJsonCfg holds the unparsed ips section configuration as found in the
@@ -169,15 +169,13 @@ func (c IPsCfg) AsMapInterface() any {
}
type IPsOptsJson struct {
UsageID []*DynamicInterfaceOpt `json:"*usageID"`
TTL []*DynamicInterfaceOpt `json:"*ttl"`
Units []*DynamicInterfaceOpt `json:"*units"`
AllocationID []*DynamicInterfaceOpt `json:"*allocationID"`
TTL []*DynamicInterfaceOpt `json:"*ttl"`
}
type IPsOpts struct {
UsageID []*DynamicStringOpt
TTL []*DynamicDurationOpt
Units []*DynamicFloat64Opt
AllocationID []*DynamicStringOpt
TTL []*DynamicDurationOpt
}
func (o *IPsOpts) loadFromJSONCfg(jc *IPsOptsJson) error {
@@ -187,12 +185,12 @@ func (o *IPsOpts) loadFromJSONCfg(jc *IPsOptsJson) error {
// NOTE: prepend to the existing slice to ensure that the default opts that
// always match are at the end.
if jc.UsageID != nil {
usageID, err := InterfaceToDynamicStringOpts(jc.UsageID)
if jc.AllocationID != nil {
allocID, err := InterfaceToDynamicStringOpts(jc.AllocationID)
if err != nil {
return err
}
o.UsageID = append(usageID, o.UsageID...)
o.AllocationID = append(allocID, o.AllocationID...)
}
if jc.TTL != nil {
ttl, err := IfaceToDurationDynamicOpts(jc.TTL)
@@ -201,31 +199,22 @@ func (o *IPsOpts) loadFromJSONCfg(jc *IPsOptsJson) error {
}
o.TTL = append(ttl, o.TTL...)
}
if jc.Units != nil {
units, err := InterfaceToFloat64DynamicOpts(jc.Units)
if err != nil {
return err
}
o.Units = append(units, o.Units...)
}
return nil
}
// Clone returns a deep copy of IPsOpts.
func (o *IPsOpts) Clone() *IPsOpts {
return &IPsOpts{
UsageID: CloneDynamicStringOpt(o.UsageID),
TTL: CloneDynamicDurationOpt(o.TTL),
Units: CloneDynamicFloat64Opt(o.Units),
AllocationID: CloneDynamicStringOpt(o.AllocationID),
TTL: CloneDynamicDurationOpt(o.TTL),
}
}
// AsMapInterface returns the config as a map[string]any.
func (o *IPsOpts) AsMapInterface() map[string]any {
return map[string]any{
utils.MetaUsageIDCfg: o.UsageID,
utils.MetaUnitsCfg: o.Units,
utils.MetaTTLCfg: o.TTL,
utils.MetaAllocationID: o.AllocationID,
utils.MetaTTLCfg: o.TTL,
}
}
@@ -233,15 +222,12 @@ func diffIPsOptsJsonCfg(d *IPsOptsJson, v1, v2 *IPsOpts) *IPsOptsJson {
if d == nil {
d = new(IPsOptsJson)
}
if !DynamicStringOptEqual(v1.UsageID, v2.UsageID) {
d.UsageID = DynamicStringToInterfaceOpts(v2.UsageID)
if !DynamicStringOptEqual(v1.AllocationID, v2.AllocationID) {
d.AllocationID = DynamicStringToInterfaceOpts(v2.AllocationID)
}
if !DynamicDurationOptEqual(v1.TTL, v2.TTL) {
d.TTL = DurationToIfaceDynamicOpts(v2.TTL)
}
if !DynamicFloat64OptEqual(v1.Units, v2.Units) {
d.Units = Float64ToInterfaceDynamicOpts(v2.Units)
}
return d
}

View File

@@ -37,9 +37,9 @@ func (s *IPService) V1GetIPAllocationsForEvent(ctx *context.Context, args *utils
return utils.NewErrMandatoryIeMissing(missing...)
}
var usageID string
if usageID, err = engine.GetStringOpts(ctx, args.Tenant, args.AsDataProvider(), nil, s.fltrs, s.cfg.IPsCfg().Opts.UsageID,
utils.OptsIPsUsageID); err != nil {
var allocID string
if allocID, err = engine.GetStringOpts(ctx, args.Tenant, args.AsDataProvider(), nil, s.fltrs, s.cfg.IPsCfg().Opts.AllocationID,
utils.OptsIPsAllocationID); err != nil {
return
}
@@ -50,8 +50,8 @@ func (s *IPService) V1GetIPAllocationsForEvent(ctx *context.Context, args *utils
}
usageTTL := utils.DurationPointer(ttl)
if usageID == utils.EmptyString {
return utils.NewErrMandatoryIeMissing(utils.UsageID)
if allocID == utils.EmptyString {
return utils.NewErrMandatoryIeMissing(utils.AllocationID)
}
tnt := args.Tenant
if tnt == utils.EmptyString {
@@ -78,7 +78,7 @@ func (s *IPService) V1GetIPAllocationsForEvent(ctx *context.Context, args *utils
// end of RPC caching
var mtcRLs IPAllocationsList
if mtcRLs, err = s.matchingIPAllocationsForEvent(ctx, tnt, args, usageID, usageTTL); err != nil {
if mtcRLs, err = s.matchingIPAllocationsForEvent(ctx, tnt, args, allocID, usageTTL); err != nil {
return err
}
*reply = mtcRLs
@@ -95,14 +95,9 @@ func (s *IPService) V1AuthorizeIP(ctx *context.Context, args *utils.CGREvent, re
return utils.NewErrMandatoryIeMissing(missing...)
}
var usageID string
if usageID, err = engine.GetStringOpts(ctx, args.Tenant, args.AsDataProvider(), nil, s.fltrs, s.cfg.IPsCfg().Opts.UsageID,
utils.OptsIPsUsageID); err != nil {
return
}
if _, err = engine.GetFloat64Opts(ctx, args.Tenant, args.AsDataProvider(), nil, s.fltrs, s.cfg.IPsCfg().Opts.Units,
utils.OptsIPsUnits); err != nil {
var allocID string
if allocID, err = engine.GetStringOpts(ctx, args.Tenant, args.AsDataProvider(), nil, s.fltrs, s.cfg.IPsCfg().Opts.AllocationID,
utils.OptsIPsAllocationID); err != nil {
return
}
@@ -113,8 +108,8 @@ func (s *IPService) V1AuthorizeIP(ctx *context.Context, args *utils.CGREvent, re
}
usageTTL := utils.DurationPointer(ttl)
if usageID == utils.EmptyString {
return utils.NewErrMandatoryIeMissing(utils.UsageID)
if allocID == utils.EmptyString {
return utils.NewErrMandatoryIeMissing(utils.AllocationID)
}
tnt := args.Tenant
@@ -142,7 +137,7 @@ func (s *IPService) V1AuthorizeIP(ctx *context.Context, args *utils.CGREvent, re
// end of RPC caching
var mtcRLs IPAllocationsList
if mtcRLs, err = s.matchingIPAllocationsForEvent(ctx, tnt, args, usageID, usageTTL); err != nil {
if mtcRLs, err = s.matchingIPAllocationsForEvent(ctx, tnt, args, allocID, usageTTL); err != nil {
return err
}
defer mtcRLs.unlock()
@@ -165,14 +160,9 @@ func (s *IPService) V1AllocateIP(ctx *context.Context, args *utils.CGREvent, rep
return utils.NewErrMandatoryIeMissing(missing...)
}
var usageID string
if usageID, err = engine.GetStringOpts(ctx, args.Tenant, args.AsDataProvider(), nil, s.fltrs, s.cfg.IPsCfg().Opts.UsageID,
utils.OptsIPsUsageID); err != nil {
return
}
if _, err = engine.GetFloat64Opts(ctx, args.Tenant, args.AsDataProvider(), nil, s.fltrs, s.cfg.IPsCfg().Opts.Units,
utils.OptsIPsUnits); err != nil {
var allocID string
if allocID, err = engine.GetStringOpts(ctx, args.Tenant, args.AsDataProvider(), nil, s.fltrs, s.cfg.IPsCfg().Opts.AllocationID,
utils.OptsIPsAllocationID); err != nil {
return
}
@@ -183,8 +173,8 @@ func (s *IPService) V1AllocateIP(ctx *context.Context, args *utils.CGREvent, rep
}
usageTTL := utils.DurationPointer(ttl)
if usageID == utils.EmptyString {
return utils.NewErrMandatoryIeMissing(utils.UsageID)
if allocID == utils.EmptyString {
return utils.NewErrMandatoryIeMissing(utils.AllocationID)
}
tnt := args.Tenant
@@ -212,7 +202,7 @@ func (s *IPService) V1AllocateIP(ctx *context.Context, args *utils.CGREvent, rep
// end of RPC caching
var mtcRLs IPAllocationsList
if mtcRLs, err = s.matchingIPAllocationsForEvent(ctx, tnt, args, usageID,
if mtcRLs, err = s.matchingIPAllocationsForEvent(ctx, tnt, args, allocID,
usageTTL); err != nil {
return err
}
@@ -240,9 +230,9 @@ func (s *IPService) V1ReleaseIP(ctx *context.Context, args *utils.CGREvent, repl
return utils.NewErrMandatoryIeMissing(missing...)
}
var usageID string
if usageID, err = engine.GetStringOpts(ctx, args.Tenant, args.AsDataProvider(), nil, s.fltrs, s.cfg.IPsCfg().Opts.UsageID,
utils.OptsIPsUsageID); err != nil {
var allocID string
if allocID, err = engine.GetStringOpts(ctx, args.Tenant, args.AsDataProvider(), nil, s.fltrs, s.cfg.IPsCfg().Opts.AllocationID,
utils.OptsIPsAllocationID); err != nil {
return
}
@@ -253,8 +243,8 @@ func (s *IPService) V1ReleaseIP(ctx *context.Context, args *utils.CGREvent, repl
}
usageTTL := utils.DurationPointer(ttl)
if usageID == utils.EmptyString {
return utils.NewErrMandatoryIeMissing(utils.UsageID)
if allocID == utils.EmptyString {
return utils.NewErrMandatoryIeMissing(utils.AllocationID)
}
tnt := args.Tenant
@@ -282,7 +272,7 @@ func (s *IPService) V1ReleaseIP(ctx *context.Context, args *utils.CGREvent, repl
// end of RPC caching
var mtcRLs IPAllocationsList
if mtcRLs, err = s.matchingIPAllocationsForEvent(ctx, tnt, args, usageID,
if mtcRLs, err = s.matchingIPAllocationsForEvent(ctx, tnt, args, allocID,
usageTTL); err != nil {
return
}

View File

@@ -226,7 +226,7 @@ cgrates.org,IPs2,*string:~*req.Account:1002,;20,2s,false,POOL1,*string:~*req.Des
t.Error(err)
}
usageID := "api_usage"
allocID := "api_usage"
var evIPs IPAllocationsList
if err := client.Call(context.Background(), utils.IPsV1GetIPAllocationsForEvent,
&utils.CGREvent{
@@ -236,7 +236,7 @@ cgrates.org,IPs2,*string:~*req.Account:1002,;20,2s,false,POOL1,*string:~*req.Des
utils.AccountField: "1001",
},
APIOpts: map[string]any{
utils.OptsIPsUsageID: usageID,
utils.OptsIPsAllocationID: allocID,
},
}, &evIPs); err != nil {
t.Error(err)
@@ -250,11 +250,7 @@ cgrates.org,IPs2,*string:~*req.Account:1002,;20,2s,false,POOL1,*string:~*req.Des
Event: map[string]any{
utils.AccountField: "1001",
},
APIOpts: map[string]any{
// utils.OptsIPsUsageID: usageID,
// utils.OptsIPsTTL: time.Second,
// utils.OptsIPsUnits: 2,
},
APIOpts: map[string]any{},
}, &reply); err != nil {
t.Error(err)
}
@@ -266,11 +262,7 @@ cgrates.org,IPs2,*string:~*req.Account:1002,;20,2s,false,POOL1,*string:~*req.Des
Event: map[string]any{
utils.AccountField: "1001",
},
APIOpts: map[string]any{
// utils.OptsIPsUsageID: usageID,
// utils.OptsIPsTTL: time.Second,
// utils.OptsIPsUnits: 2,
},
APIOpts: map[string]any{},
}, &reply); err != nil {
t.Error(err)
}
@@ -282,11 +274,7 @@ cgrates.org,IPs2,*string:~*req.Account:1002,;20,2s,false,POOL1,*string:~*req.Des
Event: map[string]any{
utils.AccountField: "1001",
},
APIOpts: map[string]any{
// utils.OptsIPsUsageID: usageID,
// utils.OptsIPsTTL: time.Second,
// utils.OptsIPsUnits: 2,
},
APIOpts: map[string]any{},
}, &reply); err != nil {
t.Error(err)
}

View File

@@ -708,6 +708,7 @@ const (
DNSAgent = "DNSAgent"
TLSNoCaps = "tls"
UsageID = "UsageID"
AllocationID = "AllocationID"
Replacement = "Replacement"
Regexp = "Regexp"
Order = "Order"
@@ -2586,9 +2587,9 @@ const (
OptsResourcesUsageTTL = "*rsUsageTTL"
// IPs
OptsIPsUnits = "*ipUnits"
OptsIPsUsageID = "*ipUsageID"
OptsIPsTTL = "*ipTTL"
OptsIPsAllocationID = "*ipAllocationID"
OptsIPsTTL = "*ipTTL"
MetaAllocationID = "*allocationID"
// Routes
OptsRoutesProfilesCount = "*rouProfilesCount"