package main import ( "encoding/json" "fmt" "net/url" "os" "strings" "cloud.o-forge.io/core/oc-catalog/models" "github.com/mitchellh/mapstructure" "github.com/sbabiv/xml2map" "github.com/tidwall/gjson" ) type Link struct { Src string Dst string } type Graph struct { Links []Link Datas []models.DataModel Computings []models.ComputingModel Datacenters []models.DatacenterModel Storages []models.StorageModel 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") if err != nil { return nil, err } workspaces := make(map[string]string) result := gjson.Get(string(body), "Workflows") result.ForEach(func(key, value gjson.Result) bool { workspaces[key.Str] = value.String() return true // keep iterating }) 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() if err != nil { 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{})) _ = v // fmt.Printf("One: %v", v) case []map[string]interface{}: cells = append(cells, obj.([]map[string]interface{})...) // 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) dictionnary[id] = element["@rID"].(string) g.addElementByType(element) } } else { // Case object : contains user's input through the GUI if _, ok := element["mxCell"]; ok { // Attribute values // Extracts the cell ids element = element["mxCell"].(map[string]interface{}) if _, ok := element["@style"]; ok { if _, ok2 := element["@rID"]; ok2 { // Resolve elements // fmt.Print(id + ": ") // fmt.Println(element["@rID"], element["@rType"]) // fmt.Println(element) dictionnary[id] = element["@rID"].(string) g.addElementByType(element) } } } } // register links 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)) } } // translate links for _, link := range idlinks { g.Links = append(g.Links, Link{Src: dictionnary[link.Src], Dst: dictionnary[link.Dst]}) fmt.Println("Link: " + link.Src + " " + link.Dst + " : " + dictionnary[link.Src] + " " + dictionnary[link.Dst]) } 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) if err != nil { return err } json.Unmarshal(resp, &d) g.Datas = append(g.Datas, d) return nil } func (g *Graph) AddDatacenterModel(id string) error { var d models.DatacenterModel resp, err := g.ws.Get("v1/datacenter/" + id) if err != nil { return err } json.Unmarshal(resp, &d) g.Datacenters = append(g.Datacenters, d) return nil } func (g *Graph) AddComputingModel(id string) error { var c models.ComputingModel resp, err := g.ws.Get("v1/computing/" + id) if err != nil { return err } json.Unmarshal(resp, &c) g.Computings = append(g.Computings, c) return nil } func (g *Graph) AddStorageModel(id string) error { var s models.StorageModel resp, err := g.ws.Get("v1/data/" + id) if err != nil { return err } json.Unmarshal(resp, &s) g.Storages = append(g.Storages, s) 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 } func (g *Graph) ExportToHelm(id string) error { return nil }