module name

This commit is contained in:
mr 2024-07-18 13:35:14 +02:00
parent 7d5bdadbef
commit 1999213f8b
19 changed files with 103 additions and 132 deletions

View File

@ -4,11 +4,14 @@ import (
"context"
"encoding/json"
"errors"
lib "oc-lib"
"oc-lib/dbs"
"os"
"time"
lib "cloud.o-forge.io/core/oc-lib"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/logs"
"github.com/rs/zerolog"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
@ -26,39 +29,45 @@ var (
ResourceMap map[string]interface{}
)
func Init(collections []string) {
var MONGOService = MongoDB{}
type MongoDB struct {
Logger zerolog.Logger
}
func (m *MongoDB) Init(collections []string) {
// var baseConfig string
var err error
var conf map[string]string
var DBname string
m.Logger = logs.CreateLogger("oc-lib", "")
ResourceMap = make(map[string]interface{})
lib.Logger = lib.CreateLogger("oclib", "")
logger := logs.CreateLogger("oclib", "")
db_conf, err := os.ReadFile("tests/oclib_conf.json")
if err != nil {
lib.Logger.Fatal().Msg("Could not find configuration file")
logger.Fatal().Msg("Could not find configuration file")
}
json.Unmarshal(db_conf, &conf)
DBname = lib.GetConfig().DCNAME + "-" + lib.GetConfig().DBPOINT
lib.Logger.Info().Msg("Connecting to" + lib.GetConfig().MongoURL)
logger.Info().Msg("Connecting to" + lib.GetConfig().MongoURL)
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
createClient(lib.GetConfig().MongoURL)
m.createClient(lib.GetConfig().MongoURL)
lib.Logger.Info().Msg("Connecting mongo client to db " + DBname)
prepareDB(collections, lib.GetConfig().DCNAME, lib.GetConfig().DBPOINT)
logger.Info().Msg("Connecting mongo client to db " + DBname)
m.prepareDB(collections, lib.GetConfig().DCNAME, lib.GetConfig().DBPOINT)
lib.Logger.Info().Msg("Database is READY")
logger.Info().Msg("Database is READY")
}
func createClient(MongoURL string) {
func (m *MongoDB) createClient(MongoURL string) {
var err error
@ -71,30 +80,30 @@ func createClient(MongoURL string) {
clientOptions := options.Client().ApplyURI(MongoURL).SetBSONOptions(bsonOpts)
mngoClient, err = mongo.Connect(MngoCtx, clientOptions)
if err != nil {
lib.Logger.Fatal().Msg("Mongodb NewClient " + MongoURL + ":" + "err")
m.Logger.Fatal().Msg("Mongodb NewClient " + MongoURL + ":" + "err")
panic(err)
}
// Ping the primary
if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil {
lib.Logger.Fatal().Msg("Mongodb connect " + MongoURL + ":" + "err")
m.Logger.Fatal().Msg("Mongodb connect " + MongoURL + ":" + "err")
panic(err)
}
if err = mngoClient.Ping(MngoCtx, nil); err != nil {
lib.Logger.Fatal().Msg("Mongodb ping " + MongoURL + ":" + "err")
m.Logger.Fatal().Msg("Mongodb ping " + MongoURL + ":" + "err")
panic(err)
}
}
func prepareDB(list_collection []string, dc_name string, db_point string) {
func (m *MongoDB) prepareDB(list_collection []string, dc_name string, db_point string) {
var err error
DBname := dc_name + "-" + db_point
mngoDB = mngoClient.Database(DBname)
existingCollections, err = mngoDB.ListCollectionNames(MngoCtx, bson.D{})
if err != nil {
lib.Logger.Fatal().Msg("Error contacting MongoDB\n" + err.Error())
m.Logger.Fatal().Msg("Error contacting MongoDB\n" + err.Error())
}
collectionMap := make(map[string]bool)
for _, name := range existingCollections {
@ -105,17 +114,16 @@ func prepareDB(list_collection []string, dc_name string, db_point string) {
for _, collection_name := range list_collection {
new_collection := mngoDB.Collection(collection_name)
if _, exists := collectionMap[collection_name]; !exists {
createCollection(collection_name, new_collection)
m.createCollection(collection_name, new_collection)
} else {
CollectionMap[collection_name] = new_collection
}
}
}
// Creates the collection with index specified in mongo/mongo_collections
// or use the basic collection creation function
func createCollection(collection_name string, new_collection *mongo.Collection) {
func (m *MongoDB) createCollection(collection_name string, new_collection *mongo.Collection) {
var err error
CollectionMap[collection_name] = new_collection
_, exists := IndexesMap[collection_name]
@ -123,10 +131,10 @@ func createCollection(collection_name string, new_collection *mongo.Collection)
if _, err = new_collection.Indexes().CreateMany(MngoCtx, IndexesMap[collection_name]); err != nil {
var cmdErr mongo.CommandError
if errors.As(err, &cmdErr) && cmdErr.Code != 85 {
lib.Logger.Fatal().Msg("Error creating indexes for " + collection_name + " collection : \n" + err.Error())
m.Logger.Fatal().Msg("Error creating indexes for " + collection_name + " collection : \n" + err.Error())
panic(err)
} else if !errors.As(err, &cmdErr) {
lib.Logger.Fatal().Msg("Unexpected error: " + err.Error())
m.Logger.Fatal().Msg("Unexpected error: " + err.Error())
panic(err)
}
}
@ -136,7 +144,7 @@ func createCollection(collection_name string, new_collection *mongo.Collection)
}
func DeleteOne(id string, collection_name string) (int64, error) {
func (m *MongoDB) DeleteOne(id string, collection_name string) (int64, error) {
filter := bson.M{"_id": GetObjIDFromString(id)}
targetDBCollection := CollectionMap[collection_name]
opts := options.Delete().SetHint(bson.D{{Key: "_id", Value: 1}})
@ -145,13 +153,13 @@ func DeleteOne(id string, collection_name string) (int64, error) {
result, err := targetDBCollection.DeleteOne(MngoCtx, filter, opts)
if err != nil {
lib.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
return 0, err
}
return result.DeletedCount, nil
}
func UpdateOne(set map[string]interface{}, id string, collection_name string) (string, error) {
func (m *MongoDB) UpdateOne(set map[string]interface{}, id string, collection_name string) (string, error) {
filter := bson.M{"_id": GetObjIDFromString(id)}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
@ -159,13 +167,13 @@ func UpdateOne(set map[string]interface{}, id string, collection_name string) (s
result, err := targetDBCollection.UpdateOne(MngoCtx, filter, dbs.InputToBson(set, true))
if err != nil {
lib.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
return "", err
}
return result.UpsertedID.(primitive.ObjectID).Hex(), nil
}
func StoreOne(obj interface{}, collection_name string) (string, error) {
func (m *MongoDB) StoreOne(obj interface{}, collection_name string) (string, error) {
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
@ -173,14 +181,14 @@ func StoreOne(obj interface{}, collection_name string) (string, error) {
result, err := targetDBCollection.InsertOne(MngoCtx, obj)
if err != nil {
lib.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
return "", err
}
return result.InsertedID.(primitive.ObjectID).Hex(), nil
}
func LoadOne(id string, collection_name string) (res *mongo.SingleResult, err error) {
func (m *MongoDB) LoadOne(id string, collection_name string) (res *mongo.SingleResult, err error) {
filter := bson.M{"_id": GetObjIDFromString(id)}
targetDBCollection := CollectionMap[collection_name]
@ -189,7 +197,7 @@ func LoadOne(id string, collection_name string) (res *mongo.SingleResult, err er
res = targetDBCollection.FindOne(MngoCtx, filter)
if res.Err() != nil {
lib.Logger.Error().Msg("Couldn't find resource " + id + ". Error : " + res.Err().Error())
m.Logger.Error().Msg("Couldn't find resource " + id + ". Error : " + res.Err().Error())
err = res.Err()
return nil, err
}

View File

@ -1,41 +0,0 @@
package oclib
import (
"oc-lib/logs"
"os"
"runtime"
"time"
"github.com/rs/zerolog"
)
var Logger zerolog.Logger
// CreateLogger
// Create a new logger
// Parameters:
// - appname: string : the name of the application using oclib
// - url: string : the url of a loki logger, console log only if ""
// Returns:
// - zerolog.Logger : the logger that will log for the library and the app
func CreateLogger(appname string, url string) zerolog.Logger {
if url != "" {
labels := map[string]string{
"app": "app",
"code": "go",
"platform": runtime.GOOS,
}
lokiWriter := logs.NewLokiWriter(url, labels)
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
multiWriter := zerolog.MultiLevelWriter(consoleWriter, lokiWriter)
Logger = zerolog.New(multiWriter).With().Timestamp().Logger()
} else {
Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
return Logger
}

View File

@ -1,15 +1,15 @@
package models
import (
oclib "oc-lib"
"oc-lib/dbs/mongo"
r "oc-lib/models/resources"
d "oc-lib/models/resources/data"
dc "oc-lib/models/resources/datacenter"
p "oc-lib/models/resources/processing"
s "oc-lib/models/resources/storage"
"oc-lib/models/utils"
w "oc-lib/models/workflow"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs"
r "cloud.o-forge.io/core/oc-lib/models/resources"
d "cloud.o-forge.io/core/oc-lib/models/resources/data"
dc "cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
p "cloud.o-forge.io/core/oc-lib/models/resources/processing"
s "cloud.o-forge.io/core/oc-lib/models/resources/storage"
"cloud.o-forge.io/core/oc-lib/models/utils"
w "cloud.o-forge.io/core/oc-lib/models/workflow"
)
var models = map[string]utils.DBObject{
@ -21,7 +21,7 @@ var models = map[string]utils.DBObject{
}
func Model(model string) utils.DBObject {
log := oclib.CreateLogger("oclib", "")
log := logs.CreateLogger("oclib", "")
if _, ok := models[model]; ok {
return models[model]
}
@ -34,5 +34,5 @@ func Init() {
for db := range models {
dbs = append(dbs, db)
}
mongo.Init(dbs)
mongo.MONGOService.Init(dbs)
}

View File

@ -1,8 +1,8 @@
package data
import (
resources "oc-lib/models/resources"
"oc-lib/models/utils"
resources "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type Data struct {

View File

@ -1,8 +1,8 @@
package data
import (
mongo "oc-lib/dbs/mongo"
"oc-lib/models/utils"
mongo "cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type DataMongoAccessor struct {
@ -18,7 +18,7 @@ func (dma *DataMongoAccessor) UpdateOne(set map[string]interface{}, id string) u
}
func (dma *DataMongoAccessor) StoreOne(data utils.DBObject) utils.DBObject {
id, err := mongo.StoreOne(data.(*Data), "data")
id, err := mongo.MONGOService.StoreOne(data.(*Data), "data")
if err != nil {
dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return &Data{}
@ -28,7 +28,7 @@ func (dma *DataMongoAccessor) StoreOne(data utils.DBObject) utils.DBObject {
func (dma *DataMongoAccessor) LoadOne(id string) utils.DBObject {
var data Data
res_mongo, err := mongo.LoadOne(id, "data")
res_mongo, err := mongo.MONGOService.LoadOne(id, "data")
if err != nil {
dma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return &Data{}

View File

@ -1,9 +1,10 @@
package data
import (
resources "oc-lib/models/resources"
"testing"
resources "cloud.o-forge.io/core/oc-lib/models/resources"
"github.com/stretchr/testify/assert"
)

View File

@ -1,8 +1,8 @@
package datacenter
import (
resources "oc-lib/models/resources"
"oc-lib/models/utils"
resources "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type Datacenter struct {

View File

@ -1,9 +1,9 @@
package datacenter
import (
"oc-lib/dbs/mongo"
logs "oc-lib/logs"
"oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
logs "cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type DatacenterMongoAccessor struct {
@ -19,7 +19,7 @@ func (dma *DatacenterMongoAccessor) UpdateOne(set map[string]interface{}, id str
}
func (dma *DatacenterMongoAccessor) StoreOne(data utils.DBObject) utils.DBObject {
id, err := mongo.StoreOne(data.(*Datacenter), "data")
id, err := mongo.MONGOService.StoreOne(data.(*Datacenter), "data")
if err != nil {
dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return &Datacenter{}
@ -30,7 +30,7 @@ func (dma *DatacenterMongoAccessor) StoreOne(data utils.DBObject) utils.DBObject
func (dca *DatacenterMongoAccessor) LoadOne(id string) utils.DBObject {
var datacenter Datacenter
res_mongo, err := mongo.LoadOne(id, "datacenter")
res_mongo, err := mongo.MONGOService.LoadOne(id, "datacenter")
if err != nil {
l := logs.CreateLogger("oclib", "")
l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())

View File

@ -1,9 +1,10 @@
package datacenter
import (
resources "oc-lib/models/resources"
"testing"
resources "cloud.o-forge.io/core/oc-lib/models/resources"
"github.com/stretchr/testify/assert"
)

View File

@ -1,8 +1,8 @@
package processing
import (
resources "oc-lib/models/resources"
"oc-lib/models/utils"
resources "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type Processing struct {

View File

@ -1,9 +1,9 @@
package processing
import (
"oc-lib/dbs/mongo"
"oc-lib/logs"
"oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type ProcessingMongoAccessor struct {
@ -19,7 +19,7 @@ func (dma *ProcessingMongoAccessor) UpdateOne(set map[string]interface{}, id str
}
func (dma *ProcessingMongoAccessor) StoreOne(data utils.DBObject) utils.DBObject {
id, err := mongo.StoreOne(data.(*Processing), "data")
id, err := mongo.MONGOService.StoreOne(data.(*Processing), "data")
if err != nil {
dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return &Processing{}
@ -31,7 +31,7 @@ func (pma *ProcessingMongoAccessor) LoadOne(id string) utils.DBObject {
var processing Processing
res_mongo, err := mongo.LoadOne(id, "processing")
res_mongo, err := mongo.MONGOService.LoadOne(id, "processing")
if err != nil {
l := logs.CreateLogger("oclib", "")
l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())

View File

@ -1,9 +1,10 @@
package processing
import (
resources "oc-lib/models/resources"
"testing"
resources "cloud.o-forge.io/core/oc-lib/models/resources"
"github.com/stretchr/testify/assert"
)

View File

@ -1,8 +1,8 @@
package storage
import (
resources "oc-lib/models/resources"
"oc-lib/models/utils"
resources "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type URL struct {

View File

@ -1,9 +1,9 @@
package storage
import (
"oc-lib/dbs/mongo"
"oc-lib/logs"
"oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type StorageMongoAccessor struct {
@ -19,7 +19,7 @@ func (dma *StorageMongoAccessor) UpdateOne(set map[string]interface{}, id string
}
func (dma *StorageMongoAccessor) StoreOne(data utils.DBObject) utils.DBObject {
id, err := mongo.StoreOne(data.(*Storage), "data")
id, err := mongo.MONGOService.StoreOne(data.(*Storage), "data")
if err != nil {
dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return &Storage{}
@ -31,7 +31,7 @@ func (schedulema *StorageMongoAccessor) LoadOne(id string) utils.DBObject {
var storage Storage
res_mongo, err := mongo.LoadOne(id, "storage")
res_mongo, err := mongo.MONGOService.LoadOne(id, "storage")
if err != nil {
l := logs.CreateLogger("oclib", "")
l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())

View File

@ -1,9 +1,10 @@
package storage
import (
resources "oc-lib/models/resources"
"testing"
resources "cloud.o-forge.io/core/oc-lib/models/resources"
"github.com/stretchr/testify/assert"
)

View File

@ -1,9 +1,8 @@
package utils
import (
oclib "oc-lib"
"oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs"
"github.com/rs/zerolog"
)
@ -12,11 +11,11 @@ type AbstractAccessor struct {
}
func (dma *AbstractAccessor) SetLogger() {
dma.Logger = oclib.CreateLogger("oclib", "")
dma.Logger = logs.CreateLogger("oclib", "")
}
func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) DBObject {
res := accessor.LoadOne(id)
_, err := mongo.DeleteOne(id, "data")
_, err := mongo.MONGOService.DeleteOne(id, "data")
if err != nil {
dma.Logger.Error().Msg("Could not delete " + id + " to db. Error: " + err.Error())
}
@ -24,7 +23,7 @@ func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) DBOb
}
func (dma *AbstractAccessor) GenericUpdateOne(set map[string]interface{}, id string, accessor Accessor) DBObject {
id, err := mongo.UpdateOne(set, id, "data")
id, err := mongo.MONGOService.UpdateOne(set, id, "data")
if err != nil {
dma.Logger.Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
}

View File

@ -1,13 +1,13 @@
package oclib
import (
"oc-lib/models/resources"
"oc-lib/models/resources/data"
"oc-lib/models/resources/datacenter"
"oc-lib/models/resources/processing"
"oc-lib/models/resources/storage"
"oc-lib/models/utils"
"oc-lib/models/workflow/graph"
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/resources/data"
"cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
"cloud.o-forge.io/core/oc-lib/models/resources/processing"
"cloud.o-forge.io/core/oc-lib/models/resources/storage"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/models/workflow/graph"
)
const WORKFLOW = "workflow"

View File

@ -1,9 +1,9 @@
package oclib
import (
"oc-lib/dbs/mongo"
"oc-lib/logs"
"oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type WorkflowMongoAccessor struct {
@ -19,7 +19,7 @@ func (dma *WorkflowMongoAccessor) UpdateOne(set map[string]interface{}, id strin
}
func (dma *WorkflowMongoAccessor) StoreOne(data utils.DBObject) utils.DBObject {
id, err := mongo.StoreOne(data.(*Workflow), "data")
id, err := mongo.MONGOService.StoreOne(data.(*Workflow), "data")
if err != nil {
dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return &Workflow{}
@ -30,7 +30,7 @@ func (dma *WorkflowMongoAccessor) StoreOne(data utils.DBObject) utils.DBObject {
func (wfa *WorkflowMongoAccessor) LoadOne(id string) utils.DBObject {
var workflow Workflow
res_mongo, err := mongo.LoadOne(id, "workflow")
res_mongo, err := mongo.MONGOService.LoadOne(id, "workflow")
if err != nil {
l := logs.CreateLogger("oclib", "")
l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())

View File

@ -1,9 +1,10 @@
package oclib
import (
"oc-lib/models/resources"
"testing"
"cloud.o-forge.io/core/oc-lib/models/resources"
"github.com/stretchr/testify/assert"
)