Files
oc-catalog/controllers/resource.go
2026-03-17 14:41:22 +01:00

117 lines
3.8 KiB
Go
Executable File

package controllers
import (
"fmt"
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 resource
type ResourceController struct {
beego.Controller
}
// @Title GetAll
// @Description find resource by id
// @Param is_draft query string false "draft wished"
// @Success 200 {resource} models.resource
// @router / [get]
func (o *ResourceController) GetAll() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
results := map[string]interface{}{}
isDraft := o.Ctx.Input.Query("is_draft")
for _, resource := range []oclib.LibDataEnum{
data_collection, comp_collection, storage_collection,
processing_collection, workflow_collection} {
d := oclib.NewRequest(resource, user, peerID, groups, nil).LoadAll(isDraft == "true")
if d.Code != 200 || len(d.Data) == 0 {
results[resource.String()] = []interface{}{}
} else {
results[resource.String()] = d.Data
}
}
o.Data["json"] = map[string]interface{}{"data": results, "code": 200, "error": ""}
o.ServeJSON()
}
// @Title Get
// @Description find resource by key word
// @Param search path string true "the search you want to get"
// @Param is_draft query string false "draft wished"
// @Success 200 {resource} models.resource
// @router /search/:search [get]
func (o *ResourceController) Search() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
search := o.Ctx.Input.Param(":search")
isDraft := o.Ctx.Input.Query("is_draft")
results := map[string]interface{}{}
for _, resource := range []oclib.LibDataEnum{
data_collection, comp_collection, storage_collection,
processing_collection, workflow_collection} {
fmt.Println("search", search)
d := oclib.NewRequest(resource, user, peerID, groups, nil).Search(nil, search, isDraft == "true")
if d.Code != 200 || len(d.Data) == 0 {
results[resource.String()] = []interface{}{}
} else {
results[resource.String()] = d.Data
}
}
o.Data["json"] = map[string]interface{}{"data": results, "code": 200, "error": ""}
o.ServeJSON()
}
// @Title PostPlantUML
// @Description parse plantuml text and return all formed resource objects
// @Param body body string true "PlantUML text content"
// @Success 200 {resource} models.resource
// @Failure 406 {string} string "Bad request"
// @router /plantuml [post]
func (o *ResourceController) PostPlantUML() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
body := o.Ctx.Input.CopyBody(1000000)
req := &tools.APIRequest{Username: user, PeerID: peerID, Groups: groups}
wf, err := parsePlantUMLText(body, req)
if err != nil {
o.Data["json"] = map[string]interface{}{"data": nil, "code": 406, "error": err.Error()}
o.ServeJSON()
return
}
o.Data["json"] = map[string]interface{}{
"data": map[string]interface{}{
"compute": wf.ComputeResources,
"data": wf.DataResources,
"storage": wf.StorageResources,
"processing": wf.ProcessingResources,
"workflow": wf.WorkflowResources,
},
"code": 200,
"error": nil,
}
o.ServeJSON()
}
// @Title Get
// @Description find resource by id
// @Param id path string true "the id you want to get"
// @Success 200 {resource} models.resource
// @router /:id [get]
func (o *ResourceController) Get() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
results := map[string]interface{}{}
for _, resource := range []oclib.LibDataEnum{
data_collection, comp_collection, storage_collection,
processing_collection, workflow_collection} {
d := oclib.NewRequest(resource, user, peerID, groups, nil).LoadOne(id)
if d.Code != 200 {
results[resource.String()] = nil
} else {
results[resource.String()] = d.Data
}
}
o.Data["json"] = map[string]interface{}{"data": results, "code": 200, "error": ""}
o.ServeJSON()
}