removed componentParser to reuse existing methods and json.Unmarshal

This commit is contained in:
pb 2024-04-09 14:38:52 +02:00
parent 86062f3d88
commit 6bbb2a0b40
2 changed files with 64 additions and 154 deletions

View File

@ -1,86 +0,0 @@
package main
import (
"fmt"
"cloud.o-forge.io/core/oc-catalog/models"
"github.com/mitchellh/mapstructure"
)
// This module allows us to :
// - Reduce the size of graph.go
// - apply specific object construction logic depending on the component's type
// So far the unpacking of json/map data into the models has the caveat of not applying to the nested struct
func ConstructComputingObject(id string, component_info map[string]interface{}, user_inputs map[string]interface{}) (computing models.ComputingModel) {
computing.ID = id
err := mapstructure.Decode(component_info,&computing.ComputingNEWModel)
if err != nil {
fmt.Println("Computing : Error unpacking json into objects info")
log.Err(err)
}
err = mapstructure.Decode(user_inputs,&computing.ComputingNEWModel)
if err != nil {
fmt.Println("Computing : Error unpacking data input into comp object")
log.Err(err).Msg(err.Error())
}
return
}
func ConstructDataObject(id string, component_info map[string]interface{}, user_inputs map[string]interface{}) (data models.DataModel) {
data.ID = id
err := mapstructure.Decode(component_info,&data.DataNEWModel)
if err != nil {
fmt.Println("Data: Error unpacking json into objects info")
log.Err(err)
}
err = mapstructure.Decode(user_inputs,&data.DataNEWModel)
if err != nil {
fmt.Println("Data: Error unpacking data input into comp object")
log.Err(err).Msg(err.Error())
}
return
}
func ConstructDatacenterObject(id string, component_info map[string]interface{}, user_inputs map[string]interface{}) (datacenter models.DatacenterModel) {
datacenter.ID = id
err := mapstructure.Decode(component_info,&datacenter.DatacenterNEWModel)
if err != nil {
fmt.Println("Datacenter: Error unpacking json into objects info")
log.Err(err)
}
err = mapstructure.Decode(user_inputs,&datacenter.DatacenterNEWModel)
if err != nil {
fmt.Println("Datacenter: Error unpacking data input into comp object")
log.Err(err).Msg(err.Error())
}
return
}
func ConstructStorageObject(id string, component_info map[string]interface{}, user_inputs map[string]interface{}) (storage models.StorageModel) {
storage.ID = id
err := mapstructure.Decode(component_info,&storage.StorageNEWModel)
if err != nil {
fmt.Println("Storage: Error unpacking json into objects info")
log.Err(err)
}
err = mapstructure.Decode(user_inputs,&storage.StorageNEWModel)
if err != nil {
fmt.Println("Storage: Error unpacking data input into comp object")
log.Err(err).Msg(err.Error())
}
return
}

View File

@ -84,7 +84,7 @@ func (g *Graph) LoadFrom(workspace string) error {
g.GetWorkflowComponents(workspace) g.GetWorkflowComponents(workspace)
// for _, element := range cells { for _, element := range cells {
// id := element["@id"].(string) // id := element["@id"].(string)
// // Case MXCell // // Case MXCell
// if _, ok := element["@style"]; ok { // if _, ok := element["@style"]; ok {
@ -116,14 +116,14 @@ func (g *Graph) LoadFrom(workspace string) error {
// } // }
// } // }
// // register links // register links
// if src, ok := element["@source"]; ok { if src, ok := element["@source"]; ok {
// //src = element["@source"].(string) //src = element["@source"].(string)
// idlinks = append(idlinks, Link{Src: src.(string), Dst: element["@target"].(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))
// } }
// } }
// translate links // translate links
for _, link := range idlinks { for _, link := range idlinks {
@ -133,20 +133,20 @@ func (g *Graph) LoadFrom(workspace string) error {
return nil return nil
} }
func (g *Graph) addElementByType(element map[string]interface{}) { // func (g *Graph) addElementByType(element map[string]interface{}) {
if element["@rType"] == "data" { // if element["@rType"] == "data" {
g.AddDataModel(element["@rID"].(string)) // g.AddDataModel(element["@rID"].(string))
} // }
if element["@rType"] == "datacenter" { // if element["@rType"] == "datacenter" {
g.AddDatacenterModel(element["@rID"].(string)) // g.AddDatacenterModel(element["@rID"].(string))
} // }
if element["@rType"] == "computing" { // if element["@rType"] == "computing" {
g.AddComputingModel(element["@rID"].(string)) // g.AddComputingModel(element["@rID"].(string))
} // }
if element["@rType"] == "storage" { // if element["@rType"] == "storage" {
g.AddStorageModel(element["@rID"].(string)) // g.AddStorageModel(element["@rID"].(string))
} // }
} // }
// TODO : extract all the JSON/data processing to a new object that takes // TODO : extract all the JSON/data processing to a new object that takes
// the current graph as an attribute and deals with adding new objects // the current graph as an attribute and deals with adding new objects
@ -163,29 +163,21 @@ func (g *Graph) GetWorkflowComponents(workflow string){
if (result.Type != gjson.Null) { if (result.Type != gjson.Null) {
result.ForEach(func(id, value gjson.Result) bool{ result.ForEach(func(id, value gjson.Result) bool{
component_db, ok := value.Value().(map[string]interface{}) comp_id := value.Get("referenceID").Str
if !ok {
fmt.Printf("error getting map from json")
}
comp_id := component_db["referenceID"].(string)
var obj interface{}
if (comp_id != "") { if (comp_id != "") {
component_map, _ := g.GetComponentById(component_type, comp_id)
switch component_type { switch component_type {
case "computing": case "computing":
obj = ConstructComputingObject(id.Str,component_map,component_db) g.AddComputingModel(comp_id, value)
case "data": case "data":
obj = ConstructDataObject(id.Str,component_map,component_db) g.AddDataModel(comp_id, value)
case "datacenter": case "datacenter":
obj = ConstructDatacenterObject(id.Str,component_map,component_db) g.AddDatacenterModel(comp_id, value)
case "storage": case "storage":
obj = ConstructStorageObject(id.Str,component_map,component_db) g.AddStorageModel(comp_id, value)
default : default :
logs.Critical("Component type doesn't match a know type : " + component_type) logs.Critical("Component type doesn't match a know type : " + component_type)
} }
g.AddComponentObject(component_type, obj)
} }
return true return true
}) })
@ -234,46 +226,50 @@ func (g *Graph) GetComponentById(component_type string, id string) (map[string]i
return comp, nil return comp, nil
} }
func (g *Graph) AddDataModel(id string) error { func (g *Graph) AddDataModel(id string, user_input gjson.Result) error {
var d models.DataModel var d models.DataModel
resp, err := g.ws.Get("v1/data/" + id) resp, err := g.ws.Get("v1/data/" + id)
if err != nil { if err != nil {
return err return err
} }
json.Unmarshal(resp, &d) json.Unmarshal(resp, &d)
json.Unmarshal([]byte(user_input.Raw),&d.DataNEWModel)
g.Datas = append(g.Datas, d) g.Datas = append(g.Datas, d)
return nil return nil
} }
func (g *Graph) AddDatacenterModel(id string) error { func (g *Graph) AddDatacenterModel(id string, user_input gjson.Result) error {
var d models.DatacenterModel var d models.DatacenterModel
resp, err := g.ws.Get("v1/datacenter/" + id) resp, err := g.ws.Get("v1/datacenter/" + id)
if err != nil { if err != nil {
return err return err
} }
json.Unmarshal(resp, &d) json.Unmarshal(resp, &d)
json.Unmarshal([]byte(user_input.Raw),&d.DatacenterNEWModel)
g.Datacenters = append(g.Datacenters, d) g.Datacenters = append(g.Datacenters, d)
return nil return nil
} }
func (g *Graph) AddComputingModel(id string) error { func (g *Graph) AddComputingModel(id string, user_input gjson.Result) error {
var c models.ComputingModel var c models.ComputingModel
resp, err := g.ws.Get("v1/computing/" + id) resp, err := g.ws.Get("v1/computing/" + id)
if err != nil { if err != nil {
return err return err
} }
json.Unmarshal(resp, &c) json.Unmarshal(resp, &c)
json.Unmarshal([]byte(user_input.Raw),&c.ComputingNEWModel)
g.Computings = append(g.Computings, c) g.Computings = append(g.Computings, c)
return nil return nil
} }
func (g *Graph) AddStorageModel(id string) error { func (g *Graph) AddStorageModel(id string, user_input gjson.Result) error {
var s models.StorageModel var s models.StorageModel
resp, err := g.ws.Get("v1/data/" + id) resp, err := g.ws.Get("v1/data/" + id)
if err != nil { if err != nil {
return err return err
} }
json.Unmarshal(resp, &s) json.Unmarshal(resp, &s)
json.Unmarshal([]byte(user_input.Raw),&s.StorageNEWModel)
g.Storages = append(g.Storages, s) g.Storages = append(g.Storages, s)
return nil return nil
} }