mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-20 06:38:45 +05:00
add implementation for new ips module
This commit is contained in:
committed by
Dan Christian Bogos
parent
1b4324157d
commit
eed6b8a51a
@@ -49,6 +49,12 @@ type DataDBMock struct {
|
||||
RemoveResourceDrvF func(ctx *context.Context, tnt, id string) error
|
||||
SetResourceDrvF func(ctx *context.Context, r *utils.Resource) error
|
||||
GetResourceDrvF func(ctx *context.Context, tenant, id string) (*utils.Resource, error)
|
||||
GetIPProfileDrvF func(ctx *context.Context, tnt, id string) (*utils.IPProfile, error)
|
||||
SetIPProfileDrvF func(ctx *context.Context, ipp *utils.IPProfile) error
|
||||
RemoveIPProfileDrvF func(ctx *context.Context, tnt, id string) error
|
||||
RemoveIPDrvF func(ctx *context.Context, tnt, id string) error
|
||||
SetIPDrvF func(ctx *context.Context, ip *utils.IP) error
|
||||
GetIPDrvF func(ctx *context.Context, tenant, id string) (*utils.IP, error)
|
||||
SetTrendProfileDrvF func(ctx *context.Context, tr *utils.TrendProfile) (err error)
|
||||
GetTrendProfileDrvF func(ctx *context.Context, tenant string, id string) (sq *utils.TrendProfile, err error)
|
||||
RemTrendProfileDrvF func(ctx *context.Context, tenant string, id string) (err error)
|
||||
@@ -163,6 +169,48 @@ func (dbM *DataDBMock) RemoveResourceDrv(ctx *context.Context, tnt, id string) e
|
||||
return utils.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (dbM *DataDBMock) GetIPProfileDrv(ctx *context.Context, tnt, id string) (*utils.IPProfile, error) {
|
||||
if dbM.GetIPProfileDrvF != nil {
|
||||
return dbM.GetIPProfileDrvF(ctx, tnt, id)
|
||||
}
|
||||
return nil, utils.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (dbM *DataDBMock) SetIPProfileDrv(ctx *context.Context, ipp *utils.IPProfile) error {
|
||||
if dbM.SetIPProfileDrvF != nil {
|
||||
return dbM.SetIPProfileDrvF(ctx, ipp)
|
||||
}
|
||||
return utils.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (dbM *DataDBMock) RemoveIPProfileDrv(ctx *context.Context, tnt string, id string) error {
|
||||
if dbM.RemoveIPProfileDrvF != nil {
|
||||
return dbM.RemoveIPProfileDrvF(ctx, tnt, id)
|
||||
}
|
||||
return utils.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (dbM *DataDBMock) GetIPDrv(ctx *context.Context, tenant, id string) (*utils.IP, error) {
|
||||
if dbM.GetIPDrvF != nil {
|
||||
return dbM.GetIPDrvF(ctx, tenant, id)
|
||||
}
|
||||
return nil, utils.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (dbM *DataDBMock) SetIPDrv(ctx *context.Context, ip *utils.IP) error {
|
||||
if dbM.SetIPDrvF != nil {
|
||||
return dbM.SetIPDrvF(ctx, ip)
|
||||
}
|
||||
return utils.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (dbM *DataDBMock) RemoveIPDrv(ctx *context.Context, tnt, id string) error {
|
||||
if dbM.RemoveIPDrvF != nil {
|
||||
return dbM.RemoveIPDrvF(ctx, tnt, id)
|
||||
}
|
||||
return utils.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (dbM *DataDBMock) GetLoadHistory(int, bool, string) ([]*utils.LoadInstance, error) {
|
||||
return nil, utils.ErrNotImplemented
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ var (
|
||||
filterIndexesPrefixMap = utils.StringSet{
|
||||
utils.AttributeFilterIndexes: {},
|
||||
utils.ResourceFilterIndexes: {},
|
||||
utils.IPFilterIndexes: {},
|
||||
utils.StatFilterIndexes: {},
|
||||
utils.ThresholdFilterIndexes: {},
|
||||
utils.RouteFilterIndexes: {},
|
||||
@@ -48,6 +49,8 @@ var (
|
||||
cachePrefixMap = utils.StringSet{
|
||||
utils.ResourceProfilesPrefix: {},
|
||||
utils.ResourcesPrefix: {},
|
||||
utils.IPProfilesPrefix: {},
|
||||
utils.IPsPrefix: {},
|
||||
utils.StatQueuePrefix: {},
|
||||
utils.StatQueueProfilePrefix: {},
|
||||
utils.ThresholdPrefix: {},
|
||||
@@ -66,6 +69,7 @@ var (
|
||||
utils.ActionProfilePrefix: {},
|
||||
utils.AttributeFilterIndexes: {},
|
||||
utils.ResourceFilterIndexes: {},
|
||||
utils.IPFilterIndexes: {},
|
||||
utils.StatFilterIndexes: {},
|
||||
utils.ThresholdFilterIndexes: {},
|
||||
utils.RouteFilterIndexes: {},
|
||||
@@ -158,6 +162,16 @@ func (dm *DataManager) CacheDataFromDB(ctx *context.Context, prfx string, ids []
|
||||
lkID := guardian.Guardian.GuardIDs("", dm.cfg.GeneralCfg().LockingTimeout, utils.ResourceLockKey(tntID.Tenant, tntID.ID))
|
||||
_, err = dm.GetResource(ctx, tntID.Tenant, tntID.ID, false, true, utils.NonTransactional)
|
||||
guardian.Guardian.UnguardIDs(lkID)
|
||||
case utils.IPProfilesPrefix:
|
||||
tntID := utils.NewTenantID(dataID)
|
||||
lkID := guardian.Guardian.GuardIDs("", dm.cfg.GeneralCfg().LockingTimeout, utils.IPProfileLockKey(tntID.Tenant, tntID.ID))
|
||||
_, err = dm.GetIPProfile(ctx, tntID.Tenant, tntID.ID, false, true, utils.NonTransactional)
|
||||
guardian.Guardian.UnguardIDs(lkID)
|
||||
case utils.IPsPrefix:
|
||||
tntID := utils.NewTenantID(dataID)
|
||||
lkID := guardian.Guardian.GuardIDs("", dm.cfg.GeneralCfg().LockingTimeout, utils.IPLockKey(tntID.Tenant, tntID.ID))
|
||||
_, err = dm.GetIP(ctx, tntID.Tenant, tntID.ID, false, true, utils.NonTransactional)
|
||||
guardian.Guardian.UnguardIDs(lkID)
|
||||
case utils.StatQueueProfilePrefix:
|
||||
tntID := utils.NewTenantID(dataID)
|
||||
lkID := guardian.Guardian.GuardIDs("", dm.cfg.GeneralCfg().LockingTimeout, statQueueProfileLockKey(tntID.Tenant, tntID.ID))
|
||||
@@ -216,6 +230,12 @@ func (dm *DataManager) CacheDataFromDB(ctx *context.Context, prfx string, ids []
|
||||
return
|
||||
}
|
||||
_, err = dm.GetIndexes(ctx, utils.CacheResourceFilterIndexes, tntCtx, idxKey, utils.NonTransactional, false, true)
|
||||
case utils.IPFilterIndexes:
|
||||
var tntCtx, idxKey string
|
||||
if tntCtx, idxKey, err = splitFilterIndex(dataID); err != nil {
|
||||
return
|
||||
}
|
||||
_, err = dm.GetIndexes(ctx, utils.CacheIPFilterIndexes, tntCtx, idxKey, utils.NonTransactional, false, true)
|
||||
case utils.StatFilterIndexes:
|
||||
var tntCtx, idxKey string
|
||||
if tntCtx, idxKey, err = splitFilterIndex(dataID); err != nil {
|
||||
@@ -1663,6 +1683,241 @@ func (dm *DataManager) RemoveResourceProfile(ctx *context.Context, tenant, id st
|
||||
return dm.RemoveResource(ctx, tenant, id)
|
||||
}
|
||||
|
||||
func (dm *DataManager) GetIP(ctx *context.Context, tenant, id string, cacheRead, cacheWrite bool,
|
||||
transactionID string) (ip *utils.IP, err error) {
|
||||
tntID := utils.ConcatenatedKey(tenant, id)
|
||||
if cacheRead {
|
||||
if x, ok := Cache.Get(utils.CacheIPs, tntID); ok {
|
||||
if x == nil {
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
return x.(*utils.IP), nil
|
||||
}
|
||||
}
|
||||
if dm == nil {
|
||||
err = utils.ErrNoDatabaseConn
|
||||
return
|
||||
}
|
||||
ip, err = dm.dataDB.GetIPDrv(ctx, tenant, id)
|
||||
if err != nil {
|
||||
if itm := dm.cfg.DataDbCfg().Items[utils.MetaIPs]; err == utils.ErrNotFound && itm.Remote {
|
||||
if err = dm.connMgr.Call(ctx, dm.cfg.DataDbCfg().RmtConns,
|
||||
utils.ReplicatorSv1GetIP,
|
||||
&utils.TenantIDWithAPIOpts{
|
||||
TenantID: &utils.TenantID{Tenant: tenant, ID: id},
|
||||
APIOpts: utils.GenerateDBItemOpts(itm.APIKey, itm.RouteID, utils.EmptyString,
|
||||
utils.FirstNonEmpty(dm.cfg.DataDbCfg().RmtConnID,
|
||||
dm.cfg.GeneralCfg().NodeID)),
|
||||
}, &ip); err == nil {
|
||||
err = dm.dataDB.SetIPDrv(ctx, ip)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
err = utils.CastRPCErr(err)
|
||||
if err == utils.ErrNotFound && cacheWrite {
|
||||
if errCh := Cache.Set(ctx, utils.CacheIPs, tntID, nil, nil,
|
||||
cacheCommit(transactionID), transactionID); errCh != nil {
|
||||
return nil, errCh
|
||||
}
|
||||
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if cacheWrite {
|
||||
if errCh := Cache.Set(ctx, utils.CacheIPs, tntID, ip, nil,
|
||||
cacheCommit(transactionID), transactionID); errCh != nil {
|
||||
return nil, errCh
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (dm *DataManager) SetIP(ctx *context.Context, ip *utils.IP) (err error) {
|
||||
if dm == nil {
|
||||
return utils.ErrNoDatabaseConn
|
||||
}
|
||||
if err = dm.DataDB().SetIPDrv(ctx, ip); err != nil {
|
||||
return
|
||||
}
|
||||
if itm := dm.cfg.DataDbCfg().Items[utils.MetaIPs]; itm.Replicate {
|
||||
err = replicate(ctx, dm.connMgr, dm.cfg.DataDbCfg().RplConns,
|
||||
dm.cfg.DataDbCfg().RplFiltered,
|
||||
utils.IPsPrefix, ip.TenantID(), // this are used to get the host IDs from cache
|
||||
utils.ReplicatorSv1SetIP,
|
||||
&utils.IPWithAPIOpts{
|
||||
IP: ip,
|
||||
APIOpts: utils.GenerateDBItemOpts(itm.APIKey, itm.RouteID,
|
||||
dm.cfg.DataDbCfg().RplCache, utils.EmptyString)})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (dm *DataManager) RemoveIP(ctx *context.Context, tenant, id string) (err error) {
|
||||
if dm == nil {
|
||||
return utils.ErrNoDatabaseConn
|
||||
}
|
||||
if err = dm.DataDB().RemoveIPDrv(ctx, tenant, id); err != nil {
|
||||
return
|
||||
}
|
||||
if itm := dm.cfg.DataDbCfg().Items[utils.MetaIPs]; itm.Replicate {
|
||||
replicate(ctx, dm.connMgr, dm.cfg.DataDbCfg().RplConns,
|
||||
dm.cfg.DataDbCfg().RplFiltered,
|
||||
utils.IPsPrefix, utils.ConcatenatedKey(tenant, id), // this are used to get the host IDs from cache
|
||||
utils.ReplicatorSv1RemoveIP,
|
||||
&utils.TenantIDWithAPIOpts{
|
||||
TenantID: &utils.TenantID{Tenant: tenant, ID: id},
|
||||
APIOpts: utils.GenerateDBItemOpts(itm.APIKey, itm.RouteID,
|
||||
dm.cfg.DataDbCfg().RplCache, utils.EmptyString)})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (dm *DataManager) GetIPProfile(ctx *context.Context, tenant, id string, cacheRead, cacheWrite bool,
|
||||
transactionID string) (ipp *utils.IPProfile, err error) {
|
||||
tntID := utils.ConcatenatedKey(tenant, id)
|
||||
if cacheRead {
|
||||
if x, ok := Cache.Get(utils.CacheIPProfiles, tntID); ok {
|
||||
if x == nil {
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
return x.(*utils.IPProfile), nil
|
||||
}
|
||||
}
|
||||
if dm == nil {
|
||||
err = utils.ErrNoDatabaseConn
|
||||
return
|
||||
}
|
||||
ipp, err = dm.dataDB.GetIPProfileDrv(ctx, tenant, id)
|
||||
if err != nil {
|
||||
if itm := dm.cfg.DataDbCfg().Items[utils.MetaIPProfiles]; err == utils.ErrNotFound && itm.Remote {
|
||||
if err = dm.connMgr.Call(ctx, dm.cfg.DataDbCfg().RmtConns,
|
||||
utils.ReplicatorSv1GetIPProfile, &utils.TenantIDWithAPIOpts{
|
||||
TenantID: &utils.TenantID{Tenant: tenant, ID: id},
|
||||
APIOpts: utils.GenerateDBItemOpts(itm.APIKey, itm.RouteID, utils.EmptyString,
|
||||
utils.FirstNonEmpty(dm.cfg.DataDbCfg().RmtConnID,
|
||||
dm.cfg.GeneralCfg().NodeID)),
|
||||
}, &ipp); err == nil {
|
||||
err = dm.dataDB.SetIPProfileDrv(ctx, ipp)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
err = utils.CastRPCErr(err)
|
||||
if err == utils.ErrNotFound && cacheWrite {
|
||||
if errCh := Cache.Set(ctx, utils.CacheIPProfiles, tntID, nil, nil,
|
||||
cacheCommit(transactionID), transactionID); errCh != nil {
|
||||
return nil, errCh
|
||||
}
|
||||
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if cacheWrite {
|
||||
if errCh := Cache.Set(ctx, utils.CacheIPProfiles, tntID, ipp, nil,
|
||||
cacheCommit(transactionID), transactionID); errCh != nil {
|
||||
return nil, errCh
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (dm *DataManager) SetIPProfile(ctx *context.Context, ipp *utils.IPProfile, withIndex bool) (err error) {
|
||||
if dm == nil {
|
||||
return utils.ErrNoDatabaseConn
|
||||
}
|
||||
if withIndex {
|
||||
if err := dm.checkFilters(ctx, ipp.Tenant, ipp.FilterIDs); err != nil {
|
||||
// if we get a broken filter do not set the profile
|
||||
return fmt.Errorf("%+s for item with ID: %+v",
|
||||
err, ipp.TenantID())
|
||||
}
|
||||
}
|
||||
oldIPP, err := dm.GetIPProfile(ctx, ipp.Tenant, ipp.ID, true, false, utils.NonTransactional)
|
||||
if err != nil && err != utils.ErrNotFound {
|
||||
return err
|
||||
}
|
||||
if err = dm.DataDB().SetIPProfileDrv(ctx, ipp); err != nil {
|
||||
return err
|
||||
}
|
||||
if withIndex {
|
||||
var oldFiltersIDs *[]string
|
||||
if oldIPP != nil {
|
||||
oldFiltersIDs = &oldIPP.FilterIDs
|
||||
}
|
||||
if err := updatedIndexes(ctx, dm, utils.CacheIPFilterIndexes, ipp.Tenant,
|
||||
utils.EmptyString, ipp.ID, oldFiltersIDs, ipp.FilterIDs, false); err != nil {
|
||||
return err
|
||||
}
|
||||
Cache.Clear([]string{utils.CacheEventIPs})
|
||||
}
|
||||
if itm := dm.cfg.DataDbCfg().Items[utils.MetaIPProfiles]; itm.Replicate {
|
||||
if err = replicate(ctx, dm.connMgr, dm.cfg.DataDbCfg().RplConns,
|
||||
dm.cfg.DataDbCfg().RplFiltered,
|
||||
utils.IPProfilesPrefix, ipp.TenantID(), // this are used to get the host IDs from cache
|
||||
utils.ReplicatorSv1SetIPProfile,
|
||||
&utils.IPProfileWithAPIOpts{
|
||||
IPProfile: ipp,
|
||||
APIOpts: utils.GenerateDBItemOpts(itm.APIKey, itm.RouteID,
|
||||
dm.cfg.DataDbCfg().RplCache, utils.EmptyString)}); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
if oldIPP == nil || // create the resource if it didn't exist before
|
||||
oldIPP.TTL != ipp.TTL ||
|
||||
oldIPP.Stored != ipp.Stored && oldIPP.Stored { // reset the resource if the profile changed this fields
|
||||
err = dm.SetIP(ctx, &utils.IP{
|
||||
Tenant: ipp.Tenant,
|
||||
ID: ipp.ID,
|
||||
Usages: make(map[string]*utils.IPUsage),
|
||||
})
|
||||
} else if _, errRs := dm.GetIP(ctx, ipp.Tenant, ipp.ID, // do not try to get the resource if the configuration changed
|
||||
true, false, utils.NonTransactional); errRs == utils.ErrNotFound { // the resource does not exist
|
||||
err = dm.SetIP(ctx, &utils.IP{
|
||||
Tenant: ipp.Tenant,
|
||||
ID: ipp.ID,
|
||||
Usages: make(map[string]*utils.IPUsage),
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (dm *DataManager) RemoveIPProfile(ctx *context.Context, tenant, id string, withIndex bool) (err error) {
|
||||
if dm == nil {
|
||||
return utils.ErrNoDatabaseConn
|
||||
}
|
||||
oldIPP, err := dm.GetIPProfile(ctx, tenant, id, true, false, utils.NonTransactional)
|
||||
if err != nil && err != utils.ErrNotFound {
|
||||
return err
|
||||
}
|
||||
if err = dm.DataDB().RemoveIPProfileDrv(ctx, tenant, id); err != nil {
|
||||
return
|
||||
}
|
||||
if oldIPP == nil {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
if withIndex {
|
||||
if err = removeIndexFiltersItem(ctx, dm, utils.CacheIPFilterIndexes, tenant, id, oldIPP.FilterIDs); err != nil {
|
||||
return
|
||||
}
|
||||
if err = removeItemFromFilterIndex(ctx, dm, utils.CacheIPFilterIndexes,
|
||||
tenant, utils.EmptyString, id, oldIPP.FilterIDs); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
if itm := dm.cfg.DataDbCfg().Items[utils.MetaIPProfiles]; itm.Replicate {
|
||||
replicate(ctx, dm.connMgr, dm.cfg.DataDbCfg().RplConns,
|
||||
dm.cfg.DataDbCfg().RplFiltered,
|
||||
utils.IPProfilesPrefix, utils.ConcatenatedKey(tenant, id), // this are used to get the host IDs from cache
|
||||
utils.ReplicatorSv1RemoveIPProfile,
|
||||
&utils.TenantIDWithAPIOpts{
|
||||
TenantID: &utils.TenantID{Tenant: tenant, ID: id},
|
||||
APIOpts: utils.GenerateDBItemOpts(itm.APIKey, itm.RouteID,
|
||||
dm.cfg.DataDbCfg().RplCache, utils.EmptyString)})
|
||||
}
|
||||
return dm.RemoveIP(ctx, tenant, id)
|
||||
}
|
||||
|
||||
func (dm *DataManager) HasData(category, subject, tenant string) (has bool, err error) {
|
||||
if dm == nil {
|
||||
err = utils.ErrNoDatabaseConn
|
||||
|
||||
@@ -234,6 +234,9 @@ func NewDispatcherService(val any) (_ IntService, err error) {
|
||||
case strings.HasPrefix(m, utils.ResourceS):
|
||||
m = strings.TrimPrefix(m, utils.ResourceS)
|
||||
key = utils.ResourceS
|
||||
case strings.HasPrefix(m, utils.IPs):
|
||||
m = strings.TrimPrefix(m, utils.IPs)
|
||||
key = utils.IPs
|
||||
case strings.HasPrefix(m, utils.RouteS):
|
||||
m = strings.TrimPrefix(m, utils.RouteS)
|
||||
key = utils.RouteS
|
||||
|
||||
@@ -273,10 +273,14 @@ func GetDefaultEmptyCacheStats() map[string]*ltcache.CacheStats {
|
||||
utils.CacheChargerFilterIndexes: {},
|
||||
utils.CacheChargerProfiles: {},
|
||||
utils.CacheEventResources: {},
|
||||
utils.CacheEventIPs: {},
|
||||
utils.CacheFilters: {},
|
||||
utils.CacheResourceFilterIndexes: {},
|
||||
utils.CacheResourceProfiles: {},
|
||||
utils.CacheResources: {},
|
||||
utils.CacheIPFilterIndexes: {},
|
||||
utils.CacheIPProfiles: {},
|
||||
utils.CacheIPs: {},
|
||||
utils.CacheRPCResponses: {},
|
||||
utils.CacheStatFilterIndexes: {},
|
||||
utils.CacheStatQueueProfiles: {},
|
||||
@@ -585,6 +589,7 @@ var serviceReceivers = map[string]string{
|
||||
utils.ERs: utils.ErSv1,
|
||||
utils.RateS: utils.RateSv1,
|
||||
utils.ResourceS: utils.ResourceSv1,
|
||||
utils.IPs: utils.IPsV1,
|
||||
utils.RouteS: utils.RouteSv1,
|
||||
utils.SessionS: utils.SessionSv1,
|
||||
utils.StatS: utils.StatSv1,
|
||||
|
||||
@@ -46,6 +46,12 @@ type DataDB interface {
|
||||
GetResourceDrv(*context.Context, string, string) (*utils.Resource, error)
|
||||
SetResourceDrv(*context.Context, *utils.Resource) error
|
||||
RemoveResourceDrv(*context.Context, string, string) error
|
||||
GetIPProfileDrv(*context.Context, string, string) (*utils.IPProfile, error)
|
||||
SetIPProfileDrv(*context.Context, *utils.IPProfile) error
|
||||
RemoveIPProfileDrv(*context.Context, string, string) error
|
||||
GetIPDrv(*context.Context, string, string) (*utils.IP, error)
|
||||
SetIPDrv(*context.Context, *utils.IP) error
|
||||
RemoveIPDrv(*context.Context, string, string) error
|
||||
GetLoadHistory(int, bool, string) ([]*utils.LoadInstance, error)
|
||||
AddLoadHistory(*utils.LoadInstance, int, string) error
|
||||
GetIndexesDrv(ctx *context.Context, idxItmType, tntCtx, idxKey, transactionID string) (indexes map[string]utils.StringSet, err error)
|
||||
|
||||
@@ -241,6 +241,44 @@ func (iDB *InternalDB) RemoveResourceDrv(_ *context.Context, tenant, id string)
|
||||
return
|
||||
}
|
||||
|
||||
func (iDB *InternalDB) GetIPProfileDrv(_ *context.Context, tenant, id string) (*utils.IPProfile, error) {
|
||||
if x, ok := iDB.db.Get(utils.CacheIPProfiles, utils.ConcatenatedKey(tenant, id)); ok && x != nil {
|
||||
return x.(*utils.IPProfile), nil
|
||||
}
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
|
||||
func (iDB *InternalDB) SetIPProfileDrv(_ *context.Context, ipp *utils.IPProfile) error {
|
||||
iDB.db.Set(utils.CacheIPProfiles, ipp.TenantID(), ipp, nil,
|
||||
true, utils.NonTransactional)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (iDB *InternalDB) RemoveIPProfileDrv(_ *context.Context, tenant, id string) error {
|
||||
iDB.db.Remove(utils.CacheIPProfiles, utils.ConcatenatedKey(tenant, id),
|
||||
true, utils.NonTransactional)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (iDB *InternalDB) GetIPDrv(_ *context.Context, tenant, id string) (*utils.IP, error) {
|
||||
if x, ok := iDB.db.Get(utils.CacheIPs, utils.ConcatenatedKey(tenant, id)); ok && x != nil {
|
||||
return x.(*utils.IP), nil
|
||||
}
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
|
||||
func (iDB *InternalDB) SetIPDrv(_ *context.Context, ip *utils.IP) error {
|
||||
iDB.db.Set(utils.CacheIPs, ip.TenantID(), ip, nil,
|
||||
true, utils.NonTransactional)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (iDB *InternalDB) RemoveIPDrv(_ *context.Context, tenant, id string) error {
|
||||
iDB.db.Remove(utils.CacheIPs, utils.ConcatenatedKey(tenant, id),
|
||||
true, utils.NonTransactional)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (iDB *InternalDB) GetLoadHistory(int, bool, string) ([]*utils.LoadInstance, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -57,8 +57,10 @@ const (
|
||||
ColLht = "load_history"
|
||||
ColVer = "versions"
|
||||
ColRsP = "resource_profiles"
|
||||
ColIPp = "ip_profiles"
|
||||
ColIndx = "indexes"
|
||||
ColRes = "resources"
|
||||
ColIPs = "ips"
|
||||
ColSqs = "statqueues"
|
||||
ColSqp = "statqueue_profiles"
|
||||
ColTps = "threshold_profiles"
|
||||
@@ -299,7 +301,7 @@ func (ms *MongoStorage) ensureIndexesForCol(col string) error { // exported for
|
||||
switch col {
|
||||
case ColAct, ColApl, ColAAp, ColAtr, ColRpl, ColDst, ColRds, ColLht, ColIndx:
|
||||
err = ms.ensureIndex(col, true, "key")
|
||||
case ColRsP, ColRes, ColSqs, ColRgp, ColTrs, ColTrd, ColSqp, ColTps, ColThs, ColRts, ColAttr, ColFlt, ColCpp, ColRpp, ColApp, ColAnp:
|
||||
case ColRsP, ColRes, ColIPp, ColIPs, ColSqs, ColRgp, ColTrs, ColTrd, ColSqp, ColTps, ColThs, ColRts, ColAttr, ColFlt, ColCpp, ColRpp, ColApp, ColAnp:
|
||||
err = ms.ensureIndex(col, true, "tenant", "id")
|
||||
case ColRpf, ColShg, ColAcc:
|
||||
err = ms.ensureIndex(col, true, "id")
|
||||
@@ -323,8 +325,9 @@ func (ms *MongoStorage) EnsureIndexes(cols ...string) error {
|
||||
if ms.IsDataDB() {
|
||||
cols = []string{
|
||||
ColAct, ColApl, ColAAp, ColAtr, ColRpl, ColDst, ColRds, ColLht, ColIndx,
|
||||
ColRsP, ColRes, ColSqs, ColSqp, ColTps, ColThs, ColRts, ColAttr, ColFlt,
|
||||
ColCpp, ColRpp, ColApp, ColRpf, ColShg, ColAcc, ColAnp, ColTrd, ColTrs,
|
||||
ColRsP, ColRes, ColIPp, ColIPs, ColSqs, ColSqp, ColTps, ColThs, ColRts,
|
||||
ColAttr, ColFlt, ColCpp, ColRpp, ColApp, ColRpf, ColShg, ColAcc, ColAnp,
|
||||
ColTrd, ColTrs,
|
||||
}
|
||||
} else {
|
||||
cols = []string{utils.CDRsTBL}
|
||||
@@ -460,6 +463,10 @@ func (ms *MongoStorage) GetKeysForPrefix(ctx *context.Context, prefix string) (k
|
||||
keys, qryErr = ms.getAllKeysMatchingTenantID(sctx, ColRsP, utils.ResourceProfilesPrefix, tntID)
|
||||
case utils.ResourcesPrefix:
|
||||
keys, qryErr = ms.getAllKeysMatchingTenantID(sctx, ColRes, utils.ResourcesPrefix, tntID)
|
||||
case utils.IPProfilesPrefix:
|
||||
keys, qryErr = ms.getAllKeysMatchingTenantID(sctx, ColIPp, utils.IPProfilesPrefix, tntID)
|
||||
case utils.IPsPrefix:
|
||||
keys, qryErr = ms.getAllKeysMatchingTenantID(sctx, ColIPs, utils.IPsPrefix, tntID)
|
||||
case utils.StatQueuePrefix:
|
||||
keys, qryErr = ms.getAllKeysMatchingTenantID(sctx, ColSqs, utils.StatQueuePrefix, tntID)
|
||||
case utils.StatQueueProfilePrefix:
|
||||
@@ -494,6 +501,8 @@ func (ms *MongoStorage) GetKeysForPrefix(ctx *context.Context, prefix string) (k
|
||||
keys, qryErr = ms.getAllIndexKeys(sctx, utils.AttributeFilterIndexes)
|
||||
case utils.ResourceFilterIndexes:
|
||||
keys, qryErr = ms.getAllIndexKeys(sctx, utils.ResourceFilterIndexes)
|
||||
case utils.IPFilterIndexes:
|
||||
keys, qryErr = ms.getAllIndexKeys(sctx, utils.IPFilterIndexes)
|
||||
case utils.StatFilterIndexes:
|
||||
keys, qryErr = ms.getAllIndexKeys(sctx, utils.StatFilterIndexes)
|
||||
case utils.ThresholdFilterIndexes:
|
||||
@@ -530,6 +539,10 @@ func (ms *MongoStorage) HasDataDrv(ctx *context.Context, category, subject, tena
|
||||
count, err = ms.getCol(ColRes).CountDocuments(sctx, bson.M{"tenant": tenant, "id": subject})
|
||||
case utils.ResourceProfilesPrefix:
|
||||
count, err = ms.getCol(ColRsP).CountDocuments(sctx, bson.M{"tenant": tenant, "id": subject})
|
||||
case utils.IPsPrefix:
|
||||
count, err = ms.getCol(ColIPs).CountDocuments(sctx, bson.M{"tenant": tenant, "id": subject})
|
||||
case utils.IPProfilesPrefix:
|
||||
count, err = ms.getCol(ColIPp).CountDocuments(sctx, bson.M{"tenant": tenant, "id": subject})
|
||||
case utils.StatQueuePrefix:
|
||||
count, err = ms.getCol(ColSqs).CountDocuments(sctx, bson.M{"tenant": tenant, "id": subject})
|
||||
case utils.StatQueueProfilePrefix:
|
||||
@@ -739,6 +752,72 @@ func (ms *MongoStorage) RemoveResourceDrv(ctx *context.Context, tenant, id strin
|
||||
})
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetIPProfileDrv(ctx *context.Context, tenant, id string) (*utils.IPProfile, error) {
|
||||
ipp := new(utils.IPProfile)
|
||||
err := ms.query(ctx, func(sctx mongo.SessionContext) error {
|
||||
sr := ms.getCol(ColIPp).FindOne(sctx, bson.M{"tenant": tenant, "id": id})
|
||||
decodeErr := sr.Decode(ipp)
|
||||
if errors.Is(decodeErr, mongo.ErrNoDocuments) {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
return decodeErr
|
||||
})
|
||||
return ipp, err
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) SetIPProfileDrv(ctx *context.Context, ipp *utils.IPProfile) error {
|
||||
return ms.query(ctx, func(sctx mongo.SessionContext) error {
|
||||
_, err := ms.getCol(ColIPp).UpdateOne(sctx, bson.M{"tenant": ipp.Tenant, "id": ipp.ID},
|
||||
bson.M{"$set": ipp},
|
||||
options.Update().SetUpsert(true),
|
||||
)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) RemoveIPProfileDrv(ctx *context.Context, tenant, id string) error {
|
||||
return ms.query(ctx, func(sctx mongo.SessionContext) error {
|
||||
dr, err := ms.getCol(ColIPp).DeleteOne(sctx, bson.M{"tenant": tenant, "id": id})
|
||||
if dr.DeletedCount == 0 {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetIPDrv(ctx *context.Context, tenant, id string) (*utils.IP, error) {
|
||||
ip := new(utils.IP)
|
||||
err := ms.query(ctx, func(sctx mongo.SessionContext) error {
|
||||
sr := ms.getCol(ColIPs).FindOne(sctx, bson.M{"tenant": tenant, "id": id})
|
||||
decodeErr := sr.Decode(ip)
|
||||
if errors.Is(decodeErr, mongo.ErrNoDocuments) {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
return decodeErr
|
||||
})
|
||||
return ip, err
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) SetIPDrv(ctx *context.Context, ip *utils.IP) error {
|
||||
return ms.query(ctx, func(sctx mongo.SessionContext) error {
|
||||
_, err := ms.getCol(ColIPs).UpdateOne(sctx, bson.M{"tenant": ip.Tenant, "id": ip.ID},
|
||||
bson.M{"$set": ip},
|
||||
options.Update().SetUpsert(true),
|
||||
)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) RemoveIPDrv(ctx *context.Context, tenant, id string) error {
|
||||
return ms.query(ctx, func(sctx mongo.SessionContext) error {
|
||||
dr, err := ms.getCol(ColIPs).DeleteOne(sctx, bson.M{"tenant": tenant, "id": id})
|
||||
if dr.DeletedCount == 0 {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetRankingProfileDrv(ctx *context.Context, tenant, id string) (*utils.RankingProfile, error) {
|
||||
rgProfile := new(utils.RankingProfile)
|
||||
err := ms.query(ctx, func(sctx mongo.SessionContext) error {
|
||||
|
||||
@@ -420,6 +420,60 @@ func (rs *RedisStorage) RemoveResourceDrv(ctx *context.Context, tenant, id strin
|
||||
return rs.Cmd(nil, redisDEL, utils.ResourcesPrefix+utils.ConcatenatedKey(tenant, id))
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) GetIPProfileDrv(ctx *context.Context, tenant, id string) (*utils.IPProfile, error) {
|
||||
var values []byte
|
||||
if err := rs.Cmd(&values, redisGET, utils.IPProfilesPrefix+utils.ConcatenatedKey(tenant, id)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(values) == 0 {
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
var ipp *utils.IPProfile
|
||||
if err := rs.ms.Unmarshal(values, &ipp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ipp, nil
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) SetIPProfileDrv(ctx *context.Context, ipp *utils.IPProfile) error {
|
||||
result, err := rs.ms.Marshal(ipp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return rs.Cmd(nil, redisSET, utils.IPProfilesPrefix+ipp.TenantID(), string(result))
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) RemoveIPProfileDrv(ctx *context.Context, tenant, id string) error {
|
||||
return rs.Cmd(nil, redisDEL, utils.IPProfilesPrefix+utils.ConcatenatedKey(tenant, id))
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) GetIPDrv(ctx *context.Context, tenant, id string) (*utils.IP, error) {
|
||||
var values []byte
|
||||
if err := rs.Cmd(&values, redisGET, utils.IPsPrefix+utils.ConcatenatedKey(tenant, id)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(values) == 0 {
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
var ip *utils.IP
|
||||
if err := rs.ms.Unmarshal(values, &ip); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) SetIPDrv(ctx *context.Context, ip *utils.IP) error {
|
||||
result, err := rs.ms.Marshal(ip)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return rs.Cmd(nil, redisSET, utils.IPsPrefix+ip.TenantID(), string(result))
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) RemoveIPDrv(ctx *context.Context, tenant, id string) error {
|
||||
return rs.Cmd(nil, redisDEL, utils.IPsPrefix+utils.ConcatenatedKey(tenant, id))
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) GetVersions(itm string) (vrs Versions, err error) {
|
||||
if itm != "" {
|
||||
var fldVal int64
|
||||
|
||||
Reference in New Issue
Block a user