retrieve computing objects from workflow json

This commit is contained in:
pb 2024-04-04 12:31:12 +02:00
parent 8e7835f85b
commit 23ee20a59f
2 changed files with 121 additions and 55 deletions

40
go.mod
View File

@ -1,13 +1,13 @@
module cloud.o-forge.io/core/oc-scheduler
module oc-scheduler
go 1.19
go 1.21.3
require (
cloud.o-forge.io/core/oc-catalog v0.0.0-20230303134311-88c21d182884
cloud.o-forge.io/core/oc-catalog v0.0.0-20240402132424-650fb92e39a8
github.com/goraz/onion v0.1.3
github.com/rs/zerolog v1.29.0
github.com/rs/zerolog v1.32.0
github.com/sbabiv/xml2map v1.2.1
github.com/tidwall/gjson v1.14.4
github.com/tidwall/gjson v1.17.1
)
require (
@ -17,15 +17,13 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/golang/snappy v0.0.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/compress v1.11.7 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
@ -34,22 +32,22 @@ require (
github.com/prometheus/common v0.15.0 // indirect
github.com/prometheus/procfs v0.3.0 // indirect
github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/vk496/cron v1.2.0 // indirect
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c // indirect
github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc // indirect
go.mongodb.org/mongo-driver v1.4.5 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
golang.org/x/tools v0.1.12 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
golang.org/x/mod v0.4.1 // indirect
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 // indirect
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.3.5 // indirect
golang.org/x/tools v0.1.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.1 // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

136
graph.go
View File

@ -8,6 +8,7 @@ import (
"strings"
"cloud.o-forge.io/core/oc-catalog/models"
"github.com/mitchellh/mapstructure"
"github.com/sbabiv/xml2map"
"github.com/tidwall/gjson"
)
@ -26,6 +27,8 @@ type Graph struct {
ws HttpQuery
}
// Create a dictionnaries with each each existing workflow from a workspace, associated to the JSON representation of its content
func (g *Graph) GetGraphList(apiurl string) (map[string]string, error) {
g.ws.Init(apiurl)
body, err := g.ws.Get("v1/workspace/list")
@ -41,12 +44,16 @@ func (g *Graph) GetGraphList(apiurl string) (map[string]string, error) {
return workspaces, nil
}
// Create the objects from the mxgraphxml stored in the workflow given as a parameter
func (g *Graph) LoadFrom(workspace string) error {
// Extract the xmlgraph from the given workspace
xml := gjson.Get(workspace, "MxgraphXML").String()
decodedValue, err := url.QueryUnescape(xml)
if err != nil {
return err
}
os.WriteFile("graph.xml", []byte(decodedValue), 0660)
decoder := xml2map.NewDecoder(strings.NewReader(decodedValue))
result, err := decoder.Decode()
@ -54,43 +61,42 @@ func (g *Graph) LoadFrom(workspace string) error {
return err
}
// Retrieve the content of mxcell and object elements
cells := result["mxGraphModel"].(map[string]interface{})["root"].(map[string]interface{})["mxCell"].([]map[string]interface{})
obj := result["mxGraphModel"].(map[string]interface{})["root"].(map[string]interface{})["object"]
// Append the content of obj to cells depending on if it's a single key/value pair or a list of it
switch v := obj.(type) {
case map[string]interface{}:
cells = append(cells, obj.(map[string]interface{}))
fmt.Printf("One: %v", v)
_ = v
// fmt.Printf("One: %v", v)
case []map[string]interface{}:
cells = append(cells, obj.([]map[string]interface{})...)
fmt.Printf("Many: %v", v)
// fmt.Printf("Many: %v", v)
}
dictionnary := make(map[string]string)
var idlinks []Link
g.GetWorkflowComponents(workspace)
for _, element := range cells {
id := element["@id"].(string)
// Case MXCell
if _, ok := element["@style"]; ok {
if _, ok2 := element["@rID"]; ok2 {
// Resolve elements
fmt.Print(id + ": ")
fmt.Println(element["@rID"], element["@rType"])
fmt.Println(element)
// fmt.Print(id + ": ")
// fmt.Println(element["@rID"], element["@rType"])
// fmt.Println(element)
dictionnary[id] = element["@rID"].(string)
if element["@rType"] == "data" {
g.AddDataModel(element["@rID"].(string))
}
if element["@rType"] == "datacenter" {
g.AddDatacenterModel(element["@rID"].(string))
}
if element["@rType"] == "computing" {
g.AddComputingModel(element["@rID"].(string))
}
if element["@rType"] == "storage" {
g.AddStorageModel(element["@rID"].(string))
}
g.addElementByType(element)
}
} else {
// Case object
// Case object : contains user's input through the GUI
if _, ok := element["mxCell"]; ok {
// Attribute values
@ -99,22 +105,11 @@ func (g *Graph) LoadFrom(workspace string) error {
if _, ok := element["@style"]; ok {
if _, ok2 := element["@rID"]; ok2 {
// Resolve elements
fmt.Print(id + ": ")
fmt.Println(element["@rID"], element["@rType"])
fmt.Println(element)
// fmt.Print(id + ": ")
// fmt.Println(element["@rID"], element["@rType"])
// fmt.Println(element)
dictionnary[id] = element["@rID"].(string)
if element["@rType"] == "data" {
g.AddDataModel(element["@rID"].(string))
}
if element["@rType"] == "datacenter" {
g.AddDatacenterModel(element["@rID"].(string))
}
if element["@rType"] == "computing" {
g.AddComputingModel(element["@rID"].(string))
}
if element["@rType"] == "storage" {
g.AddStorageModel(element["@rID"].(string))
}
g.addElementByType(element)
}
}
}
@ -124,7 +119,7 @@ func (g *Graph) LoadFrom(workspace string) error {
if src, ok := element["@source"]; ok {
//src = element["@source"].(string)
idlinks = append(idlinks, Link{Src: src.(string), Dst: element["@target"].(string)})
fmt.Println("Link: " + src.(string) + " " + element["@target"].(string))
// fmt.Println("Link: " + src.(string) + " " + element["@target"].(string))
}
}
@ -136,6 +131,59 @@ func (g *Graph) LoadFrom(workspace string) error {
return nil
}
func (g *Graph) addElementByType(element map[string]interface{}) {
if element["@rType"] == "data" {
g.AddDataModel(element["@rID"].(string))
}
if element["@rType"] == "datacenter" {
g.AddDatacenterModel(element["@rID"].(string))
}
if element["@rType"] == "computing" {
g.AddComputingModel(element["@rID"].(string))
g.AddUserInput(element["@rID"].(string))
}
if element["@rType"] == "storage" {
g.AddStorageModel(element["@rID"].(string))
}
}
func (g *Graph) GetWorkflowComponents(workspace string){
// retrieve if list of components' object
result := gjson.Get(workspace, "computing")
os.WriteFile("computing.json", []byte(result.String()), 0660)
result.ForEach(func(id, value gjson.Result) bool{
m, ok := value.Value().(map[string]interface{})
if !ok {
fmt.Printf("error getting map from json")
}
var comp_obj models.ComputingModel
comp_obj.ID = id.Str
err := mapstructure.Decode(m,&comp_obj.ComputingNEWModel)
if err != nil {
fmt.Print("Error unpacking json into comp object")
}
g.Computings = append(g.Computings, comp_obj)
return true
})
// var slice_obj []models.ComputingModel
// var response map[string]interface{}
// json.Unmarshal(computings_obj,response)
// if len(computings_obj) != 0 {
// for _, obj := range(computings_obj){
// // var new_comp models.ComputingModel
// // json.Unmarshal(obj,&new_comp)
// fmt.Print(obj)
// }
// }
}
func (g *Graph) AddDataModel(id string) error {
var d models.DataModel
resp, err := g.ws.Get("v1/data/" + id)
@ -180,6 +228,25 @@ func (g *Graph) AddStorageModel(id string) error {
return nil
}
// Add the user input to the object
// So far only computing (command, args, env var) and storage (path to the storing point) allow user input
func (g *Graph) AddUserInput(id string) error {
// TODO refactor the code in oc-catalog models so that we can call the addUserInput regardless of the class
// Detect which type of component
// Retrieve the component from its ID
fmt.Printf("component %s",id)
// switch type
// case computing
// Computing.addUserInput(map[string]interface{})
// case storage
// Storage..addUserInput(map[string]interface{})
return nil
}
func (g *Graph) ExportToArgo(id string) error {
return nil
}
@ -188,3 +255,4 @@ func (g *Graph) ExportToHelm(id string) error {
return nil
}