mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-19 22:28:45 +05:00
adde GetMatchingAlias to alias API
This commit is contained in:
@@ -97,11 +97,22 @@ func (al *Alias) SetId(id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type AttrMatchingAlias struct {
|
||||
Destination string
|
||||
Direction string
|
||||
Tenant string
|
||||
Category string
|
||||
Account string
|
||||
Subject string
|
||||
Group string
|
||||
}
|
||||
|
||||
type AliasService interface {
|
||||
SetAlias(Alias, *string) error
|
||||
UpdateAlias(Alias, *string) error
|
||||
RemoveAlias(Alias, *string) error
|
||||
GetAlias(Alias, *Alias) error
|
||||
GetMatchingAlias(AttrMatchingAlias, *string) error
|
||||
}
|
||||
|
||||
type AliasHandler struct {
|
||||
@@ -191,6 +202,43 @@ func (am *AliasHandler) GetAlias(al Alias, result *Alias) error {
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
|
||||
func (am *AliasHandler) GetMatchingAlias(attr AttrMatchingAlias, result *string) error {
|
||||
response := Alias{}
|
||||
if err := aliasService.GetAlias(Alias{
|
||||
Direction: attr.Direction,
|
||||
Tenant: attr.Tenant,
|
||||
Category: attr.Category,
|
||||
Account: attr.Account,
|
||||
Subject: attr.Subject,
|
||||
Group: attr.Group,
|
||||
}, &response); err != nil {
|
||||
return err
|
||||
}
|
||||
// sort according to weight
|
||||
values := response.Values.GetWeightSlice()
|
||||
// check destination ids
|
||||
|
||||
for _, p := range utils.SplitPrefix(attr.Destination, MIN_PREFIX_MATCH) {
|
||||
if x, err := cache2go.GetCached(utils.DESTINATION_PREFIX + p); err == nil {
|
||||
for _, aliasHandler := range values {
|
||||
for alias, aliasDestIds := range aliasHandler {
|
||||
destIds := x.(map[interface{}]struct{})
|
||||
for idId := range destIds {
|
||||
dId := idId.(string)
|
||||
for _, aliasDestId := range aliasDestIds {
|
||||
if aliasDestId == utils.ANY || aliasDestId == dId {
|
||||
*result = alias
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
|
||||
type ProxyAliasService struct {
|
||||
Client *rpcclient.RpcClient
|
||||
}
|
||||
@@ -219,45 +267,10 @@ func (ps *ProxyAliasService) GetAlias(al Alias, alias *Alias) error {
|
||||
return ps.Client.Call("AliasV1.GetAlias", al, alias)
|
||||
}
|
||||
|
||||
func (ps *ProxyAliasService) GetMatchingAlias(attr AttrMatchingAlias, alias *string) error {
|
||||
return ps.Client.Call("AliasV1.GetMatchingAlias", attr, alias)
|
||||
}
|
||||
|
||||
func (ps *ProxyAliasService) ReloadAliases(in string, reply *string) error {
|
||||
return ps.Client.Call("AliasV1.ReloadAliases", in, reply)
|
||||
}
|
||||
|
||||
func GetBestAlias(destination, direction, tenant, category, account, subject, group string) (string, error) {
|
||||
if aliasService == nil {
|
||||
return "", nil
|
||||
}
|
||||
response := Alias{}
|
||||
if err := aliasService.GetAlias(Alias{
|
||||
Direction: direction,
|
||||
Tenant: tenant,
|
||||
Category: category,
|
||||
Account: account,
|
||||
Subject: subject,
|
||||
Group: group,
|
||||
}, &response); err != nil {
|
||||
return "", err
|
||||
}
|
||||
// sort according to weight
|
||||
values := response.Values.GetWeightSlice()
|
||||
// check destination ids
|
||||
|
||||
for _, p := range utils.SplitPrefix(destination, MIN_PREFIX_MATCH) {
|
||||
if x, err := cache2go.GetCached(utils.DESTINATION_PREFIX + p); err == nil {
|
||||
for _, aliasHandler := range values {
|
||||
for alias, aliasDestIds := range aliasHandler {
|
||||
destIds := x.(map[interface{}]struct{})
|
||||
for idId := range destIds {
|
||||
dId := idId.(string)
|
||||
for _, aliasDestId := range aliasDestIds {
|
||||
if aliasDestId == utils.ANY || aliasDestId == dId {
|
||||
return alias, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", utils.ErrNotFound
|
||||
}
|
||||
|
||||
@@ -303,9 +303,19 @@ func (cd *CallDescriptor) addRatingInfos(ris RatingInfos) bool {
|
||||
// The prefixLen is limiting the length of the destination prefix.
|
||||
func (cd *CallDescriptor) GetKey(subject string) string {
|
||||
// check if subject is alias
|
||||
if alias, err := GetBestAlias(cd.Destination, cd.Direction, cd.Tenant, cd.Category, cd.Account, cd.Subject, utils.ALIAS_GROUP_RP); err == nil && alias != "" {
|
||||
subject = alias
|
||||
cd.Subject = alias
|
||||
if aliasService != nil {
|
||||
var alias string
|
||||
if err := aliasService.GetMatchingAlias(AttrMatchingAlias{
|
||||
Destination: cd.Destination,
|
||||
Direction: cd.Direction,
|
||||
Tenant: cd.Tenant,
|
||||
Category: cd.Category,
|
||||
Account: cd.Account,
|
||||
Subject: cd.Subject,
|
||||
Group: utils.ALIAS_GROUP_RP}, &alias); err == nil && alias != "" {
|
||||
subject = alias
|
||||
cd.Subject = alias
|
||||
}
|
||||
}
|
||||
return utils.ConcatenatedKey(cd.Direction, cd.Tenant, cd.Category, subject)
|
||||
}
|
||||
@@ -315,9 +325,20 @@ func (cd *CallDescriptor) GetAccountKey() string {
|
||||
subj := cd.Subject
|
||||
if cd.Account != "" {
|
||||
// check if subject is alias
|
||||
alias, err := GetBestAlias(cd.Destination, cd.Direction, cd.Tenant, cd.Category, cd.Account, cd.Subject, utils.ALIAS_GROUP_ACC)
|
||||
if err == nil && alias != "" {
|
||||
cd.Account = alias
|
||||
if aliasService != nil {
|
||||
var alias string
|
||||
err := aliasService.GetMatchingAlias(
|
||||
AttrMatchingAlias{
|
||||
Destination: cd.Destination,
|
||||
Direction: cd.Direction,
|
||||
Tenant: cd.Tenant,
|
||||
Category: cd.Category,
|
||||
Account: cd.Account,
|
||||
Subject: cd.Subject,
|
||||
Group: utils.ALIAS_GROUP_ACC}, &alias)
|
||||
if err == nil && alias != "" {
|
||||
cd.Account = alias
|
||||
}
|
||||
}
|
||||
subj = cd.Account
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user