apropriate db

This commit is contained in:
mr 2024-07-18 16:46:54 +02:00
parent d273fdc57f
commit 3e9eae007b
17 changed files with 92 additions and 93 deletions

View File

@ -7,12 +7,12 @@ import (
dc "cloud.o-forge.io/core/oc-lib/models/resources/datacenter" dc "cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
p "cloud.o-forge.io/core/oc-lib/models/resources/processing" p "cloud.o-forge.io/core/oc-lib/models/resources/processing"
s "cloud.o-forge.io/core/oc-lib/models/resources/storage" s "cloud.o-forge.io/core/oc-lib/models/resources/storage"
w "cloud.o-forge.io/core/oc-lib/models/resources/workflow"
"cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/models/utils"
w "cloud.o-forge.io/core/oc-lib/models/workflow"
) )
var models = map[string]func() utils.DBObject{ var models = map[string]func() utils.DBObject{
w.WORKFLOW: func() utils.DBObject { return &w.Workflow{} }, r.WORKFLOW.String(): func() utils.DBObject { return &w.Workflow{} },
r.DATA.String(): func() utils.DBObject { return &d.Data{} }, r.DATA.String(): func() utils.DBObject { return &d.Data{} },
r.DATACENTER.String(): func() utils.DBObject { return &dc.Datacenter{} }, r.DATACENTER.String(): func() utils.DBObject { return &dc.Datacenter{} },
r.STORAGE.String(): func() utils.DBObject { return &s.Storage{} }, r.STORAGE.String(): func() utils.DBObject { return &s.Storage{} },

View File

@ -29,6 +29,6 @@ func (d *Data) GetType() resources.ResourceType {
func (d *Data) GetAccessor() utils.Accessor { func (d *Data) GetAccessor() utils.Accessor {
data := &DataMongoAccessor{} data := &DataMongoAccessor{}
data.SetLogger() data.SetLogger(resources.DATA)
return data return data
} }

View File

@ -18,7 +18,7 @@ func (dma *DataMongoAccessor) UpdateOne(set map[string]interface{}, id string) (
} }
func (dma *DataMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) { 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), dma.GetType())
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 nil, err return nil, err
@ -28,7 +28,7 @@ func (dma *DataMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, err
func (dma *DataMongoAccessor) LoadOne(id string) (utils.DBObject, error) { 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, dma.GetType())
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 nil, err return nil, err

View File

@ -52,6 +52,6 @@ func (d *Datacenter) GetType() resources.ResourceType {
func (d *Datacenter) GetAccessor() utils.Accessor { func (d *Datacenter) GetAccessor() utils.Accessor {
data := &DatacenterMongoAccessor{} data := &DatacenterMongoAccessor{}
data.SetLogger() data.SetLogger(resources.DATACENTER)
return data return data
} }

View File

@ -2,7 +2,6 @@ package datacenter
import ( import (
"cloud.o-forge.io/core/oc-lib/dbs/mongo" "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" "cloud.o-forge.io/core/oc-lib/models/utils"
) )
@ -10,30 +9,29 @@ type DatacenterMongoAccessor struct {
utils.AbstractAccessor utils.AbstractAccessor
} }
func (dma *DatacenterMongoAccessor) DeleteOne(id string) (utils.DBObject, error) { func (dca *DatacenterMongoAccessor) DeleteOne(id string) (utils.DBObject, error) {
return dma.GenericDeleteOne(id, dma) return dca.GenericDeleteOne(id, dca)
} }
func (dma *DatacenterMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) { func (dca *DatacenterMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) {
return dma.GenericUpdateOne(set, id, dma) return dca.GenericUpdateOne(set, id, dca)
} }
func (dma *DatacenterMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) { func (dca *DatacenterMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
id, err := mongo.MONGOService.StoreOne(data.(*Datacenter), "data") id, err := mongo.MONGOService.StoreOne(data.(*Datacenter), dca.GetType())
if err != nil { if err != nil {
dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error()) dca.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return nil, err return nil, err
} }
return dma.LoadOne(id) return dca.LoadOne(id)
} }
func (dca *DatacenterMongoAccessor) LoadOne(id string) (utils.DBObject, error) { 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, dca.GetType())
if err != nil { if err != nil {
l := logs.CreateLogger("oclib", "") dca.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, err return nil, err
} }

View File

@ -46,6 +46,6 @@ func (p *Processing) GetType() resources.ResourceType {
func (d *Processing) GetAccessor() utils.Accessor { func (d *Processing) GetAccessor() utils.Accessor {
data := &ProcessingMongoAccessor{} data := &ProcessingMongoAccessor{}
data.SetLogger() data.SetLogger(resources.PROCESSING)
return data return data
} }

View File

@ -10,28 +10,28 @@ type ProcessingMongoAccessor struct {
utils.AbstractAccessor utils.AbstractAccessor
} }
func (dma *ProcessingMongoAccessor) DeleteOne(id string) (utils.DBObject, error) { func (pma *ProcessingMongoAccessor) DeleteOne(id string) (utils.DBObject, error) {
return dma.GenericDeleteOne(id, dma) return pma.GenericDeleteOne(id, pma)
} }
func (dma *ProcessingMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) { func (pma *ProcessingMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) {
return dma.GenericUpdateOne(set, id, dma) return pma.GenericUpdateOne(set, id, pma)
} }
func (dma *ProcessingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) { func (pma *ProcessingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
id, err := mongo.MONGOService.StoreOne(data.(*Processing), "data") id, err := mongo.MONGOService.StoreOne(data.(*Processing), pma.GetType())
if err != nil { if err != nil {
dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error()) pma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return nil, err return nil, err
} }
return dma.LoadOne(id) return pma.LoadOne(id)
} }
func (pma *ProcessingMongoAccessor) LoadOne(id string) (utils.DBObject, error) { func (pma *ProcessingMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
var processing Processing var processing Processing
res_mongo, err := mongo.MONGOService.LoadOne(id, "processing") res_mongo, err := mongo.MONGOService.LoadOne(id, pma.GetType())
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())

View File

@ -33,12 +33,8 @@ func (dma *Storage) Deserialize(j map[string]interface{}) utils.DBObject {
return dma return dma
} }
func (s *Storage) GetType() resources.ResourceType {
return resources.STORAGE
}
func (d *Storage) GetAccessor() utils.Accessor { func (d *Storage) GetAccessor() utils.Accessor {
data := &StorageMongoAccessor{} data := &StorageMongoAccessor{}
data.SetLogger() data.SetLogger(resources.STORAGE)
return data return data
} }

View File

@ -10,28 +10,28 @@ type StorageMongoAccessor struct {
utils.AbstractAccessor utils.AbstractAccessor
} }
func (dma *StorageMongoAccessor) DeleteOne(id string) (utils.DBObject, error) { func (sma *StorageMongoAccessor) DeleteOne(id string) (utils.DBObject, error) {
return dma.GenericDeleteOne(id, dma) return sma.GenericDeleteOne(id, sma)
} }
func (dma *StorageMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) { func (sma *StorageMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) {
return dma.GenericUpdateOne(set, id, dma) return sma.GenericUpdateOne(set, id, sma)
} }
func (dma *StorageMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) { func (sma *StorageMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
id, err := mongo.MONGOService.StoreOne(data.(*Storage), "data") id, err := mongo.MONGOService.StoreOne(data.(*Storage), sma.GetType())
if err != nil { if err != nil {
dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error()) sma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return nil, err return nil, err
} }
return dma.LoadOne(id) return sma.LoadOne(id)
} }
func (schedulema *StorageMongoAccessor) LoadOne(id string) (utils.DBObject, error) { func (sma *StorageMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
var storage Storage var storage Storage
res_mongo, err := mongo.MONGOService.LoadOne(id, "storage") res_mongo, err := mongo.MONGOService.LoadOne(id, sma.GetType())
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())

View File

@ -8,12 +8,10 @@ import (
"cloud.o-forge.io/core/oc-lib/models/resources/datacenter" "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/processing"
"cloud.o-forge.io/core/oc-lib/models/resources/storage" "cloud.o-forge.io/core/oc-lib/models/resources/storage"
"cloud.o-forge.io/core/oc-lib/models/resources/workflow/graph"
"cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/models/workflow/graph"
) )
const WORKFLOW = "workflow"
type Workflow struct { type Workflow struct {
resources.AbstractResource resources.AbstractResource
Graph graph.Graph `bson:"graph,omitempty" json:"graph,omitempty"` Graph graph.Graph `bson:"graph,omitempty" json:"graph,omitempty"`
@ -26,7 +24,7 @@ type Workflow struct {
func (d *Workflow) GetAccessor() utils.Accessor { func (d *Workflow) GetAccessor() utils.Accessor {
data := &WorkflowMongoAccessor{} data := &WorkflowMongoAccessor{}
data.SetLogger() data.SetLogger(resources.WORKFLOW)
return data return data
} }

View File

@ -0,0 +1,38 @@
package oclib
import (
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type WorkflowMongoAccessor struct {
utils.AbstractAccessor
}
func (wfa *WorkflowMongoAccessor) DeleteOne(id string) (utils.DBObject, error) {
return wfa.GenericDeleteOne(id, wfa)
}
func (wfa *WorkflowMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) {
return wfa.GenericUpdateOne(set, id, wfa)
}
func (wfa *WorkflowMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
id, err := mongo.MONGOService.StoreOne(data.(*Workflow), wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return nil, err
}
return wfa.LoadOne(id)
}
func (wfa *WorkflowMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
var workflow Workflow
res_mongo, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, err
}
res_mongo.Decode(&workflow)
return &workflow, nil
}

View File

@ -3,15 +3,22 @@ package utils
import ( 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/logs" "cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/models/resources"
"github.com/rs/zerolog" "github.com/rs/zerolog"
) )
type AbstractAccessor struct { type AbstractAccessor struct {
Logger zerolog.Logger Logger zerolog.Logger
Type string
} }
func (dma *AbstractAccessor) SetLogger() { func (dma *AbstractAccessor) GetType() string {
dma.Logger = logs.CreateLogger("oclib", "") return dma.Type
}
func (dma *AbstractAccessor) SetLogger(t resources.ResourceType) {
dma.Logger = logs.CreateLogger("oclib", t.String())
dma.Type = t.String()
} }
func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBObject, error) { func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBObject, error) {
res, err := accessor.LoadOne(id) res, err := accessor.LoadOne(id)
@ -19,7 +26,7 @@ func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBO
dma.Logger.Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error()) dma.Logger.Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
return nil, err return nil, err
} }
_, err = mongo.MONGOService.DeleteOne(id, "data") _, err = mongo.MONGOService.DeleteOne(id, accessor.GetType())
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 nil, err
@ -28,7 +35,7 @@ func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBO
} }
func (dma *AbstractAccessor) GenericUpdateOne(set map[string]interface{}, id string, accessor Accessor) (DBObject, error) { 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, accessor.GetType())
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

@ -1,5 +1,7 @@
package utils package utils
import "cloud.o-forge.io/core/oc-lib/models/resources"
type DBObject interface { type DBObject interface {
GetName() string GetName() string
Deserialize(j map[string]interface{}) DBObject Deserialize(j map[string]interface{}) DBObject
@ -7,7 +9,8 @@ type DBObject interface {
} }
type Accessor interface { type Accessor interface {
SetLogger() SetLogger(t resources.ResourceType)
GetType() string
LoadOne(id string) (DBObject, error) LoadOne(id string) (DBObject, error)
DeleteOne(id string) (DBObject, error) DeleteOne(id string) (DBObject, error)
StoreOne(data DBObject) (DBObject, error) StoreOne(data DBObject) (DBObject, error)

View File

@ -1,41 +0,0 @@
package oclib
import (
"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 {
utils.AbstractAccessor
}
func (dma *WorkflowMongoAccessor) DeleteOne(id string) (utils.DBObject, error) {
return dma.GenericDeleteOne(id, dma)
}
func (dma *WorkflowMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) {
return dma.GenericUpdateOne(set, id, dma)
}
func (dma *WorkflowMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
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 nil, err
}
return dma.LoadOne(id)
}
func (wfa *WorkflowMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
var workflow 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())
return nil, err
}
res_mongo.Decode(&workflow)
return &workflow, nil
}