Make Action Profiles storable in MySQL and Postgres

This commit is contained in:
arberkatellari
2025-11-04 10:36:03 +02:00
committed by Dan Christian Bogos
parent 89653a9c80
commit d78f34bdc5
18 changed files with 346 additions and 53 deletions

View File

@@ -415,3 +415,14 @@ type IPAllocationMdl struct {
func (IPAllocationMdl) TableName() string {
return utils.TBLIPAllocations
}
type ActionProfileJSONMdl struct {
PK uint `gorm:"primary_key"`
Tenant string `index:"0" re:".*"`
ID string `index:"1" re:".*"`
ActionProfile utils.JSONB `gorm:"type:jsonb" index:"2" re:".*"`
}
func (ActionProfileJSONMdl) TableName() string {
return utils.TBLActionProfilesJSON
}

View File

@@ -104,6 +104,8 @@ func (sqls *SQLStorage) GetKeysForPrefix(ctx *context.Context, prefix string) (k
keys, err = sqls.getAllKeysMatchingTenantID(ctx, utils.TBLIPProfiles, tntID)
case utils.IPAllocationsPrefix:
keys, err = sqls.getAllKeysMatchingTenantID(ctx, utils.TBLIPAllocations, tntID)
case utils.ActionProfilePrefix:
keys, err = sqls.getAllKeysMatchingTenantID(ctx, utils.TBLActionProfilesJSON, tntID)
default:
err = fmt.Errorf("unsupported prefix in GetKeysForPrefix: %q", prefix)
}
@@ -488,7 +490,7 @@ func (sqls *SQLStorage) GetIPAllocationsDrv(ctx *context.Context, tenant, id str
if len(result) == 0 {
return nil, utils.ErrNotFound
}
return utils.MapStringInterfaceToIPAllocations(result[0].IPAllocation)
return utils.MapStringInterfaceToIPAllocations(result[0].IPAllocation), nil
}
func (sqls *SQLStorage) SetIPAllocationsDrv(ctx *context.Context, ip *utils.IPAllocations) error {
@@ -523,6 +525,51 @@ func (sqls *SQLStorage) RemoveIPAllocationsDrv(ctx *context.Context, tenant, id
return nil
}
func (sqls *SQLStorage) GetActionProfileDrv(ctx *context.Context, tenant, id string) (ap *utils.ActionProfile, err error) {
var result []*ActionProfileJSONMdl
if err := sqls.db.Model(&ActionProfileJSONMdl{}).Where(&ActionProfileJSONMdl{Tenant: tenant,
ID: id}).Find(&result).Error; err != nil {
return nil, err
}
if len(result) == 0 {
return nil, utils.ErrNotFound
}
ap, err = utils.MapStringInterfaceToActionProfile(result[0].ActionProfile)
return
}
func (sqls *SQLStorage) SetActionProfileDrv(ctx *context.Context, ap *utils.ActionProfile) (err error) {
tx := sqls.db.Begin()
mdl := &ActionProfileJSONMdl{
Tenant: ap.Tenant,
ID: ap.ID,
ActionProfile: ap.AsMapStringInterface(),
}
if err := tx.Model(&ActionProfileJSONMdl{}).Where(
ActionProfileJSONMdl{Tenant: mdl.Tenant, ID: mdl.ID}).Delete(
ActionProfileJSONMdl{}).Error; err != nil {
tx.Rollback()
return err
}
if err := tx.Save(mdl).Error; err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
func (sqls *SQLStorage) RemoveActionProfileDrv(ctx *context.Context, tenant, id string) (err error) {
tx := sqls.db.Begin()
if err := tx.Model(&ActionProfileJSONMdl{}).Where(&ActionProfileJSONMdl{Tenant: tenant, ID: id}).
Delete(&ActionProfileJSONMdl{}).Error; err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
// AddLoadHistory DataDB method not implemented yet
func (sqls *SQLStorage) AddLoadHistory(ldInst *utils.LoadInstance,
loadHistSize int, transactionID string) error {
@@ -815,20 +862,6 @@ func (sqls *SQLStorage) RemoveRateProfileDrv(ctx *context.Context, tenant, id st
return utils.ErrNotImplemented
}
// DataDB method not implemented yet
func (sqls *SQLStorage) GetActionProfileDrv(ctx *context.Context, tenant, id string) (ap *utils.ActionProfile, err error) {
return nil, utils.ErrNotImplemented
}
// DataDB method not implemented yet
func (sqls *SQLStorage) SetActionProfileDrv(ctx *context.Context, ap *utils.ActionProfile) (err error) {
return utils.ErrNotImplemented
}
func (sqls *SQLStorage) RemoveActionProfileDrv(ctx *context.Context, tenant, id string) (err error) {
return utils.ErrNotImplemented
}
// GetIndexesDrv DataDB method not implemented yet
func (sqls *SQLStorage) GetIndexesDrv(ctx *context.Context, idxItmType, tntCtx, idxKey, transactionID string) (indexes map[string]utils.StringSet, err error) {
return nil, utils.ErrNotImplemented