2024-08-02 13:34:39 +02:00
|
|
|
package workflow_builder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
oclib "cloud.o-forge.io/core/oc-lib"
|
|
|
|
workflow "cloud.o-forge.io/core/oc-lib/models/workflow"
|
|
|
|
)
|
|
|
|
|
|
|
|
type WorflowDB struct {
|
2024-08-19 11:43:40 +02:00
|
|
|
Workflow *workflow.Workflow
|
2024-08-02 13:34:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the obj!ects from the mxgraphxml stored in the workflow given as a parameter
|
|
|
|
func (w *WorflowDB) LoadFrom(workflow_id string) error {
|
2024-08-19 11:43:40 +02:00
|
|
|
var err error
|
|
|
|
if w.Workflow, err = w.getWorkflow(workflow_id); err != nil {
|
2024-08-02 13:34:39 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use oclib to retrieve the graph contained in the workflow referenced
|
2024-08-19 11:43:40 +02:00
|
|
|
func (w *WorflowDB) getWorkflow(workflow_id string) (workflow *workflow.Workflow, err error) {
|
|
|
|
logger := oclib.GetLogger()
|
2024-08-02 13:34:39 +02:00
|
|
|
|
2024-08-19 11:43:40 +02:00
|
|
|
lib_data := oclib.LoadOne(oclib.LibDataEnum(oclib.WORKFLOW), workflow_id)
|
2024-10-11 13:44:16 +02:00
|
|
|
fmt.Println(lib_data.Code, lib_data.Err)
|
2024-08-02 13:34:39 +02:00
|
|
|
if lib_data.Code != 200 {
|
2024-08-19 11:43:40 +02:00
|
|
|
logger.Error().Msg("Error loading the graph")
|
2024-08-02 13:34:39 +02:00
|
|
|
return workflow, errors.New(lib_data.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
new_wf := lib_data.ToWorkflow()
|
|
|
|
if new_wf == nil {
|
2024-08-19 11:43:40 +02:00
|
|
|
logger.Error().Msg("WorflowDB object is empty for " + workflow_id)
|
2024-08-02 13:34:39 +02:00
|
|
|
return workflow, errors.New("WorflowDB can't be empty")
|
|
|
|
}
|
|
|
|
return new_wf, nil
|
|
|
|
}
|
|
|
|
|
2024-10-11 13:44:16 +02:00
|
|
|
func (w *WorflowDB) ExportToArgo(timeout int) (string, int, error) {
|
2024-08-19 11:43:40 +02:00
|
|
|
logger := oclib.GetLogger()
|
|
|
|
|
|
|
|
if len(w.Workflow.Name) == 0 || w.Workflow.Graph == nil {
|
2024-10-11 13:44:16 +02:00
|
|
|
return "", 0, fmt.Errorf("can't export a graph that has not been loaded yet")
|
2024-08-02 13:34:39 +02:00
|
|
|
}
|
2024-08-19 11:43:40 +02:00
|
|
|
|
2024-10-11 13:44:16 +02:00
|
|
|
argo_builder := ArgoBuilder{OriginWorkflow: w.Workflow, Timeout: timeout}
|
|
|
|
filename, stepMax, _, _, err := argo_builder.CreateDAG(true)
|
2024-08-02 13:34:39 +02:00
|
|
|
if err != nil {
|
2024-08-19 11:43:40 +02:00
|
|
|
logger.Error().Msg("Could not create the argo file for " + w.Workflow.Name)
|
2024-10-11 13:44:16 +02:00
|
|
|
return "", 0, err
|
2024-08-02 13:34:39 +02:00
|
|
|
}
|
2024-10-11 13:44:16 +02:00
|
|
|
return filename, stepMax, nil
|
2024-08-02 13:34:39 +02:00
|
|
|
}
|
|
|
|
|
2024-08-19 11:43:40 +02:00
|
|
|
// TODO implement this function
|
2024-08-02 13:34:39 +02:00
|
|
|
func (w *WorflowDB) ExportToHelm(id string) error {
|
|
|
|
return nil
|
|
|
|
}
|