From b466b60238703b572d49207be1878f4f930c0d7c Mon Sep 17 00:00:00 2001 From: TeoV Date: Wed, 9 Jan 2019 07:04:46 -0500 Subject: [PATCH] Update tp suppliers to consider tenant --- apier/v1/tpsuppliers.go | 37 ++++++++++++-------------------- apier/v1/tpsuppliers_it_test.go | 20 +++++++---------- engine/model_helpers.go | 1 + engine/storage_csv.go | 2 +- engine/storage_interface.go | 2 +- engine/storage_map_stordb.go | 2 +- engine/storage_mongo_stordb.go | 5 ++++- engine/storage_sql.go | 5 ++++- engine/tp_reader.go | 2 +- engine/tpexporter.go | 2 +- engine/tpimporter_csv.go | 2 +- migrator/tp_suppliers.go | 2 +- migrator/tp_suppliers_it_test.go | 4 ++-- 13 files changed, 40 insertions(+), 46 deletions(-) diff --git a/apier/v1/tpsuppliers.go b/apier/v1/tpsuppliers.go index c8bdcf1ed..4ac78406d 100644 --- a/apier/v1/tpsuppliers.go +++ b/apier/v1/tpsuppliers.go @@ -23,28 +23,23 @@ import ( ) // Creates a new SupplierProfile within a tariff plan -func (self *ApierV1) SetTPSupplierProfile(attrs utils.TPSupplierProfile, reply *string) error { - if missing := utils.MissingStructFields(&attrs, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { +func (self *ApierV1) SetTPSupplierProfile(attrs *utils.TPSupplierProfile, reply *string) error { + if missing := utils.MissingStructFields(attrs, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { return utils.NewErrMandatoryIeMissing(missing...) } - if err := self.StorDb.SetTPSuppliers([]*utils.TPSupplierProfile{&attrs}); err != nil { + if err := self.StorDb.SetTPSuppliers([]*utils.TPSupplierProfile{attrs}); err != nil { return utils.NewErrServerError(err) } *reply = utils.OK return nil } -type AttrGetTPSupplierProfile struct { - TPid string // Tariff plan id - ID string // Filter id -} - // Queries specific SupplierProfile on tariff plan -func (self *ApierV1) GetTPSupplierProfile(attr AttrGetTPSupplierProfile, reply *utils.TPSupplierProfile) error { - if missing := utils.MissingStructFields(&attr, []string{"TPid", "ID"}); len(missing) != 0 { //Params missing +func (self *ApierV1) GetTPSupplierProfile(attr *utils.TPTntID, reply *utils.TPSupplierProfile) error { + if missing := utils.MissingStructFields(attr, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if spp, err := self.StorDb.GetTPSuppliers(attr.TPid, attr.ID); err != nil { + if spp, err := self.StorDb.GetTPSuppliers(attr.TPid, attr.Tenant, attr.ID); err != nil { if err.Error() != utils.ErrNotFound.Error() { err = utils.NewErrServerError(err) } @@ -61,11 +56,12 @@ type AttrGetTPSupplierProfileIDs struct { } // Queries SupplierProfile identities on specific tariff plan. -func (self *ApierV1) GetTPSupplierProfileIDs(attrs AttrGetTPSupplierProfileIDs, reply *[]string) error { - if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing +func (self *ApierV1) GetTPSupplierProfileIDs(attrs *AttrGetTPSupplierProfileIDs, 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.TBLTPSuppliers, utils.TPDistinctIds{"id"}, nil, &attrs.Paginator); err != nil { + if ids, err := self.StorDb.GetTpTableIds(attrs.TPid, utils.TBLTPSuppliers, 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) GetTPSupplierProfileIDs(attrs AttrGetTPSupplierProfileIDs, return nil } -type AttrRemTPSupplierProfile struct { - TPid string // Tariff plan id - Tenant string - ID string // LCR id -} - // Removes specific SupplierProfile on Tariff plan -func (self *ApierV1) RemTPSupplierProfile(attrs AttrRemTPSupplierProfile, reply *string) error { - if missing := utils.MissingStructFields(&attrs, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { //Params missing +func (self *ApierV1) RemTPSupplierProfile(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.TBLTPSuppliers, attrs.TPid, map[string]string{"tenant": attrs.Tenant, "id": attrs.ID}); err != nil { + if err := self.StorDb.RemTpData(utils.TBLTPSuppliers, attrs.TPid, + map[string]string{"tenant": attrs.Tenant, "id": attrs.ID}); err != nil { return utils.NewErrServerError(err) } else { *reply = utils.OK diff --git a/apier/v1/tpsuppliers_it_test.go b/apier/v1/tpsuppliers_it_test.go index 199aa16cf..1a62a303d 100644 --- a/apier/v1/tpsuppliers_it_test.go +++ b/apier/v1/tpsuppliers_it_test.go @@ -82,12 +82,8 @@ func testTPSplPrfInitCfg(t *testing.T) { } tpSplPrfCfg.DataFolderPath = tpSplPrfDataDire // Share DataFolderPath through config towards StoreDb for Flush() config.SetCgrConfig(tpSplPrfCfg) - switch tpSplPrfConfigDIR { - case "tutmongo": // Mongo needs more time to reset db, need to investigate - tpSplPrfDelay = 2000 - default: - tpSplPrfDelay = 1000 - } + tpSplPrfDelay = 1000 + } // Wipe out the cdr database @@ -116,7 +112,7 @@ func testTPSplPrfRPCConn(t *testing.T) { func testTPSplPrfGetTPSplPrfBeforeSet(t *testing.T) { var reply *utils.TPSupplier if err := tpSplPrfRPC.Call("ApierV1.GetTPSupplierProfile", - &AttrGetTPSupplierProfile{TPid: "TP1", ID: "SUPL_1"}, + &utils.TPTntID{TPid: "TP1", Tenant: "cgrates.org", ID: "SUPL_1"}, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() { t.Error(err) } @@ -161,7 +157,7 @@ func testTPSplPrfSetTPSplPrf(t *testing.T) { func testTPSplPrfGetTPSplPrfAfterSet(t *testing.T) { var reply *utils.TPSupplierProfile if err := tpSplPrfRPC.Call("ApierV1.GetTPSupplierProfile", - &AttrGetTPSupplierProfile{TPid: "TP1", ID: "SUPL_1"}, &reply); err != nil { + &utils.TPTntID{TPid: "TP1", Tenant: "cgrates.org", ID: "SUPL_1"}, &reply); err != nil { t.Error(err) } else if !reflect.DeepEqual(tpSplPr, reply) { t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(tpSplPr), utils.ToJSON(reply)) @@ -241,18 +237,18 @@ func testTPSplPrfGetTPSplPrfAfterUpdate(t *testing.T) { }, } if err := tpSplPrfRPC.Call("ApierV1.GetTPSupplierProfile", - &AttrGetTPSupplierProfile{TPid: "TP1", ID: "SUPL_1"}, &reply); err != nil { + &utils.TPTntID{TPid: "TP1", Tenant: "cgrates.org", ID: "SUPL_1"}, &reply); err != nil { t.Error(err) } else if !reflect.DeepEqual(tpSplPr.Suppliers, reply.Suppliers) && !reflect.DeepEqual(tpSplPr.Suppliers, reverseSuppliers) { - t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(tpSplPr), utils.ToJSON(reply)) + t.Errorf("Expecting: %+v,\n received: %+v", utils.ToJSON(tpSplPr), utils.ToJSON(reply)) } } func testTPSplPrfRemTPSplPrf(t *testing.T) { var resp string if err := tpSplPrfRPC.Call("ApierV1.RemTPSupplierProfile", - &AttrRemTPSupplierProfile{TPid: "TP1", Tenant: "cgrates.org", ID: "SUPL_1"}, + &utils.TPTntID{TPid: "TP1", Tenant: "cgrates.org", ID: "SUPL_1"}, &resp); err != nil { t.Error(err) } else if resp != utils.OK { @@ -263,7 +259,7 @@ func testTPSplPrfRemTPSplPrf(t *testing.T) { func testTPSplPrfGetTPSplPrfAfterRemove(t *testing.T) { var reply *utils.TPSupplierProfile if err := tpSplPrfRPC.Call("ApierV1.GetTPSupplierProfile", - &AttrGetTPSupplierProfile{TPid: "TP1", ID: "SUPL_1"}, + &utils.TPTntID{TPid: "TP1", Tenant: "cgrates.org", ID: "SUPL_1"}, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() { t.Error(err) } diff --git a/engine/model_helpers.go b/engine/model_helpers.go index ce59f3fd6..eedade5dc 100644 --- a/engine/model_helpers.go +++ b/engine/model_helpers.go @@ -2249,6 +2249,7 @@ func APItoModelTPSuppliers(st *utils.TPSupplierProfile) (mdls TpSuppliers) { } mdl.SupplierWeight = supl.Weight mdl.SupplierParameters = supl.SupplierParameters + mdl.SupplierBlocker = supl.Blocker mdls = append(mdls, mdl) } return diff --git a/engine/storage_csv.go b/engine/storage_csv.go index 289890572..60933b378 100644 --- a/engine/storage_csv.go +++ b/engine/storage_csv.go @@ -649,7 +649,7 @@ func (csvs *CSVStorage) GetTPFilters(tpid, tenant, id string) ([]*utils.TPFilter return tpFilter.AsTPFilter(), nil } -func (csvs *CSVStorage) GetTPSuppliers(tpid, id string) ([]*utils.TPSupplierProfile, error) { +func (csvs *CSVStorage) GetTPSuppliers(tpid, tenant, id string) ([]*utils.TPSupplierProfile, error) { csvReader, fp, err := csvs.readerFunc(csvs.suppProfilesFn, csvs.sep, getColumnCount(TpSupplier{})) if err != nil { //log.Print("Could not load lcrProfile file: ", err) diff --git a/engine/storage_interface.go b/engine/storage_interface.go index 3b485b069..405078d88 100644 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -186,7 +186,7 @@ type LoadReader interface { GetTPStats(string, string, string) ([]*utils.TPStats, error) GetTPThresholds(string, string, string) ([]*utils.TPThreshold, error) GetTPFilters(string, string, string) ([]*utils.TPFilterProfile, error) - GetTPSuppliers(string, string) ([]*utils.TPSupplierProfile, error) + GetTPSuppliers(string, string, string) ([]*utils.TPSupplierProfile, error) GetTPAttributes(string, string, string) ([]*utils.TPAttributeProfile, error) GetTPChargers(string, string) ([]*utils.TPChargerProfile, error) } diff --git a/engine/storage_map_stordb.go b/engine/storage_map_stordb.go index 774babe80..bbf870e44 100755 --- a/engine/storage_map_stordb.go +++ b/engine/storage_map_stordb.go @@ -85,7 +85,7 @@ func (ms *MapStorage) GetTPThresholds(tpid, tenant, id string) (ths []*utils.TPT 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) { +func (ms *MapStorage) GetTPSuppliers(tpid, tenant, id string) (supps []*utils.TPSupplierProfile, err error) { return nil, utils.ErrNotImplemented } func (ms *MapStorage) GetTPAttributes(tpid, tenant, id string) (attrs []*utils.TPAttributeProfile, err error) { diff --git a/engine/storage_mongo_stordb.go b/engine/storage_mongo_stordb.go index a9fb82cae..7c8dac653 100644 --- a/engine/storage_mongo_stordb.go +++ b/engine/storage_mongo_stordb.go @@ -1473,11 +1473,14 @@ func (ms *MongoStorage) SetTPFilters(tpTHs []*utils.TPFilterProfile) (err error) }) } -func (ms *MongoStorage) GetTPSuppliers(tpid, id string) ([]*utils.TPSupplierProfile, error) { +func (ms *MongoStorage) GetTPSuppliers(tpid, tenant, id string) ([]*utils.TPSupplierProfile, error) { filter := bson.M{"tpid": tpid} if id != "" { filter["id"] = id } + if tenant != "" { + filter["tenant"] = tenant + } var results []*utils.TPSupplierProfile err := ms.client.UseSession(ms.ctx, func(sctx mongo.SessionContext) (err error) { cur, err := ms.getCol(utils.TBLTPSuppliers).Find(sctx, filter) diff --git a/engine/storage_sql.go b/engine/storage_sql.go index 9184beeb1..f14abc277 100644 --- a/engine/storage_sql.go +++ b/engine/storage_sql.go @@ -1550,12 +1550,15 @@ func (self *SQLStorage) GetTPFilters(tpid, tenant, id string) ([]*utils.TPFilter return aths, nil } -func (self *SQLStorage) GetTPSuppliers(tpid, id string) ([]*utils.TPSupplierProfile, error) { +func (self *SQLStorage) GetTPSuppliers(tpid, tenant, id string) ([]*utils.TPSupplierProfile, error) { var sps TpSuppliers 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(&sps).Error; err != nil { return nil, err } diff --git a/engine/tp_reader.go b/engine/tp_reader.go index 4d4492030..fb947e6f8 100644 --- a/engine/tp_reader.go +++ b/engine/tp_reader.go @@ -1379,7 +1379,7 @@ func (tpr *TpReader) LoadFilters() error { } func (tpr *TpReader) LoadSupplierProfilesFiltered(tag string) (err error) { - rls, err := tpr.lr.GetTPSuppliers(tpr.tpid, tag) + rls, err := tpr.lr.GetTPSuppliers(tpr.tpid, "", tag) if err != nil { return err } diff --git a/engine/tpexporter.go b/engine/tpexporter.go index 84a8914ac..461d892f3 100644 --- a/engine/tpexporter.go +++ b/engine/tpexporter.go @@ -262,7 +262,7 @@ func (self *TPExporter) Run() error { } } - storDataSuppliers, err := self.storDb.GetTPSuppliers(self.tpID, "") + storDataSuppliers, err := self.storDb.GetTPSuppliers(self.tpID, "", "") if err != nil && err.Error() != utils.ErrNotFound.Error() { return err } diff --git a/engine/tpimporter_csv.go b/engine/tpimporter_csv.go index 62e99d4d8..bd5e3c768 100644 --- a/engine/tpimporter_csv.go +++ b/engine/tpimporter_csv.go @@ -374,7 +374,7 @@ func (self *TPCSVImporter) importSuppliers(fn string) error { if self.Verbose { log.Printf("Processing file: <%s> ", fn) } - rls, err := self.csvr.GetTPSuppliers(self.TPid, "") + rls, err := self.csvr.GetTPSuppliers(self.TPid, "", "") if err != nil { return err } diff --git a/migrator/tp_suppliers.go b/migrator/tp_suppliers.go index f465e603b..c14e92ea4 100644 --- a/migrator/tp_suppliers.go +++ b/migrator/tp_suppliers.go @@ -39,7 +39,7 @@ func (m *Migrator) migrateCurrentTPSuppliers() (err error) { } for _, id := range ids { - suppliers, err := m.storDBIn.StorDB().GetTPSuppliers(tpid, id) + suppliers, err := m.storDBIn.StorDB().GetTPSuppliers(tpid, "", id) if err != nil { return err } diff --git a/migrator/tp_suppliers_it_test.go b/migrator/tp_suppliers_it_test.go index c9c7d7797..348062024 100644 --- a/migrator/tp_suppliers_it_test.go +++ b/migrator/tp_suppliers_it_test.go @@ -155,7 +155,7 @@ func testTpSplITMove(t *testing.T) { func testTpSplITCheckData(t *testing.T) { result, err := tpSplMigrator.storDBOut.StorDB().GetTPSuppliers( - tpSuppliers[0].TPid, tpSuppliers[0].ID) + tpSuppliers[0].TPid, "", tpSuppliers[0].ID) if err != nil { t.Error("Error when getting TpSuppliers ", err.Error()) } @@ -164,7 +164,7 @@ func testTpSplITCheckData(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(tpSuppliers[0]), utils.ToJSON(result[0])) } result, err = tpSplMigrator.storDBIn.StorDB().GetTPSuppliers( - tpSuppliers[0].TPid, tpSuppliers[0].ID) + tpSuppliers[0].TPid, "", tpSuppliers[0].ID) if err != utils.ErrNotFound { t.Error(err) }