From 4de43a301c4f56110cd816e87c8ad19627bf9990 Mon Sep 17 00:00:00 2001 From: mr Date: Wed, 30 Oct 2024 11:17:52 +0100 Subject: [PATCH] neo local peer draft --- dbs/mongo/mongo.go | 5 --- .../collaborative_area_mongo_accessor.go | 7 ++-- models/peer/peer.go | 19 ++++++++--- models/peer/peer_mongo_accessor.go | 2 ++ models/utils/abstracts.go | 3 +- models/workflow/workflow_mongo_accessor.go | 2 +- models/workspace/workspace_mongo_accessor.go | 2 +- static/peer_static.go | 32 ------------------- 8 files changed, 23 insertions(+), 49 deletions(-) delete mode 100644 static/peer_static.go diff --git a/dbs/mongo/mongo.go b/dbs/mongo/mongo.go index 3ae054b..4f21a51 100644 --- a/dbs/mongo/mongo.go +++ b/dbs/mongo/mongo.go @@ -8,7 +8,6 @@ import ( "cloud.o-forge.io/core/oc-lib/dbs" "cloud.o-forge.io/core/oc-lib/logs" - "cloud.o-forge.io/core/oc-lib/static" "github.com/rs/zerolog" "go.mongodb.org/mongo-driver/bson" @@ -132,10 +131,6 @@ func (m *MongoDB) prepareDB(list_collection []string, config MongoConf) { new_collection := mngoDB.Collection(collection_name) if _, exists := collectionMap[collection_name]; !exists { m.createCollection(collection_name, new_collection) - if collection_name == "peer" { - id, p := static.GetMyLocalBsonPeer() - m.StoreOne(p, id, collection_name) - } } else { CollectionMap[collection_name] = new_collection } diff --git a/models/collaborative_area/collaborative_area_mongo_accessor.go b/models/collaborative_area/collaborative_area_mongo_accessor.go index a46e333..20ba8d9 100644 --- a/models/collaborative_area/collaborative_area_mongo_accessor.go +++ b/models/collaborative_area/collaborative_area_mongo_accessor.go @@ -11,7 +11,6 @@ import ( "cloud.o-forge.io/core/oc-lib/models/utils" w "cloud.o-forge.io/core/oc-lib/models/workflow" "cloud.o-forge.io/core/oc-lib/models/workspace" - "cloud.o-forge.io/core/oc-lib/static" "cloud.o-forge.io/core/oc-lib/tools" ) @@ -155,7 +154,7 @@ func (wfa *collaborativeAreaMongoAccessor) deleteToPeer(shared *CollaborativeAre } paccess := (&peer.Peer{}) for _, v := range shared.Peers { - if (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: v}}).IsMySelf() { + if ok, _ := (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: v}}).IsMySelf(); ok { continue } b, err := paccess.LaunchPeerExecution(v, shared.UUID, tools.COLLABORATIVE_AREA, tools.DELETE, nil, wfa.Caller) @@ -173,7 +172,7 @@ func (wfa *collaborativeAreaMongoAccessor) sendToPeer(shared *CollaborativeArea) paccess := (&peer.Peer{}) for _, v := range shared.Peers { - if (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: v}}).IsMySelf() || shared.IsSent { + if ok, _ := (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: v}}).IsMySelf(); ok || shared.IsSent { continue } shared.IsSent = true @@ -196,7 +195,7 @@ func (wfa *collaborativeAreaMongoAccessor) UpdateOne(set utils.DBObject, id stri // StoreOne stores a collaborative area in the database, it automatically share to peers if the workspace is shared func (wfa *collaborativeAreaMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) { - id, _ := static.GetMyLocalJsonPeer() // get the local peer + _, id := (&peer.Peer{}).IsMySelf() // get the local peer data.(*CollaborativeArea).CreatorID = id // set the creator id data.(*CollaborativeArea).Peers = append(data.(*CollaborativeArea).Peers, id) // add the creator id to the peers // then reset the shared fields diff --git a/models/peer/peer.go b/models/peer/peer.go index 18cfdcb..b3e5082 100644 --- a/models/peer/peer.go +++ b/models/peer/peer.go @@ -5,7 +5,6 @@ import ( "fmt" "cloud.o-forge.io/core/oc-lib/models/utils" - "cloud.o-forge.io/core/oc-lib/static" "cloud.o-forge.io/core/oc-lib/tools" "github.com/google/uuid" ) @@ -21,6 +20,14 @@ const ( BLACKLIST ) +func (m PeerState) String() string { + return [...]string{"NONE", "SELF", "PARTNER", "BLACKLIST"}[m] +} + +func (m PeerState) EnumIndex() int { + return int(m) +} + // Peer is a struct that represents a peer type Peer struct { utils.AbstractObject @@ -57,9 +64,13 @@ func (ao *Peer) RemoveExecution(exec PeerExecution) { } // IsMySelf checks if the peer is the local peer -func (ao *Peer) IsMySelf() bool { - id, _ := static.GetMyLocalJsonPeer() - return ao.UUID == id +func (ao *Peer) IsMySelf() (bool, string) { + d, code, err := ao.GetAccessor(nil).Search(nil, SELF.String()) + if code != 200 || err != nil || len(d) == 0 { + return false, "" + } + id := d[0].GetID() + return ao.UUID == id, id } // LaunchPeerExecution launches an execution on a peer diff --git a/models/peer/peer_mongo_accessor.go b/models/peer/peer_mongo_accessor.go index 5d4f7a8..43548fd 100644 --- a/models/peer/peer_mongo_accessor.go +++ b/models/peer/peer_mongo_accessor.go @@ -1,6 +1,7 @@ package peer import ( + "fmt" "strconv" "cloud.o-forge.io/core/oc-lib/dbs" @@ -70,6 +71,7 @@ func (wfa *peerMongoAccessor) Search(filters *dbs.Filters, search string) ([]uti objs := []utils.ShallowDBObject{} if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" { s, err := strconv.Atoi(search) + fmt.Println(s, err, search) if err == nil { filters = &dbs.Filters{ Or: map[string][]dbs.Filter{ // search by name if no filters are provided diff --git a/models/utils/abstracts.go b/models/utils/abstracts.go index be6946a..2f6dd5a 100644 --- a/models/utils/abstracts.go +++ b/models/utils/abstracts.go @@ -8,7 +8,6 @@ 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/static" "cloud.o-forge.io/core/oc-lib/tools" "github.com/go-playground/validator/v10" "github.com/google/uuid" @@ -42,7 +41,7 @@ func (ao *AbstractObject) GetName() string { func (ao *AbstractObject) UpToDate() { ao.UpdateDate = time.Now() - ao.LastPeerWriter, _ = static.GetMyLocalJsonPeer() + // ao.LastPeerWriter, _ = static.GetMyLocalJsonPeer() } // GetAccessor returns the accessor of the object (abstract) diff --git a/models/workflow/workflow_mongo_accessor.go b/models/workflow/workflow_mongo_accessor.go index 362ca36..95aa8c9 100644 --- a/models/workflow/workflow_mongo_accessor.go +++ b/models/workflow/workflow_mongo_accessor.go @@ -171,7 +171,7 @@ func (wfa *workflowMongoAccessor) share(realData *Workflow, delete bool, caller paccess := &peer.Peer{} for _, p := range res.(*shallow_collaborative_area.ShallowCollaborativeArea).Peers { paccess.UUID = p - if paccess.IsMySelf() { // if the peer is the current peer, never share because it will create a loop + if ok, _ := paccess.IsMySelf(); ok { // if the peer is the current peer, never share because it will create a loop continue } if delete { // if the workflow is deleted, share the deletion diff --git a/models/workspace/workspace_mongo_accessor.go b/models/workspace/workspace_mongo_accessor.go index b74a6b7..d7a9c4c 100644 --- a/models/workspace/workspace_mongo_accessor.go +++ b/models/workspace/workspace_mongo_accessor.go @@ -217,7 +217,7 @@ func (wfa *workspaceMongoAccessor) share(realData *Workspace, method tools.METHO paccess := &peer.Peer{} for _, p := range res.(*shallow_collaborative_area.ShallowCollaborativeArea).Peers { paccess.UUID = p - if paccess.IsMySelf() { // If the peer is the current peer, never share because it will create a loop + if ok, _ := paccess.IsMySelf(); ok { // If the peer is the current peer, never share because it will create a loop continue } if method == tools.DELETE { // If the workspace is deleted, share the deletion diff --git a/static/peer_static.go b/static/peer_static.go deleted file mode 100644 index c349e41..0000000 --- a/static/peer_static.go +++ /dev/null @@ -1,32 +0,0 @@ -package static - -/* -This package contains static data for the peer model -It's used to test the peer model -Temporary version, will be removed in the future and replaced with a more dynamic solution -to generate the data -*/ - -// GetMyLocalBsonPeer returns a tuple with the peer ID and the peer data in BSON format -func GetMyLocalBsonPeer() (string, map[string]interface{}) { - return "6fd0134c-fefc-427e-94c2-e01365fc5fb0", map[string]interface{}{ - "abstractobject": map[string]interface{}{ - "id": "6fd0134c-fefc-427e-94c2-e01365fc5fb0", - "name": "local_peer", - }, - "state": 1, - "url": "http://localhost", - "public_key": "public_key_lulz", - } -} - -// GetMyLocalJsonPeer returns a tuple with the peer ID and the peer data in JSON format -func GetMyLocalJsonPeer() (string, map[string]interface{}) { - return "6fd0134c-fefc-427e-94c2-e01365fc5fb0", map[string]interface{}{ - "id": "6fd0134c-fefc-427e-94c2-e01365fc5fb0", - "name": "local_peer", - "state": 1, - "url": "http://localhost", - "public_key": "public_key_lulz", - } -}