From 9ea342c4c44277277867923b19063e5d8c1ee087 Mon Sep 17 00:00:00 2001 From: mr Date: Thu, 28 Nov 2024 13:19:01 +0100 Subject: [PATCH] some --- models/booking/booking.go | 6 +-- models/booking/booking_mongo_accessor.go | 14 +++++- .../collaborative_area/collaborative_area.go | 4 +- .../collaborative_area_mongo_accessor.go | 11 ++++- models/collaborative_area/rules/rule/rule.go | 4 +- .../rules/rule/rule_mongo_accessor.go | 14 +++++- .../shallow_collaborative_area.go | 4 +- ...allow_collaborative_area_mongo_accessor.go | 14 +++++- models/peer/peer.go | 5 +-- models/peer/peer_cache.go | 6 +-- models/peer/peer_mongo_accessor.go | 23 +++++++++- models/resources/compute/compute.go | 4 +- .../compute/compute_mongo_accessor.go | 19 +++++--- models/resources/compute/compute_test.go | 5 ++- models/resources/data/data.go | 4 +- models/resources/data/data_mongo_accessor.go | 9 +++- models/resources/data/data_test.go | 5 ++- models/resources/processing/processing.go | 4 +- .../processing/processing_mongo_accessor.go | 11 ++++- .../resource_model/resource_model.go | 11 +++-- models/resources/storage/storage.go | 4 +- .../storage/storage_mongo_accessor.go | 9 +++- models/resources/storage/storage_test.go | 5 ++- models/resources/workflow/workflow.go | 4 +- .../workflow/workflow_mongo_accessor.go | 9 +++- models/resources/workflow/workflow_test.go | 5 ++- models/utils/abstracts.go | 44 +++++-------------- models/utils/interfaces.go | 1 - models/workflow/workflow.go | 4 +- .../workflow_history_mongo_accessor.go | 4 +- models/workflow/workflow_mongo_accessor.go | 10 ++++- models/workflow/workflow_test.go | 5 ++- .../workflow_execution/workflow_execution.go | 4 +- .../workflow_execution_mongo_accessor.go | 14 +++++- models/workspace/workspace.go | 4 +- .../workspace_history_mongo_accessor.go | 4 +- models/workspace/workspace_mongo_accessor.go | 13 +++++- 37 files changed, 198 insertions(+), 122 deletions(-) diff --git a/models/booking/booking.go b/models/booking/booking.go index aa8a67d..e557b24 100644 --- a/models/booking/booking.go +++ b/models/booking/booking.go @@ -26,7 +26,7 @@ func (wfa *Booking) CheckBooking(id string, start time.Time, end *time.Time) (bo return true, nil } e := *end - accessor := New() + accessor := New(tools.BOOKING, "", nil, nil) res, code, err := accessor.Search(&dbs.Filters{ And: map[string][]dbs.Filter{ // check if there is a booking on the same compute resource by filtering on the compute_resource_id, the state and the execution date "compute_resource_id": {{Operator: dbs.EQUAL.String(), Value: id}}, @@ -54,7 +54,5 @@ func (d *Booking) GetName() string { } func (d *Booking) GetAccessor(peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { - data := New() // Create a new instance of the accessor - data.Init(tools.BOOKING, peerID, groups, caller) // Initialize the accessor with the BOOKING model type - return data + return New(tools.BOOKING, peerID, groups, caller) // Create a new instance of the accessor } diff --git a/models/booking/booking_mongo_accessor.go b/models/booking/booking_mongo_accessor.go index e41c770..e66b63d 100644 --- a/models/booking/booking_mongo_accessor.go +++ b/models/booking/booking_mongo_accessor.go @@ -5,8 +5,10 @@ import ( "cloud.o-forge.io/core/oc-lib/dbs" "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" "cloud.o-forge.io/core/oc-lib/models/workflow_execution" + "cloud.o-forge.io/core/oc-lib/tools" ) type bookingMongoAccessor struct { @@ -14,8 +16,16 @@ type bookingMongoAccessor struct { } // New creates a new instance of the bookingMongoAccessor -func New() *bookingMongoAccessor { - return &bookingMongoAccessor{} +func New(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCaller) *bookingMongoAccessor { + return &bookingMongoAccessor{ + AbstractAccessor: utils.AbstractAccessor{ + Logger: logs.CreateLogger(t.String()), // Create a logger with the data type + Caller: caller, + PeerID: peerID, + Groups: groups, // Set the caller + Type: t.String(), + }, + } } /* diff --git a/models/collaborative_area/collaborative_area.go b/models/collaborative_area/collaborative_area.go index cc8a472..7317e78 100644 --- a/models/collaborative_area/collaborative_area.go +++ b/models/collaborative_area/collaborative_area.go @@ -59,9 +59,7 @@ func (ao *CollaborativeArea) VerifyAuth(peerID string, groups []string) bool { } func (d *CollaborativeArea) GetAccessor(peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { - data := New(peerID, groups) // Create a new instance of the accessor - data.Init(tools.COLLABORATIVE_AREA, peerID, groups, caller) // Initialize the accessor with the SHARED_WORKSPACE model type - return data + return New(tools.COLLABORATIVE_AREA, peerID, groups, caller) // Create a new instance of the accessor } func (d *CollaborativeArea) Trim() *CollaborativeArea { diff --git a/models/collaborative_area/collaborative_area_mongo_accessor.go b/models/collaborative_area/collaborative_area_mongo_accessor.go index dab8fc4..19a51dd 100644 --- a/models/collaborative_area/collaborative_area_mongo_accessor.go +++ b/models/collaborative_area/collaborative_area_mongo_accessor.go @@ -9,6 +9,7 @@ import ( "cloud.o-forge.io/core/oc-lib/config" "cloud.o-forge.io/core/oc-lib/dbs" "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/collaborative_area/rules/rule" "cloud.o-forge.io/core/oc-lib/models/peer" "cloud.o-forge.io/core/oc-lib/models/utils" @@ -27,9 +28,15 @@ type collaborativeAreaMongoAccessor struct { ruleAccessor utils.Accessor } -// New creates a new instance of the collaborativeAreaMongoAccessor -func New(peerID string, groups []string) *collaborativeAreaMongoAccessor { +func New(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCaller) *collaborativeAreaMongoAccessor { return &collaborativeAreaMongoAccessor{ + AbstractAccessor: utils.AbstractAccessor{ + Logger: logs.CreateLogger(t.String()), // Create a logger with the data type + Caller: caller, + PeerID: peerID, + Groups: groups, // Set the caller + Type: t.String(), + }, workspaceAccessor: (&workspace.Workspace{}).GetAccessor(peerID, groups, nil), workflowAccessor: (&w.Workflow{}).GetAccessor(peerID, groups, nil), peerAccessor: (&peer.Peer{}).GetAccessor(peerID, groups, nil), diff --git a/models/collaborative_area/rules/rule/rule.go b/models/collaborative_area/rules/rule/rule.go index be95d90..1a2f2ec 100644 --- a/models/collaborative_area/rules/rule/rule.go +++ b/models/collaborative_area/rules/rule/rule.go @@ -21,7 +21,5 @@ func (r *Rule) GenerateID() { } func (d *Rule) GetAccessor(peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { - data := New() - data.Init(tools.RULE, peerID, groups, caller) - return data + return New(tools.RULE, peerID, groups, caller) } diff --git a/models/collaborative_area/rules/rule/rule_mongo_accessor.go b/models/collaborative_area/rules/rule/rule_mongo_accessor.go index 2278252..d3d7fe6 100644 --- a/models/collaborative_area/rules/rule/rule_mongo_accessor.go +++ b/models/collaborative_area/rules/rule/rule_mongo_accessor.go @@ -3,7 +3,9 @@ package rule import ( "cloud.o-forge.io/core/oc-lib/dbs" "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" + "cloud.o-forge.io/core/oc-lib/tools" ) type ruleMongoAccessor struct { @@ -11,8 +13,16 @@ type ruleMongoAccessor struct { } // New creates a new instance of the ruleMongoAccessor -func New() *ruleMongoAccessor { - return &ruleMongoAccessor{} +func New(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCaller) *ruleMongoAccessor { + return &ruleMongoAccessor{ + AbstractAccessor: utils.AbstractAccessor{ + Logger: logs.CreateLogger(t.String()), // Create a logger with the data type + Caller: caller, + PeerID: peerID, + Groups: groups, // Set the caller + Type: t.String(), + }, + } } // GetType returns the type of the rule diff --git a/models/collaborative_area/shallow_collaborative_area/shallow_collaborative_area.go b/models/collaborative_area/shallow_collaborative_area/shallow_collaborative_area.go index cf95b1e..24429d3 100644 --- a/models/collaborative_area/shallow_collaborative_area/shallow_collaborative_area.go +++ b/models/collaborative_area/shallow_collaborative_area/shallow_collaborative_area.go @@ -19,7 +19,5 @@ type ShallowCollaborativeArea struct { } func (d *ShallowCollaborativeArea) GetAccessor(peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { - data := New() - data.Init(tools.COLLABORATIVE_AREA, peerID, groups, caller) - return data + return New(tools.COLLABORATIVE_AREA, peerID, groups, caller) } diff --git a/models/collaborative_area/shallow_collaborative_area/shallow_collaborative_area_mongo_accessor.go b/models/collaborative_area/shallow_collaborative_area/shallow_collaborative_area_mongo_accessor.go index 2f5bfd5..72fb553 100644 --- a/models/collaborative_area/shallow_collaborative_area/shallow_collaborative_area_mongo_accessor.go +++ b/models/collaborative_area/shallow_collaborative_area/shallow_collaborative_area_mongo_accessor.go @@ -3,15 +3,25 @@ package shallow_collaborative_area import ( "cloud.o-forge.io/core/oc-lib/dbs" "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" + "cloud.o-forge.io/core/oc-lib/tools" ) type shallowSharedWorkspaceMongoAccessor struct { utils.AbstractAccessor } -func New() *shallowSharedWorkspaceMongoAccessor { - return &shallowSharedWorkspaceMongoAccessor{} +func New(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCaller) *shallowSharedWorkspaceMongoAccessor { + return &shallowSharedWorkspaceMongoAccessor{ + AbstractAccessor: utils.AbstractAccessor{ + Logger: logs.CreateLogger(t.String()), // Create a logger with the data type + Caller: caller, + PeerID: peerID, + Groups: groups, // Set the caller + Type: t.String(), + }, + } } func (wfa *shallowSharedWorkspaceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) { diff --git a/models/peer/peer.go b/models/peer/peer.go index 2746898..170fa3a 100644 --- a/models/peer/peer.go +++ b/models/peer/peer.go @@ -63,7 +63,7 @@ func (ao *Peer) RemoveExecution(exec PeerExecution) { // IsMySelf checks if the peer is the local peer func (ao *Peer) IsMySelf() (bool, string) { - d, code, err := New().Search(nil, SELF.String()) + d, code, err := New(tools.PEER, "", nil, nil).Search(nil, SELF.String()) if code != 200 || err != nil || len(d) == 0 { return false, "" } @@ -77,7 +77,6 @@ func (p *Peer) LaunchPeerExecution(peerID string, dataID string, dt tools.DataTy return cache.LaunchPeerExecution(peerID, dataID, dt, method, body, caller) // Launch the execution on the peer through the cache } func (d *Peer) GetAccessor(peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { - data := New() // Create a new instance of the accessor - data.Init(tools.PEER, peerID, groups, caller) // Initialize the accessor with the PEER model type + data := New(tools.PEER, peerID, groups, caller) // Create a new instance of the accessor return data } diff --git a/models/peer/peer_cache.go b/models/peer/peer_cache.go index 104adc1..8a5bae4 100644 --- a/models/peer/peer_cache.go +++ b/models/peer/peer_cache.go @@ -56,7 +56,7 @@ func (p *PeerCache) urlFormat(url string, dt tools.DataType) string { // checkPeerStatus checks the status of a peer func (p *PeerCache) checkPeerStatus(peerID string, appName string, caller *tools.HTTPCaller) (*Peer, bool) { api := tools.API{} - access := New() + access := NewShallow() res, code, _ := access.LoadOne(peerID) // Load the peer from db if code != 200 { // no peer no party return nil, false @@ -101,7 +101,7 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string, DataID: dataID, } mypeer.AddExecution(*pexec) - New().UpdateOne(mypeer, peerID) // Update the peer in the db + NewShallow().UpdateOne(mypeer, peerID) // Update the peer in the db return nil, errors.New("peer is not reachable") } else { if mypeer == nil { @@ -111,7 +111,7 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string, url = p.urlFormat((mypeer.Url)+meth, dt) // Format the URL tmp := mypeer.FailedExecution // Get the failed executions list mypeer.FailedExecution = []PeerExecution{} // Reset the failed executions list - New().UpdateOne(mypeer, peerID) // Update the peer in the db + NewShallow().UpdateOne(mypeer, peerID) // Update the peer in the db for _, v := range tmp { // Retry the failed executions go p.exec(v.Url, tools.ToMethod(v.Method), v.Body, caller) } diff --git a/models/peer/peer_mongo_accessor.go b/models/peer/peer_mongo_accessor.go index f14cabd..c357f0e 100644 --- a/models/peer/peer_mongo_accessor.go +++ b/models/peer/peer_mongo_accessor.go @@ -5,7 +5,9 @@ import ( "cloud.o-forge.io/core/oc-lib/dbs" "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" + "cloud.o-forge.io/core/oc-lib/tools" ) type peerMongoAccessor struct { @@ -13,8 +15,25 @@ type peerMongoAccessor struct { } // New creates a new instance of the peerMongoAccessor -func New() *peerMongoAccessor { - return &peerMongoAccessor{} +func NewShallow() *peerMongoAccessor { + return &peerMongoAccessor{ + utils.AbstractAccessor{ + Logger: logs.CreateLogger(tools.PEER.String()), // Create a logger with the data type + Type: tools.PEER.String(), + }, + } +} + +func New(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCaller) *peerMongoAccessor { + return &peerMongoAccessor{ + utils.AbstractAccessor{ + Logger: logs.CreateLogger(t.String()), // Create a logger with the data type + Caller: caller, + PeerID: peerID, + Groups: groups, // Set the caller + Type: t.String(), + }, + } } /* diff --git a/models/resources/compute/compute.go b/models/resources/compute/compute.go index 9307edc..80597c6 100644 --- a/models/resources/compute/compute.go +++ b/models/resources/compute/compute.go @@ -53,9 +53,7 @@ type ComputeResource struct { } func (d *ComputeResource) GetAccessor(peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { - data := New(peerID, groups) - data.Init(tools.COMPUTE_RESOURCE, peerID, groups, caller) - return data + return New(tools.COMPUTE_RESOURCE, peerID, groups, caller) } // CPU is a struct that represents a CPU diff --git a/models/resources/compute/compute_mongo_accessor.go b/models/resources/compute/compute_mongo_accessor.go index 9b84cca..2c8a067 100644 --- a/models/resources/compute/compute_mongo_accessor.go +++ b/models/resources/compute/compute_mongo_accessor.go @@ -2,8 +2,10 @@ package compute import ( "cloud.o-forge.io/core/oc-lib/dbs" + "cloud.o-forge.io/core/oc-lib/logs" "cloud.o-forge.io/core/oc-lib/models/resources/resource_model" "cloud.o-forge.io/core/oc-lib/models/utils" + "cloud.o-forge.io/core/oc-lib/tools" ) type computeMongoAccessor struct { @@ -11,10 +13,15 @@ type computeMongoAccessor struct { } // New creates a new instance of the computeMongoAccessor -func New(peerID string, groups []string) *computeMongoAccessor { +func New(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCaller) *computeMongoAccessor { return &computeMongoAccessor{ utils.AbstractAccessor{ ResourceModelAccessor: resource_model.New(), + Logger: logs.CreateLogger(t.String()), // Create a logger with the data type + Caller: caller, + PeerID: peerID, + Groups: groups, // Set the caller + Type: t.String(), }, } } @@ -53,12 +60,11 @@ func (dca *computeMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) func (wfa *computeMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) { resources, _, err := wfa.ResourceModelAccessor.Search(nil, wfa.GetType()) - return utils.GenericLoadAll[*ComputeResource](func(d utils.DBObject, a []utils.ShallowDBObject) []utils.ShallowDBObject { + return utils.GenericLoadAll[*ComputeResource](func(d utils.DBObject) utils.ShallowDBObject { if err == nil && len(resources) > 0 { d.(*ComputeResource).ResourceModel = resources[0].(*resource_model.ResourceModel) } - a = append(a, d) // only get the abstract resource ! - return a + return d }, wfa) } @@ -66,11 +72,10 @@ func (wfa *computeMongoAccessor) Search(filters *dbs.Filters, search string) ([] root := &ComputeResource{} resources, _, err := wfa.ResourceModelAccessor.Search(nil, wfa.GetType()) return utils.GenericSearch[*ComputeResource](filters, search, root.GetResourceFilter(search), - func(d utils.DBObject, a []utils.ShallowDBObject) []utils.ShallowDBObject { + func(d utils.DBObject) utils.ShallowDBObject { if err == nil && len(resources) > 0 { d.(*ComputeResource).ResourceModel = resources[0].(*resource_model.ResourceModel) } - a = append(a, d) // only get the abstract resource ! - return a + return d }, wfa) } diff --git a/models/resources/compute/compute_test.go b/models/resources/compute/compute_test.go index f75ed1f..b2b14ec 100644 --- a/models/resources/compute/compute_test.go +++ b/models/resources/compute/compute_test.go @@ -5,6 +5,7 @@ import ( "cloud.o-forge.io/core/oc-lib/models/resources/resource_model" "cloud.o-forge.io/core/oc-lib/models/utils" + "cloud.o-forge.io/core/oc-lib/tools" "github.com/stretchr/testify/assert" ) @@ -21,7 +22,7 @@ func TestStoreOneCompute(t *testing.T) { }, } - dcma := New("", nil) + dcma := New(tools.COMPUTE_RESOURCE, "", nil, nil) id, _, _ := dcma.StoreOne(&dc) assert.NotEmpty(t, id) @@ -39,7 +40,7 @@ func TestLoadOneCompute(t *testing.T) { }, } - dcma := New("", nil) + dcma := New(tools.COMPUTE_RESOURCE, "", nil, nil) new_dc, _, _ := dcma.StoreOne(&dc) assert.Equal(t, dc, new_dc) diff --git a/models/resources/data/data.go b/models/resources/data/data.go index c823bd5..6a1f75c 100644 --- a/models/resources/data/data.go +++ b/models/resources/data/data.go @@ -38,7 +38,5 @@ type DataResource struct { } func (d *DataResource) GetAccessor(peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { - data := New() // Create a new instance of the accessor - data.Init(tools.DATA_RESOURCE, peerID, groups, caller) // Initialize the accessor with the DATA_RESOURCE model type - return data + return New(tools.DATA_RESOURCE, peerID, groups, caller) // Create a new instance of the accessor } diff --git a/models/resources/data/data_mongo_accessor.go b/models/resources/data/data_mongo_accessor.go index dccfc23..5b60c3b 100644 --- a/models/resources/data/data_mongo_accessor.go +++ b/models/resources/data/data_mongo_accessor.go @@ -5,8 +5,10 @@ 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/models/resources/resource_model" "cloud.o-forge.io/core/oc-lib/models/utils" + "cloud.o-forge.io/core/oc-lib/tools" ) type dataMongoAccessor struct { @@ -14,10 +16,15 @@ type dataMongoAccessor struct { } // New creates a new instance of the dataMongoAccessor -func New() *dataMongoAccessor { +func New(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCaller) *dataMongoAccessor { return &dataMongoAccessor{ utils.AbstractAccessor{ ResourceModelAccessor: resource_model.New(), + Logger: logs.CreateLogger(t.String()), // Create a logger with the data type + Caller: caller, + PeerID: peerID, + Groups: groups, // Set the caller + Type: t.String(), }, } } diff --git a/models/resources/data/data_test.go b/models/resources/data/data_test.go index 00f5861..0cb5e34 100644 --- a/models/resources/data/data_test.go +++ b/models/resources/data/data_test.go @@ -5,6 +5,7 @@ import ( "cloud.o-forge.io/core/oc-lib/models/resources/resource_model" "cloud.o-forge.io/core/oc-lib/models/utils" + "cloud.o-forge.io/core/oc-lib/tools" "github.com/stretchr/testify/assert" ) @@ -25,7 +26,7 @@ func TestStoreOneData(t *testing.T) { }, } - dma := New() + dma := New(tools.DATA_RESOURCE, "", nil, nil) id, _, _ := dma.StoreOne(&d) assert.NotEmpty(t, id) @@ -47,7 +48,7 @@ func TestLoadOneDate(t *testing.T) { }, } - dma := New() + dma := New(tools.DATA_RESOURCE, "", nil, nil) new_d, _, _ := dma.StoreOne(&d) assert.Equal(t, d, new_d) } diff --git a/models/resources/processing/processing.go b/models/resources/processing/processing.go index 05f8bbd..4d971ff 100644 --- a/models/resources/processing/processing.go +++ b/models/resources/processing/processing.go @@ -40,7 +40,5 @@ type ProcessingResource struct { } func (d *ProcessingResource) GetAccessor(peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { - data := New() // Create a new instance of the accessor - data.Init(tools.PROCESSING_RESOURCE, peerID, groups, caller) // Initialize the accessor with the PROCESSING_RESOURCE model type - return data + return New(tools.PROCESSING_RESOURCE, peerID, groups, caller) // Create a new instance of the accessor } diff --git a/models/resources/processing/processing_mongo_accessor.go b/models/resources/processing/processing_mongo_accessor.go index 614db32..8385258 100644 --- a/models/resources/processing/processing_mongo_accessor.go +++ b/models/resources/processing/processing_mongo_accessor.go @@ -5,19 +5,26 @@ import ( "cloud.o-forge.io/core/oc-lib/dbs" "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/resource_model" "cloud.o-forge.io/core/oc-lib/models/utils" + "cloud.o-forge.io/core/oc-lib/tools" ) type processingMongoAccessor struct { utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller) } -// New creates a new instance of the processingMongoAccessor -func New() *processingMongoAccessor { +// New creates a new instance of the storageMongoAccessor +func New(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCaller) *processingMongoAccessor { return &processingMongoAccessor{ utils.AbstractAccessor{ ResourceModelAccessor: resource_model.New(), + Logger: logs.CreateLogger(t.String()), // Create a logger with the data type + Caller: caller, + PeerID: peerID, + Groups: groups, // Set the caller + Type: t.String(), }, } } diff --git a/models/resources/resource_model/resource_model.go b/models/resources/resource_model/resource_model.go index c49b42a..32fecda 100644 --- a/models/resources/resource_model/resource_model.go +++ b/models/resources/resource_model/resource_model.go @@ -144,9 +144,14 @@ func (abs *ResourceModel) VerifyAuth(peerID string, groups []string) bool { } func (d *ResourceModel) GetAccessor(peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { - data := &ResourceModelMongoAccessor{} - data.Init(tools.RESOURCE_MODEL, peerID, groups, caller) - return data + return &ResourceModelMongoAccessor{ + utils.AbstractAccessor{ + Type: tools.RESOURCE_MODEL.String(), + PeerID: peerID, + Groups: groups, + Caller: caller, + }, + } } func (dma *ResourceModel) Deserialize(j map[string]interface{}, obj utils.DBObject) utils.DBObject { diff --git a/models/resources/storage/storage.go b/models/resources/storage/storage.go index d8ebbf8..9604a01 100644 --- a/models/resources/storage/storage.go +++ b/models/resources/storage/storage.go @@ -57,7 +57,5 @@ type StorageResource struct { } func (d *StorageResource) GetAccessor(peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { - data := New() // Create a new instance of the accessor - data.Init(tools.STORAGE_RESOURCE, peerID, groups, caller) // Initialize the accessor with the STORAGE_RESOURCE model type - return data + return New(tools.STORAGE_RESOURCE, peerID, groups, caller) // Create a new instance of the accessor } diff --git a/models/resources/storage/storage_mongo_accessor.go b/models/resources/storage/storage_mongo_accessor.go index 49421ff..098335e 100644 --- a/models/resources/storage/storage_mongo_accessor.go +++ b/models/resources/storage/storage_mongo_accessor.go @@ -5,8 +5,10 @@ import ( "cloud.o-forge.io/core/oc-lib/dbs" "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/resource_model" "cloud.o-forge.io/core/oc-lib/models/utils" + "cloud.o-forge.io/core/oc-lib/tools" ) type storageMongoAccessor struct { @@ -14,10 +16,15 @@ type storageMongoAccessor struct { } // New creates a new instance of the storageMongoAccessor -func New() *storageMongoAccessor { +func New(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCaller) *storageMongoAccessor { return &storageMongoAccessor{ utils.AbstractAccessor{ ResourceModelAccessor: resource_model.New(), + Logger: logs.CreateLogger(t.String()), // Create a logger with the data type + Caller: caller, + PeerID: peerID, + Groups: groups, // Set the caller + Type: t.String(), }, } } diff --git a/models/resources/storage/storage_test.go b/models/resources/storage/storage_test.go index 586fea6..ad30fe9 100644 --- a/models/resources/storage/storage_test.go +++ b/models/resources/storage/storage_test.go @@ -5,6 +5,7 @@ import ( "cloud.o-forge.io/core/oc-lib/models/resources/resource_model" "cloud.o-forge.io/core/oc-lib/models/utils" + "cloud.o-forge.io/core/oc-lib/tools" "github.com/stretchr/testify/assert" ) @@ -21,7 +22,7 @@ func TestStoreOneStorage(t *testing.T) { }, } - sma := New() + sma := New(tools.STORAGE_RESOURCE, "peerID", []string{}, nil) id, _, _ := sma.StoreOne(&s) assert.NotEmpty(t, id) @@ -39,7 +40,7 @@ func TestLoadOneStorage(t *testing.T) { }, } - sma := New() + sma := New(tools.STORAGE_RESOURCE, "peerID", []string{}, nil) new_s, _, _ := sma.StoreOne(&s) assert.Equal(t, s, new_s) diff --git a/models/resources/workflow/workflow.go b/models/resources/workflow/workflow.go index ba0c454..5fdd7a7 100644 --- a/models/resources/workflow/workflow.go +++ b/models/resources/workflow/workflow.go @@ -14,7 +14,5 @@ type WorkflowResource struct { } func (d *WorkflowResource) GetAccessor(peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { - data := New() // Create a new instance of the accessor - data.Init(tools.WORKFLOW_RESOURCE, peerID, groups, caller) // Initialize the accessor with the WORKFLOW_RESOURCE model type - return data + return New(tools.WORKFLOW_RESOURCE, peerID, groups, caller) // Create a new instance of the accessor } diff --git a/models/resources/workflow/workflow_mongo_accessor.go b/models/resources/workflow/workflow_mongo_accessor.go index 2ca9c6a..7dd0c5f 100644 --- a/models/resources/workflow/workflow_mongo_accessor.go +++ b/models/resources/workflow/workflow_mongo_accessor.go @@ -5,18 +5,25 @@ import ( "cloud.o-forge.io/core/oc-lib/dbs" "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/resource_model" "cloud.o-forge.io/core/oc-lib/models/utils" + "cloud.o-forge.io/core/oc-lib/tools" ) type workflowResourceMongoAccessor struct { utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller) } -func New() *workflowResourceMongoAccessor { +func New(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCaller) *workflowResourceMongoAccessor { return &workflowResourceMongoAccessor{ utils.AbstractAccessor{ ResourceModelAccessor: resource_model.New(), + Logger: logs.CreateLogger(t.String()), // Create a logger with the data type + Caller: caller, + PeerID: peerID, + Groups: groups, // Set the caller + Type: t.String(), }, } } diff --git a/models/resources/workflow/workflow_test.go b/models/resources/workflow/workflow_test.go index 3cfd693..7d30257 100644 --- a/models/resources/workflow/workflow_test.go +++ b/models/resources/workflow/workflow_test.go @@ -5,6 +5,7 @@ import ( "cloud.o-forge.io/core/oc-lib/models/resources/resource_model" "cloud.o-forge.io/core/oc-lib/models/utils" + "cloud.o-forge.io/core/oc-lib/tools" "github.com/stretchr/testify/assert" ) @@ -20,7 +21,7 @@ func TestStoreOneWorkflow(t *testing.T) { }, } - wma := New() + wma := New(tools.WORKFLOW_RESOURCE, "peerID", []string{}, nil) id, _, _ := wma.StoreOne(&w) assert.NotEmpty(t, id) @@ -37,7 +38,7 @@ func TestLoadOneWorkflow(t *testing.T) { }, } - wma := New() + wma := New(tools.WORKFLOW_RESOURCE, "peerID", []string{}, nil) new_w, _, _ := wma.StoreOne(&w) assert.Equal(t, w, new_w) } diff --git a/models/utils/abstracts.go b/models/utils/abstracts.go index cfe2642..03fd075 100644 --- a/models/utils/abstracts.go +++ b/models/utils/abstracts.go @@ -7,11 +7,11 @@ import ( "cloud.o-forge.io/core/oc-lib/dbs" "cloud.o-forge.io/core/oc-lib/dbs/mongo" - "cloud.o-forge.io/core/oc-lib/logs" "cloud.o-forge.io/core/oc-lib/tools" "github.com/go-playground/validator/v10" "github.com/google/uuid" "github.com/rs/zerolog" + mgb "go.mongodb.org/mongo-driver/mongo" ) // single instance of the validator used in every model Struct to validate the fields @@ -100,15 +100,6 @@ func (dma *AbstractAccessor) GetCaller() *tools.HTTPCaller { return dma.Caller } -// Init initializes the accessor with the data type and the http caller -func (dma *AbstractAccessor) Init(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCaller) { - dma.Logger = logs.CreateLogger(t.String()) // Create a logger with the data type - dma.Caller = caller - dma.PeerID = peerID - dma.Groups = groups // Set the caller - dma.Type = t.String() // Set the data type -} - // GenericLoadOne loads one object from the database (generic) func (wfa *AbstractAccessor) GenericStoreOne(data DBObject, accessor Accessor) (DBObject, int, error) { data.GenerateID() @@ -189,48 +180,37 @@ func GenericLoadOne[T DBObject](id string, f func(DBObject) (DBObject, int, erro return f(data) } -func GenericLoadAll[T DBObject](f func(DBObject, []ShallowDBObject) []ShallowDBObject, wfa Accessor) ([]ShallowDBObject, int, error) { - results := []T{} +func genericLoadAll[T DBObject](res *mgb.Cursor, code int, err error, f func(DBObject) ShallowDBObject, wfa Accessor) ([]ShallowDBObject, int, error) { objs := []ShallowDBObject{} - res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType()) + results := []T{} if err != nil { wfa.GetLogger().Error().Msg("Could not retrieve any from db. Error: " + err.Error()) return nil, code, err } - if err = res_mongo.All(mongo.MngoCtx, &results); err != nil { + if err = res.All(mongo.MngoCtx, &results); err != nil { return nil, 404, err } for _, r := range results { if !r.VerifyAuth(wfa.GetPeerID(), wfa.GetGroups()) { continue } - objs = f(r, objs) + objs = append(objs, f(r)) } return objs, 200, nil } +func GenericLoadAll[T DBObject](f func(DBObject) ShallowDBObject, wfa Accessor) ([]ShallowDBObject, int, error) { + res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType()) + return genericLoadAll[T](res_mongo, code, err, f, wfa) +} + func GenericSearch[T DBObject](filters *dbs.Filters, search string, defaultFilters *dbs.Filters, - f func(DBObject, []ShallowDBObject) []ShallowDBObject, wfa Accessor) ([]ShallowDBObject, int, error) { - results := []T{} - objs := []ShallowDBObject{} + f func(DBObject) ShallowDBObject, wfa Accessor) ([]ShallowDBObject, int, error) { if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" { filters = defaultFilters } res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType()) - if err != nil { - wfa.GetLogger().Error().Msg("Could not store to db. Error: " + err.Error()) - return nil, code, err - } - if err = res_mongo.All(mongo.MngoCtx, &results); err != nil { - return nil, 404, err - } - for _, r := range results { - if !r.VerifyAuth(wfa.GetPeerID(), wfa.GetGroups()) { - continue - } - objs = f(r, objs) - } - return objs, 200, nil + return genericLoadAll[T](res_mongo, code, err, f, wfa) } // GenericLoadOne loads one object from the database (generic) diff --git a/models/utils/interfaces.go b/models/utils/interfaces.go index e2cd5dd..5172980 100644 --- a/models/utils/interfaces.go +++ b/models/utils/interfaces.go @@ -29,7 +29,6 @@ type DBObject interface { // Accessor is an interface that defines the basic methods for an Accessor type Accessor interface { - Init(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCaller) GetType() string GetPeerID() string GetGroups() []string diff --git a/models/workflow/workflow.go b/models/workflow/workflow.go index a7122d9..4956dc9 100644 --- a/models/workflow/workflow.go +++ b/models/workflow/workflow.go @@ -129,7 +129,5 @@ func (wfa *Workflow) CheckBooking(caller *tools.HTTPCaller) (bool, error) { } func (d *Workflow) GetAccessor(peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { - data := New(peerID, groups) // Create a new instance of the accessor - data.Init(tools.WORKFLOW, peerID, groups, caller) // Initialize the accessor with the WORKFLOW model type - return data + return New(tools.WORKFLOW, peerID, groups, caller) // Create a new instance of the accessor } diff --git a/models/workflow/workflow_history_mongo_accessor.go b/models/workflow/workflow_history_mongo_accessor.go index 19cf6a8..09ae09e 100644 --- a/models/workflow/workflow_history_mongo_accessor.go +++ b/models/workflow/workflow_history_mongo_accessor.go @@ -9,9 +9,7 @@ import ( type WorkflowHistory struct{ Workflow } func (d *WorkflowHistory) GetAccessor(peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { - data := New(peerID, groups) // Create a new instance of the accessor - data.Init(tools.WORKSPACE_HISTORY, peerID, groups, caller) // Initialize the accessor with the WORKSPACE model type - return data + return New(tools.WORKSPACE_HISTORY, peerID, groups, caller) // Create a new instance of the accessor } func (r *WorkflowHistory) GenerateID() { r.UUID = uuid.New().String() diff --git a/models/workflow/workflow_mongo_accessor.go b/models/workflow/workflow_mongo_accessor.go index 6a1ac04..09add7f 100644 --- a/models/workflow/workflow_mongo_accessor.go +++ b/models/workflow/workflow_mongo_accessor.go @@ -9,6 +9,7 @@ import ( "cloud.o-forge.io/core/oc-lib/dbs" "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/collaborative_area/shallow_collaborative_area" "cloud.o-forge.io/core/oc-lib/models/peer" "cloud.o-forge.io/core/oc-lib/models/resources" @@ -30,12 +31,19 @@ type workflowMongoAccessor struct { } // New creates a new instance of the workflowMongoAccessor -func New(peerID string, groups []string) *workflowMongoAccessor { +func New(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCaller) *workflowMongoAccessor { return &workflowMongoAccessor{ computeResourceAccessor: (&compute.ComputeResource{}).GetAccessor(peerID, groups, nil), collaborativeAreaAccessor: (&shallow_collaborative_area.ShallowCollaborativeArea{}).GetAccessor(peerID, groups, nil), executionAccessor: (&workflow_execution.WorkflowExecution{}).GetAccessor(peerID, groups, nil), workspaceAccessor: (&workspace.Workspace{}).GetAccessor(peerID, groups, nil), + AbstractAccessor: utils.AbstractAccessor{ + Logger: logs.CreateLogger(t.String()), // Create a logger with the data type + Caller: caller, + PeerID: peerID, + Groups: groups, // Set the caller + Type: t.String(), + }, } } diff --git a/models/workflow/workflow_test.go b/models/workflow/workflow_test.go index e46d55e..e5345bc 100644 --- a/models/workflow/workflow_test.go +++ b/models/workflow/workflow_test.go @@ -4,6 +4,7 @@ import ( "testing" "cloud.o-forge.io/core/oc-lib/models/utils" + "cloud.o-forge.io/core/oc-lib/tools" "github.com/stretchr/testify/assert" ) @@ -12,7 +13,7 @@ func TestStoreOneWorkflow(t *testing.T) { AbstractObject: utils.AbstractObject{Name: "testWorkflow"}, } - wma := New("", nil) + wma := New(tools.WORKFLOW, "", nil, nil) id, _, _ := wma.StoreOne(&w) assert.NotEmpty(t, id) @@ -23,7 +24,7 @@ func TestLoadOneWorkflow(t *testing.T) { AbstractObject: utils.AbstractObject{Name: "testWorkflow"}, } - wma := New("", nil) + wma := New(tools.WORKFLOW, "", nil, nil) new_w, _, _ := wma.StoreOne(&w) assert.Equal(t, w, new_w) } diff --git a/models/workflow_execution/workflow_execution.go b/models/workflow_execution/workflow_execution.go index db7dfce..a948984 100644 --- a/models/workflow_execution/workflow_execution.go +++ b/models/workflow_execution/workflow_execution.go @@ -116,7 +116,5 @@ func (d *WorkflowExecution) GetName() string { } func (d *WorkflowExecution) GetAccessor(peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { - data := New() // Create a new instance of the accessor - data.Init(tools.WORKFLOW_EXECUTION, peerID, groups, caller) // Initialize the accessor with the WORKFLOW_EXECUTION model type - return data + return New(tools.WORKFLOW_EXECUTION, peerID, groups, caller) // Create a new instance of the accessor } diff --git a/models/workflow_execution/workflow_execution_mongo_accessor.go b/models/workflow_execution/workflow_execution_mongo_accessor.go index 4ac365b..0a0975a 100644 --- a/models/workflow_execution/workflow_execution_mongo_accessor.go +++ b/models/workflow_execution/workflow_execution_mongo_accessor.go @@ -5,15 +5,25 @@ import ( "cloud.o-forge.io/core/oc-lib/dbs" "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" + "cloud.o-forge.io/core/oc-lib/tools" ) type workflowExecutionMongoAccessor struct { utils.AbstractAccessor } -func New() *workflowExecutionMongoAccessor { - return &workflowExecutionMongoAccessor{} +func New(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCaller) *workflowExecutionMongoAccessor { + return &workflowExecutionMongoAccessor{ + utils.AbstractAccessor{ + Logger: logs.CreateLogger(t.String()), // Create a logger with the data type + Caller: caller, + PeerID: peerID, + Groups: groups, // Set the caller + Type: t.String(), + }, + } } func (wfa *workflowExecutionMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) { diff --git a/models/workspace/workspace.go b/models/workspace/workspace.go index b4405e4..7b03386 100644 --- a/models/workspace/workspace.go +++ b/models/workspace/workspace.go @@ -16,7 +16,5 @@ type Workspace struct { } func (d *Workspace) GetAccessor(peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { - data := New(peerID, groups) // Create a new instance of the accessor - data.Init(tools.WORKSPACE, peerID, groups, caller) // Initialize the accessor with the WORKSPACE model type - return data + return New(tools.WORKSPACE, peerID, groups, caller) // Create a new instance of the accessor } diff --git a/models/workspace/workspace_history_mongo_accessor.go b/models/workspace/workspace_history_mongo_accessor.go index eb79fd0..bacc566 100644 --- a/models/workspace/workspace_history_mongo_accessor.go +++ b/models/workspace/workspace_history_mongo_accessor.go @@ -9,9 +9,7 @@ import ( type WorkspaceHistory struct{ Workspace } func (d *WorkspaceHistory) GetAccessor(peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { - data := New(peerID, groups) // Create a new instance of the accessor - data.Init(tools.WORKSPACE_HISTORY, peerID, groups, caller) // Initialize the accessor with the WORKSPACE model type - return data + return New(tools.WORKFLOW_HISTORY, peerID, groups, caller) // Create a new instance of the accessor } func (r *WorkspaceHistory) GenerateID() { r.UUID = uuid.New().String() diff --git a/models/workspace/workspace_mongo_accessor.go b/models/workspace/workspace_mongo_accessor.go index ee89e00..1de546d 100644 --- a/models/workspace/workspace_mongo_accessor.go +++ b/models/workspace/workspace_mongo_accessor.go @@ -6,6 +6,7 @@ import ( "cloud.o-forge.io/core/oc-lib/dbs" "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/collaborative_area/shallow_collaborative_area" "cloud.o-forge.io/core/oc-lib/models/peer" "cloud.o-forge.io/core/oc-lib/models/utils" @@ -18,8 +19,16 @@ type workspaceMongoAccessor struct { } // New creates a new instance of the workspaceMongoAccessor -func New(peerID string, groups []string) *workspaceMongoAccessor { - return &workspaceMongoAccessor{} +func New(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCaller) *workspaceMongoAccessor { + return &workspaceMongoAccessor{ + utils.AbstractAccessor{ + Logger: logs.CreateLogger(t.String()), // Create a logger with the data type + Caller: caller, + PeerID: peerID, + Groups: groups, // Set the caller + Type: t.String(), + }, + } } // DeleteOne deletes a workspace from the database, given its ID, it automatically share to peers if the workspace is shared