added TP apis for aliases, users and lcr rules

fixes #154, fixes #155, fixes #156
This commit is contained in:
Radu Ioan Fericean
2015-08-28 17:18:52 +03:00
parent a8829de649
commit ffc30e43ed
7 changed files with 175 additions and 68 deletions

View File

@@ -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...)
}

View File

@@ -18,20 +18,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
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
}
*/

View File

@@ -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...)
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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