package controllers import ( "encoding/json" "oc-catalog/infrastructure" oclib "cloud.o-forge.io/core/oc-lib" "cloud.o-forge.io/core/oc-lib/tools" beego "github.com/beego/beego/v2/server/web" ) // Operations about compute type ComputeController struct { beego.Controller } var comp_collection = oclib.LibDataEnum(oclib.COMPUTE_RESOURCE) var comp_dt = tools.COMPUTE_RESOURCE // @Title Update // @Description create computes // @Param id path string true "the compute id you want to get" // @Param body body models.compute true "The compute content" // @Success 200 {compute} models.compute // @router /:id [put] func (o *ComputeController) Put() { user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request) // store and return Id or post with UUID var res map[string]interface{} id := o.Ctx.Input.Param(":id") json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res) data := oclib.NewRequest(comp_collection, user, peerID, groups, nil).UpdateOne(res, id) if data.Err == "" { data, _ := json.Marshal(data.Data.Serialize(data.Data)) infrastructure.EmitNATS(user, tools.PropalgationMessage{ Action: tools.PB_UPDATE, DataType: comp_dt.EnumIndex(), Payload: data, }) } o.Data["json"] = data o.ServeJSON() } // @Title Create // @Description create compute // @Param compute body json true "body for compute content (Json format)" // @Success 200 {compute} models.compute // @router / [post] func (o *ComputeController) Post() { user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request) var res map[string]interface{} json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res) data := oclib.NewRequest(comp_collection, user, peerID, groups, nil).StoreOne(res) if data.Err == "" { data, _ := json.Marshal(data.Data.Serialize(data.Data)) infrastructure.EmitNATS(user, tools.PropalgationMessage{ Action: tools.PB_CREATE, DataType: comp_dt.EnumIndex(), Payload: data, }) } o.Data["json"] = data o.ServeJSON() } // @Title GetAll // @Description find compute by id // @Param is_draft query string false "draft wished" // @Success 200 {compute} models.compute // @router / [get] func (o *ComputeController) GetAll() { user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request) isDraft := o.Ctx.Input.Query("is_draft") o.Data["json"] = oclib.NewRequest(comp_collection, user, peerID, groups, nil).LoadAll(isDraft == "true") o.ServeJSON() } // @Title Get // @Description find compute by key word // @Param search path string true "the search you want to get" // @Param is_draft query string false "draft wished" // @Success 200 {compute} models.compute // @router /search/:search [get] func (o *ComputeController) Search() { user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request) isDraft := o.Ctx.Input.Query("is_draft") search := o.Ctx.Input.Param(":search") o.Data["json"] = oclib.NewRequest(comp_collection, user, peerID, groups, nil).Search(nil, search, isDraft == "true") o.ServeJSON() } // @Title Get // @Description find compute by id // @Param id path string true "the id you want to get" // @Success 200 {compute} models.compute // @router /:id [get] func (o *ComputeController) Get() { user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request) id := o.Ctx.Input.Param(":id") o.Data["json"] = oclib.NewRequest(comp_collection, user, peerID, groups, nil).LoadOne(id) o.ServeJSON() } // @Title Delete // @Description delete the compute // @Param id path string true "The id you want to delete" // @Success 200 {compute} delete success! // @router /:id [delete] func (o *ComputeController) Delete() { user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request) id := o.Ctx.Input.Param(":id") data := oclib.NewRequest(comp_collection, user, peerID, groups, nil).DeleteOne(id) if data.Err == "" { data, _ := json.Marshal(data.Data.Serialize(data.Data)) infrastructure.EmitNATS(user, tools.PropalgationMessage{ Action: tools.PB_DELETE, DataType: comp_dt.EnumIndex(), Payload: data, }) } o.Data["json"] = oclib.NewRequest(comp_collection, user, peerID, groups, nil).DeleteOne(id) o.ServeJSON() } // @Title Search Decentralized // @Description find workflow by key word // @Param search path string true "the search you want to get" // @Param is_draft query string false "draft wished" // @Success 200 {workflow} models.workflow // @router /search/:search/decentralized/:type [get] func (o *ComputeController) SearchDecentralized() { user, _, _ := oclib.ExtractTokenInfo(*o.Ctx.Request) search := o.Ctx.Input.Param(":search") t := o.Ctx.Input.Param(":type") b, err := json.Marshal(map[string]string{ "search": search, "type": t, }) infrastructure.EmitNATS(user, tools.PropalgationMessage{ Action: tools.PB_SEARCH, DataType: comp_dt.EnumIndex(), Payload: b, }) if err != nil { o.Data["json"] = map[string]interface{}{ "data": nil, "code": 400, "error": err, } o.ServeJSON() return } Websocket(o.Ctx.Request.Context(), user, o.Ctx.ResponseWriter, o.Ctx.Request) }