From b5708c18a51d43c74eec692014d92afabef138be Mon Sep 17 00:00:00 2001 From: TeoV Date: Wed, 9 Jan 2019 03:27:45 -0500 Subject: [PATCH] Update tp resource to consider tenant --- apier/v1/tpattributes.go | 4 ++-- apier/v1/tpresources.go | 25 ++++++++++--------------- apier/v1/tpresources_it_test.go | 23 ++++++++++++----------- 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_resources.go | 2 +- 12 files changed, 39 insertions(+), 37 deletions(-) diff --git a/apier/v1/tpattributes.go b/apier/v1/tpattributes.go index b3172b9ef..7a415af7c 100644 --- a/apier/v1/tpattributes.go +++ b/apier/v1/tpattributes.go @@ -56,8 +56,8 @@ type AttrGetTPAttributeProfileIds struct { } // Queries attribute identities on specific tariff plan. -func (self *ApierV1) GetTPAttributeProfileIds(attrs AttrGetTPAttributeProfileIds, reply *[]string) error { - if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing +func (self *ApierV1) GetTPAttributeProfileIds(attrs *AttrGetTPAttributeProfileIds, 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.TBLTPAttributes, utils.TPDistinctIds{"id"}, nil, &attrs.Paginator); err != nil { diff --git a/apier/v1/tpresources.go b/apier/v1/tpresources.go index 30803992a..d58815743 100644 --- a/apier/v1/tpresources.go +++ b/apier/v1/tpresources.go @@ -23,28 +23,23 @@ import ( ) // Creates a new resource within a tariff plan -func (self *ApierV1) SetTPResource(attr utils.TPResource, reply *string) error { - if missing := utils.MissingStructFields(&attr, []string{"TPid", "ID", "Limit"}); len(missing) != 0 { +func (self *ApierV1) SetTPResource(attr *utils.TPResource, reply *string) error { + if missing := utils.MissingStructFields(attr, []string{"TPid", "ID", "Limit"}); len(missing) != 0 { return utils.NewErrMandatoryIeMissing(missing...) } - if err := self.StorDb.SetTPResources([]*utils.TPResource{&attr}); err != nil { + if err := self.StorDb.SetTPResources([]*utils.TPResource{attr}); err != nil { return utils.APIErrorHandler(err) } *reply = utils.OK return nil } -type AttrGetTPResource struct { - TPid string // Tariff plan id - ID string -} - // Queries specific Resource on Tariff plan -func (self *ApierV1) GetTPResource(attr AttrGetTPResource, reply *utils.TPResource) error { - if missing := utils.MissingStructFields(&attr, []string{"TPid", "ID"}); len(missing) != 0 { //Params missing +func (self *ApierV1) GetTPResource(attr *utils.TPTntID, reply *utils.TPResource) error { + if missing := utils.MissingStructFields(attr, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } - if rls, err := self.StorDb.GetTPResources(attr.TPid, attr.ID); err != nil { + if rls, err := self.StorDb.GetTPResources(attr.TPid, attr.Tenant, attr.ID); err != nil { if err.Error() != utils.ErrNotFound.Error() { err = utils.NewErrServerError(err) } @@ -61,7 +56,7 @@ type AttrGetTPResourceIds struct { } // Queries Resource identities on specific tariff plan. -func (self *ApierV1) GetTPResourceIDs(attrs AttrGetTPResourceIds, reply *[]string) error { +func (self *ApierV1) GetTPResourceIDs(attrs *AttrGetTPResourceIds, reply *[]string) error { if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } @@ -77,11 +72,11 @@ func (self *ApierV1) GetTPResourceIDs(attrs AttrGetTPResourceIds, reply *[]strin } // Removes specific Resource on Tariff plan -func (self *ApierV1) RemTPResource(attrs AttrGetTPResource, reply *string) error { - if missing := utils.MissingStructFields(&attrs, []string{"TPid", "ID"}); len(missing) != 0 { //Params missing +func (self *ApierV1) RemTPResource(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.TBLTPResources, attrs.TPid, map[string]string{"id": attrs.ID}); err != nil { + if err := self.StorDb.RemTpData(utils.TBLTPResources, 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/tpresources_it_test.go b/apier/v1/tpresources_it_test.go index bbb8146e6..6433decb1 100644 --- a/apier/v1/tpresources_it_test.go +++ b/apier/v1/tpresources_it_test.go @@ -88,12 +88,7 @@ func testTPResInitCfg(t *testing.T) { } tpResCfg.DataFolderPath = tpResDataDir // Share DataFolderPath through config towards StoreDb for Flush() config.SetCgrConfig(tpResCfg) - switch tpResConfigDIR { - case "tutmongo": // Mongo needs more time to reset db, need to investigate - tpResDelay = 2000 - default: - tpResDelay = 1000 - } + tpResDelay = 1000 } // Wipe out the cdr database @@ -121,7 +116,9 @@ func testTPResRpcConn(t *testing.T) { func testTPResGetTPResourceBeforeSet(t *testing.T) { var reply *utils.TPResource - if err := tpResRPC.Call("ApierV1.GetTPResource", AttrGetTPResource{TPid: "TPR1", ID: "ResGroup1"}, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() { + if err := tpResRPC.Call("ApierV1.GetTPResource", + &utils.TPTntID{TPid: "TPR1", Tenant: "cgrates.org", ID: "ResGroup1"}, &reply); err == nil || + err.Error() != utils.ErrNotFound.Error() { t.Error(err) } } @@ -154,7 +151,8 @@ func testTPResSetTPResource(t *testing.T) { func testTPResGetTPResourceAfterSet(t *testing.T) { var respond *utils.TPResource - if err := tpResRPC.Call("ApierV1.GetTPResource", AttrGetTPResource{TPid: tpRes.TPid, ID: tpRes.ID}, &respond); err != nil { + if err := tpResRPC.Call("ApierV1.GetTPResource", &utils.TPTntID{TPid: "TPR1", Tenant: "cgrates.org", ID: "ResGroup1"}, + &respond); err != nil { t.Error(err) } else if !reflect.DeepEqual(tpRes, respond) { t.Errorf("Expecting : %+v, received: %+v", tpRes, respond) @@ -173,7 +171,8 @@ func testTPResUpdateTPResource(t *testing.T) { func testTPResGetTPResourceAfterUpdate(t *testing.T) { var expectedTPR *utils.TPResource - if err := tpResRPC.Call("ApierV1.GetTPResource", AttrGetTPResource{TPid: tpRes.TPid, ID: tpRes.ID}, &expectedTPR); err != nil { + if err := tpResRPC.Call("ApierV1.GetTPResource", &utils.TPTntID{TPid: "TPR1", Tenant: "cgrates.org", ID: "ResGroup1"}, + &expectedTPR); err != nil { t.Error(err) } else if !reflect.DeepEqual(tpRes, expectedTPR) { t.Errorf("Expecting: %+v, received: %+v", tpRes, expectedTPR) @@ -182,7 +181,8 @@ func testTPResGetTPResourceAfterUpdate(t *testing.T) { func testTPResRemTPResource(t *testing.T) { var resp string - if err := tpResRPC.Call("ApierV1.RemTPResource", AttrGetTPResource{TPid: tpRes.TPid, ID: tpRes.ID}, &resp); err != nil { + if err := tpResRPC.Call("ApierV1.RemTPResource", &utils.TPTntID{TPid: "TPR1", Tenant: "cgrates.org", ID: "ResGroup1"}, + &resp); err != nil { t.Error(err) } else if resp != utils.OK { t.Error("Unexpected reply returned", resp) @@ -191,7 +191,8 @@ func testTPResRemTPResource(t *testing.T) { func testTPResGetTPResourceAfterRemove(t *testing.T) { var respond *utils.TPResource - if err := tpResRPC.Call("ApierV1.GetTPResource", AttrGetTPStat{TPid: "TPS1", ID: "ResGroup1"}, &respond); err == nil || err.Error() != utils.ErrNotFound.Error() { + if err := tpResRPC.Call("ApierV1.GetTPResource", &utils.TPTntID{TPid: "TPR1", Tenant: "cgrates.org", ID: "ResGroup1"}, + &respond); err == nil || err.Error() != utils.ErrNotFound.Error() { t.Error(err) } } diff --git a/engine/storage_csv.go b/engine/storage_csv.go index db846516c..37b84a88c 100644 --- a/engine/storage_csv.go +++ b/engine/storage_csv.go @@ -537,7 +537,7 @@ func (csvs *CSVStorage) GetTPAliases(filter *utils.TPAliases) ([]*utils.TPAliase } } -func (csvs *CSVStorage) GetTPResources(tpid, id string) ([]*utils.TPResource, error) { +func (csvs *CSVStorage) GetTPResources(tpid, tenant, id string) ([]*utils.TPResource, error) { csvReader, fp, err := csvs.readerFunc(csvs.resProfilesFn, csvs.sep, getColumnCount(TpResource{})) if err != nil { //log.Print("Could not load resource limits file: ", err) diff --git a/engine/storage_interface.go b/engine/storage_interface.go index 2013af84d..c01c2acf9 100644 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -182,7 +182,7 @@ type LoadReader interface { GetTPActionPlans(string, string) ([]*utils.TPActionPlan, error) GetTPActionTriggers(string, string) ([]*utils.TPActionTriggers, error) GetTPAccountActions(*utils.TPAccountActions) ([]*utils.TPAccountActions, error) - GetTPResources(string, string) ([]*utils.TPResource, error) + GetTPResources(string, string, string) ([]*utils.TPResource, error) GetTPStats(string, string) ([]*utils.TPStats, error) GetTPThresholds(string, string) ([]*utils.TPThreshold, error) GetTPFilters(string, string) ([]*utils.TPFilterProfile, error) diff --git a/engine/storage_map_stordb.go b/engine/storage_map_stordb.go index 95e9d3c6b..49178e0d5 100755 --- a/engine/storage_map_stordb.go +++ b/engine/storage_map_stordb.go @@ -73,7 +73,7 @@ func (ms *MapStorage) GetTPActionTriggers(tpid, id string) (aTriggers []*utils.T func (ms *MapStorage) GetTPAccountActions(filter *utils.TPAccountActions) (accounts []*utils.TPAccountActions, err error) { return nil, utils.ErrNotImplemented } -func (ms *MapStorage) GetTPResources(tpid, id string) (resources []*utils.TPResource, err error) { +func (ms *MapStorage) GetTPResources(tpid, tenant, id string) (resources []*utils.TPResource, err error) { return nil, utils.ErrNotImplemented } func (ms *MapStorage) GetTPStats(tpid, id string) (stats []*utils.TPStats, err error) { diff --git a/engine/storage_mongo_stordb.go b/engine/storage_mongo_stordb.go index 55caa9114..c373037d5 100644 --- a/engine/storage_mongo_stordb.go +++ b/engine/storage_mongo_stordb.go @@ -449,11 +449,14 @@ func (ms *MongoStorage) GetTPAliases(tp *utils.TPAliases) ([]*utils.TPAliases, e return results, err } -func (ms *MongoStorage) GetTPResources(tpid, id string) ([]*utils.TPResource, error) { +func (ms *MongoStorage) GetTPResources(tpid, tenant, id string) ([]*utils.TPResource, error) { filter := bson.M{"tpid": tpid} if id != "" { filter["id"] = id } + if tenant != "" { + filter["tenant"] = tenant + } var results []*utils.TPResource err := ms.client.UseSession(ms.ctx, func(sctx mongo.SessionContext) (err error) { cur, err := ms.getCol(utils.TBLTPResources).Find(sctx, filter) diff --git a/engine/storage_sql.go b/engine/storage_sql.go index f37720ff1..17f7d7a80 100644 --- a/engine/storage_sql.go +++ b/engine/storage_sql.go @@ -1474,12 +1474,15 @@ func (self *SQLStorage) GetTPAliases(filter *utils.TPAliases) ([]*utils.TPAliase } } -func (self *SQLStorage) GetTPResources(tpid, id string) ([]*utils.TPResource, error) { +func (self *SQLStorage) GetTPResources(tpid, tenant, id string) ([]*utils.TPResource, error) { var rls TpResources 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(&rls).Error; err != nil { return nil, err } diff --git a/engine/tp_reader.go b/engine/tp_reader.go index 8f26c4433..e4e8d2043 100644 --- a/engine/tp_reader.go +++ b/engine/tp_reader.go @@ -1290,7 +1290,7 @@ func (tpr *TpReader) LoadAliases() error { } func (tpr *TpReader) LoadResourceProfilesFiltered(tag string) (err error) { - rls, err := tpr.lr.GetTPResources(tpr.tpid, tag) + rls, err := tpr.lr.GetTPResources(tpr.tpid, "", tag) if err != nil { return err } diff --git a/engine/tpexporter.go b/engine/tpexporter.go index 4ff46d302..957f1cba3 100644 --- a/engine/tpexporter.go +++ b/engine/tpexporter.go @@ -218,7 +218,7 @@ func (self *TPExporter) Run() error { } } - storDataResources, err := self.storDb.GetTPResources(self.tpID, "") + storDataResources, err := self.storDb.GetTPResources(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 dc5fa8db6..89f0e3076 100644 --- a/engine/tpimporter_csv.go +++ b/engine/tpimporter_csv.go @@ -330,7 +330,7 @@ func (self *TPCSVImporter) importResources(fn string) error { if self.Verbose { log.Printf("Processing file: <%s> ", fn) } - rls, err := self.csvr.GetTPResources(self.TPid, "") + rls, err := self.csvr.GetTPResources(self.TPid, "", "") if err != nil { return err } diff --git a/migrator/tp_resources.go b/migrator/tp_resources.go index 968e55804..b9cff3617 100644 --- a/migrator/tp_resources.go +++ b/migrator/tp_resources.go @@ -39,7 +39,7 @@ func (m *Migrator) migrateCurrentTPresources() (err error) { } for _, id := range ids { - resources, err := m.storDBIn.StorDB().GetTPResources(tpid, id) + resources, err := m.storDBIn.StorDB().GetTPResources(tpid, "", id) if err != nil { return err }