oc-lib/dbs/mongo/mongo.go

193 lines
5.7 KiB
Go
Raw Normal View History

2024-07-17 18:02:30 +02:00
package mongo
import (
"context"
"errors"
"time"
2024-07-18 13:35:14 +02:00
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/logs"
"github.com/rs/zerolog"
2024-07-17 18:02:30 +02:00
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
2024-07-18 11:51:12 +02:00
mngoClient *mongo.Client
mngoDB *mongo.Database
MngoCtx context.Context
cancel context.CancelFunc
2024-07-17 18:02:30 +02:00
2024-07-18 11:51:12 +02:00
existingCollections []string
2024-07-17 18:02:30 +02:00
2024-07-18 11:51:12 +02:00
ResourceMap map[string]interface{}
2024-07-17 18:02:30 +02:00
)
2024-07-18 13:35:14 +02:00
var MONGOService = MongoDB{}
2024-07-18 14:11:13 +02:00
type MongoConf interface {
GetUrl() string
GetDatabase() string
}
2024-07-18 13:35:14 +02:00
type MongoDB struct {
Logger zerolog.Logger
}
2024-07-18 14:11:13 +02:00
func (m *MongoDB) Init(collections []string, config MongoConf) {
2024-07-17 18:02:30 +02:00
// var baseConfig string
2024-07-18 17:55:27 +02:00
m.Logger = logs.GetLogger()
2024-07-17 18:02:30 +02:00
ResourceMap = make(map[string]interface{})
2024-07-18 11:51:12 +02:00
2024-07-18 17:55:27 +02:00
m.Logger.Info().Msg("Connecting to" + config.GetUrl())
2024-07-17 18:02:30 +02:00
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
2024-07-18 14:11:13 +02:00
m.createClient(config.GetUrl())
2024-07-17 18:02:30 +02:00
2024-07-18 17:55:27 +02:00
m.Logger.Info().Msg("Connecting mongo client to db " + config.GetDatabase())
2024-07-18 14:11:13 +02:00
m.prepareDB(collections, config)
2024-07-17 18:02:30 +02:00
2024-07-18 17:55:27 +02:00
m.Logger.Info().Msg("Database is READY")
2024-07-17 18:02:30 +02:00
}
2024-07-18 13:35:14 +02:00
func (m *MongoDB) createClient(MongoURL string) {
2024-07-17 18:02:30 +02:00
var err error
// Allows us to use marshal and unmarshall with results of FindOne() and others
2024-07-18 11:51:12 +02:00
bsonOpts := &options.BSONOptions{
2024-07-17 18:02:30 +02:00
UseJSONStructTags: true,
2024-07-18 11:51:12 +02:00
NilSliceAsEmpty: true,
2024-07-17 18:02:30 +02:00
}
clientOptions := options.Client().ApplyURI(MongoURL).SetBSONOptions(bsonOpts)
2024-07-18 11:51:12 +02:00
mngoClient, err = mongo.Connect(MngoCtx, clientOptions)
2024-07-17 18:02:30 +02:00
if err != nil {
2024-07-18 13:35:14 +02:00
m.Logger.Fatal().Msg("Mongodb NewClient " + MongoURL + ":" + "err")
2024-07-17 18:02:30 +02:00
panic(err)
}
// Ping the primary
if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil {
2024-07-18 13:35:14 +02:00
m.Logger.Fatal().Msg("Mongodb connect " + MongoURL + ":" + "err")
2024-07-17 18:02:30 +02:00
panic(err)
}
if err = mngoClient.Ping(MngoCtx, nil); err != nil {
2024-07-18 13:35:14 +02:00
m.Logger.Fatal().Msg("Mongodb ping " + MongoURL + ":" + "err")
2024-07-17 18:02:30 +02:00
panic(err)
}
}
2024-07-18 14:11:13 +02:00
func (m *MongoDB) prepareDB(list_collection []string, config MongoConf) {
2024-07-17 18:02:30 +02:00
var err error
2024-07-18 14:11:13 +02:00
mngoDB = mngoClient.Database(config.GetDatabase())
2024-07-18 11:51:12 +02:00
existingCollections, err = mngoDB.ListCollectionNames(MngoCtx, bson.D{})
2024-07-17 18:02:30 +02:00
if err != nil {
2024-07-18 13:35:14 +02:00
m.Logger.Fatal().Msg("Error contacting MongoDB\n" + err.Error())
2024-07-17 18:02:30 +02:00
}
collectionMap := make(map[string]bool)
2024-07-18 11:51:12 +02:00
for _, name := range existingCollections {
collectionMap[name] = true
}
2024-07-17 18:02:30 +02:00
// Only do the collection definition process if it doesn't already exists
// we add the collection to the collection map from mongo/mongo_utils to provide faster access to the collection
2024-07-18 11:51:12 +02:00
for _, collection_name := range list_collection {
2024-07-17 18:02:30 +02:00
new_collection := mngoDB.Collection(collection_name)
if _, exists := collectionMap[collection_name]; !exists {
2024-07-18 13:35:14 +02:00
m.createCollection(collection_name, new_collection)
2024-07-18 11:51:12 +02:00
} else {
2024-07-17 18:02:30 +02:00
CollectionMap[collection_name] = new_collection
}
2024-07-18 11:51:12 +02:00
}
2024-07-17 18:02:30 +02:00
}
// Creates the collection with index specified in mongo/mongo_collections
// or use the basic collection creation function
2024-07-18 13:35:14 +02:00
func (m *MongoDB) createCollection(collection_name string, new_collection *mongo.Collection) {
2024-07-18 11:51:12 +02:00
var err error
2024-07-17 18:02:30 +02:00
CollectionMap[collection_name] = new_collection
2024-07-18 11:51:12 +02:00
_, exists := IndexesMap[collection_name]
if exists {
2024-07-17 18:02:30 +02:00
if _, err = new_collection.Indexes().CreateMany(MngoCtx, IndexesMap[collection_name]); err != nil {
var cmdErr mongo.CommandError
if errors.As(err, &cmdErr) && cmdErr.Code != 85 {
2024-07-18 13:35:14 +02:00
m.Logger.Fatal().Msg("Error creating indexes for " + collection_name + " collection : \n" + err.Error())
2024-07-17 18:02:30 +02:00
panic(err)
} else if !errors.As(err, &cmdErr) {
2024-07-18 13:35:14 +02:00
m.Logger.Fatal().Msg("Unexpected error: " + err.Error())
2024-07-17 18:02:30 +02:00
panic(err)
}
}
} else {
mngoDB.CreateCollection(MngoCtx, collection_name)
}
}
2024-07-19 11:27:58 +02:00
func (m *MongoDB) DeleteOne(id string, collection_name string) (int64, int, error) {
2024-07-18 11:51:12 +02:00
filter := bson.M{"_id": GetObjIDFromString(id)}
targetDBCollection := CollectionMap[collection_name]
opts := options.Delete().SetHint(bson.D{{Key: "_id", Value: 1}})
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
result, err := targetDBCollection.DeleteOne(MngoCtx, filter, opts)
if err != nil {
2024-07-18 13:35:14 +02:00
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
2024-07-19 11:27:58 +02:00
return 0, 404, err
2024-07-18 11:51:12 +02:00
}
2024-07-19 11:27:58 +02:00
return result.DeletedCount, 200, nil
2024-07-18 11:51:12 +02:00
}
2024-07-19 11:27:58 +02:00
func (m *MongoDB) UpdateOne(set map[string]interface{}, id string, collection_name string) (string, int, error) {
2024-07-18 11:51:12 +02:00
filter := bson.M{"_id": GetObjIDFromString(id)}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
result, err := targetDBCollection.UpdateOne(MngoCtx, filter, dbs.InputToBson(set, true))
if err != nil {
2024-07-19 11:27:58 +02:00
m.Logger.Error().Msg("Couldn't update resource: " + err.Error())
return "", 404, err
2024-07-18 11:51:12 +02:00
}
2024-07-19 11:27:58 +02:00
return result.UpsertedID.(primitive.ObjectID).Hex(), 200, nil
2024-07-18 11:51:12 +02:00
}
2024-07-19 11:27:58 +02:00
func (m *MongoDB) StoreOne(obj interface{}, collection_name string) (string, int, error) {
2024-07-17 18:02:30 +02:00
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
2024-07-18 11:51:12 +02:00
result, err := targetDBCollection.InsertOne(MngoCtx, obj)
2024-07-17 18:02:30 +02:00
if err != nil {
2024-07-18 13:35:14 +02:00
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
2024-07-19 11:27:58 +02:00
return "", 409, err
2024-07-17 18:02:30 +02:00
}
2024-07-19 11:27:58 +02:00
return result.InsertedID.(primitive.ObjectID).Hex(), 200, nil
2024-07-17 18:02:30 +02:00
}
2024-07-19 11:27:58 +02:00
func (m *MongoDB) LoadOne(id string, collection_name string) (*mongo.SingleResult, int, error) {
2024-07-17 18:02:30 +02:00
filter := bson.M{"_id": GetObjIDFromString(id)}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
2024-07-19 11:27:58 +02:00
res := targetDBCollection.FindOne(MngoCtx, filter)
2024-07-17 18:02:30 +02:00
if res.Err() != nil {
2024-07-18 13:35:14 +02:00
m.Logger.Error().Msg("Couldn't find resource " + id + ". Error : " + res.Err().Error())
2024-07-19 11:27:58 +02:00
err := res.Err()
return nil, 404, err
2024-07-17 18:02:30 +02:00
}
2024-07-19 11:27:58 +02:00
return res, 200, nil
2024-07-17 18:02:30 +02:00
}