diff --git a/entrypoint.go b/entrypoint.go index 8f341fd..6908cfa 100644 --- a/entrypoint.go +++ b/entrypoint.go @@ -4,40 +4,50 @@ import ( "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/resources" + "cloud.o-forge.io/core/oc-lib/models/utils" ) -type LibData int +type LibDataEnum int const ( - INVALID LibData = iota - DATA = resources.DATA - PROCESSING = resources.PROCESSING - STORAGE = resources.STORAGE - DATACENTER = resources.DATACENTER - WORKFLOW = resources.WORKFLOW + INVALID LibDataEnum = iota + DATA = resources.DATA + PROCESSING = resources.PROCESSING + STORAGE = resources.STORAGE + DATACENTER = resources.DATACENTER + WORKFLOW = resources.WORKFLOW ) -func (d LibData) EnumIndex() int { +func (d LibDataEnum) EnumIndex() int { return int(d) } +type LibData struct { + Data utils.DBObject `bson:"data" json:"data"` + Err error `bson:"error" json:"error"` +} + func Init() { mongo.MONGOService.Init(models.GetModelsNames(), GetConfig()) } -func LoadOne(col LibData, id string) interface{} { - return models.Model(col.EnumIndex()).GetAccessor().LoadOne(id) +func LoadOne(col LibDataEnum, id string) LibData { + 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{} { - return models.Model(col.EnumIndex()).GetAccessor().UpdateOne(set, id) +func UpdateOne(col LibDataEnum, set map[string]interface{}, id string) LibData { + d, err := models.Model(col.EnumIndex()).GetAccessor().UpdateOne(set, id) + return LibData{Data: d, Err: err} } -func DeleteOne(col LibData, id string) interface{} { - return models.Model(col.EnumIndex()).GetAccessor().DeleteOne(id) +func DeleteOne(col LibDataEnum, id string) LibData { + 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()) - return model.GetAccessor().StoreOne(model.Deserialize(object)) + d, err := model.GetAccessor().StoreOne(model.Deserialize(object)) + return LibData{Data: d, Err: err} } diff --git a/models/resources/data/data_mongo_accessor.go b/models/resources/data/data_mongo_accessor.go index f4b3b19..e4450f0 100644 --- a/models/resources/data/data_mongo_accessor.go +++ b/models/resources/data/data_mongo_accessor.go @@ -9,31 +9,31 @@ type DataMongoAccessor struct { utils.AbstractAccessor } -func (dma *DataMongoAccessor) DeleteOne(id string) utils.DBObject { +func (dma *DataMongoAccessor) DeleteOne(id string) (utils.DBObject, error) { 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) } -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") if err != nil { dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error()) - return &Data{} + return nil, err } return dma.LoadOne(id) } -func (dma *DataMongoAccessor) LoadOne(id string) utils.DBObject { +func (dma *DataMongoAccessor) LoadOne(id string) (utils.DBObject, error) { var data 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{} + return nil, err } res_mongo.Decode(&data) - return &data + return &data, nil } diff --git a/models/resources/data/data_test.go b/models/resources/data/data_test.go index f765332..5ba8353 100644 --- a/models/resources/data/data_test.go +++ b/models/resources/data/data_test.go @@ -22,7 +22,7 @@ func TestStoreOneData(t *testing.T) { } dma := DataMongoAccessor{} - id := dma.StoreOne(&d) + id, _ := dma.StoreOne(&d) assert.NotEmpty(t, id) } @@ -41,6 +41,6 @@ func TestLoadOneDate(t *testing.T) { } dma := DataMongoAccessor{} - new_d := dma.StoreOne(&d) + new_d, _ := dma.StoreOne(&d) assert.Equal(t, d, new_d) } diff --git a/models/resources/datacenter/datacenter_mongo_accessor.go b/models/resources/datacenter/datacenter_mongo_accessor.go index 1a2c15f..342899e 100644 --- a/models/resources/datacenter/datacenter_mongo_accessor.go +++ b/models/resources/datacenter/datacenter_mongo_accessor.go @@ -10,34 +10,34 @@ type DatacenterMongoAccessor struct { utils.AbstractAccessor } -func (dma *DatacenterMongoAccessor) DeleteOne(id string) utils.DBObject { +func (dma *DatacenterMongoAccessor) DeleteOne(id string) (utils.DBObject, error) { 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) } -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") if err != nil { dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error()) - return &Datacenter{} + return nil, err } return dma.LoadOne(id) } -func (dca *DatacenterMongoAccessor) LoadOne(id string) utils.DBObject { +func (dca *DatacenterMongoAccessor) LoadOne(id string) (utils.DBObject, error) { var datacenter 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()) - return &Datacenter{} + return nil, err } res_mongo.Decode(&datacenter) - return &datacenter + return &datacenter, nil } diff --git a/models/resources/datacenter/datacenter_test.go b/models/resources/datacenter/datacenter_test.go index 2d79e5f..ee7e8af 100644 --- a/models/resources/datacenter/datacenter_test.go +++ b/models/resources/datacenter/datacenter_test.go @@ -22,7 +22,7 @@ func TestStoreOneDatacenter(t *testing.T) { } dcma := DatacenterMongoAccessor{} - id := dcma.StoreOne(&dc) + id, _ := dcma.StoreOne(&dc) assert.NotEmpty(t, id) } @@ -41,7 +41,7 @@ func TestLoadOneDatacenter(t *testing.T) { } dcma := DatacenterMongoAccessor{} - new_dc := dcma.StoreOne(&dc) + new_dc, _ := dcma.StoreOne(&dc) assert.Equal(t, dc, new_dc) } diff --git a/models/resources/processing/processing_mongo_accessor.go b/models/resources/processing/processing_mongo_accessor.go index fea0dc9..b0c4da7 100644 --- a/models/resources/processing/processing_mongo_accessor.go +++ b/models/resources/processing/processing_mongo_accessor.go @@ -10,24 +10,24 @@ type ProcessingMongoAccessor struct { utils.AbstractAccessor } -func (dma *ProcessingMongoAccessor) DeleteOne(id string) utils.DBObject { +func (dma *ProcessingMongoAccessor) DeleteOne(id string) (utils.DBObject, error) { 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) } -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") if err != nil { dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error()) - return &Processing{} + return nil, err } return dma.LoadOne(id) } -func (pma *ProcessingMongoAccessor) LoadOne(id string) utils.DBObject { +func (pma *ProcessingMongoAccessor) LoadOne(id string) (utils.DBObject, error) { var processing Processing @@ -35,10 +35,10 @@ func (pma *ProcessingMongoAccessor) LoadOne(id string) utils.DBObject { if err != nil { l := logs.CreateLogger("oclib", "") l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error()) - return &Processing{} + return nil, err } res_mongo.Decode(&processing) - return &processing + return &processing, nil } diff --git a/models/resources/processing/processing_test.go b/models/resources/processing/processing_test.go index 1e22248..2642c2c 100644 --- a/models/resources/processing/processing_test.go +++ b/models/resources/processing/processing_test.go @@ -22,7 +22,7 @@ func TestStoreOneProcessing(t *testing.T) { } sma := ProcessingMongoAccessor{} - id := sma.StoreOne(&p) + id, _ := sma.StoreOne(&p) assert.NotEmpty(t, id) } @@ -41,6 +41,6 @@ func TestLoadOneProcessing(t *testing.T) { } sma := ProcessingMongoAccessor{} - new_s := sma.StoreOne(&p) + new_s, _ := sma.StoreOne(&p) assert.Equal(t, p, new_s) } diff --git a/models/resources/storage/storage_mongo_accessor.go b/models/resources/storage/storage_mongo_accessor.go index 4507e5c..d123bbd 100644 --- a/models/resources/storage/storage_mongo_accessor.go +++ b/models/resources/storage/storage_mongo_accessor.go @@ -10,24 +10,24 @@ type StorageMongoAccessor struct { utils.AbstractAccessor } -func (dma *StorageMongoAccessor) DeleteOne(id string) utils.DBObject { +func (dma *StorageMongoAccessor) DeleteOne(id string) (utils.DBObject, error) { 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) } -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") if err != nil { dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error()) - return &Storage{} + return nil, err } return dma.LoadOne(id) } -func (schedulema *StorageMongoAccessor) LoadOne(id string) utils.DBObject { +func (schedulema *StorageMongoAccessor) LoadOne(id string) (utils.DBObject, error) { var storage Storage @@ -35,10 +35,10 @@ func (schedulema *StorageMongoAccessor) LoadOne(id string) utils.DBObject { if err != nil { l := logs.CreateLogger("oclib", "") l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error()) - return &Storage{} + return nil, err } res_mongo.Decode(&storage) - return &storage + return &storage, nil } diff --git a/models/resources/storage/storage_test.go b/models/resources/storage/storage_test.go index c5c63ee..d5194de 100644 --- a/models/resources/storage/storage_test.go +++ b/models/resources/storage/storage_test.go @@ -22,7 +22,7 @@ func TestStoreOneStorage(t *testing.T) { } sma := StorageMongoAccessor{} - id := sma.StoreOne(&s) + id, _ := sma.StoreOne(&s) assert.NotEmpty(t, id) } @@ -41,7 +41,7 @@ func TestLoadOneStorage(t *testing.T) { } sma := StorageMongoAccessor{} - new_s := sma.StoreOne(&s) + new_s, _ := sma.StoreOne(&s) assert.Equal(t, s, new_s) } diff --git a/models/utils/abstracts.go b/models/utils/abstracts.go index ba84c3f..9d9bd4e 100644 --- a/models/utils/abstracts.go +++ b/models/utils/abstracts.go @@ -13,16 +13,21 @@ type AbstractAccessor struct { func (dma *AbstractAccessor) SetLogger() { dma.Logger = logs.CreateLogger("oclib", "") } -func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) DBObject { - res := accessor.LoadOne(id) - _, err := mongo.MONGOService.DeleteOne(id, "data") +func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBObject, error) { + res, err := accessor.LoadOne(id) + 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 { 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") 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 97db5d1..cf7b631 100644 --- a/models/utils/interfaces.go +++ b/models/utils/interfaces.go @@ -8,8 +8,8 @@ type DBObject interface { type Accessor interface { SetLogger() - LoadOne(id string) DBObject - DeleteOne(id string) DBObject - StoreOne(data DBObject) DBObject - UpdateOne(set map[string]interface{}, id string) DBObject + LoadOne(id string) (DBObject, error) + DeleteOne(id string) (DBObject, error) + StoreOne(data DBObject) (DBObject, error) + UpdateOne(set map[string]interface{}, id string) (DBObject, error) } diff --git a/models/workflow/workflow_mongo_accessor.go b/models/workflow/workflow_mongo_accessor.go index 1e7c00e..597abee 100644 --- a/models/workflow/workflow_mongo_accessor.go +++ b/models/workflow/workflow_mongo_accessor.go @@ -10,34 +10,32 @@ type WorkflowMongoAccessor struct { utils.AbstractAccessor } -func (dma *WorkflowMongoAccessor) DeleteOne(id string) utils.DBObject { +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 { +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 { +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 &Workflow{} + return nil, err } return dma.LoadOne(id) } -func (wfa *WorkflowMongoAccessor) LoadOne(id string) utils.DBObject { +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 &Workflow{} + return nil, err } - res_mongo.Decode(&workflow) - - return &workflow + return &workflow, nil } diff --git a/models/workflow/workflow_test.go b/models/workflow/workflow_test.go index cb9cded..d3a6163 100644 --- a/models/workflow/workflow_test.go +++ b/models/workflow/workflow_test.go @@ -21,7 +21,7 @@ func TestStoreOneWorkflow(t *testing.T) { } wma := WorkflowMongoAccessor{} - id := wma.StoreOne(&w) + id, _ := wma.StoreOne(&w) assert.NotEmpty(t, id) } @@ -39,6 +39,6 @@ func TestLoadOneWorkflow(t *testing.T) { } wma := WorkflowMongoAccessor{} - new_w := wma.StoreOne(&w) + new_w, _ := wma.StoreOne(&w) assert.Equal(t, w, new_w) }