deprecated-oc-catalog/services/mongo.go

164 lines
4.0 KiB
Go
Raw Normal View History

2023-03-03 14:43:11 +01:00
package services
import (
"context"
"time"
"cloud.o-forge.io/core/oc-catalog/conf"
2023-03-03 14:43:11 +01:00
"github.com/beego/beego/v2/core/logs"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx"
)
type MongoCollectionNames struct {
DATA string
COMPUTING string
STORAGE string
DATACENTER string
WORKSPACE string
SCHEDULE string
}
var (
mngoClient *mongo.Client
mngoDB *mongo.Database
MngoCtx context.Context
MngoNamesCollection = MongoCollectionNames{
DATA: "datas",
COMPUTING: "computings",
STORAGE: "storages",
DATACENTER: "datacenters",
WORKSPACE: "WORKSPACE",
SCHEDULE: "SCHEDULE",
}
MngoCollData *mongo.Collection
MngoCollComputing *mongo.Collection
MngoCollStorage *mongo.Collection
MngoCollDatacenter *mongo.Collection
MngoCollWorkspace *mongo.Collection
MngoCollSchedule *mongo.Collection
)
// func GetMongoDBclient() *mongo.Client {
// return mongoDBclient
// }
func MongoDisconnect() {
if err := mngoClient.Disconnect(MngoCtx); err != nil {
panic(err)
}
}
func Mongoinit(DBname string) {
// var baseConfig string
var err error
MongoURL := conf.GetConfig().MongoURL
// if len(os.Getenv("DOCKER_ENVIRONMENT")) == 0 {
// baseConfig = "mongodb"
// } else {
// baseConfig = "mongodb_docker"
// }
2023-03-03 14:43:11 +01:00
// MongoURL, err := beego.AppConfig.String(baseConfig + "::url")
// if err != nil {
// logs.Critical("MongoDB URI error: %v", err)
2023-03-03 14:43:11 +01:00
// panic(err)
// }
logs.Info("Connecting to %v", MongoURL)
clientOptions := options.Client().ApplyURI(MongoURL)
mngoClient, _ = mongo.NewClient(options.Client().ApplyURI(MongoURL))
if err = mngoClient.Connect(MngoCtx); err != nil {
logs.Critical("Mongodb NewClient %v: %v", MongoURL, err)
panic(err)
}
2023-03-03 14:43:11 +01:00
MngoCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Ping the primary
if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil {
logs.Critical("Mongodb Connect %v: %v", MongoURL, err)
2023-03-03 14:43:11 +01:00
panic(err)
}
if err = mngoClient.Ping(MngoCtx, nil); err != nil {
logs.Critical("Mongodb Ping %v: %v", MongoURL, err)
2023-03-03 14:43:11 +01:00
panic(err)
}
2024-02-15 10:52:29 +01:00
logs.Info("Connecting mongo client to db %v", DBname)
2023-03-03 14:43:11 +01:00
mngoDB = mngoClient.Database(DBname)
MngoCollData = mngoDB.Collection(MngoNamesCollection.DATA)
MngoCollComputing = mngoDB.Collection(MngoNamesCollection.COMPUTING)
MngoCollStorage = mngoDB.Collection(MngoNamesCollection.STORAGE)
MngoCollDatacenter = mngoDB.Collection(MngoNamesCollection.DATACENTER)
MngoCollWorkspace = mngoDB.Collection(MngoNamesCollection.WORKSPACE)
MngoCollSchedule = mngoDB.Collection(MngoNamesCollection.SCHEDULE)
if _, err = MngoCollData.Indexes().CreateMany(MngoCtx, []mongo.IndexModel{
{
Keys: bsonx.Doc{
{Key: "description", Value: bsonx.String("text")},
{Key: "example", Value: bsonx.String("text")},
},
},
}); err != nil && err.(mongo.CommandError).Code != 85 {
logs.Critical(err)
panic(err)
}
if _, err = MngoCollComputing.Indexes().CreateMany(MngoCtx, []mongo.IndexModel{
{
Keys: bsonx.Doc{
{Key: "description", Value: bsonx.String("text")},
{Key: "owner", Value: bsonx.String("text")},
{Key: "license", Value: bsonx.String("text")},
},
},
}); err != nil && err.(mongo.CommandError).Code != 85 {
logs.Critical(err)
panic(err)
}
if _, err = MngoCollStorage.Indexes().CreateMany(MngoCtx, []mongo.IndexModel{
{
Keys: bsonx.Doc{
{Key: "name", Value: bsonx.String("text")},
{Key: "description", Value: bsonx.String("text")},
},
},
}); err != nil && err.(mongo.CommandError).Code != 85 {
logs.Critical(err)
panic(err)
}
if _, err = MngoCollDatacenter.Indexes().CreateMany(MngoCtx, []mongo.IndexModel{
{
Keys: bsonx.Doc{
{Key: "name", Value: bsonx.String("text")},
{Key: "description", Value: bsonx.String("text")},
{Key: "owner", Value: bsonx.String("text")},
},
},
}); err != nil && err.(mongo.CommandError).Code != 85 {
logs.Critical(err)
panic(err)
}
logs.Info("Database is READY")
}