started implementation of tpaliases and tpusers

This commit is contained in:
Radu Ioan Fericean
2015-08-21 22:43:50 +03:00
parent bef775009a
commit 206a1dd960
6 changed files with 263 additions and 10 deletions

108
apier/v1/tpaliases.go Normal file
View File

@@ -0,0 +1,108 @@
/*
Real-time Charging System for Telecom & ISP environments
Copyright (C) 2012-2015 ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
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 alias within a tariff plan
func (self *ApierV1) SetTPAlias(attrs utils.AttrSetTPAlias, 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 {
return utils.NewErrServerError(err)
}
*reply = "OK"
return nil
}
type AttrGetTPAlias struct {
TPid string // Tariff plan id
Direction string
Tenant string
Category string
Account string
Subject string
Group string
}
// Queries specific Alias on Tariff plan
func (self *ApierV1) GetTPAlias(attr AttrGetTPAlias, reply *engine.Alias) error {
if missing := utils.MissingStructFields(&attr, []string{"TPid"}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
al := &engine.TpAlias{
Tpid: attr.TPid,
Direction: attr.Direction,
Tenant: attr.Tenant,
Category: attr.Category,
Account: attr.Account,
Subject: attr.Subject,
Group: attr.Group,
}
if tms, err := self.StorDb.GetTpAliases(al); err != nil {
return utils.NewErrServerError(err)
} else if len(tms) == 0 {
return utils.ErrNotFound
} else {
tmMap, err := engine.TpAliases(tms).GetAliases()
if err != nil {
return err
}
*reply = *tmMap[al.GetId()]
}
return nil
}
type AttrGetTPAliasIds struct {
TPid string // Tariff plan id
utils.Paginator
}
// Queries alias identities on specific tariff plan.
func (self *ApierV1) GetTPAliasIds(attrs AttrGetTPAliasIds, reply *[]string) error {
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_ALIASES, utils.TPDistinctIds{"tag"}, nil, &attrs.Paginator); err != nil {
return utils.NewErrServerError(err)
} else if ids == nil {
return utils.ErrNotFound
} else {
*reply = ids
}
return nil
}
// Removes specific Alias on Tariff plan
func (self *ApierV1) RemTPAlias(attrs AttrGetTPAlias, reply *string) error {
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "AliasId"}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
if err := self.StorDb.RemTpData(utils.TBL_TP_ALIASES, attrs.TPid); err != nil {
return utils.NewErrServerError(err)
} else {
*reply = "OK"
}
return nil
}

100
apier/v1/tpusers.go Normal file
View File

@@ -0,0 +1,100 @@
/*
Real-time Charging System for Telecom & ISP environments
Copyright (C) 2012-2015 ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
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 alias within a tariff plan
func (self *ApierV1) SetTPUser(attrs utils.AttrSetTPUser, 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 {
return utils.NewErrServerError(err)
}
*reply = "OK"
return nil
}
type AttrGetTPUser struct {
TPid string // Tariff plan id
Tenant string
UserName string
}
// Queries specific User on Tariff plan
func (self *ApierV1) GetTPUser(attr AttrGetTPUser, reply *engine.UserProfile) error {
if missing := utils.MissingStructFields(&attr, []string{"TPid"}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
usr := &engine.TpUser{
Tpid: attr.TPid,
Tenant: attr.Tenant,
UserName: attr.UserName,
}
if tms, err := self.StorDb.GetTpUsers(usr); err != nil {
return utils.NewErrServerError(err)
} else if len(tms) == 0 {
return utils.ErrNotFound
} else {
tmMap, err := engine.TpUsers(tms).GetUsers()
if err != nil {
return err
}
*reply = *tmMap[usr.GetId()]
}
return nil
}
type AttrGetTPUserIds struct {
TPid string // Tariff plan id
utils.Paginator
}
// Queries alias identities on specific tariff plan.
func (self *ApierV1) GetTPUserIds(attrs AttrGetTPUserIds, reply *[]string) error {
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_USERS, utils.TPDistinctIds{"tag"}, nil, &attrs.Paginator); err != nil {
return utils.NewErrServerError(err)
} else if ids == nil {
return utils.ErrNotFound
} else {
*reply = ids
}
return nil
}
// Removes specific User on Tariff plan
func (self *ApierV1) RemTPUser(attrs AttrGetTPUser, reply *string) error {
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "UserId"}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
if err := self.StorDb.RemTpData(utils.TBL_TP_USERS, attrs.TPid); err != nil {
return utils.NewErrServerError(err)
} else {
*reply = "OK"
}
return nil
}

View File

@@ -358,14 +358,23 @@ func APItoModelCdrStat(stats *utils.TPCdrStats) (result []TpCdrstat) {
return
}
func APItoModelApierAlias(t *utils.ApierTPTiming) (result *TpTiming) {
return &TpTiming{
Tpid: t.TPid,
Tag: t.TimingId,
Years: t.Years,
Months: t.Months,
MonthDays: t.MonthDays,
WeekDays: t.WeekDays,
Time: t.Time,
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 APItoModelUsers(attr *utils.AttrSetTPUser) (result *TpUser) {
return &TpUser{
Tpid: attr.TPid,
Tenant: attr.Tenant,
AttributeName: attr.AttributeName,
AttributeValue: attr.AttributeValue,
}
}

View File

@@ -163,7 +163,7 @@ func (self *SQLStorage) RemTpData(table, tpid string, args ...string) error {
tx := self.db.Begin()
if len(table) == 0 { // Remove tpid out of all tables
for _, tblName := range []string{utils.TBL_TP_TIMINGS, utils.TBL_TP_DESTINATIONS, utils.TBL_TP_RATES, utils.TBL_TP_DESTINATION_RATES, utils.TBL_TP_RATING_PLANS, utils.TBL_TP_RATE_PROFILES,
utils.TBL_TP_SHARED_GROUPS, utils.TBL_TP_CDR_STATS, utils.TBL_TP_LCRS, utils.TBL_TP_ACTIONS, utils.TBL_TP_ACTION_PLANS, utils.TBL_TP_ACTION_TRIGGERS, utils.TBL_TP_ACCOUNT_ACTIONS, utils.TBL_TP_DERIVED_CHARGERS} {
utils.TBL_TP_SHARED_GROUPS, utils.TBL_TP_CDR_STATS, utils.TBL_TP_LCRS, utils.TBL_TP_ACTIONS, utils.TBL_TP_ACTION_PLANS, utils.TBL_TP_ACTION_TRIGGERS, utils.TBL_TP_ACCOUNT_ACTIONS, utils.TBL_TP_DERIVED_CHARGERS, utils.TBL_TP_ALIASES} {
if err := tx.Table(tblName).Where("tpid = ?", tpid).Delete(nil).Error; err != nil {
tx.Rollback()
return err

View File

@@ -519,6 +519,24 @@ 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
@@ -1095,3 +1113,19 @@ type AttrGetCallCost struct {
CgrId string // Unique id of the CDR
RunId string // Run Id
}
type TpAlias struct {
Direction string
Tenant string
Category string
Account string
Subject string
Group string
Values []*AliasValue
}
type AliasValue struct {
DestinationId string
Alias string
Weight float64
}

View File

@@ -60,6 +60,8 @@ const (
TBL_TP_ACTION_TRIGGERS = "tp_action_triggers"
TBL_TP_ACCOUNT_ACTIONS = "tp_account_actions"
TBL_TP_DERIVED_CHARGERS = "tp_derived_chargers"
TBL_TP_USERS = "tp_users"
TBL_TP_ALIASES = "tp_aliases"
TBL_CDRS_PRIMARY = "cdrs_primary"
TBL_CDRS_EXTRA = "cdrs_extra"
TBL_COST_DETAILS = "cost_details"