Change DB Name to accept variable 91/102791/2
authorenyinna1234 <enyinna.ochulor@intel.com>
Mon, 2 Mar 2020 17:09:46 +0000 (09:09 -0800)
committerenyinna1234 <enyinna.ochulor@intel.com>
Thu, 5 Mar 2020 12:58:16 +0000 (04:58 -0800)
This enables the initialization of the mongo database name with
a variable.

Issue-ID: MULTICLOUD-996
Signed-off-by: Enyinna Ochulor <enyinna.ochulor@intel.com>
Change-Id: Id3f07b47cedde16235ee7078e1e6f4d287106d29

src/orchestrator/cmd/main.go
src/orchestrator/pkg/infra/db/store.go
src/orchestrator/pkg/infra/db/store_test.go

index 179cf97..a4c46fe 100644 (file)
@@ -34,7 +34,7 @@ func main() {
 
        rand.Seed(time.Now().UnixNano())
 
-       err := db.InitializeDatabaseConnection()
+       err := db.InitializeDatabaseConnection("mco")
        if err != nil {
                log.Println("Unable to initialize database connection...")
                log.Println(err)
index 0cf3ef6..f8cb446 100644 (file)
@@ -64,12 +64,12 @@ type Store interface {
 }
 
 // CreateDBClient creates the DB client
-func createDBClient(dbType string) error {
+func createDBClient(dbType string, dbName string) error {
        var err error
        switch dbType {
        case "mongo":
                // create a mongodb database with orchestrator as the name
-               DBconn, err = NewMongoStore("orchestrator", nil)
+               DBconn, err = NewMongoStore(dbName, nil)
        default:
                return pkgerrors.New(dbType + "DB not supported")
        }
@@ -96,8 +96,8 @@ func DeSerialize(str string, v interface{}) error {
 
 // InitializeDatabaseConnection sets up the connection to the
 // configured database to allow the application to talk to it.
-func InitializeDatabaseConnection() error {
-       err := createDBClient(config.GetConfiguration().DatabaseType)
+func InitializeDatabaseConnection(dbName string) error {
+       err := createDBClient(config.GetConfiguration().DatabaseType, dbName)
        if err != nil {
                return pkgerrors.Cause(err)
        }
index 42a4178..fb23e23 100644 (file)
@@ -23,7 +23,7 @@ func TestCreateDBClient(t *testing.T) {
        t.Run("Successfully create DB client", func(t *testing.T) {
                expected := &MongoStore{}
 
-               err := createDBClient("mongo")
+               err := createDBClient("mongo", "testdb")
                if err != nil {
                        t.Fatalf("CreateDBClient returned an error (%s)", err)
                }
@@ -32,7 +32,7 @@ func TestCreateDBClient(t *testing.T) {
                }
        })
        t.Run("Fail to create client for unsupported DB", func(t *testing.T) {
-               err := createDBClient("fakeDB")
+               err := createDBClient("fakeDB", "testdb2")
                if err == nil {
                        t.Fatal("CreateDBClient didn't return an error")
                }