correct share infinite loop
This commit is contained in:
@@ -1,74 +0,0 @@
|
||||
package collaborative_area
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||
"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/models/workspace/shared/rules/rule"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// SharedWorkspace is a struct that represents a shared workspace
|
||||
// WARNING : it got a shallow object version, this one is the full version
|
||||
// full version is the one used by API
|
||||
// other one is a shallow version used by the Lib for import cycle problem purposes
|
||||
type CollaborativeArea struct {
|
||||
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
|
||||
IsSent bool `json:"is_sent" bson:"-"` // IsSent is a flag that indicates if the workspace is sent
|
||||
CreatorID string `json:"peer_id,omitempty" bson:"peer_id,omitempty" validate:"required"` // CreatorID is the ID of the creator
|
||||
Version string `json:"version,omitempty" bson:"version,omitempty"` // Version is the version of the workspace
|
||||
Description string `json:"description,omitempty" bson:"description,omitempty" validate:"required"` // Description is the description of the workspace
|
||||
Attributes map[string]interface{} `json:"attributes,omitempty" bson:"attributes,omitempty"` // Attributes is the attributes of the workspace (TODO)
|
||||
Workspaces []string `json:"workspaces,omitempty" bson:"workspaces,omitempty"` // Workspaces is the workspaces of the workspace
|
||||
Workflows []string `json:"workflows,omitempty" bson:"workflows,omitempty"` // Workflows is the workflows of the workspace
|
||||
Peers []string `json:"peers,omitempty" bson:"peers,omitempty"` // Peers is the peers of the workspace
|
||||
Rules []string `json:"rules,omitempty" bson:"rules,omitempty"` // Rules is the rules of the workspace
|
||||
|
||||
SharedRules []*rule.Rule `json:"shared_rules,omitempty" bson:"-"` // SharedRules is the shared rules of the workspace
|
||||
SharedWorkspaces []*workspace.Workspace `json:"shared_workspaces,omitempty" bson:"-"` // SharedWorkspaces is the shared workspaces of the workspace
|
||||
SharedWorkflows []*w.Workflow `json:"shared_workflows,omitempty" bson:"-"` // SharedWorkflows is the shared workflows of the workspace
|
||||
SharedPeers []*peer.Peer `json:"shared_peers,omitempty" bson:"-"` // SharedPeers is the shared peers of the workspace
|
||||
}
|
||||
|
||||
func (ao *CollaborativeArea) GetID() string {
|
||||
return ao.UUID
|
||||
}
|
||||
|
||||
func (r *CollaborativeArea) GenerateID() {
|
||||
if r.UUID == "" {
|
||||
r.UUID = uuid.New().String()
|
||||
}
|
||||
}
|
||||
|
||||
func (d *CollaborativeArea) GetName() string {
|
||||
return d.Name
|
||||
}
|
||||
|
||||
func (d *CollaborativeArea) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
|
||||
data := New() // Create a new instance of the accessor
|
||||
data.Init(tools.COLLABORATIVE_AREA, caller) // Initialize the accessor with the SHARED_WORKSPACE model type
|
||||
return data
|
||||
}
|
||||
|
||||
func (dma *CollaborativeArea) Deserialize(j map[string]interface{}) utils.DBObject {
|
||||
b, err := json.Marshal(j)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
json.Unmarshal(b, dma)
|
||||
return dma
|
||||
}
|
||||
|
||||
func (dma *CollaborativeArea) Serialize() map[string]interface{} {
|
||||
var m map[string]interface{}
|
||||
b, err := json.Marshal(dma)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
json.Unmarshal(b, &m)
|
||||
return m
|
||||
}
|
@@ -1,330 +0,0 @@
|
||||
package collaborative_area
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||
"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/models/workspace/shared/rules/rule"
|
||||
"cloud.o-forge.io/core/oc-lib/static"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
// SharedWorkspace is a struct that represents a collaborative area
|
||||
type collaborativeAreaMongoAccessor struct {
|
||||
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||||
}
|
||||
|
||||
// New creates a new instance of the collaborativeAreaMongoAccessor
|
||||
func New() *collaborativeAreaMongoAccessor {
|
||||
return &collaborativeAreaMongoAccessor{}
|
||||
}
|
||||
|
||||
// DeleteOne deletes a collaborative area from the database, given its ID, it automatically share to peers if the workspace is shared
|
||||
func (wfa *collaborativeAreaMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
set, code, _ := wfa.LoadOne(id)
|
||||
if code == 200 { // always delete on peers than recreate
|
||||
wfa.deleteToPeer(set.(*CollaborativeArea))
|
||||
}
|
||||
wfa.sharedWorkflow(&CollaborativeArea{}, id) // create all shared workflows
|
||||
wfa.sharedWorkspace(&CollaborativeArea{}, id) // create all collaborative areas
|
||||
return wfa.GenericDeleteOne(id, wfa) // then add on yours
|
||||
}
|
||||
|
||||
/*
|
||||
sharedWorkspace is a function that shares the collaborative area to the peers
|
||||
*/
|
||||
func (wfa *collaborativeAreaMongoAccessor) sharedWorkspace(shared *CollaborativeArea, id string) {
|
||||
eldest, code, _ := wfa.LoadOne(id) // get the eldest
|
||||
accessor := (&workspace.Workspace{}).GetAccessor(nil)
|
||||
if code == 200 {
|
||||
eld := eldest.(*CollaborativeArea)
|
||||
if eld.Workspaces != nil { // update all your workspaces in the eldest by replacing shared ref by an empty string
|
||||
for _, v := range eld.Workspaces {
|
||||
accessor.UpdateOne(&workspace.Workspace{Shared: ""}, v)
|
||||
if wfa.Caller != nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[tools.WORKSPACE] == nil {
|
||||
continue
|
||||
}
|
||||
paccess := (&peer.Peer{}) // send to all peers
|
||||
for _, p := range shared.Peers { // delete the collaborative area on the peer
|
||||
b, err := paccess.LaunchPeerExecution(p, v, tools.WORKSPACE, tools.DELETE, nil, wfa.Caller)
|
||||
if err != nil && b == nil {
|
||||
wfa.Logger.Error().Msg("Could not send to peer " + p + ". Error: " + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if shared.Workspaces != nil {
|
||||
for _, v := range shared.Workspaces { // update all the collaborative areas
|
||||
workspace, code, _ := accessor.UpdateOne(&workspace.Workspace{Shared: shared.UUID}, v) // add the shared ref to workspace
|
||||
if wfa.Caller != nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[tools.WORKSPACE] == nil {
|
||||
continue
|
||||
}
|
||||
for _, p := range shared.Peers {
|
||||
if code != 200 {
|
||||
continue
|
||||
}
|
||||
paccess := (&peer.Peer{}) // send to all peers, add the collaborative area on the peer
|
||||
s := workspace.Serialize()
|
||||
s["name"] = fmt.Sprintf("%v", s["name"]) + "_" + p
|
||||
b, err := paccess.LaunchPeerExecution(p, v, tools.WORKSPACE, tools.POST, s, wfa.Caller)
|
||||
if err != nil && b == nil {
|
||||
wfa.Logger.Error().Msg("Could not send to peer " + p + ". Error: " + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// deleting on peers before adding, to avoid conflicts on peers side
|
||||
// because you have no reference to the remote collaborative area
|
||||
}
|
||||
|
||||
// sharedWorkflow is a function that shares the shared workflow to the peers
|
||||
func (wfa *collaborativeAreaMongoAccessor) sharedWorkflow(shared *CollaborativeArea, id string) {
|
||||
accessor := (&w.Workflow{}).GetAccessor(nil)
|
||||
eldest, code, _ := wfa.LoadOne(id) // get the eldest
|
||||
if code == 200 {
|
||||
eld := eldest.(*CollaborativeArea)
|
||||
if eld.Workflows != nil {
|
||||
for _, v := range eld.Workflows {
|
||||
data, code, _ := accessor.LoadOne(v)
|
||||
if code == 200 {
|
||||
s := data.(*w.Workflow)
|
||||
new := []string{}
|
||||
for _, id2 := range s.Shared {
|
||||
if id2 != id {
|
||||
new = append(new, id2)
|
||||
}
|
||||
} // kick the shared reference in your old shared workflow
|
||||
n := &w.Workflow{}
|
||||
n.Shared = new
|
||||
accessor.UpdateOne(n, v)
|
||||
if wfa.Caller != nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[tools.WORKFLOW] == nil {
|
||||
continue
|
||||
}
|
||||
paccess := (&peer.Peer{}) // send to all peers
|
||||
for _, p := range shared.Peers { // delete the shared workflow on the peer
|
||||
b, err := paccess.LaunchPeerExecution(p, v, tools.WORKFLOW, tools.DELETE, nil, wfa.Caller)
|
||||
if err != nil && b == nil {
|
||||
wfa.Logger.Error().Msg("Could not send to peer " + p + ". Error: " + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if shared.Workflows != nil { // update all the shared workflows
|
||||
for _, v := range shared.Workflows {
|
||||
data, code, _ := accessor.LoadOne(v)
|
||||
if code == 200 {
|
||||
s := data.(*w.Workflow)
|
||||
if !slices.Contains(s.Shared, id) {
|
||||
s.Shared = append(s.Shared, id)
|
||||
workflow, code, _ := accessor.UpdateOne(s, v)
|
||||
if wfa.Caller != nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[tools.WORKFLOW] == nil {
|
||||
continue
|
||||
}
|
||||
paccess := (&peer.Peer{})
|
||||
for _, p := range shared.Peers { // send to all peers
|
||||
if code == 200 {
|
||||
s := workflow.Serialize() // add the shared workflow on the peer
|
||||
s["name"] = fmt.Sprintf("%v", s["name"]) + "_" + p
|
||||
b, err := paccess.LaunchPeerExecution(p, shared.UUID, tools.WORKFLOW, tools.POST, s, wfa.Caller)
|
||||
if err != nil && b == nil {
|
||||
wfa.Logger.Error().Msg("Could not send to peer " + p + ". Error: " + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// deleting on peers before adding, to avoid conflicts on peers side
|
||||
// because you have no reference to the remote shared workflow
|
||||
}
|
||||
|
||||
// sharedWorkspace is a function that shares the collaborative area to the peers
|
||||
func (wfa *collaborativeAreaMongoAccessor) deleteToPeer(shared *CollaborativeArea) {
|
||||
if wfa.Caller == nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[tools.COLLABORATIVE_AREA] == nil {
|
||||
return
|
||||
}
|
||||
paccess := (&peer.Peer{})
|
||||
for _, v := range shared.Peers {
|
||||
if (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: v}}).IsMySelf() {
|
||||
continue
|
||||
}
|
||||
b, err := paccess.LaunchPeerExecution(v, shared.UUID, tools.COLLABORATIVE_AREA, tools.DELETE, nil, wfa.Caller)
|
||||
if err != nil && b == nil {
|
||||
wfa.Logger.Error().Msg("Could not send to peer " + v + ". Error: " + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// sharedWorkspace is a function that shares the collaborative area to the peers
|
||||
func (wfa *collaborativeAreaMongoAccessor) sendToPeer(shared *CollaborativeArea) {
|
||||
if wfa.Caller == nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[tools.COLLABORATIVE_AREA] == nil {
|
||||
return
|
||||
}
|
||||
|
||||
paccess := (&peer.Peer{})
|
||||
for _, v := range shared.Peers {
|
||||
if (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: v}}).IsMySelf() || shared.IsSent {
|
||||
continue
|
||||
}
|
||||
shared.IsSent = true
|
||||
b, err := paccess.LaunchPeerExecution(v, v, tools.COLLABORATIVE_AREA, tools.POST, shared.Serialize(), wfa.Caller)
|
||||
if err != nil && b == nil {
|
||||
wfa.Logger.Error().Msg("Could not send to peer " + v + ". Error: " + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateOne updates a collaborative area in the database, given its ID and the new data, it automatically share to peers if the workspace is shared
|
||||
func (wfa *collaborativeAreaMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
res, code, err := wfa.GenericUpdateOne(set.(*CollaborativeArea), id, wfa, &CollaborativeArea{})
|
||||
wfa.deleteToPeer(res.(*CollaborativeArea)) // delete the collaborative area on the peer
|
||||
wfa.sharedWorkflow(res.(*CollaborativeArea), id) // replace all shared workflows
|
||||
wfa.sharedWorkspace(res.(*CollaborativeArea), id) // replace all collaborative areas (not shared worspace obj but workspace one)
|
||||
wfa.sendToPeer(res.(*CollaborativeArea)) // send the collaborative area (collaborative area object) to the peers
|
||||
return res, code, err
|
||||
}
|
||||
|
||||
// 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
|
||||
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
|
||||
if data.(*CollaborativeArea).Workspaces == nil {
|
||||
data.(*CollaborativeArea).Workspaces = []string{}
|
||||
}
|
||||
if data.(*CollaborativeArea).Workflows == nil {
|
||||
data.(*CollaborativeArea).Workflows = []string{}
|
||||
}
|
||||
if data.(*CollaborativeArea).Rules == nil {
|
||||
data.(*CollaborativeArea).Rules = []string{}
|
||||
}
|
||||
|
||||
d, code, err := wfa.GenericStoreOne(data.(*CollaborativeArea), wfa)
|
||||
|
||||
if code == 200 {
|
||||
wfa.sharedWorkflow(d.(*CollaborativeArea), d.GetID()) // create all shared workflows
|
||||
wfa.sharedWorkspace(d.(*CollaborativeArea), d.GetID()) // create all collaborative areas
|
||||
wfa.sendToPeer(d.(*CollaborativeArea)) // send the collaborative area (collaborative area object) to the peers
|
||||
}
|
||||
return data, code, err
|
||||
}
|
||||
|
||||
// CopyOne copies a CollaborativeArea in the database
|
||||
func (wfa *collaborativeAreaMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return wfa.StoreOne(data)
|
||||
}
|
||||
|
||||
// enrich is a function that enriches the CollaborativeArea with the shared objects
|
||||
func (wfa *collaborativeAreaMongoAccessor) enrich(sharedWorkspace *CollaborativeArea) *CollaborativeArea {
|
||||
access := (&workspace.Workspace{}).GetAccessor(nil)
|
||||
res, code, _ := access.Search(&dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{
|
||||
"abstractobject.id": {{Operator: dbs.IN.String(), Value: sharedWorkspace.Workspaces}},
|
||||
},
|
||||
}, "")
|
||||
if code == 200 {
|
||||
for _, r := range res {
|
||||
sharedWorkspace.SharedWorkspaces = append(sharedWorkspace.SharedWorkspaces, r.(*workspace.Workspace))
|
||||
}
|
||||
}
|
||||
access = (&w.Workflow{}).GetAccessor(nil)
|
||||
res, code, _ = access.Search(&dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{
|
||||
"abstractobject.id": {{Operator: dbs.IN.String(), Value: sharedWorkspace.Workflows}},
|
||||
},
|
||||
}, "")
|
||||
if code == 200 {
|
||||
for _, r := range res {
|
||||
sharedWorkspace.SharedWorkflows = append(sharedWorkspace.SharedWorkflows, r.(*w.Workflow))
|
||||
}
|
||||
}
|
||||
access = (&peer.Peer{}).GetAccessor(nil)
|
||||
res, code, _ = access.Search(&dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{
|
||||
"abstractobject.id": {{Operator: dbs.IN.String(), Value: sharedWorkspace.Peers}},
|
||||
},
|
||||
}, "")
|
||||
if code == 200 {
|
||||
for _, r := range res {
|
||||
sharedWorkspace.SharedPeers = append(sharedWorkspace.SharedPeers, r.(*peer.Peer))
|
||||
}
|
||||
}
|
||||
access = (&rule.Rule{}).GetAccessor(nil)
|
||||
res, code, _ = access.Search(&dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{
|
||||
"abstractobject.id": {{Operator: dbs.IN.String(), Value: sharedWorkspace.Rules}},
|
||||
},
|
||||
}, "")
|
||||
if code == 200 {
|
||||
for _, r := range res {
|
||||
sharedWorkspace.SharedRules = append(sharedWorkspace.SharedRules, r.(*rule.Rule))
|
||||
}
|
||||
}
|
||||
return sharedWorkspace
|
||||
}
|
||||
|
||||
// LoadOne loads a collaborative area from the database, given its ID and enrich it
|
||||
func (wfa *collaborativeAreaMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
var sharedWorkspace CollaborativeArea
|
||||
res_mongo, code, 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, code, err
|
||||
}
|
||||
res_mongo.Decode(&sharedWorkspace)
|
||||
return wfa.enrich(&sharedWorkspace), 200, nil // enrich the collaborative area
|
||||
}
|
||||
|
||||
// LoadAll loads all the collaborative areas from the database and enrich them
|
||||
func (wfa collaborativeAreaMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
|
||||
objs := []utils.ShallowDBObject{}
|
||||
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
|
||||
if err != nil {
|
||||
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
var results []CollaborativeArea
|
||||
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
|
||||
return nil, 404, err
|
||||
}
|
||||
for _, r := range results {
|
||||
objs = append(objs, wfa.enrich(&r)) // enrich the collaborative area
|
||||
}
|
||||
return objs, 200, nil
|
||||
}
|
||||
|
||||
// Search searches for collaborative areas in the database, given some filters OR a search string
|
||||
func (wfa *collaborativeAreaMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
|
||||
objs := []utils.ShallowDBObject{}
|
||||
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
|
||||
filters = &dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{ // search by name only by default can be override
|
||||
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
|
||||
},
|
||||
}
|
||||
}
|
||||
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
|
||||
if err != nil {
|
||||
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
var results []CollaborativeArea
|
||||
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
|
||||
return nil, 404, err
|
||||
}
|
||||
for _, r := range results {
|
||||
objs = append(objs, wfa.enrich(&r)) // enrich the collaborative area
|
||||
}
|
||||
return objs, 200, nil
|
||||
}
|
@@ -1,56 +0,0 @@
|
||||
package rule
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
/*
|
||||
* Rule is a struct that represents a rule of a shared workspace
|
||||
*/
|
||||
type Rule struct {
|
||||
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
|
||||
Description string `json:"description,omitempty" bson:"description,omitempty"` // Description is the description of the rule
|
||||
Condition string `json:"condition,omitempty" bson:"condition,omitempty"` // NOT DEFINITIVE TO SPECIFICATION
|
||||
Actions []string `json:"actions,omitempty" bson:"actions,omitempty"` // NOT DEFINITIVE TO SPECIFICATION
|
||||
}
|
||||
|
||||
func (ao *Rule) GetID() string {
|
||||
return ao.UUID
|
||||
}
|
||||
|
||||
func (r *Rule) GenerateID() {
|
||||
r.UUID = uuid.New().String()
|
||||
}
|
||||
|
||||
func (d *Rule) GetName() string {
|
||||
return d.Name
|
||||
}
|
||||
|
||||
func (d *Rule) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
|
||||
data := New()
|
||||
data.Init(tools.RULE, caller)
|
||||
return data
|
||||
}
|
||||
|
||||
func (dma *Rule) Deserialize(j map[string]interface{}) utils.DBObject {
|
||||
b, err := json.Marshal(j)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
json.Unmarshal(b, dma)
|
||||
return dma
|
||||
}
|
||||
|
||||
func (dma *Rule) Serialize() map[string]interface{} {
|
||||
var m map[string]interface{}
|
||||
b, err := json.Marshal(dma)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
json.Unmarshal(b, &m)
|
||||
return m
|
||||
}
|
@@ -1,90 +0,0 @@
|
||||
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/models/utils"
|
||||
)
|
||||
|
||||
type ruleMongoAccessor struct {
|
||||
utils.AbstractAccessor
|
||||
}
|
||||
|
||||
// New creates a new instance of the ruleMongoAccessor
|
||||
func New() *ruleMongoAccessor {
|
||||
return &ruleMongoAccessor{}
|
||||
}
|
||||
|
||||
// GetType returns the type of the rule
|
||||
func (wfa *ruleMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
return wfa.GenericDeleteOne(id, wfa)
|
||||
}
|
||||
|
||||
// UpdateOne updates a rule in the database
|
||||
func (wfa *ruleMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
return wfa.GenericUpdateOne(set.(*Rule), id, wfa, &Rule{})
|
||||
}
|
||||
|
||||
// StoreOne stores a rule in the database
|
||||
func (wfa *ruleMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return wfa.GenericStoreOne(data.(*Rule), wfa)
|
||||
}
|
||||
|
||||
func (wfa *ruleMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return wfa.GenericStoreOne(data, wfa)
|
||||
}
|
||||
|
||||
// LoadOne loads a rule from the database
|
||||
func (wfa *ruleMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
var rule Rule
|
||||
res_mongo, code, 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, code, err
|
||||
}
|
||||
res_mongo.Decode(&rule)
|
||||
return &rule, 200, nil
|
||||
}
|
||||
|
||||
// LoadAll loads all rules from the database
|
||||
func (wfa ruleMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
|
||||
objs := []utils.ShallowDBObject{}
|
||||
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
|
||||
if err != nil {
|
||||
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
var results []Rule
|
||||
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
|
||||
return nil, 404, err
|
||||
}
|
||||
for _, r := range results {
|
||||
objs = append(objs, &r)
|
||||
}
|
||||
return objs, 200, nil
|
||||
}
|
||||
|
||||
// Search searches for rules in the database, given some filters OR a search string
|
||||
func (wfa *ruleMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
|
||||
objs := []utils.ShallowDBObject{}
|
||||
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
|
||||
filters = &dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{ // filter by name if no filters are provided
|
||||
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
|
||||
},
|
||||
}
|
||||
}
|
||||
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
|
||||
if err != nil {
|
||||
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
var results []Rule
|
||||
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
|
||||
return nil, 404, err
|
||||
}
|
||||
for _, r := range results {
|
||||
objs = append(objs, &r)
|
||||
}
|
||||
return objs, 200, nil
|
||||
}
|
@@ -1,61 +0,0 @@
|
||||
package shallow_collaborative_area
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ShallowCollaborativeArea struct {
|
||||
utils.AbstractObject
|
||||
IsSent bool `json:"is_sent" bson:"-"`
|
||||
CreatorID string `json:"peer_id,omitempty" bson:"peer_id,omitempty" validate:"required"`
|
||||
Version string `json:"version,omitempty" bson:"version,omitempty"`
|
||||
Description string `json:"description,omitempty" bson:"description,omitempty" validate:"required"`
|
||||
Attributes map[string]interface{} `json:"attributes,omitempty" bson:"attributes,omitempty"`
|
||||
Workspaces []string `json:"workspaces,omitempty" bson:"workspaces,omitempty"`
|
||||
Workflows []string `json:"workflows,omitempty" bson:"workflows,omitempty"`
|
||||
Peers []string `json:"peers,omitempty" bson:"peers,omitempty"`
|
||||
Rules []string `json:"rules,omitempty" bson:"rules,omitempty"`
|
||||
}
|
||||
|
||||
func (ao *ShallowCollaborativeArea) GetID() string {
|
||||
return ao.UUID
|
||||
}
|
||||
|
||||
func (r *ShallowCollaborativeArea) GenerateID() {
|
||||
if r.UUID == "" {
|
||||
r.UUID = uuid.New().String()
|
||||
}
|
||||
}
|
||||
|
||||
func (d *ShallowCollaborativeArea) GetName() string {
|
||||
return d.Name
|
||||
}
|
||||
|
||||
func (d *ShallowCollaborativeArea) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
|
||||
data := New()
|
||||
data.Init(tools.COLLABORATIVE_AREA, caller)
|
||||
return data
|
||||
}
|
||||
|
||||
func (dma *ShallowCollaborativeArea) Deserialize(j map[string]interface{}) utils.DBObject {
|
||||
b, err := json.Marshal(j)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
json.Unmarshal(b, dma)
|
||||
return dma
|
||||
}
|
||||
|
||||
func (dma *ShallowCollaborativeArea) Serialize() map[string]interface{} {
|
||||
var m map[string]interface{}
|
||||
b, err := json.Marshal(dma)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
json.Unmarshal(b, &m)
|
||||
return m
|
||||
}
|
@@ -1,83 +0,0 @@
|
||||
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/models/utils"
|
||||
)
|
||||
|
||||
type shallowSharedWorkspaceMongoAccessor struct {
|
||||
utils.AbstractAccessor
|
||||
}
|
||||
|
||||
func New() *shallowSharedWorkspaceMongoAccessor {
|
||||
return &shallowSharedWorkspaceMongoAccessor{}
|
||||
}
|
||||
|
||||
func (wfa *shallowSharedWorkspaceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
return wfa.GenericDeleteOne(id, wfa)
|
||||
}
|
||||
|
||||
func (wfa *shallowSharedWorkspaceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
return wfa.GenericUpdateOne(set.(*ShallowCollaborativeArea), id, wfa, &ShallowCollaborativeArea{})
|
||||
}
|
||||
|
||||
func (wfa *shallowSharedWorkspaceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return wfa.GenericStoreOne(data.(*ShallowCollaborativeArea), wfa)
|
||||
}
|
||||
|
||||
func (wfa *shallowSharedWorkspaceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return wfa.StoreOne(data)
|
||||
}
|
||||
|
||||
func (wfa *shallowSharedWorkspaceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
var sharedWorkspace ShallowCollaborativeArea
|
||||
res_mongo, code, 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, code, err
|
||||
}
|
||||
res_mongo.Decode(&sharedWorkspace)
|
||||
return &sharedWorkspace, 200, nil
|
||||
}
|
||||
|
||||
func (wfa shallowSharedWorkspaceMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
|
||||
objs := []utils.ShallowDBObject{}
|
||||
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
|
||||
if err != nil {
|
||||
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
var results []ShallowCollaborativeArea
|
||||
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
|
||||
return nil, 404, err
|
||||
}
|
||||
for _, r := range results {
|
||||
objs = append(objs, &r)
|
||||
}
|
||||
return objs, 200, nil
|
||||
}
|
||||
|
||||
func (wfa *shallowSharedWorkspaceMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
|
||||
objs := []utils.ShallowDBObject{}
|
||||
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
|
||||
filters = &dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{
|
||||
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
|
||||
},
|
||||
}
|
||||
}
|
||||
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
|
||||
if err != nil {
|
||||
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
var results []ShallowCollaborativeArea
|
||||
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
|
||||
return nil, 404, err
|
||||
}
|
||||
for _, r := range results {
|
||||
objs = append(objs, &r)
|
||||
}
|
||||
return objs, 200, nil
|
||||
}
|
@@ -5,6 +5,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/models/collaborative_area/shallow_collaborative_area"
|
||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources/data"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
|
||||
@@ -12,7 +13,6 @@ import (
|
||||
"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/workspace/shared/shallow_collaborative_area"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
@@ -31,7 +31,7 @@ func New() *workspaceMongoAccessor {
|
||||
func (wfa *workspaceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
res, code, err := wfa.GenericDeleteOne(id, wfa)
|
||||
if code == 200 && res != nil {
|
||||
wfa.share(res.(*Workspace), true, wfa.Caller) // Share the deletion to the peers
|
||||
wfa.share(res.(*Workspace), tools.DELETE, wfa.Caller) // Share the deletion to the peers
|
||||
}
|
||||
return res, code, err
|
||||
}
|
||||
@@ -57,7 +57,7 @@ func (wfa *workspaceMongoAccessor) UpdateOne(set utils.DBObject, id string) (uti
|
||||
}
|
||||
res, code, err := wfa.GenericUpdateOne(set, id, wfa, &Workspace{})
|
||||
if code == 200 && res != nil {
|
||||
wfa.share(res.(*Workspace), false, wfa.Caller)
|
||||
wfa.share(res.(*Workspace), tools.PUT, wfa.Caller)
|
||||
}
|
||||
return res, code, err
|
||||
}
|
||||
@@ -204,8 +204,8 @@ func (wfa *workspaceMongoAccessor) Search(filters *dbs.Filters, search string) (
|
||||
/*
|
||||
This function is used to share the workspace with the peers
|
||||
*/
|
||||
func (wfa *workspaceMongoAccessor) share(realData *Workspace, delete bool, caller *tools.HTTPCaller) {
|
||||
if realData.Shared == "" {
|
||||
func (wfa *workspaceMongoAccessor) share(realData *Workspace, method tools.METHOD, caller *tools.HTTPCaller) {
|
||||
if realData.Shared == "" || caller.Disabled {
|
||||
return
|
||||
}
|
||||
access := (&shallow_collaborative_area.ShallowCollaborativeArea{}).GetAccessor(nil)
|
||||
@@ -220,7 +220,7 @@ func (wfa *workspaceMongoAccessor) share(realData *Workspace, delete bool, calle
|
||||
if paccess.IsMySelf() { // If the peer is the current peer, never share because it will create a loop
|
||||
continue
|
||||
}
|
||||
if delete { // If the workspace is deleted, share the deletion
|
||||
if method == tools.DELETE { // If the workspace is deleted, share the deletion
|
||||
history := NewHistory()
|
||||
history.StoreOne(history.MapFromWorkspace(res.(*Workspace)))
|
||||
_, err = paccess.LaunchPeerExecution(p, res.GetID(), tools.WORKSPACE, tools.DELETE, map[string]interface{}{}, caller)
|
||||
|
Reference in New Issue
Block a user