New type ResourceWithConfig + Available method

This commit is contained in:
porosnicuadrian
2021-02-19 16:55:00 +02:00
committed by Dan Christian Bogos
parent f74608dedc
commit 2ccf3f7fa5
7 changed files with 75 additions and 14 deletions

View File

@@ -101,8 +101,8 @@ func (dDP *dynamicDP) fieldAsInterface(fldPath []string) (val interface{}, err e
return dp.FieldAsInterface(fldPath[2:])
case utils.MetaResources:
// sample of fieldName : ~*resources.ResourceID.Field
var reply *Resource
if err := connMgr.Call(dDP.resConns, nil, utils.ResourceSv1GetResource,
var reply *ResourceWithConfig
if err := connMgr.Call(dDP.resConns, nil, utils.ResourceSv1GetResourceWithConfig,
&utils.TenantID{Tenant: dDP.tenant, ID: fldPath[1]}, &reply); err != nil {
return nil, err
}

View File

@@ -162,8 +162,8 @@ func (r *Resource) TotalUsage() (tU float64) {
// Available returns the available number of units
// Exported method to be used by filterS
func (r *Resource) Available() float64 {
return r.rPrf.Limit - r.totalUsage()
func (r *ResourceWithConfig) Available() float64 {
return r.Config.Limit - r.totalUsage()
}
// recordUsage records a new usage
@@ -815,6 +815,39 @@ func (rS *ResourceService) V1GetResource(arg *utils.TenantIDWithOpts, reply *Res
return nil
}
type ResourceWithConfig struct {
*Resource
Config *ResourceProfile
}
func (rS *ResourceService) V1GetResourceWithConfig(arg *utils.TenantIDWithOpts, reply *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.cgrcfg.GeneralCfg().DefaultTenant
}
var res *Resource
res, err = rS.dm.GetResource(tnt, arg.ID, true, true, utils.NonTransactional)
if err != nil {
return
}
if res.rPrf == nil {
var cfg *ResourceProfile
cfg, err = rS.dm.GetResourceProfile(tnt, arg.ID, true, true, utils.NonTransactional)
if err != nil {
return
}
res.rPrf = cfg
}
*reply = ResourceWithConfig{
Resource: res,
Config: res.rPrf,
}
return
}
// Reload stops the backupLoop and restarts it
func (rS *ResourceService) Reload() {
close(rS.stopBackup)