mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-15 21:29:52 +05:00
Update tp filter to consider tenant
This commit is contained in:
committed by
Dan Christian Bogos
parent
297eb915a7
commit
c9ce2f6bc5
@@ -23,28 +23,23 @@ import (
|
||||
)
|
||||
|
||||
// Creates a new FilterProfile within a tariff plan
|
||||
func (self *ApierV1) SetTPFilterProfile(attrs utils.TPFilterProfile, reply *string) error {
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 {
|
||||
func (self *ApierV1) SetTPFilterProfile(attrs *utils.TPFilterProfile, reply *string) error {
|
||||
if missing := utils.MissingStructFields(attrs, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 {
|
||||
return utils.NewErrMandatoryIeMissing(missing...)
|
||||
}
|
||||
if err := self.StorDb.SetTPFilters([]*utils.TPFilterProfile{&attrs}); err != nil {
|
||||
if err := self.StorDb.SetTPFilters([]*utils.TPFilterProfile{attrs}); err != nil {
|
||||
return utils.NewErrServerError(err)
|
||||
}
|
||||
*reply = utils.OK
|
||||
return nil
|
||||
}
|
||||
|
||||
type AttrGetTPFilterProfile struct {
|
||||
TPid string // Tariff plan id
|
||||
ID string // Filter id
|
||||
}
|
||||
|
||||
// Queries specific FilterProfile on tariff plan
|
||||
func (self *ApierV1) GetTPFilterProfile(attr AttrGetTPFilterProfile, reply *utils.TPFilterProfile) error {
|
||||
if missing := utils.MissingStructFields(&attr, []string{"TPid", "ID"}); len(missing) != 0 { //Params missing
|
||||
func (self *ApierV1) GetTPFilterProfile(attr *utils.TPTntID, reply *utils.TPFilterProfile) error {
|
||||
if missing := utils.MissingStructFields(attr, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { //Params missing
|
||||
return utils.NewErrMandatoryIeMissing(missing...)
|
||||
}
|
||||
if filter, err := self.StorDb.GetTPFilters(attr.TPid, attr.ID); err != nil {
|
||||
if filter, err := self.StorDb.GetTPFilters(attr.TPid, attr.Tenant, attr.ID); err != nil {
|
||||
if err.Error() != utils.ErrNotFound.Error() {
|
||||
err = utils.NewErrServerError(err)
|
||||
}
|
||||
@@ -61,11 +56,12 @@ type AttrGetTPFilterProfileIds struct {
|
||||
}
|
||||
|
||||
// Queries FilterProfile identities on specific tariff plan.
|
||||
func (self *ApierV1) GetTPFilterProfileIds(attrs AttrGetTPFilterProfileIds, reply *[]string) error {
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
|
||||
func (self *ApierV1) GetTPFilterProfileIds(attrs *AttrGetTPFilterProfileIds, 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.TBLTPFilters, utils.TPDistinctIds{"id"}, nil, &attrs.Paginator); err != nil {
|
||||
if ids, err := self.StorDb.GetTpTableIds(attrs.TPid, utils.TBLTPFilters, utils.TPDistinctIds{"id"},
|
||||
nil, &attrs.Paginator); err != nil {
|
||||
if err.Error() != utils.ErrNotFound.Error() {
|
||||
err = utils.NewErrServerError(err)
|
||||
}
|
||||
@@ -76,18 +72,13 @@ func (self *ApierV1) GetTPFilterProfileIds(attrs AttrGetTPFilterProfileIds, repl
|
||||
return nil
|
||||
}
|
||||
|
||||
type AttrRemTPFilterProfile struct {
|
||||
TPid string // Tariff plan id
|
||||
Tenant string
|
||||
ID string // Filter id
|
||||
}
|
||||
|
||||
// Removes specific FilterProfile on Tariff plan
|
||||
func (self *ApierV1) RemTPFilterProfile(attrs AttrRemTPFilterProfile, reply *string) error {
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { //Params missing
|
||||
func (self *ApierV1) RemTPFilterProfile(attrs *utils.TPTntID, reply *string) error {
|
||||
if missing := utils.MissingStructFields(attrs, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { //Params missing
|
||||
return utils.NewErrMandatoryIeMissing(missing...)
|
||||
}
|
||||
if err := self.StorDb.RemTpData(utils.TBLTPFilters, attrs.TPid, map[string]string{"tenant": attrs.Tenant, "id": attrs.ID}); err != nil {
|
||||
if err := self.StorDb.RemTpData(utils.TBLTPFilters, attrs.TPid,
|
||||
map[string]string{"tenant": attrs.Tenant, "id": attrs.ID}); err != nil {
|
||||
return utils.NewErrServerError(err)
|
||||
} else {
|
||||
*reply = utils.OK
|
||||
|
||||
@@ -89,12 +89,8 @@ func testTPFilterInitCfg(t *testing.T) {
|
||||
}
|
||||
tpFilterCfg.DataFolderPath = tpFilterDataDir // Share DataFolderPath through config towards StoreDb for Flush()
|
||||
config.SetCgrConfig(tpFilterCfg)
|
||||
switch tpFilterConfigDIR {
|
||||
case "tutmongo": // Mongo needs more time to reset db, need to investigate
|
||||
tpFilterDelay = 2000
|
||||
default:
|
||||
tpFilterDelay = 1000
|
||||
}
|
||||
tpFilterDelay = 1000
|
||||
|
||||
}
|
||||
|
||||
// Wipe out the cdr database
|
||||
@@ -123,7 +119,8 @@ func testTPFilterRpcConn(t *testing.T) {
|
||||
func ttestTPFilterGetTPFilterBeforeSet(t *testing.T) {
|
||||
var reply *utils.TPFilterProfile
|
||||
if err := tpFilterRPC.Call("ApierV1.GetTPFilterProfile",
|
||||
&AttrGetTPFilterProfile{TPid: "TP1", ID: "Filter"}, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() {
|
||||
&utils.TPTntID{TPid: "TP1", Tenant: "cgrates.org", ID: "Filter"},
|
||||
&reply); err == nil || err.Error() != utils.ErrNotFound.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
@@ -157,7 +154,7 @@ func testTPFilterSetTPFilter(t *testing.T) {
|
||||
func testTPFilterGetTPFilterAfterSet(t *testing.T) {
|
||||
var reply *utils.TPFilterProfile
|
||||
if err := tpFilterRPC.Call("ApierV1.GetTPFilterProfile",
|
||||
&AttrGetTPFilterProfile{TPid: "TP1", ID: "Filter"}, &reply); err != nil {
|
||||
&utils.TPTntID{TPid: "TP1", Tenant: "cgrates.org", ID: "Filter"}, &reply); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(tpFilter, reply) {
|
||||
t.Errorf("Expecting : %+v, received: %+v", tpFilter, reply)
|
||||
@@ -199,17 +196,17 @@ func testTPFilterUpdateTPFilter(t *testing.T) {
|
||||
func testTPFilterGetTPFilterAfterUpdate(t *testing.T) {
|
||||
var reply *utils.TPFilterProfile
|
||||
if err := tpFilterRPC.Call("ApierV1.GetTPFilterProfile",
|
||||
&AttrGetTPFilterProfile{TPid: "TP1", ID: "Filter"}, &reply); err != nil {
|
||||
&utils.TPTntID{TPid: "TP1", Tenant: "cgrates.org", ID: "Filter"}, &reply); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(tpFilter, reply) {
|
||||
t.Errorf("Expecting : %+v, received: %+v", tpFilter, reply)
|
||||
t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(tpFilter), utils.ToJSON(reply))
|
||||
}
|
||||
}
|
||||
|
||||
func testTPFilterRemTPFilter(t *testing.T) {
|
||||
var resp string
|
||||
if err := tpFilterRPC.Call("ApierV1.RemTPFilterProfile",
|
||||
&AttrRemTPFilterProfile{TPid: "TP1", Tenant: "cgrates.org", ID: "Filter"}, &resp); err != nil {
|
||||
&utils.TPTntID{TPid: "TP1", Tenant: "cgrates.org", ID: "Filter"}, &resp); err != nil {
|
||||
t.Error(err)
|
||||
} else if resp != utils.OK {
|
||||
t.Error("Unexpected reply returned", resp)
|
||||
@@ -219,7 +216,8 @@ func testTPFilterRemTPFilter(t *testing.T) {
|
||||
func testTPFilterGetTPFilterAfterRemove(t *testing.T) {
|
||||
var reply *utils.TPFilterProfile
|
||||
if err := tpFilterRPC.Call("ApierV1.GetTPFilterProfile",
|
||||
&AttrGetTPFilterProfile{TPid: "TP1", ID: "Filter"}, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() {
|
||||
&utils.TPTntID{TPid: "TP1", Tenant: "cgrates.org", ID: "Filter"},
|
||||
&reply); err == nil || err.Error() != utils.ErrNotFound.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -621,7 +621,7 @@ func (csvs *CSVStorage) GetTPThresholds(tpid, tenant, id string) ([]*utils.TPThr
|
||||
return tpThreshold.AsTPThreshold(), nil
|
||||
}
|
||||
|
||||
func (csvs *CSVStorage) GetTPFilters(tpid, id string) ([]*utils.TPFilterProfile, error) {
|
||||
func (csvs *CSVStorage) GetTPFilters(tpid, tenant, id string) ([]*utils.TPFilterProfile, error) {
|
||||
csvReader, fp, err := csvs.readerFunc(csvs.filterFn, csvs.sep, getColumnCount(TpFilter{}))
|
||||
if err != nil {
|
||||
//log.Print("Could not load filter file: ", err)
|
||||
|
||||
@@ -185,7 +185,7 @@ type LoadReader interface {
|
||||
GetTPResources(string, string, string) ([]*utils.TPResource, error)
|
||||
GetTPStats(string, string, string) ([]*utils.TPStats, error)
|
||||
GetTPThresholds(string, string, string) ([]*utils.TPThreshold, error)
|
||||
GetTPFilters(string, string) ([]*utils.TPFilterProfile, error)
|
||||
GetTPFilters(string, string, string) ([]*utils.TPFilterProfile, error)
|
||||
GetTPSuppliers(string, string) ([]*utils.TPSupplierProfile, error)
|
||||
GetTPAttributes(string, string, string) ([]*utils.TPAttributeProfile, error)
|
||||
GetTPChargers(string, string) ([]*utils.TPChargerProfile, error)
|
||||
|
||||
@@ -82,7 +82,7 @@ func (ms *MapStorage) GetTPStats(tpid, tenant, id string) (stats []*utils.TPStat
|
||||
func (ms *MapStorage) GetTPThresholds(tpid, tenant, id string) (ths []*utils.TPThreshold, err error) {
|
||||
return nil, utils.ErrNotImplemented
|
||||
}
|
||||
func (ms *MapStorage) GetTPFilters(tpid, id string) (fltrs []*utils.TPFilterProfile, err error) {
|
||||
func (ms *MapStorage) GetTPFilters(tpid, tenant, id string) (fltrs []*utils.TPFilterProfile, err error) {
|
||||
return nil, utils.ErrNotImplemented
|
||||
}
|
||||
func (ms *MapStorage) GetTPSuppliers(tpid, id string) (supps []*utils.TPSupplierProfile, err error) {
|
||||
|
||||
@@ -1425,11 +1425,14 @@ func (ms *MongoStorage) SetTPThresholds(tpTHs []*utils.TPThreshold) (err error)
|
||||
})
|
||||
}
|
||||
|
||||
func (ms *MongoStorage) GetTPFilters(tpid, id string) ([]*utils.TPFilterProfile, error) {
|
||||
func (ms *MongoStorage) GetTPFilters(tpid, tenant, id string) ([]*utils.TPFilterProfile, error) {
|
||||
filter := bson.M{"tpid": tpid}
|
||||
if id != "" {
|
||||
filter["id"] = id
|
||||
}
|
||||
if tenant != "" {
|
||||
filter["tenant"] = tenant
|
||||
}
|
||||
results := []*utils.TPFilterProfile{}
|
||||
err := ms.client.UseSession(ms.ctx, func(sctx mongo.SessionContext) (err error) {
|
||||
cur, err := ms.getCol(utils.TBLTPFilters).Find(sctx, filter)
|
||||
|
||||
@@ -1531,12 +1531,15 @@ func (self *SQLStorage) GetTPThresholds(tpid, tenant, id string) ([]*utils.TPThr
|
||||
return aths, nil
|
||||
}
|
||||
|
||||
func (self *SQLStorage) GetTPFilters(tpid, id string) ([]*utils.TPFilterProfile, error) {
|
||||
func (self *SQLStorage) GetTPFilters(tpid, tenant, id string) ([]*utils.TPFilterProfile, error) {
|
||||
var ths TpFilterS
|
||||
q := self.db.Where("tpid = ?", tpid)
|
||||
if len(id) != 0 {
|
||||
q = q.Where("id = ?", id)
|
||||
}
|
||||
if len(tenant) != 0 {
|
||||
q = q.Where("tenant = ?", tenant)
|
||||
}
|
||||
if err := q.Find(&ths).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1362,7 +1362,7 @@ func (tpr *TpReader) LoadThresholds() error {
|
||||
}
|
||||
|
||||
func (tpr *TpReader) LoadFiltersFiltered(tag string) error {
|
||||
tps, err := tpr.lr.GetTPFilters(tpr.tpid, tag)
|
||||
tps, err := tpr.lr.GetTPFilters(tpr.tpid, "", tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -251,7 +251,7 @@ func (self *TPExporter) Run() error {
|
||||
}
|
||||
}
|
||||
|
||||
storDataFilters, err := self.storDb.GetTPFilters(self.tpID, "")
|
||||
storDataFilters, err := self.storDb.GetTPFilters(self.tpID, "", "")
|
||||
if err != nil && err.Error() != utils.ErrNotFound.Error() {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -363,7 +363,7 @@ func (self *TPCSVImporter) importFilters(fn string) error {
|
||||
if self.Verbose {
|
||||
log.Printf("Processing file: <%s> ", fn)
|
||||
}
|
||||
sts, err := self.csvr.GetTPFilters(self.TPid, "")
|
||||
sts, err := self.csvr.GetTPFilters(self.TPid, "", "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ func (m *Migrator) migrateCurrentTPfilters() (err error) {
|
||||
return err
|
||||
}
|
||||
for _, id := range ids {
|
||||
fltrs, err := m.storDBIn.StorDB().GetTPFilters(tpid, id)
|
||||
fltrs, err := m.storDBIn.StorDB().GetTPFilters(tpid, "", id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -144,7 +144,7 @@ func testTpFltrITMove(t *testing.T) {
|
||||
|
||||
func testTpFltrITCheckData(t *testing.T) {
|
||||
result, err := tpFltrMigrator.storDBOut.StorDB().GetTPFilters(
|
||||
tpFilters[0].TPid, tpFilters[0].ID)
|
||||
tpFilters[0].TPid, "", tpFilters[0].ID)
|
||||
if err != nil {
|
||||
t.Error("Error when getting TpFilter ", err.Error())
|
||||
}
|
||||
@@ -152,7 +152,7 @@ func testTpFltrITCheckData(t *testing.T) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", tpFilters[0], result[0])
|
||||
}
|
||||
result, err = tpFltrMigrator.storDBIn.StorDB().GetTPFilters(
|
||||
tpFilters[0].TPid, tpFilters[0].ID)
|
||||
tpFilters[0].TPid, "", tpFilters[0].ID)
|
||||
if err != utils.ErrNotFound {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user