From 3e9eae007b2a8f83988c493e96d5e99b221089c1 Mon Sep 17 00:00:00 2001 From: mr Date: Thu, 18 Jul 2024 16:46:54 +0200 Subject: [PATCH] apropriate db --- models/models.go | 4 +- models/resources/data/data.go | 2 +- models/resources/data/data_mongo_accessor.go | 4 +- models/resources/datacenter/datacenter.go | 2 +- .../datacenter/datacenter_mongo_accessor.go | 22 +++++----- models/resources/processing/processing.go | 2 +- .../processing/processing_mongo_accessor.go | 18 ++++---- models/resources/storage/storage.go | 6 +-- .../storage/storage_mongo_accessor.go | 20 ++++----- .../{ => resources}/workflow/graph/graph.go | 0 models/{ => resources}/workflow/workflow.go | 6 +-- .../workflow/workflow_mongo_accessor.go | 38 +++++++++++++++++ .../workflow/workflow_schedule.go | 0 .../{ => resources}/workflow/workflow_test.go | 0 models/utils/abstracts.go | 15 +++++-- models/utils/interfaces.go | 5 ++- models/workflow/workflow_mongo_accessor.go | 41 ------------------- 17 files changed, 92 insertions(+), 93 deletions(-) rename models/{ => resources}/workflow/graph/graph.go (100%) rename models/{ => resources}/workflow/workflow.go (93%) create mode 100644 models/resources/workflow/workflow_mongo_accessor.go rename models/{ => resources}/workflow/workflow_schedule.go (100%) rename models/{ => resources}/workflow/workflow_test.go (100%) delete mode 100644 models/workflow/workflow_mongo_accessor.go diff --git a/models/models.go b/models/models.go index 56a228c..b3e4614 100644 --- a/models/models.go +++ b/models/models.go @@ -7,12 +7,12 @@ import ( 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" + w "cloud.o-forge.io/core/oc-lib/models/resources/workflow" "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{ - 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.DATACENTER.String(): func() utils.DBObject { return &dc.Datacenter{} }, r.STORAGE.String(): func() utils.DBObject { return &s.Storage{} }, diff --git a/models/resources/data/data.go b/models/resources/data/data.go index 1eb44a6..132f15f 100644 --- a/models/resources/data/data.go +++ b/models/resources/data/data.go @@ -29,6 +29,6 @@ func (d *Data) GetType() resources.ResourceType { func (d *Data) GetAccessor() utils.Accessor { data := &DataMongoAccessor{} - data.SetLogger() + data.SetLogger(resources.DATA) return data } diff --git a/models/resources/data/data_mongo_accessor.go b/models/resources/data/data_mongo_accessor.go index e4450f0..516dda1 100644 --- a/models/resources/data/data_mongo_accessor.go +++ b/models/resources/data/data_mongo_accessor.go @@ -18,7 +18,7 @@ func (dma *DataMongoAccessor) UpdateOne(set map[string]interface{}, id string) ( } 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 { dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error()) 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) { var data Data - res_mongo, err := mongo.MONGOService.LoadOne(id, "data") + res_mongo, err := mongo.MONGOService.LoadOne(id, dma.GetType()) if err != nil { dma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error()) return nil, err diff --git a/models/resources/datacenter/datacenter.go b/models/resources/datacenter/datacenter.go index 4f13dec..923fcdf 100644 --- a/models/resources/datacenter/datacenter.go +++ b/models/resources/datacenter/datacenter.go @@ -52,6 +52,6 @@ func (d *Datacenter) GetType() resources.ResourceType { func (d *Datacenter) GetAccessor() utils.Accessor { data := &DatacenterMongoAccessor{} - data.SetLogger() + data.SetLogger(resources.DATACENTER) return data } diff --git a/models/resources/datacenter/datacenter_mongo_accessor.go b/models/resources/datacenter/datacenter_mongo_accessor.go index 342899e..d8f52a2 100644 --- a/models/resources/datacenter/datacenter_mongo_accessor.go +++ b/models/resources/datacenter/datacenter_mongo_accessor.go @@ -2,7 +2,6 @@ package datacenter import ( "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" ) @@ -10,30 +9,29 @@ type DatacenterMongoAccessor struct { utils.AbstractAccessor } -func (dma *DatacenterMongoAccessor) DeleteOne(id string) (utils.DBObject, error) { - return dma.GenericDeleteOne(id, dma) +func (dca *DatacenterMongoAccessor) DeleteOne(id string) (utils.DBObject, error) { + return dca.GenericDeleteOne(id, dca) } -func (dma *DatacenterMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) { - return dma.GenericUpdateOne(set, id, dma) +func (dca *DatacenterMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) { + return dca.GenericUpdateOne(set, id, dca) } -func (dma *DatacenterMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) { - id, err := mongo.MONGOService.StoreOne(data.(*Datacenter), "data") +func (dca *DatacenterMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) { + id, err := mongo.MONGOService.StoreOne(data.(*Datacenter), dca.GetType()) 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 dma.LoadOne(id) + return dca.LoadOne(id) } func (dca *DatacenterMongoAccessor) LoadOne(id string) (utils.DBObject, error) { var datacenter Datacenter - res_mongo, err := mongo.MONGOService.LoadOne(id, "datacenter") + res_mongo, err := mongo.MONGOService.LoadOne(id, dca.GetType()) if err != nil { - l := logs.CreateLogger("oclib", "") - l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error()) + dca.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error()) return nil, err } diff --git a/models/resources/processing/processing.go b/models/resources/processing/processing.go index f5d72d7..29bc19a 100644 --- a/models/resources/processing/processing.go +++ b/models/resources/processing/processing.go @@ -46,6 +46,6 @@ func (p *Processing) GetType() resources.ResourceType { func (d *Processing) GetAccessor() utils.Accessor { data := &ProcessingMongoAccessor{} - data.SetLogger() + data.SetLogger(resources.PROCESSING) return data } diff --git a/models/resources/processing/processing_mongo_accessor.go b/models/resources/processing/processing_mongo_accessor.go index b0c4da7..4084ee9 100644 --- a/models/resources/processing/processing_mongo_accessor.go +++ b/models/resources/processing/processing_mongo_accessor.go @@ -10,28 +10,28 @@ type ProcessingMongoAccessor struct { utils.AbstractAccessor } -func (dma *ProcessingMongoAccessor) DeleteOne(id string) (utils.DBObject, error) { - return dma.GenericDeleteOne(id, dma) +func (pma *ProcessingMongoAccessor) DeleteOne(id string) (utils.DBObject, error) { + return pma.GenericDeleteOne(id, pma) } -func (dma *ProcessingMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) { - return dma.GenericUpdateOne(set, id, dma) +func (pma *ProcessingMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) { + return pma.GenericUpdateOne(set, id, pma) } -func (dma *ProcessingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) { - id, err := mongo.MONGOService.StoreOne(data.(*Processing), "data") +func (pma *ProcessingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) { + id, err := mongo.MONGOService.StoreOne(data.(*Processing), pma.GetType()) 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 dma.LoadOne(id) + return pma.LoadOne(id) } func (pma *ProcessingMongoAccessor) LoadOne(id string) (utils.DBObject, error) { var processing Processing - res_mongo, err := mongo.MONGOService.LoadOne(id, "processing") + res_mongo, err := mongo.MONGOService.LoadOne(id, pma.GetType()) if err != nil { l := logs.CreateLogger("oclib", "") l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error()) diff --git a/models/resources/storage/storage.go b/models/resources/storage/storage.go index 0c138c5..761a9e9 100644 --- a/models/resources/storage/storage.go +++ b/models/resources/storage/storage.go @@ -33,12 +33,8 @@ func (dma *Storage) Deserialize(j map[string]interface{}) utils.DBObject { return dma } -func (s *Storage) GetType() resources.ResourceType { - return resources.STORAGE -} - func (d *Storage) GetAccessor() utils.Accessor { data := &StorageMongoAccessor{} - data.SetLogger() + data.SetLogger(resources.STORAGE) return data } diff --git a/models/resources/storage/storage_mongo_accessor.go b/models/resources/storage/storage_mongo_accessor.go index d123bbd..3afe423 100644 --- a/models/resources/storage/storage_mongo_accessor.go +++ b/models/resources/storage/storage_mongo_accessor.go @@ -10,28 +10,28 @@ type StorageMongoAccessor struct { utils.AbstractAccessor } -func (dma *StorageMongoAccessor) DeleteOne(id string) (utils.DBObject, error) { - return dma.GenericDeleteOne(id, dma) +func (sma *StorageMongoAccessor) DeleteOne(id string) (utils.DBObject, error) { + return sma.GenericDeleteOne(id, sma) } -func (dma *StorageMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) { - return dma.GenericUpdateOne(set, id, dma) +func (sma *StorageMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) { + return sma.GenericUpdateOne(set, id, sma) } -func (dma *StorageMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) { - id, err := mongo.MONGOService.StoreOne(data.(*Storage), "data") +func (sma *StorageMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) { + id, err := mongo.MONGOService.StoreOne(data.(*Storage), sma.GetType()) 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 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 - res_mongo, err := mongo.MONGOService.LoadOne(id, "storage") + res_mongo, err := mongo.MONGOService.LoadOne(id, sma.GetType()) if err != nil { l := logs.CreateLogger("oclib", "") l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error()) diff --git a/models/workflow/graph/graph.go b/models/resources/workflow/graph/graph.go similarity index 100% rename from models/workflow/graph/graph.go rename to models/resources/workflow/graph/graph.go diff --git a/models/workflow/workflow.go b/models/resources/workflow/workflow.go similarity index 93% rename from models/workflow/workflow.go rename to models/resources/workflow/workflow.go index 910b080..1c72734 100644 --- a/models/workflow/workflow.go +++ b/models/resources/workflow/workflow.go @@ -8,12 +8,10 @@ import ( "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/resources/workflow/graph" "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 { resources.AbstractResource Graph graph.Graph `bson:"graph,omitempty" json:"graph,omitempty"` @@ -26,7 +24,7 @@ type Workflow struct { func (d *Workflow) GetAccessor() utils.Accessor { data := &WorkflowMongoAccessor{} - data.SetLogger() + data.SetLogger(resources.WORKFLOW) return data } diff --git a/models/resources/workflow/workflow_mongo_accessor.go b/models/resources/workflow/workflow_mongo_accessor.go new file mode 100644 index 0000000..5a35c2a --- /dev/null +++ b/models/resources/workflow/workflow_mongo_accessor.go @@ -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 +} diff --git a/models/workflow/workflow_schedule.go b/models/resources/workflow/workflow_schedule.go similarity index 100% rename from models/workflow/workflow_schedule.go rename to models/resources/workflow/workflow_schedule.go diff --git a/models/workflow/workflow_test.go b/models/resources/workflow/workflow_test.go similarity index 100% rename from models/workflow/workflow_test.go rename to models/resources/workflow/workflow_test.go diff --git a/models/utils/abstracts.go b/models/utils/abstracts.go index 9d9bd4e..0ca3e31 100644 --- a/models/utils/abstracts.go +++ b/models/utils/abstracts.go @@ -3,15 +3,22 @@ package utils 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/resources" "github.com/rs/zerolog" ) type AbstractAccessor struct { Logger zerolog.Logger + Type string } -func (dma *AbstractAccessor) SetLogger() { - dma.Logger = logs.CreateLogger("oclib", "") +func (dma *AbstractAccessor) GetType() string { + 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) { 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()) return nil, err } - _, err = mongo.MONGOService.DeleteOne(id, "data") + _, err = mongo.MONGOService.DeleteOne(id, accessor.GetType()) if err != nil { dma.Logger.Error().Msg("Could not delete " + id + " to db. Error: " + err.Error()) 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) { - id, err := mongo.MONGOService.UpdateOne(set, id, "data") + id, err := mongo.MONGOService.UpdateOne(set, id, accessor.GetType()) if err != nil { dma.Logger.Error().Msg("Could not update " + id + " to db. Error: " + err.Error()) } diff --git a/models/utils/interfaces.go b/models/utils/interfaces.go index cf7b631..682fa92 100644 --- a/models/utils/interfaces.go +++ b/models/utils/interfaces.go @@ -1,5 +1,7 @@ package utils +import "cloud.o-forge.io/core/oc-lib/models/resources" + type DBObject interface { GetName() string Deserialize(j map[string]interface{}) DBObject @@ -7,7 +9,8 @@ type DBObject interface { } type Accessor interface { - SetLogger() + SetLogger(t resources.ResourceType) + GetType() string LoadOne(id string) (DBObject, error) DeleteOne(id string) (DBObject, error) StoreOne(data DBObject) (DBObject, error) diff --git a/models/workflow/workflow_mongo_accessor.go b/models/workflow/workflow_mongo_accessor.go deleted file mode 100644 index 597abee..0000000 --- a/models/workflow/workflow_mongo_accessor.go +++ /dev/null @@ -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 -}