mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-16 13:49:53 +05:00
added TP apis for aliases, users and lcr rules
fixes #154, fixes #155, fixes #156
This commit is contained in:
@@ -358,23 +358,43 @@ func APItoModelCdrStat(stats *utils.TPCdrStats) (result []TpCdrstat) {
|
||||
return
|
||||
}
|
||||
|
||||
func APItoModelAliases(attr *utils.AttrSetTPAlias) (result *TpAlias) {
|
||||
return &TpAlias{
|
||||
Tpid: attr.TPid,
|
||||
Direction: attr.Direction,
|
||||
Tenant: attr.Tenant,
|
||||
Category: attr.Category,
|
||||
Account: attr.Account,
|
||||
Subject: attr.Subject,
|
||||
Group: attr.Group,
|
||||
func APItoModelAliases(attr *utils.TPAliases) (result []TpAlias) {
|
||||
for _, v := range attr.Values {
|
||||
result = append(result, TpAlias{
|
||||
Tpid: attr.TPid,
|
||||
Direction: attr.Direction,
|
||||
Tenant: attr.Tenant,
|
||||
Category: attr.Category,
|
||||
Account: attr.Account,
|
||||
Subject: attr.Subject,
|
||||
Group: attr.Group,
|
||||
DestinationId: v.DestinationId,
|
||||
Alias: v.Alias,
|
||||
Weight: v.Weight,
|
||||
})
|
||||
}
|
||||
if len(attr.Values) == 0 {
|
||||
result = append(result, TpAlias{
|
||||
Tpid: attr.TPid,
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func APItoModelUsers(attr *utils.AttrSetTPUser) (result *TpUser) {
|
||||
return &TpUser{
|
||||
Tpid: attr.TPid,
|
||||
Tenant: attr.Tenant,
|
||||
AttributeName: attr.AttributeName,
|
||||
AttributeValue: attr.AttributeValue,
|
||||
func APItoModelUsers(attr *utils.TPUsers) (result []TpUser) {
|
||||
for _, p := range attr.Profile {
|
||||
result = append(result, TpUser{
|
||||
Tpid: attr.TPid,
|
||||
Tenant: attr.Tenant,
|
||||
UserName: attr.UserName,
|
||||
AttributeName: p.AttrName,
|
||||
AttributeValue: p.AttrValue,
|
||||
})
|
||||
}
|
||||
if len(attr.Profile) == 0 {
|
||||
result = append(result, TpUser{
|
||||
Tpid: attr.TPid,
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -717,44 +717,46 @@ func ValueOrDefault(val string, deflt string) string {
|
||||
|
||||
type TpUsers []TpUser
|
||||
|
||||
func (tps TpUsers) GetUsers() (map[string]*UserProfile, error) {
|
||||
users := make(map[string]*UserProfile)
|
||||
func (tps TpUsers) GetUsers() (map[string]*utils.TPUsers, error) {
|
||||
users := make(map[string]*utils.TPUsers)
|
||||
for _, tp := range tps {
|
||||
var user *UserProfile
|
||||
var user *utils.TPUsers
|
||||
var found bool
|
||||
if user, found = users[tp.GetId()]; !found {
|
||||
user = &UserProfile{
|
||||
user = &utils.TPUsers{
|
||||
Tenant: tp.Tenant,
|
||||
UserName: tp.UserName,
|
||||
Profile: make(map[string]string),
|
||||
}
|
||||
users[tp.GetId()] = user
|
||||
}
|
||||
user.Profile[tp.AttributeName] = tp.AttributeValue
|
||||
user.Profile = append(user.Profile,
|
||||
&utils.TPUserProfile{
|
||||
AttrName: tp.AttributeName,
|
||||
AttrValue: tp.AttributeValue,
|
||||
})
|
||||
}
|
||||
return users, nil
|
||||
}
|
||||
|
||||
type TpAliases []TpAlias
|
||||
|
||||
func (tps TpAliases) GetAliases() (map[string]*Alias, error) {
|
||||
als := make(map[string]*Alias)
|
||||
func (tps TpAliases) GetAliases() (map[string]*utils.TPAliases, error) {
|
||||
als := make(map[string]*utils.TPAliases)
|
||||
for _, tp := range tps {
|
||||
var al *Alias
|
||||
var al *utils.TPAliases
|
||||
var found bool
|
||||
if al, found = als[tp.GetId()]; !found {
|
||||
al = &Alias{
|
||||
al = &utils.TPAliases{
|
||||
Direction: tp.Direction,
|
||||
Tenant: tp.Tenant,
|
||||
Category: tp.Category,
|
||||
Account: tp.Account,
|
||||
Subject: tp.Subject,
|
||||
Group: tp.Group,
|
||||
Values: make(AliasValues, 0),
|
||||
}
|
||||
als[tp.GetId()] = al
|
||||
}
|
||||
al.Values = append(al.Values, &AliasValue{
|
||||
al.Values = append(al.Values, &utils.TPAliasValue{
|
||||
DestinationId: tp.DestinationId,
|
||||
Alias: tp.Alias,
|
||||
Weight: tp.Weight,
|
||||
@@ -762,3 +764,33 @@ func (tps TpAliases) GetAliases() (map[string]*Alias, error) {
|
||||
}
|
||||
return als, nil
|
||||
}
|
||||
|
||||
type TpLcrRules []TpLcrRule
|
||||
|
||||
func (tps TpLcrRules) GetLcrRules() (map[string]*utils.TPLcrRules, error) {
|
||||
lcrs := make(map[string]*utils.TPLcrRules)
|
||||
for _, tp := range tps {
|
||||
var lcr *utils.TPLcrRules
|
||||
var found bool
|
||||
if lcr, found = lcrs[tp.GetLcrRuleId()]; !found {
|
||||
lcr = &utils.TPLcrRules{
|
||||
LcrRulesId: tp.GetLcrRuleId(),
|
||||
}
|
||||
lcrs[tp.GetLcrRuleId()] = lcr
|
||||
}
|
||||
lcr.LcrRules = append(lcr.LcrRules, &utils.TPLcrRule{
|
||||
Direction: tp.Direction,
|
||||
Tenant: tp.Tenant,
|
||||
Category: tp.Category,
|
||||
Account: tp.Account,
|
||||
Subject: tp.Subject,
|
||||
DestinationId: tp.DestinationTag,
|
||||
RpCategory: tp.RpCategory,
|
||||
Strategy: tp.Strategy,
|
||||
StrategyParams: tp.StrategyParams,
|
||||
ActivationTime: tp.ActivationTime,
|
||||
Weight: tp.Weight,
|
||||
})
|
||||
}
|
||||
return lcrs, nil
|
||||
}
|
||||
|
||||
@@ -1062,7 +1062,24 @@ func (tpr *TpReader) LoadUsers() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tpr.users, err = TpUsers(tps).GetUsers()
|
||||
userMap, err := TpUsers(tps).GetUsers()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for key, usr := range userMap {
|
||||
up, found := tpr.users[key]
|
||||
if !found {
|
||||
up = &UserProfile{
|
||||
Tenant: usr.Tenant,
|
||||
UserName: usr.UserName,
|
||||
Profile: make(map[string]string),
|
||||
}
|
||||
tpr.users[key] = up
|
||||
}
|
||||
for _, p := range usr.Profile {
|
||||
up.Profile[p.AttrName] = p.AttrValue
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1094,7 +1111,32 @@ func (tpr *TpReader) LoadAliases() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tpr.aliases, err = TpAliases(tps).GetAliases()
|
||||
alMap, err := TpAliases(tps).GetAliases()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for key, tal := range alMap {
|
||||
al, found := tpr.aliases[key]
|
||||
if !found {
|
||||
al = &Alias{
|
||||
Direction: tal.Direction,
|
||||
Tenant: tal.Tenant,
|
||||
Category: tal.Category,
|
||||
Account: tal.Account,
|
||||
Subject: tal.Subject,
|
||||
Group: tal.Group,
|
||||
Values: make(AliasValues, 0),
|
||||
}
|
||||
tpr.aliases[key] = al
|
||||
}
|
||||
for _, v := range tal.Values {
|
||||
al.Values = append(al.Values, &AliasValue{
|
||||
DestinationId: v.DestinationId,
|
||||
Alias: v.Alias,
|
||||
Weight: v.Weight,
|
||||
})
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user