From 6170f8cbf4e15093550a0be88d8c72e54d379141 Mon Sep 17 00:00:00 2001 From: DanB Date: Thu, 17 Nov 2016 17:03:01 +0100 Subject: [PATCH] TPDestination.DestinationId -> DestinationID --- apier/v1/apier_local_test.go | 14 +++++++------- apier/v1/tpdestinations.go | 5 ++--- apier/v2/tp_it_test.go | 16 ++++++++-------- engine/model_converters.go | 4 ++-- engine/model_helpers_test.go | 2 +- engine/storage_mongo_datadb.go | 32 ++++++++++++++------------------ utils/apitpdata.go | 2 +- 7 files changed, 35 insertions(+), 40 deletions(-) diff --git a/apier/v1/apier_local_test.go b/apier/v1/apier_local_test.go index c212e2da0..5c032e1d0 100644 --- a/apier/v1/apier_local_test.go +++ b/apier/v1/apier_local_test.go @@ -213,12 +213,12 @@ func TestApierTPDestination(t *testing.T) { return } reply := "" - dstDe := &utils.TPDestination{TPid: utils.TEST_SQL, DestinationId: "GERMANY", Prefixes: []string{"+49"}} - dstDeMobile := &utils.TPDestination{TPid: utils.TEST_SQL, DestinationId: "GERMANY_MOBILE", Prefixes: []string{"+4915", "+4916", "+4917"}} - dstFs := &utils.TPDestination{TPid: utils.TEST_SQL, DestinationId: "FS_USERS", Prefixes: []string{"10"}} + dstDe := &utils.TPDestination{TPid: utils.TEST_SQL, DestinationID: "GERMANY", Prefixes: []string{"+49"}} + dstDeMobile := &utils.TPDestination{TPid: utils.TEST_SQL, DestinationID: "GERMANY_MOBILE", Prefixes: []string{"+4915", "+4916", "+4917"}} + dstFs := &utils.TPDestination{TPid: utils.TEST_SQL, DestinationID: "FS_USERS", Prefixes: []string{"10"}} dstDe2 := new(utils.TPDestination) *dstDe2 = *dstDe // Data which we use for remove, still keeping the sample data to check proper loading - dstDe2.DestinationId = "GERMANY2" + dstDe2.DestinationID = "GERMANY2" for _, dst := range []*utils.TPDestination{dstDe, dstDeMobile, dstFs, dstDe2} { if err := rater.Call("ApierV1.SetTPDestination", dst, &reply); err != nil { t.Error("Got error on ApierV1.SetTPDestination: ", err.Error()) @@ -235,18 +235,18 @@ func TestApierTPDestination(t *testing.T) { // Check missing params if err := rater.Call("ApierV1.SetTPDestination", new(utils.TPDestination), &reply); err == nil { t.Error("Calling ApierV1.SetTPDestination, expected error, received: ", reply) - } else if err.Error() != "MANDATORY_IE_MISSING:[TPid DestinationId Prefixes]" { + } else if err.Error() != "MANDATORY_IE_MISSING:[TPid DestinationID Prefixes]" { t.Error("Calling ApierV1.SetTPDestination got unexpected error: ", err.Error()) } // Test get var rplyDstDe2 *utils.TPDestination - if err := rater.Call("ApierV1.GetTPDestination", AttrGetTPDestination{dstDe2.TPid, dstDe2.DestinationId}, &rplyDstDe2); err != nil { + if err := rater.Call("ApierV1.GetTPDestination", AttrGetTPDestination{dstDe2.TPid, dstDe2.DestinationID}, &rplyDstDe2); err != nil { t.Error("Calling ApierV1.GetTPDestination, got error: ", err.Error()) } else if !reflect.DeepEqual(dstDe2, rplyDstDe2) { t.Errorf("Calling ApierV1.GetTPDestination expected: %v, received: %v", dstDe2, rplyDstDe2) } // Test remove - if err := rater.Call("ApierV1.RemTPDestination", AttrGetTPDestination{dstDe2.TPid, dstDe2.DestinationId}, &reply); err != nil { + if err := rater.Call("ApierV1.RemTPDestination", AttrGetTPDestination{dstDe2.TPid, dstDe2.DestinationID}, &reply); err != nil { t.Error("Calling ApierV1.RemTPTiming, got error: ", err.Error()) } else if reply != "OK" { t.Error("Calling ApierV1.RemTPTiming received: ", reply) diff --git a/apier/v1/tpdestinations.go b/apier/v1/tpdestinations.go index d6f727bdc..1bf1299a2 100644 --- a/apier/v1/tpdestinations.go +++ b/apier/v1/tpdestinations.go @@ -24,7 +24,7 @@ import ( // Creates a new destination within a tariff plan func (self *ApierV1) SetTPDestination(attrs utils.TPDestination, reply *string) error { - if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DestinationId", "Prefixes"}); len(missing) != 0 { //Params missing + if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DestinationID", "Prefixes"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } ds := engine.APItoModelDestination(&attrs) @@ -46,7 +46,6 @@ func (self *ApierV1) GetTPDestination(attrs AttrGetTPDestination, reply *utils.T return utils.NewErrMandatoryIeMissing(missing...) } if storData, err := self.StorDb.GetTpDestinations(attrs.TPid, attrs.DestinationId); err != nil { - return utils.NewErrServerError(err) } else if len(storData) == 0 { return utils.ErrNotFound @@ -57,7 +56,7 @@ func (self *ApierV1) GetTPDestination(attrs AttrGetTPDestination, reply *utils.T } *reply = utils.TPDestination{ TPid: attrs.TPid, - DestinationId: dsts[attrs.DestinationId].Id, + DestinationID: dsts[attrs.DestinationId].Id, Prefixes: dsts[attrs.DestinationId].Prefixes} } return nil diff --git a/apier/v2/tp_it_test.go b/apier/v2/tp_it_test.go index 517b2dab8..9a274383c 100644 --- a/apier/v2/tp_it_test.go +++ b/apier/v2/tp_it_test.go @@ -207,18 +207,18 @@ func testTPitTimings(t *testing.T) { func testTPitDestinations(t *testing.T) { var reply string // DST_1002,1002 - dst1002 := &utils.TPDestination{TPid: testTPid, DestinationId: "DST_1002", Prefixes: []string{"1002"}} + dst1002 := &utils.TPDestination{TPid: testTPid, DestinationID: "DST_1002", Prefixes: []string{"1002"}} // DST_1003,1003 - dst1003 := &utils.TPDestination{TPid: testTPid, DestinationId: "DST_1003", Prefixes: []string{"1003"}} + dst1003 := &utils.TPDestination{TPid: testTPid, DestinationID: "DST_1003", Prefixes: []string{"1003"}} // DST_1007,1007 - dst1007 := &utils.TPDestination{TPid: testTPid, DestinationId: "DST_1007", Prefixes: []string{"1007"}} + dst1007 := &utils.TPDestination{TPid: testTPid, DestinationID: "DST_1007", Prefixes: []string{"1007"}} // DST_FS,10 - dstFS := &utils.TPDestination{TPid: testTPid, DestinationId: "DST_FS", Prefixes: []string{"10"}} + dstFS := &utils.TPDestination{TPid: testTPid, DestinationID: "DST_FS", Prefixes: []string{"10"}} // DST_DE_MOBILE,+49151 // DST_DE_MOBILE,+49161 // DST_DE_MOBILE,+49171 - dstDEMobile := &utils.TPDestination{TPid: testTPid, DestinationId: "DST_DE_MOBILE", Prefixes: []string{"+49151", "+49161", "+49171"}} - dstDUMMY := &utils.TPDestination{TPid: testTPid, DestinationId: "DUMMY_REMOVE", Prefixes: []string{"999"}} + dstDEMobile := &utils.TPDestination{TPid: testTPid, DestinationID: "DST_DE_MOBILE", Prefixes: []string{"+49151", "+49161", "+49171"}} + dstDUMMY := &utils.TPDestination{TPid: testTPid, DestinationID: "DUMMY_REMOVE", Prefixes: []string{"999"}} for _, dst := range []*utils.TPDestination{dst1002, dst1003, dst1007, dstFS, dstDEMobile, dstDUMMY} { if err := tpRPC.Call("ApierV2.SetTPDestination", dst, &reply); err != nil { t.Error("Got error on ApierV2.SetTPDestination: ", err.Error()) @@ -228,13 +228,13 @@ func testTPitDestinations(t *testing.T) { } // Test get var rplyDst *utils.TPDestination - if err := tpRPC.Call("ApierV2.GetTPDestination", v1.AttrGetTPDestination{testTPid, dstDEMobile.DestinationId}, &rplyDst); err != nil { + if err := tpRPC.Call("ApierV2.GetTPDestination", v1.AttrGetTPDestination{testTPid, dstDEMobile.DestinationID}, &rplyDst); err != nil { t.Error("Calling ApierV2.GetTPDestination, got error: ", err.Error()) } else if len(dstDEMobile.Prefixes) != len(rplyDst.Prefixes) { t.Errorf("Calling ApierV2.GetTPDestination expected: %v, received: %v", dstDEMobile, rplyDst) } // Test remove - if err := tpRPC.Call("ApierV2.RemTPDestination", v1.AttrGetTPDestination{testTPid, dstDUMMY.DestinationId}, &reply); err != nil { + if err := tpRPC.Call("ApierV2.RemTPDestination", v1.AttrGetTPDestination{testTPid, dstDUMMY.DestinationID}, &reply); err != nil { t.Error("Calling ApierV1.RemTPTiming, got error: ", err.Error()) } else if reply != utils.OK { t.Error("Calling ApierV2.RemTPTiming received: ", reply) diff --git a/engine/model_converters.go b/engine/model_converters.go index 2e460b38d..071a3c335 100644 --- a/engine/model_converters.go +++ b/engine/model_converters.go @@ -51,14 +51,14 @@ func APItoModelDestination(dest *utils.TPDestination) (result []TpDestination) { for _, p := range dest.Prefixes { result = append(result, TpDestination{ Tpid: dest.TPid, - Tag: dest.DestinationId, + Tag: dest.DestinationID, Prefix: p, }) } if len(dest.Prefixes) == 0 { result = append(result, TpDestination{ Tpid: dest.TPid, - Tag: dest.DestinationId, + Tag: dest.DestinationID, }) } return diff --git a/engine/model_helpers_test.go b/engine/model_helpers_test.go index 89d9e483e..7f013edc6 100644 --- a/engine/model_helpers_test.go +++ b/engine/model_helpers_test.go @@ -53,7 +53,7 @@ func TestModelHelperCsvDump(t *testing.T) { func TestTPDestinationAsExportSlice(t *testing.T) { tpDst := &utils.TPDestination{ TPid: "TEST_TPID", - DestinationId: "TEST_DEST", + DestinationID: "TEST_DEST", Prefixes: []string{"49", "49176", "49151"}, } expectedSlc := [][]string{ diff --git a/engine/storage_mongo_datadb.go b/engine/storage_mongo_datadb.go index d56eb75be..4c0627508 100644 --- a/engine/storage_mongo_datadb.go +++ b/engine/storage_mongo_datadb.go @@ -95,7 +95,15 @@ func NewMongoStorage(host, port, db, user, pass, storageType string, cdrsIndexes return nil, err } session.SetMode(mgo.Strong, true) - return &MongoStorage{db: db, session: session, storageType: storageType, ms: NewCodecMsgpackMarshaler(), cacheCfg: cacheCfg, loadHistorySize: loadHistorySize, cdrsIndexes: cdrsIndexes}, nil + ms = &MongoStorage{db: db, session: session, storageType: storageType, ms: NewCodecMsgpackMarshaler(), cacheCfg: cacheCfg, loadHistorySize: loadHistorySize, cdrsIndexes: cdrsIndexes} + if cNames, err := session.DB(ms.db).CollectionNames(); err != nil { + return nil, err + } else if len(cNames) == 0 { // create indexes only if database is empty + if err = ms.EnsureIndexes(); err != nil { + return nil, err + } + } + return } type MongoStorage struct { @@ -113,8 +121,9 @@ func (ms *MongoStorage) conn(col string) (*mgo.Session, *mgo.Collection) { return sessionCopy, sessionCopy.DB(ms.db).C(col) } +// EnsureIndexes creates db indexes func (ms *MongoStorage) EnsureIndexes() (err error) { - dbSession, _ := ms.conn("") + dbSession := ms.session.Copy() defer dbSession.Close() db := dbSession.DB(ms.db) idx := mgo.Index{ @@ -305,22 +314,9 @@ func (ms *MongoStorage) Close() { } func (ms *MongoStorage) Flush(ignore string) (err error) { - session := ms.session.Copy() - defer session.Close() - db := session.DB(ms.db) - collections, err := db.CollectionNames() - if err != nil { - return err - } - for _, c := range collections { - if strings.HasPrefix(c, "system.") { // cannot drop system ns due to mongo errors - continue - } - if _, err = db.C(c).RemoveAll(bson.M{}); err != nil { - return err - } - } - return nil + dbSession := ms.session.Copy() + defer dbSession.Close() + return dbSession.DB(ms.db).DropDatabase() } func (ms *MongoStorage) RebuildReverseForPrefix(prefix string) error { diff --git a/utils/apitpdata.go b/utils/apitpdata.go index b833b3ee3..eb9eabbbc 100644 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -40,7 +40,7 @@ type Paginator struct { type TPDestination struct { TPid string // Tariff plan id - DestinationId string // Destination id + DestinationID string // Destination id Prefixes []string // Prefixes attached to this destination }