From ffc30e43edee59ea83eb08793aa26f41923f31fb Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 28 Aug 2015 17:18:52 +0300 Subject: [PATCH] added TP apis for aliases, users and lcr rules fixes #154, fixes #155, fixes #156 --- apier/v1/tpaliases.go | 6 ++-- apier/v1/tplcrrules.go | 32 ++++++++++++---------- apier/v1/tpusers.go | 6 ++-- engine/model_converters.go | 50 ++++++++++++++++++++++++---------- engine/model_helpers.go | 56 ++++++++++++++++++++++++++++++-------- engine/tp_reader.go | 46 +++++++++++++++++++++++++++++-- utils/apitpdata.go | 47 ++++++++++++++++++++------------ 7 files changed, 175 insertions(+), 68 deletions(-) diff --git a/apier/v1/tpaliases.go b/apier/v1/tpaliases.go index 05ced89e7..bc343d42f 100644 --- a/apier/v1/tpaliases.go +++ b/apier/v1/tpaliases.go @@ -24,12 +24,12 @@ import ( ) // Creates a new alias within a tariff plan -func (self *ApierV1) SetTPAlias(attrs utils.AttrSetTPAlias, reply *string) error { +func (self *ApierV1) SetTPAlias(attrs utils.TPAliases, reply *string) error { if missing := utils.MissingStructFields(&attrs, []string{"TPid", "Direction", "Tenant", "Category", "Account", "Subject", "Group"}); len(missing) != 0 { return utils.NewErrMandatoryIeMissing(missing...) } tm := engine.APItoModelAliases(&attrs) - if err := self.StorDb.SetTpAliases([]engine.TpAlias{*tm}); err != nil { + if err := self.StorDb.SetTpAliases(tm); err != nil { return utils.NewErrServerError(err) } *reply = "OK" @@ -47,7 +47,7 @@ type AttrGetTPAlias struct { } // Queries specific Alias on Tariff plan -func (self *ApierV1) GetTPAlias(attr AttrGetTPAlias, reply *engine.Alias) error { +func (self *ApierV1) GetTPAliases(attr AttrGetTPAlias, reply *utils.TPAliases) error { if missing := utils.MissingStructFields(&attr, []string{"TPid"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } diff --git a/apier/v1/tplcrrules.go b/apier/v1/tplcrrules.go index 009480cf5..b947c097d 100644 --- a/apier/v1/tplcrrules.go +++ b/apier/v1/tplcrrules.go @@ -18,20 +18,18 @@ along with this program. If not, see package v1 -/* +import ( + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + // Creates a new LcrRules profile within a tariff plan func (self *ApierV1) SetTPLcrRules(attrs utils.TPLcrRules, reply *string) error { - if missing := utils.MissingStructFields(&attrs, []string{"TPid", "LcrRulesId", "LcrRules"}); len(missing) != 0 { + if missing := utils.MissingStructFields(&attrs, []string{"TPid", "LcrRulesId", "Identifier", "Weight"}); len(missing) != 0 { return utils.NewErrMandatoryIeMissing(missing...) } - for _, action := range attrs.LcrRules { - requiredFields := []string{"Identifier", "Weight"} - - if missing := utils.MissingStructFields(action, requiredFields); len(missing) != 0 { - return fmt.Errorf("%s:LcrAction:%s:%v", utils.ERR_MANDATORY_IE_MISSING, action.Identifier, missing) - } - } - if err := self.StorDb.SetTPLcrRules(attrs.TPid, map[string][]*utils.TPLcrRule{attrs.LcrRulesId: attrs.LcrRules}); err != nil { + tm := engine.APItoModelLcrRule(&attrs) + if err := self.StorDb.SetTpLCRs(tm); err != nil { return utils.NewErrServerError(err) } *reply = "OK" @@ -50,16 +48,21 @@ func (self *ApierV1) GetTPLcrRules(attrs AttrGetTPLcrRules, reply *utils.TPLcrRu } if lcrs, err := self.StorDb.GetTpLCRs(attrs.TPid, attrs.LcrId); err != nil { return utils.NewErrServerError(err) - } else if len(acts) == 0 { + } else if len(lcrs) == 0 { return utils.ErrNotFound } else { - *reply = utils.TPLcrRules{TPid: attrs.TPid, LcrRulesId: attrs.LcrRulesId, LcrRules: lcrs[attrs.LcrRulesId]} + tmMap, err := engine.TpLcrRules(lcrs).GetLcrRules() + if err != nil { + return err + } + *reply = *tmMap[attrs.LcrId] } return nil } type AttrGetTPLcrActionIds struct { TPid string // Tariff plan id + utils.Paginator } // Queries LcrRules identities on specific tariff plan. @@ -67,7 +70,7 @@ func (self *ApierV1) GetTPLcrActionIds(attrs AttrGetTPLcrActionIds, reply *[]str if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if ids, err := self.StorDb.GetTPTableIds(attrs.TPid, utils.TBL_TP_LCRS, "id", nil); err != nil { + if ids, err := self.StorDb.GetTpTableIds(attrs.TPid, utils.TBL_TP_LCRS, utils.TPDistinctIds{"tag"}, nil, &attrs.Paginator); err != nil { return utils.NewErrServerError(err) } else if ids == nil { return utils.ErrNotFound @@ -82,11 +85,10 @@ func (self *ApierV1) RemTPLcrRules(attrs AttrGetTPLcrRules, reply *string) error if missing := utils.MissingStructFields(&attrs, []string{"TPid", "LcrRulesId"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if err := self.StorDb.RemTPData(utils.TBL_TP_LCRS, attrs.TPid, attrs.LcrRulesId); err != nil { + if err := self.StorDb.RemTpData(utils.TBL_TP_LCRS, attrs.TPid, attrs.LcrId); err != nil { return utils.NewErrServerError(err) } else { *reply = "OK" } return nil } -*/ diff --git a/apier/v1/tpusers.go b/apier/v1/tpusers.go index dfc12be00..8d84b8dd9 100644 --- a/apier/v1/tpusers.go +++ b/apier/v1/tpusers.go @@ -24,12 +24,12 @@ import ( ) // Creates a new alias within a tariff plan -func (self *ApierV1) SetTPUser(attrs utils.AttrSetTPUser, reply *string) error { +func (self *ApierV1) SetTPUser(attrs utils.TPUsers, reply *string) error { if missing := utils.MissingStructFields(&attrs, []string{"TPid", "Direction", "Tenant", "Category", "Account", "Subject", "Group"}); len(missing) != 0 { return utils.NewErrMandatoryIeMissing(missing...) } tm := engine.APItoModelUsers(&attrs) - if err := self.StorDb.SetTpUsers([]engine.TpUser{*tm}); err != nil { + if err := self.StorDb.SetTpUsers(tm); err != nil { return utils.NewErrServerError(err) } *reply = "OK" @@ -43,7 +43,7 @@ type AttrGetTPUser struct { } // Queries specific User on Tariff plan -func (self *ApierV1) GetTPUser(attr AttrGetTPUser, reply *engine.UserProfile) error { +func (self *ApierV1) GetTPUser(attr AttrGetTPUser, reply *utils.TPUsers) error { if missing := utils.MissingStructFields(&attr, []string{"TPid"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } diff --git a/engine/model_converters.go b/engine/model_converters.go index 5f61cf7d4..c91bd160b 100644 --- a/engine/model_converters.go +++ b/engine/model_converters.go @@ -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 } diff --git a/engine/model_helpers.go b/engine/model_helpers.go index ffd25a751..4796bb17f 100644 --- a/engine/model_helpers.go +++ b/engine/model_helpers.go @@ -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 +} diff --git a/engine/tp_reader.go b/engine/tp_reader.go index f86da841f..68906bd04 100644 --- a/engine/tp_reader.go +++ b/engine/tp_reader.go @@ -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 } diff --git a/utils/apitpdata.go b/utils/apitpdata.go index 8dad41fa0..6f36a3bb7 100644 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -321,6 +321,35 @@ type TPLcrRule struct { Weight float64 } +type TPAliases struct { + TPid string + Direction string + Tenant string + Category string + Account string + Subject string + Group string + Values []*TPAliasValue +} + +type TPAliasValue struct { + DestinationId string + Alias string + Weight float64 +} + +type TPUsers struct { + TPid string + Tenant string + UserName string + Profile []*TPUserProfile +} + +type TPUserProfile struct { + AttrName string + AttrValue string +} + type TPCdrStats struct { TPid string CdrStatsId string @@ -519,24 +548,6 @@ type AttrGetAccounts struct { Limit int // Limit number of items retrieved } -type AttrSetTPAlias struct { - TPid string - Direction string - Tenant string - Category string - Account string - Subject string - Group string -} - -type AttrSetTPUser struct { - TPid string - Tenant string - UserName string - AttributeName string - AttributeValue string -} - // Data used to do remote cache reloads via api type ApiReloadCache struct { DestinationIds []string