mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-15 05:09:54 +05:00
Add Get/Set/Remove ResourceProfile in Datamanager
This commit is contained in:
committed by
Dan Christian Bogos
parent
3c896d6181
commit
935ff50f49
@@ -63,7 +63,7 @@ func (apierV1 *ApierV1) GetResourceProfile(arg utils.TenantID, reply *engine.Res
|
||||
if missing := utils.MissingStructFields(&arg, []string{"Tenant", "ID"}); len(missing) != 0 { //Params missing
|
||||
return utils.NewErrMandatoryIeMissing(missing...)
|
||||
}
|
||||
if rcfg, err := apierV1.DataManager.DataDB().GetResourceProfile(arg.Tenant, arg.ID, false, utils.NonTransactional); err != nil {
|
||||
if rcfg, err := apierV1.DataManager.GetResourceProfile(arg.Tenant, arg.ID, false, utils.NonTransactional); err != nil {
|
||||
if err.Error() != utils.ErrNotFound.Error() {
|
||||
err = utils.NewErrServerError(err)
|
||||
}
|
||||
@@ -79,7 +79,7 @@ func (apierV1 *ApierV1) SetResourceProfile(res *engine.ResourceProfile, reply *s
|
||||
if missing := utils.MissingStructFields(res, []string{"Tenant", "ID"}); len(missing) != 0 {
|
||||
return utils.NewErrMandatoryIeMissing(missing...)
|
||||
}
|
||||
if err := apierV1.DataManager.DataDB().SetResourceProfile(res); err != nil {
|
||||
if err := apierV1.DataManager.SetResourceProfile(res); err != nil {
|
||||
return utils.APIErrorHandler(err)
|
||||
}
|
||||
cache.RemKey(utils.ResourceProfilesPrefix+utils.ConcatenatedKey(res.Tenant, res.ID), true, "") // ToDo: Remove here with autoreload
|
||||
@@ -92,7 +92,7 @@ func (apierV1 *ApierV1) RemResourceProfile(arg utils.TenantID, reply *string) er
|
||||
if missing := utils.MissingStructFields(&arg, []string{"Tenant", "ID"}); len(missing) != 0 { //Params missing
|
||||
return utils.NewErrMandatoryIeMissing(missing...)
|
||||
}
|
||||
if err := apierV1.DataManager.DataDB().RemoveResourceProfile(arg.Tenant, arg.ID, utils.NonTransactional); err != nil {
|
||||
if err := apierV1.DataManager.RemoveResourceProfile(arg.Tenant, arg.ID, utils.NonTransactional); err != nil {
|
||||
if err.Error() != utils.ErrNotFound.Error() {
|
||||
err = utils.NewErrServerError(err)
|
||||
}
|
||||
|
||||
@@ -188,7 +188,7 @@ func (dm *DataManager) CacheDataFromDB(prfx string, ids []string, mustBeCached b
|
||||
_, err = dm.DataDB().GetReverseAlias(dataID, true, utils.NonTransactional)
|
||||
case utils.ResourceProfilesPrefix:
|
||||
tntID := utils.NewTenantID(dataID)
|
||||
_, err = dm.DataDB().GetResourceProfile(tntID.Tenant, tntID.ID, true, utils.NonTransactional)
|
||||
_, err = dm.GetResourceProfile(tntID.Tenant, tntID.ID, true, utils.NonTransactional)
|
||||
case utils.ResourcesPrefix:
|
||||
tntID := utils.NewTenantID(dataID)
|
||||
_, err = dm.GetResource(tntID.Tenant, tntID.ID, true, utils.NonTransactional)
|
||||
@@ -460,3 +460,37 @@ func (dm *DataManager) RemoveResource(tenant, id, transactionID string) (err err
|
||||
cacheCommit(transactionID), transactionID)
|
||||
return
|
||||
}
|
||||
|
||||
func (dm *DataManager) GetResourceProfile(tenant, id string, skipCache bool, transactionID string) (rp *ResourceProfile, err error) {
|
||||
key := utils.ResourceProfilesPrefix + utils.ConcatenatedKey(tenant, id)
|
||||
if !skipCache {
|
||||
if x, ok := cache.Get(key); ok {
|
||||
if x == nil {
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
return x.(*ResourceProfile), nil
|
||||
}
|
||||
}
|
||||
rp, err = dm.dataDB.GetResourceProfileDrv(tenant, id)
|
||||
if err != nil {
|
||||
if err == utils.ErrNotFound {
|
||||
cache.Set(key, nil, cacheCommit(transactionID), transactionID)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
cache.Set(key, rp, cacheCommit(transactionID), transactionID)
|
||||
return
|
||||
}
|
||||
|
||||
func (dm *DataManager) SetResourceProfile(rp *ResourceProfile) (err error) {
|
||||
return dm.DataDB().SetResourceProfileDrv(rp)
|
||||
}
|
||||
|
||||
func (dm *DataManager) RemoveResourceProfile(tenant, id, transactionID string) (err error) {
|
||||
if err = dm.DataDB().RemoveResourceProfileDrv(tenant, id); err != nil {
|
||||
return
|
||||
}
|
||||
cache.RemKey(utils.ResourceProfilesPrefix+utils.ConcatenatedKey(tenant, id),
|
||||
cacheCommit(transactionID), transactionID)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -315,7 +315,7 @@ func TestLoaderITWriteToDatabase(t *testing.T) {
|
||||
|
||||
for _, mapIDs := range loader.resProfiles {
|
||||
for _, rl := range mapIDs {
|
||||
rcv, err := loader.dataStorage.GetResourceProfile(rl.Tenant, rl.ID, true, utils.NonTransactional)
|
||||
rcv, err := loader.dm.GetResourceProfile(rl.Tenant, rl.ID, true, utils.NonTransactional)
|
||||
if err != nil {
|
||||
t.Error("Failed GetResourceProfile: ", err.Error())
|
||||
}
|
||||
|
||||
@@ -809,7 +809,7 @@ func testOnStorITCacheResourceProfile(t *testing.T) {
|
||||
Thresholds: []string{"TEST_ACTIONS"},
|
||||
UsageTTL: time.Duration(1 * time.Millisecond),
|
||||
}
|
||||
if err := onStor.DataDB().SetResourceProfile(rCfg); err != nil {
|
||||
if err := onStor.SetResourceProfile(rCfg); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
expectedR := []string{"rsp_cgrates.org:RL_TEST"}
|
||||
@@ -1812,13 +1812,13 @@ func testOnStorITCRUDResourceProfile(t *testing.T) {
|
||||
Thresholds: []string{"TEST_ACTIONS"},
|
||||
UsageTTL: time.Duration(1 * time.Millisecond),
|
||||
}
|
||||
if _, rcvErr := onStor.DataDB().GetResourceProfile(rL.Tenant, rL.ID, true, utils.NonTransactional); rcvErr != utils.ErrNotFound {
|
||||
if _, rcvErr := onStor.GetResourceProfile(rL.Tenant, rL.ID, true, utils.NonTransactional); rcvErr != utils.ErrNotFound {
|
||||
t.Error(rcvErr)
|
||||
}
|
||||
if err := onStor.DataDB().SetResourceProfile(rL); err != nil {
|
||||
if err := onStor.SetResourceProfile(rL); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if rcv, err := onStor.DataDB().GetResourceProfile(rL.Tenant, rL.ID, true, utils.NonTransactional); err != nil {
|
||||
if rcv, err := onStor.GetResourceProfile(rL.Tenant, rL.ID, true, utils.NonTransactional); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(rL, rcv) {
|
||||
t.Errorf("Expecting: %v, received: %v", rL, rcv)
|
||||
@@ -1831,7 +1831,7 @@ func testOnStorITCRUDResourceProfile(t *testing.T) {
|
||||
// t.Error(rcvErr)
|
||||
// }
|
||||
//
|
||||
if rcv, err := onStor.DataDB().GetResourceProfile(rL.Tenant, rL.ID, false, utils.NonTransactional); err != nil {
|
||||
if rcv, err := onStor.GetResourceProfile(rL.Tenant, rL.ID, false, utils.NonTransactional); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(rL, rcv) {
|
||||
t.Errorf("Expecting: %v, received: %v", rL, rcv)
|
||||
@@ -1839,10 +1839,10 @@ func testOnStorITCRUDResourceProfile(t *testing.T) {
|
||||
// if err = onStor.DataDB().SelectDatabase(onStorCfg); err != nil {
|
||||
// t.Error(err)
|
||||
// }
|
||||
if err := onStor.DataDB().RemoveResourceProfile(rL.Tenant, rL.ID, utils.NonTransactional); err != nil {
|
||||
if err := onStor.RemoveResourceProfile(rL.Tenant, rL.ID, utils.NonTransactional); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if _, rcvErr := onStor.DataDB().GetResourceProfile(rL.Tenant, rL.ID, true, utils.NonTransactional); rcvErr != utils.ErrNotFound {
|
||||
if _, rcvErr := onStor.GetResourceProfile(rL.Tenant, rL.ID, true, utils.NonTransactional); rcvErr != utils.ErrNotFound {
|
||||
t.Error(rcvErr)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -435,7 +435,7 @@ func (rS *ResourceService) matchingResourcesForEvent(tenant string, ev map[strin
|
||||
guardian.Guardian.GuardIDs(config.CgrConfig().LockingTimeout, lockIDs...)
|
||||
defer guardian.Guardian.UnguardIDs(lockIDs...)
|
||||
for resName := range rIDs {
|
||||
rPrf, err := rS.dm.DataDB().GetResourceProfile(tenant, resName, false, utils.NonTransactional)
|
||||
rPrf, err := rS.dm.GetResourceProfile(tenant, resName, false, utils.NonTransactional)
|
||||
if err != nil {
|
||||
if err == utils.ErrNotFound {
|
||||
continue
|
||||
|
||||
@@ -99,9 +99,9 @@ type DataDB interface {
|
||||
RemoveAlias(string, string) error
|
||||
SetReverseAlias(*Alias, string) error
|
||||
GetReverseAlias(string, bool, string) ([]string, error)
|
||||
GetResourceProfile(string, string, bool, string) (*ResourceProfile, error)
|
||||
SetResourceProfile(*ResourceProfile) error
|
||||
RemoveResourceProfile(string, string, string) error
|
||||
GetResourceProfileDrv(string, string) (*ResourceProfile, error)
|
||||
SetResourceProfileDrv(*ResourceProfile) error
|
||||
RemoveResourceProfileDrv(string, string) error
|
||||
GetResourceDrv(string, string) (*Resource, error)
|
||||
SetResourceDrv(*Resource) error
|
||||
RemoveResourceDrv(string, string) error
|
||||
|
||||
@@ -787,7 +787,7 @@ func (ms *MapStorage) SetReverseAlias(al *Alias, transactionID string) (err erro
|
||||
|
||||
func (ms *MapStorage) RemoveAlias(key string, transactionID string) error {
|
||||
// get alias for values list
|
||||
al, err := ms.GetAlias(key, false, transactionID)
|
||||
al, err := ms.GetAlias(key, false, utils.NonTransactional)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1139,21 +1139,12 @@ func (ms *MapStorage) GetSMCost(cgrid, source, runid, originHost, originID strin
|
||||
return
|
||||
}
|
||||
|
||||
func (ms *MapStorage) GetResourceProfile(tenant, id string, skipCache bool, transactionID string) (rsp *ResourceProfile, err error) {
|
||||
func (ms *MapStorage) GetResourceProfileDrv(tenant, id string) (rsp *ResourceProfile, err error) {
|
||||
ms.mu.RLock()
|
||||
defer ms.mu.RUnlock()
|
||||
key := utils.ResourceProfilesPrefix + utils.ConcatenatedKey(tenant, id)
|
||||
if !skipCache {
|
||||
if x, ok := cache.Get(key); ok {
|
||||
if x != nil {
|
||||
return x.(*ResourceProfile), nil
|
||||
}
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
}
|
||||
values, ok := ms.dict[key]
|
||||
if !ok {
|
||||
cache.Set(key, nil, cacheCommit(transactionID), transactionID)
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
err = ms.ms.Unmarshal(values, &rsp)
|
||||
@@ -1165,11 +1156,10 @@ func (ms *MapStorage) GetResourceProfile(tenant, id string, skipCache bool, tran
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
cache.Set(key, rsp, cacheCommit(transactionID), transactionID)
|
||||
return
|
||||
}
|
||||
|
||||
func (ms *MapStorage) SetResourceProfile(r *ResourceProfile) error {
|
||||
func (ms *MapStorage) SetResourceProfileDrv(r *ResourceProfile) error {
|
||||
ms.mu.Lock()
|
||||
defer ms.mu.Unlock()
|
||||
result, err := ms.ms.Marshal(r)
|
||||
@@ -1180,12 +1170,11 @@ func (ms *MapStorage) SetResourceProfile(r *ResourceProfile) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ms *MapStorage) RemoveResourceProfile(tenant, id string, transactionID string) error {
|
||||
func (ms *MapStorage) RemoveResourceProfileDrv(tenant, id string) error {
|
||||
ms.mu.Lock()
|
||||
defer ms.mu.Unlock()
|
||||
key := utils.ResourceProfilesPrefix + utils.ConcatenatedKey(tenant, id)
|
||||
delete(ms.dict, key)
|
||||
cache.RemKey(key, cacheCommit(transactionID), transactionID)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1753,23 +1753,13 @@ func (ms *MongoStorage) GetAllCdrStats() (css []*CdrStats, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetResourceProfile(tenant, id string, skipCache bool, transactionID string) (rp *ResourceProfile, err error) {
|
||||
key := utils.ResourceProfilesPrefix + utils.ConcatenatedKey(tenant, id)
|
||||
if !skipCache {
|
||||
if x, ok := cache.Get(key); ok {
|
||||
if x == nil {
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
return x.(*ResourceProfile), nil
|
||||
}
|
||||
}
|
||||
func (ms *MongoStorage) GetResourceProfileDrv(tenant, id string) (rp *ResourceProfile, err error) {
|
||||
session, col := ms.conn(colRsP)
|
||||
defer session.Close()
|
||||
rp = new(ResourceProfile)
|
||||
if err = col.Find(bson.M{"tenant": tenant, "id": id}).One(rp); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
err = utils.ErrNotFound
|
||||
cache.Set(key, nil, cacheCommit(transactionID), transactionID)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -1778,25 +1768,22 @@ func (ms *MongoStorage) GetResourceProfile(tenant, id string, skipCache bool, tr
|
||||
return
|
||||
}
|
||||
}
|
||||
cache.Set(key, rp, cacheCommit(transactionID), transactionID)
|
||||
return
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) SetResourceProfile(rp *ResourceProfile) (err error) {
|
||||
func (ms *MongoStorage) SetResourceProfileDrv(rp *ResourceProfile) (err error) {
|
||||
session, col := ms.conn(colRsP)
|
||||
defer session.Close()
|
||||
_, err = col.Upsert(bson.M{"tenant": rp.Tenant, "id": rp.ID}, rp)
|
||||
return
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) RemoveResourceProfile(tenant, id string, transactionID string) (err error) {
|
||||
func (ms *MongoStorage) RemoveResourceProfileDrv(tenant, id string) (err error) {
|
||||
session, col := ms.conn(colRsP)
|
||||
defer session.Close()
|
||||
if err = col.Remove(bson.M{"tenant": tenant, "id": id}); err != nil {
|
||||
return
|
||||
}
|
||||
cache.RemKey(utils.ResourceProfilesPrefix+utils.ConcatenatedKey(tenant, id),
|
||||
cacheCommit(transactionID), transactionID)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1237,20 +1237,11 @@ func (rs *RedisStorage) GetAllCdrStats() (css []*CdrStats, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) GetResourceProfile(tenant, id string, skipCache bool, transactionID string) (rsp *ResourceProfile, err error) {
|
||||
func (rs *RedisStorage) GetResourceProfileDrv(tenant, id string) (rsp *ResourceProfile, err error) {
|
||||
key := utils.ResourceProfilesPrefix + utils.ConcatenatedKey(tenant, id)
|
||||
if !skipCache {
|
||||
if x, ok := cache.Get(key); ok {
|
||||
if x == nil {
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
return x.(*ResourceProfile), nil
|
||||
}
|
||||
}
|
||||
var values []byte
|
||||
if values, err = rs.Cmd("GET", key).Bytes(); err != nil {
|
||||
if err == redis.ErrRespNil { // did not find the destination
|
||||
cache.Set(key, nil, cacheCommit(transactionID), transactionID)
|
||||
err = utils.ErrNotFound
|
||||
}
|
||||
return
|
||||
@@ -1263,11 +1254,10 @@ func (rs *RedisStorage) GetResourceProfile(tenant, id string, skipCache bool, tr
|
||||
return
|
||||
}
|
||||
}
|
||||
cache.Set(key, rsp, cacheCommit(transactionID), transactionID)
|
||||
return
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) SetResourceProfile(rsp *ResourceProfile) error {
|
||||
func (rs *RedisStorage) SetResourceProfileDrv(rsp *ResourceProfile) error {
|
||||
result, err := rs.ms.Marshal(rsp)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -1275,12 +1265,11 @@ func (rs *RedisStorage) SetResourceProfile(rsp *ResourceProfile) error {
|
||||
return rs.Cmd("SET", utils.ResourceProfilesPrefix+rsp.TenantID(), result).Err
|
||||
}
|
||||
|
||||
func (rs *RedisStorage) RemoveResourceProfile(tenant, id string, transactionID string) (err error) {
|
||||
func (rs *RedisStorage) RemoveResourceProfileDrv(tenant, id string) (err error) {
|
||||
key := utils.ResourceProfilesPrefix + utils.ConcatenatedKey(tenant, id)
|
||||
if err = rs.Cmd("DEL", key).Err; err != nil {
|
||||
return
|
||||
}
|
||||
cache.RemKey(key, cacheCommit(transactionID), transactionID)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -2021,7 +2021,7 @@ func (tpr *TpReader) WriteToDatabase(flush, verbose, disable_reverse bool) (err
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = tpr.dm.DataDB().SetResourceProfile(rsp); err != nil {
|
||||
if err = tpr.dm.SetResourceProfile(rsp); err != nil {
|
||||
return err
|
||||
}
|
||||
if verbose {
|
||||
|
||||
Reference in New Issue
Block a user