test of a lightest formula of code

This commit is contained in:
mr
2024-11-28 11:05:54 +01:00
parent 15ca06aba8
commit 2816e3ea35
36 changed files with 574 additions and 783 deletions

View File

@@ -1,7 +1,6 @@
package workflow
import (
"encoding/json"
"errors"
"cloud.o-forge.io/core/oc-lib/models/peer"
@@ -108,7 +107,7 @@ func (wfa *Workflow) CheckBooking(caller *tools.HTTPCaller) (bool, error) {
if wfa.Graph == nil { // no graph no booking
return false, nil
}
accessor := (&compute.ComputeResource{}).GetAccessor(nil)
accessor := (&compute.ComputeResource{}).GetAccessor("", []string{}, caller)
for _, link := range wfa.Graph.Links {
if ok, dc_id := wfa.isDCLink(link); ok { // check if the link is a link between a compute and a resource
dc, code, _ := accessor.LoadOne(dc_id)
@@ -129,31 +128,8 @@ func (wfa *Workflow) CheckBooking(caller *tools.HTTPCaller) (bool, error) {
return true, nil
}
func (d *Workflow) GetName() string {
return d.Name
}
func (d *Workflow) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
data := New() // Create a new instance of the accessor
data.Init(tools.WORKFLOW, caller) // Initialize the accessor with the WORKFLOW model type
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
}
func (dma *Workflow) 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 *Workflow) 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
}

View File

@@ -8,9 +8,9 @@ import (
type WorkflowHistory struct{ Workflow }
func (d *WorkflowHistory) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
data := New() // Create a new instance of the accessor
data.Init(tools.WORKSPACE_HISTORY, caller) // Initialize the accessor with the WORKSPACE model type
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
}
func (r *WorkflowHistory) GenerateID() {

View File

@@ -22,11 +22,21 @@ import (
type workflowMongoAccessor struct {
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
computeResourceAccessor utils.Accessor
collaborativeAreaAccessor utils.Accessor
executionAccessor utils.Accessor
workspaceAccessor utils.Accessor
}
// New creates a new instance of the workflowMongoAccessor
func New() *workflowMongoAccessor {
return &workflowMongoAccessor{}
func New(peerID string, groups []string) *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),
}
}
/*
@@ -127,7 +137,6 @@ func (wfa *workflowMongoAccessor) book(id string, realData *Workflow, execs []*w
g = realData.Graph
}
if g != nil && g.Links != nil && len(g.Links) > 0 { // if the graph is set and has links then book the workflow (even on ourselves)
accessor := (&compute.ComputeResource{}).GetAccessor(nil)
isDCFound := []string{}
for _, link := range g.Links {
if ok, dc_id := realData.isDCLink(link); ok { // check if the link is a link between a compute and a resource booking is only on compute
@@ -135,7 +144,7 @@ func (wfa *workflowMongoAccessor) book(id string, realData *Workflow, execs []*w
continue
} // if the compute is already found, skip it
isDCFound = append(isDCFound, dc_id)
dc, code, _ := accessor.LoadOne(dc_id)
dc, code, _ := wfa.computeResourceAccessor.LoadOne(dc_id)
if code != 200 {
continue
}
@@ -169,8 +178,7 @@ func (wfa *workflowMongoAccessor) share(realData *Workflow, delete bool, caller
return
}
for _, sharedID := range realData.Shared { // loop through the shared ids
access := (&shallow_collaborative_area.ShallowCollaborativeArea{}).GetAccessor(nil)
res, code, _ := access.LoadOne(sharedID)
res, code, _ := wfa.collaborativeAreaAccessor.LoadOne(sharedID)
if code != 200 {
continue
}
@@ -186,7 +194,8 @@ func (wfa *workflowMongoAccessor) share(realData *Workflow, delete bool, caller
history.StoreOne(history.MapFromWorkflow(res.(*Workflow)))
_, err = paccess.LaunchPeerExecution(p, res.GetID(), tools.WORKFLOW, tools.DELETE, map[string]interface{}{}, caller)
} else { // if the workflow is updated, share the update
_, err = paccess.LaunchPeerExecution(p, res.GetID(), tools.WORKFLOW, tools.PUT, res.Serialize(), caller)
_, err = paccess.LaunchPeerExecution(p, res.GetID(), tools.WORKFLOW, tools.PUT,
res.Serialize(res), caller)
}
}
if err != nil {
@@ -211,7 +220,6 @@ func (wfa *workflowMongoAccessor) execution(id string, realData *Workflow, delet
return 409, err
}
accessor := (&workflow_execution.WorkflowExecution{}).GetAccessor(nil)
execs, err := wfa.getExecutions(id, realData) // get the executions of the workflow
if err != nil {
return 422, err
@@ -226,7 +234,7 @@ func (wfa *workflowMongoAccessor) execution(id string, realData *Workflow, delet
}
fmt.Println("BOOKING", delete)
for _, obj := range execs {
_, code, err := accessor.StoreOne(obj)
_, code, err := wfa.executionAccessor.StoreOne(obj)
fmt.Println("EXEC", code, err)
if code != 200 {
return code, err
@@ -301,21 +309,20 @@ func (wfa *workflowMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject,
// it stores the workflow resources in a specific workspace to never have a conflict in UI and logic
func (wfa *workflowMongoAccessor) execute(workflow *Workflow, delete bool, active bool) {
accessor := (&workspace.Workspace{}).GetAccessor(nil)
filters := &dbs.Filters{
Or: map[string][]dbs.Filter{ // filter by standard workspace name attached to a workflow
"abstractobject.name": {{dbs.LIKE.String(), workflow.Name + "_workspace"}},
},
}
resource, _, err := accessor.Search(filters, "")
resource, _, err := wfa.workspaceAccessor.Search(filters, "")
if delete { // if delete is set to true, delete the workspace
for _, r := range resource {
accessor.DeleteOne(r.GetID())
wfa.workspaceAccessor.DeleteOne(r.GetID())
}
return
}
if err == nil && len(resource) > 0 { // if the workspace already exists, update it
accessor.UpdateOne(&workspace.Workspace{
wfa.workspaceAccessor.UpdateOne(&workspace.Workspace{
Active: active,
ResourceSet: resources.ResourceSet{
Datas: workflow.Datas,
@@ -326,7 +333,7 @@ func (wfa *workflowMongoAccessor) execute(workflow *Workflow, delete bool, activ
},
}, resource[0].GetID())
} else { // if the workspace does not exist, create it
accessor.StoreOne(&workspace.Workspace{
wfa.workspaceAccessor.StoreOne(&workspace.Workspace{
Active: active,
AbstractObject: utils.AbstractObject{Name: workflow.Name + "_workspace"},
ResourceSet: resources.ResourceSet{
@@ -354,7 +361,6 @@ func (wfa *workflowMongoAccessor) LoadOne(id string) (utils.DBObject, int, error
if (workflow.Schedule.End != nil && now.After(*workflow.Schedule.End)) || (workflow.Schedule.End == nil && workflow.Schedule.Start != nil && now.After(*workflow.Schedule.Start)) { // if the start date is passed, then you can book
workflow.ScheduleActive = false
wfa.GenericRawUpdateOne(&workflow, id, wfa)
} // if the start date is passed, update the executions
}
wfa.execute(&workflow, false, true) // if no workspace is attached to the workflow, create it

View File

@@ -12,7 +12,7 @@ func TestStoreOneWorkflow(t *testing.T) {
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
}
wma := New()
wma := New("", nil)
id, _, _ := wma.StoreOne(&w)
assert.NotEmpty(t, id)
@@ -23,7 +23,7 @@ func TestLoadOneWorkflow(t *testing.T) {
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
}
wma := New()
wma := New("", nil)
new_w, _, _ := wma.StoreOne(&w)
assert.Equal(t, w, new_w)
}