determine returnal

This commit is contained in:
mr 2024-07-18 15:35:30 +02:00
parent 6d104288b7
commit 08100eb57f
13 changed files with 85 additions and 72 deletions

View File

@ -4,12 +4,13 @@ import (
"cloud.o-forge.io/core/oc-lib/dbs/mongo" "cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models" "cloud.o-forge.io/core/oc-lib/models"
"cloud.o-forge.io/core/oc-lib/models/resources" "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
) )
type LibData int type LibDataEnum int
const ( const (
INVALID LibData = iota INVALID LibDataEnum = iota
DATA = resources.DATA DATA = resources.DATA
PROCESSING = resources.PROCESSING PROCESSING = resources.PROCESSING
STORAGE = resources.STORAGE STORAGE = resources.STORAGE
@ -17,27 +18,36 @@ const (
WORKFLOW = resources.WORKFLOW WORKFLOW = resources.WORKFLOW
) )
func (d LibData) EnumIndex() int { func (d LibDataEnum) EnumIndex() int {
return int(d) return int(d)
} }
type LibData struct {
Data utils.DBObject `bson:"data" json:"data"`
Err error `bson:"error" json:"error"`
}
func Init() { func Init() {
mongo.MONGOService.Init(models.GetModelsNames(), GetConfig()) mongo.MONGOService.Init(models.GetModelsNames(), GetConfig())
} }
func LoadOne(col LibData, id string) interface{} { func LoadOne(col LibDataEnum, id string) LibData {
return models.Model(col.EnumIndex()).GetAccessor().LoadOne(id) d, err := models.Model(col.EnumIndex()).GetAccessor().LoadOne(id)
return LibData{Data: d, Err: err}
} }
func UpdateOne(col LibData, set map[string]interface{}, id string) interface{} { func UpdateOne(col LibDataEnum, set map[string]interface{}, id string) LibData {
return models.Model(col.EnumIndex()).GetAccessor().UpdateOne(set, id) d, err := models.Model(col.EnumIndex()).GetAccessor().UpdateOne(set, id)
return LibData{Data: d, Err: err}
} }
func DeleteOne(col LibData, id string) interface{} { func DeleteOne(col LibDataEnum, id string) LibData {
return models.Model(col.EnumIndex()).GetAccessor().DeleteOne(id) d, err := models.Model(col.EnumIndex()).GetAccessor().DeleteOne(id)
return LibData{Data: d, Err: err}
} }
func StoreOne(col LibData, object map[string]interface{}) interface{} { func StoreOne(col LibDataEnum, object map[string]interface{}) LibData {
model := models.Model(col.EnumIndex()) model := models.Model(col.EnumIndex())
return model.GetAccessor().StoreOne(model.Deserialize(object)) d, err := model.GetAccessor().StoreOne(model.Deserialize(object))
return LibData{Data: d, Err: err}
} }

View File

@ -9,31 +9,31 @@ type DataMongoAccessor struct {
utils.AbstractAccessor utils.AbstractAccessor
} }
func (dma *DataMongoAccessor) DeleteOne(id string) utils.DBObject { func (dma *DataMongoAccessor) DeleteOne(id string) (utils.DBObject, error) {
return dma.GenericDeleteOne(id, dma) return dma.GenericDeleteOne(id, dma)
} }
func (dma *DataMongoAccessor) UpdateOne(set map[string]interface{}, id string) utils.DBObject { func (dma *DataMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) {
return dma.GenericUpdateOne(set, id, dma) return dma.GenericUpdateOne(set, id, dma)
} }
func (dma *DataMongoAccessor) StoreOne(data utils.DBObject) utils.DBObject { func (dma *DataMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
id, err := mongo.MONGOService.StoreOne(data.(*Data), "data") id, err := mongo.MONGOService.StoreOne(data.(*Data), "data")
if err != nil { if err != nil {
dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error()) dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return &Data{} return nil, err
} }
return dma.LoadOne(id) return dma.LoadOne(id)
} }
func (dma *DataMongoAccessor) LoadOne(id string) utils.DBObject { func (dma *DataMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
var data Data var data Data
res_mongo, err := mongo.MONGOService.LoadOne(id, "data") res_mongo, err := mongo.MONGOService.LoadOne(id, "data")
if err != nil { if err != nil {
dma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error()) dma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return &Data{} return nil, err
} }
res_mongo.Decode(&data) res_mongo.Decode(&data)
return &data return &data, nil
} }

View File

@ -22,7 +22,7 @@ func TestStoreOneData(t *testing.T) {
} }
dma := DataMongoAccessor{} dma := DataMongoAccessor{}
id := dma.StoreOne(&d) id, _ := dma.StoreOne(&d)
assert.NotEmpty(t, id) assert.NotEmpty(t, id)
} }
@ -41,6 +41,6 @@ func TestLoadOneDate(t *testing.T) {
} }
dma := DataMongoAccessor{} dma := DataMongoAccessor{}
new_d := dma.StoreOne(&d) new_d, _ := dma.StoreOne(&d)
assert.Equal(t, d, new_d) assert.Equal(t, d, new_d)
} }

View File

@ -10,34 +10,34 @@ type DatacenterMongoAccessor struct {
utils.AbstractAccessor utils.AbstractAccessor
} }
func (dma *DatacenterMongoAccessor) DeleteOne(id string) utils.DBObject { func (dma *DatacenterMongoAccessor) DeleteOne(id string) (utils.DBObject, error) {
return dma.GenericDeleteOne(id, dma) return dma.GenericDeleteOne(id, dma)
} }
func (dma *DatacenterMongoAccessor) UpdateOne(set map[string]interface{}, id string) utils.DBObject { func (dma *DatacenterMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) {
return dma.GenericUpdateOne(set, id, dma) return dma.GenericUpdateOne(set, id, dma)
} }
func (dma *DatacenterMongoAccessor) StoreOne(data utils.DBObject) utils.DBObject { func (dma *DatacenterMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
id, err := mongo.MONGOService.StoreOne(data.(*Datacenter), "data") id, err := mongo.MONGOService.StoreOne(data.(*Datacenter), "data")
if err != nil { if err != nil {
dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error()) dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return &Datacenter{} return nil, err
} }
return dma.LoadOne(id) return dma.LoadOne(id)
} }
func (dca *DatacenterMongoAccessor) LoadOne(id string) utils.DBObject { func (dca *DatacenterMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
var datacenter Datacenter var datacenter Datacenter
res_mongo, err := mongo.MONGOService.LoadOne(id, "datacenter") res_mongo, err := mongo.MONGOService.LoadOne(id, "datacenter")
if err != nil { if err != nil {
l := logs.CreateLogger("oclib", "") l := logs.CreateLogger("oclib", "")
l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error()) l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return &Datacenter{} return nil, err
} }
res_mongo.Decode(&datacenter) res_mongo.Decode(&datacenter)
return &datacenter return &datacenter, nil
} }

View File

@ -22,7 +22,7 @@ func TestStoreOneDatacenter(t *testing.T) {
} }
dcma := DatacenterMongoAccessor{} dcma := DatacenterMongoAccessor{}
id := dcma.StoreOne(&dc) id, _ := dcma.StoreOne(&dc)
assert.NotEmpty(t, id) assert.NotEmpty(t, id)
} }
@ -41,7 +41,7 @@ func TestLoadOneDatacenter(t *testing.T) {
} }
dcma := DatacenterMongoAccessor{} dcma := DatacenterMongoAccessor{}
new_dc := dcma.StoreOne(&dc) new_dc, _ := dcma.StoreOne(&dc)
assert.Equal(t, dc, new_dc) assert.Equal(t, dc, new_dc)
} }

View File

@ -10,24 +10,24 @@ type ProcessingMongoAccessor struct {
utils.AbstractAccessor utils.AbstractAccessor
} }
func (dma *ProcessingMongoAccessor) DeleteOne(id string) utils.DBObject { func (dma *ProcessingMongoAccessor) DeleteOne(id string) (utils.DBObject, error) {
return dma.GenericDeleteOne(id, dma) return dma.GenericDeleteOne(id, dma)
} }
func (dma *ProcessingMongoAccessor) UpdateOne(set map[string]interface{}, id string) utils.DBObject { func (dma *ProcessingMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) {
return dma.GenericUpdateOne(set, id, dma) return dma.GenericUpdateOne(set, id, dma)
} }
func (dma *ProcessingMongoAccessor) StoreOne(data utils.DBObject) utils.DBObject { func (dma *ProcessingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
id, err := mongo.MONGOService.StoreOne(data.(*Processing), "data") id, err := mongo.MONGOService.StoreOne(data.(*Processing), "data")
if err != nil { if err != nil {
dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error()) dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return &Processing{} return nil, err
} }
return dma.LoadOne(id) return dma.LoadOne(id)
} }
func (pma *ProcessingMongoAccessor) LoadOne(id string) utils.DBObject { func (pma *ProcessingMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
var processing Processing var processing Processing
@ -35,10 +35,10 @@ func (pma *ProcessingMongoAccessor) LoadOne(id string) utils.DBObject {
if err != nil { if err != nil {
l := logs.CreateLogger("oclib", "") l := logs.CreateLogger("oclib", "")
l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error()) l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return &Processing{} return nil, err
} }
res_mongo.Decode(&processing) res_mongo.Decode(&processing)
return &processing return &processing, nil
} }

View File

@ -22,7 +22,7 @@ func TestStoreOneProcessing(t *testing.T) {
} }
sma := ProcessingMongoAccessor{} sma := ProcessingMongoAccessor{}
id := sma.StoreOne(&p) id, _ := sma.StoreOne(&p)
assert.NotEmpty(t, id) assert.NotEmpty(t, id)
} }
@ -41,6 +41,6 @@ func TestLoadOneProcessing(t *testing.T) {
} }
sma := ProcessingMongoAccessor{} sma := ProcessingMongoAccessor{}
new_s := sma.StoreOne(&p) new_s, _ := sma.StoreOne(&p)
assert.Equal(t, p, new_s) assert.Equal(t, p, new_s)
} }

View File

@ -10,24 +10,24 @@ type StorageMongoAccessor struct {
utils.AbstractAccessor utils.AbstractAccessor
} }
func (dma *StorageMongoAccessor) DeleteOne(id string) utils.DBObject { func (dma *StorageMongoAccessor) DeleteOne(id string) (utils.DBObject, error) {
return dma.GenericDeleteOne(id, dma) return dma.GenericDeleteOne(id, dma)
} }
func (dma *StorageMongoAccessor) UpdateOne(set map[string]interface{}, id string) utils.DBObject { func (dma *StorageMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) {
return dma.GenericUpdateOne(set, id, dma) return dma.GenericUpdateOne(set, id, dma)
} }
func (dma *StorageMongoAccessor) StoreOne(data utils.DBObject) utils.DBObject { func (dma *StorageMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
id, err := mongo.MONGOService.StoreOne(data.(*Storage), "data") id, err := mongo.MONGOService.StoreOne(data.(*Storage), "data")
if err != nil { if err != nil {
dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error()) dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return &Storage{} return nil, err
} }
return dma.LoadOne(id) return dma.LoadOne(id)
} }
func (schedulema *StorageMongoAccessor) LoadOne(id string) utils.DBObject { func (schedulema *StorageMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
var storage Storage var storage Storage
@ -35,10 +35,10 @@ func (schedulema *StorageMongoAccessor) LoadOne(id string) utils.DBObject {
if err != nil { if err != nil {
l := logs.CreateLogger("oclib", "") l := logs.CreateLogger("oclib", "")
l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error()) l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return &Storage{} return nil, err
} }
res_mongo.Decode(&storage) res_mongo.Decode(&storage)
return &storage return &storage, nil
} }

View File

@ -22,7 +22,7 @@ func TestStoreOneStorage(t *testing.T) {
} }
sma := StorageMongoAccessor{} sma := StorageMongoAccessor{}
id := sma.StoreOne(&s) id, _ := sma.StoreOne(&s)
assert.NotEmpty(t, id) assert.NotEmpty(t, id)
} }
@ -41,7 +41,7 @@ func TestLoadOneStorage(t *testing.T) {
} }
sma := StorageMongoAccessor{} sma := StorageMongoAccessor{}
new_s := sma.StoreOne(&s) new_s, _ := sma.StoreOne(&s)
assert.Equal(t, s, new_s) assert.Equal(t, s, new_s)
} }

View File

@ -13,16 +13,21 @@ type AbstractAccessor struct {
func (dma *AbstractAccessor) SetLogger() { func (dma *AbstractAccessor) SetLogger() {
dma.Logger = logs.CreateLogger("oclib", "") dma.Logger = logs.CreateLogger("oclib", "")
} }
func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) DBObject { func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBObject, error) {
res := accessor.LoadOne(id) res, err := accessor.LoadOne(id)
_, err := mongo.MONGOService.DeleteOne(id, "data") if err != nil {
dma.Logger.Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
return nil, err
}
_, err = mongo.MONGOService.DeleteOne(id, "data")
if err != nil { if err != nil {
dma.Logger.Error().Msg("Could not delete " + id + " to db. Error: " + err.Error()) dma.Logger.Error().Msg("Could not delete " + id + " to db. Error: " + err.Error())
return nil, err
} }
return res return res, nil
} }
func (dma *AbstractAccessor) GenericUpdateOne(set map[string]interface{}, id string, accessor Accessor) DBObject { func (dma *AbstractAccessor) GenericUpdateOne(set map[string]interface{}, id string, accessor Accessor) (DBObject, error) {
id, err := mongo.MONGOService.UpdateOne(set, id, "data") id, err := mongo.MONGOService.UpdateOne(set, id, "data")
if err != nil { if err != nil {
dma.Logger.Error().Msg("Could not update " + id + " to db. Error: " + err.Error()) dma.Logger.Error().Msg("Could not update " + id + " to db. Error: " + err.Error())

View File

@ -8,8 +8,8 @@ type DBObject interface {
type Accessor interface { type Accessor interface {
SetLogger() SetLogger()
LoadOne(id string) DBObject LoadOne(id string) (DBObject, error)
DeleteOne(id string) DBObject DeleteOne(id string) (DBObject, error)
StoreOne(data DBObject) DBObject StoreOne(data DBObject) (DBObject, error)
UpdateOne(set map[string]interface{}, id string) DBObject UpdateOne(set map[string]interface{}, id string) (DBObject, error)
} }

View File

@ -10,34 +10,32 @@ type WorkflowMongoAccessor struct {
utils.AbstractAccessor utils.AbstractAccessor
} }
func (dma *WorkflowMongoAccessor) DeleteOne(id string) utils.DBObject { func (dma *WorkflowMongoAccessor) DeleteOne(id string) (utils.DBObject, error) {
return dma.GenericDeleteOne(id, dma) return dma.GenericDeleteOne(id, dma)
} }
func (dma *WorkflowMongoAccessor) UpdateOne(set map[string]interface{}, id string) utils.DBObject { func (dma *WorkflowMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) {
return dma.GenericUpdateOne(set, id, dma) return dma.GenericUpdateOne(set, id, dma)
} }
func (dma *WorkflowMongoAccessor) StoreOne(data utils.DBObject) utils.DBObject { func (dma *WorkflowMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
id, err := mongo.MONGOService.StoreOne(data.(*Workflow), "data") id, err := mongo.MONGOService.StoreOne(data.(*Workflow), "data")
if err != nil { if err != nil {
dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error()) dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return &Workflow{} return nil, err
} }
return dma.LoadOne(id) return dma.LoadOne(id)
} }
func (wfa *WorkflowMongoAccessor) LoadOne(id string) utils.DBObject { func (wfa *WorkflowMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
var workflow Workflow var workflow Workflow
res_mongo, err := mongo.MONGOService.LoadOne(id, "workflow") res_mongo, err := mongo.MONGOService.LoadOne(id, "workflow")
if err != nil { if err != nil {
l := logs.CreateLogger("oclib", "") l := logs.CreateLogger("oclib", "")
l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error()) l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return &Workflow{} return nil, err
} }
res_mongo.Decode(&workflow) res_mongo.Decode(&workflow)
return &workflow, nil
return &workflow
} }

View File

@ -21,7 +21,7 @@ func TestStoreOneWorkflow(t *testing.T) {
} }
wma := WorkflowMongoAccessor{} wma := WorkflowMongoAccessor{}
id := wma.StoreOne(&w) id, _ := wma.StoreOne(&w)
assert.NotEmpty(t, id) assert.NotEmpty(t, id)
} }
@ -39,6 +39,6 @@ func TestLoadOneWorkflow(t *testing.T) {
} }
wma := WorkflowMongoAccessor{} wma := WorkflowMongoAccessor{}
new_w := wma.StoreOne(&w) new_w, _ := wma.StoreOne(&w)
assert.Equal(t, w, new_w) assert.Equal(t, w, new_w)
} }