simplify call to peer

This commit is contained in:
mr 2024-08-26 12:34:23 +02:00
parent 73dc43b329
commit aff9304b1a
2 changed files with 68 additions and 6 deletions

View File

@ -3,7 +3,11 @@ package shared_workspace
import ( import (
"encoding/json" "encoding/json"
"cloud.o-forge.io/core/oc-lib/models/peer"
"cloud.o-forge.io/core/oc-lib/models/utils" "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" "cloud.o-forge.io/core/oc-lib/tools"
"github.com/google/uuid" "github.com/google/uuid"
) )
@ -18,6 +22,11 @@ type SharedWorkspace struct {
Workflows []string `json:"workflows,omitempty" bson:"workflows,omitempty"` Workflows []string `json:"workflows,omitempty" bson:"workflows,omitempty"`
Peers []string `json:"peers,omitempty" bson:"peers,omitempty"` Peers []string `json:"peers,omitempty" bson:"peers,omitempty"`
Rules []string `json:"rules,omitempty" bson:"rules,omitempty"` Rules []string `json:"rules,omitempty" bson:"rules,omitempty"`
SharedRules []*rule.Rule `json:"shared_rules,omitempty"`
SharedWorkspaces []*workspace.Workspace `json:"shared_workspaces,omitempty"`
SharedWorkflows []*w.Workflow `json:"shared_workflows,omitempty"`
SharedPeers []*peer.Peer `json:"shared_peers,omitempty"`
} }
func (ao *SharedWorkspace) GetID() string { func (ao *SharedWorkspace) GetID() string {

View File

@ -9,6 +9,7 @@ import (
"cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/models/utils"
w "cloud.o-forge.io/core/oc-lib/models/workflow" 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"
"cloud.o-forge.io/core/oc-lib/models/workspace/shared/rules/rule"
"cloud.o-forge.io/core/oc-lib/tools" "cloud.o-forge.io/core/oc-lib/tools"
) )
@ -184,16 +185,64 @@ func (wfa *sharedWorkspaceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBO
return wfa.StoreOne(data) return wfa.StoreOne(data)
} }
func (wfa *sharedWorkspaceMongoAccessor) enrich(sharedWorkspace *SharedWorkspace) *SharedWorkspace {
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.Workspaces}},
},
}, "")
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
}
func (wfa *sharedWorkspaceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) { func (wfa *sharedWorkspaceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var sharedWorkspace SharedWorkspace var sharedWorkspace *SharedWorkspace
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType()) res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
if err != nil { if err != nil {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error()) wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err return nil, code, err
} }
res_mongo.Decode(&sharedWorkspace) res_mongo.Decode(sharedWorkspace)
sharedWorkspace = wfa.enrich(sharedWorkspace)
return &sharedWorkspace, 200, nil return sharedWorkspace, 200, nil
} }
func (wfa sharedWorkspaceMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) { func (wfa sharedWorkspaceMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
@ -208,7 +257,9 @@ func (wfa sharedWorkspaceMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int,
return nil, 404, err return nil, 404, err
} }
for _, r := range results { for _, r := range results {
objs = append(objs, &r) r2 := &r
r2 = wfa.enrich(r2)
objs = append(objs, r2)
} }
return objs, 200, nil return objs, 200, nil
} }
@ -232,7 +283,9 @@ func (wfa *sharedWorkspaceMongoAccessor) Search(filters *dbs.Filters, search str
return nil, 404, err return nil, 404, err
} }
for _, r := range results { for _, r := range results {
objs = append(objs, &r) r2 := &r
r2 = wfa.enrich(r2)
objs = append(objs, r2)
} }
return objs, 200, nil return objs, 200, nil
} }