precise workspace flow

This commit is contained in:
mr 2024-07-31 11:23:59 +02:00
parent 274ce57f5b
commit e311553eb1

View File

@ -3,9 +3,9 @@ package oclib
import ( import (
"errors" "errors"
"fmt" "fmt"
"slices"
"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/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils" "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/models/workflow_execution"
"cloud.o-forge.io/core/oc-lib/models/workspace" "cloud.o-forge.io/core/oc-lib/models/workspace"
@ -74,7 +74,38 @@ func (wfa *workflowMongoAccessor) UpdateOne(set utils.DBObject, id string) (util
if code, err := wfa.execution(set.(*Workflow), true); err != nil { if code, err := wfa.execution(set.(*Workflow), true); err != nil {
return nil, code, err return nil, code, err
} }
return wfa.GenericUpdateOne(set, id, wfa, &Workflow{}) res, code, err := wfa.GenericUpdateOne(set, id, wfa, &Workflow{})
if code != 200 {
return nil, code, err
}
workflow := res.(*Workflow)
accessor := (&workspace.Workspace{}).GetAccessor()
resource, _, err := accessor.Search(workflow.Name+"_workspace", "abstractobject.name")
if err == nil && len(resource) > 0 {
for _, r := range resource {
accessor.UpdateOne(&workspace.Workspace{
ResourceSet: resources.ResourceSet{
Datas: workflow.Datas,
Processings: workflow.Processings,
Storages: workflow.Storages,
Workflows: workflow.Workflows,
Datacenters: workflow.Datacenters,
},
}, r.GetID())
}
} else {
accessor.StoreOne(&workspace.Workspace{
AbstractObject: utils.AbstractObject{Name: workflow.Name + "_workspace"},
ResourceSet: resources.ResourceSet{
Datas: workflow.Datas,
Processings: workflow.Processings,
Storages: workflow.Storages,
Workflows: workflow.Workflows,
Datacenters: workflow.Datacenters,
},
})
}
return res, code, err
} }
func (wfa *workflowMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) { func (wfa *workflowMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
@ -101,43 +132,31 @@ func (wfa *workflowMongoAccessor) LoadOne(id string) (utils.DBObject, int, error
} }
res_mongo.Decode(&workflow) res_mongo.Decode(&workflow)
// add to workspace // add to workspace
access := (&workspace.Workspace{}).GetAccessor() accessor := (&workspace.Workspace{}).GetAccessor()
ws, _, err := mongo.MONGOService.LoadFilter(map[string]interface{}{ resource, _, err := accessor.Search(workflow.Name+"_workspace", "abstractobject.name")
"active": true, if err == nil && len(resource) > 0 {
}, utils.WORKSPACE.String()) for _, r := range resource {
if err == nil { accessor.UpdateOne(&workspace.Workspace{
var results []workspace.Workspace ResourceSet: resources.ResourceSet{
if err = ws.All(mongo.MngoCtx, &results); err != nil { Datas: workflow.Datas,
return nil, 404, err Processings: workflow.Processings,
} Storages: workflow.Storages,
for _, res := range results { Workflows: workflow.Workflows,
for _, w := range workflow.Workflows { Datacenters: workflow.Datacenters,
if !slices.Contains(res.Workflows, w) { },
res.Workflows = append(res.Workflows, w) }, r.GetID())
}
}
for _, w := range workflow.Datas {
if !slices.Contains(res.Datas, w) {
res.Datas = append(res.Datas, w)
}
}
for _, w := range workflow.Datacenters {
if !slices.Contains(res.Datacenters, w) {
res.Datacenters = append(res.Datacenters, w)
}
}
for _, w := range workflow.Storages {
if !slices.Contains(res.Storages, w) {
res.Storages = append(res.Storages, w)
}
}
for _, w := range workflow.Processings {
if !slices.Contains(res.Processings, w) {
res.Processings = append(res.Processings, w)
}
}
access.UpdateOne(&res, res.GetID())
} }
} else {
accessor.StoreOne(&workspace.Workspace{
AbstractObject: utils.AbstractObject{Name: workflow.Name + "_workspace"},
ResourceSet: resources.ResourceSet{
Datas: workflow.Datas,
Processings: workflow.Processings,
Storages: workflow.Storages,
Workflows: workflow.Workflows,
Datacenters: workflow.Datacenters,
},
})
} }
return &workflow, 200, nil return &workflow, 200, nil
} }