Files
oc-catalog/controllers/resource.go

228 lines
7.0 KiB
Go
Raw Normal View History

package controllers
import (
2026-04-01 15:56:05 +02:00
"encoding/json"
2026-04-03 14:23:55 +02:00
"fmt"
2026-04-01 15:56:05 +02:00
"oc-catalog/infrastructure"
2026-04-03 14:23:55 +02:00
"strconv"
2025-01-17 17:03:32 +01:00
oclib "cloud.o-forge.io/core/oc-lib"
2026-04-01 15:56:05 +02:00
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
beego "github.com/beego/beego/v2/server/web"
)
2026-04-01 15:56:05 +02:00
// ResourceController aggregates all resource types.
type ResourceController struct {
beego.Controller
}
2026-04-01 15:56:05 +02:00
func resourceTypeEnum(t string, special bool) []oclib.LibDataEnum {
e := []oclib.LibDataEnum{}
if special && t == "resource" {
return e
}
if t == "compute" || t == "resource" {
e = append(e, oclib.LibDataEnum(oclib.COMPUTE_RESOURCE))
}
if t == "data" || t == "resource" {
e = append(e, oclib.LibDataEnum(oclib.DATA_RESOURCE))
}
if t == "storage" || t == "resource" {
e = append(e, oclib.LibDataEnum(oclib.STORAGE_RESOURCE))
}
if t == "processing" || t == "resource" {
e = append(e, oclib.LibDataEnum(oclib.PROCESSING_RESOURCE))
}
if t == "workflow" || t == "resource" {
e = append(e, oclib.LibDataEnum(oclib.WORKFLOW_RESOURCE))
}
return e
}
func (o *ResourceController) collection(special bool) []oclib.LibDataEnum {
// Extrait le type depuis le segment d'URL après "resource"
// URL forme: /oc/resource/{type}/...
typ := o.Ctx.Input.Param(":type")
return resourceTypeEnum(typ, special)
}
func (o *ResourceController) notFound() {
o.Ctx.Output.SetStatus(404)
o.Data["json"] = map[string]interface{}{"error": "unknown resource type", "code": 404, "data": nil}
o.ServeJSON()
}
// @Title GetAll
2026-04-01 15:56:05 +02:00
// @Description list all resources across all types
// @Param type path string true "the type you want to get"
2025-01-17 17:03:32 +01:00
// @Param is_draft query string false "draft wished"
// @Success 200 {resource} models.resource
2026-04-01 15:56:05 +02:00
// @router /:type [get]
func (o *ResourceController) GetAll() {
2025-01-17 17:03:32 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
isDraft := o.Ctx.Input.Query("is_draft")
2026-04-03 14:23:55 +02:00
offset, _ := strconv.Atoi(o.Ctx.Input.Query("offset"))
limit, _ := strconv.Atoi(o.Ctx.Input.Query("limit"))
2026-04-01 15:56:05 +02:00
m := map[string][]utils.ShallowDBObject{}
for _, col := range o.collection(false) {
if m[col.String()] == nil {
m[col.String()] = []utils.ShallowDBObject{}
2024-07-30 10:07:34 +02:00
}
2026-04-03 14:23:55 +02:00
s := oclib.NewRequest(col, user, peerID, groups, nil).LoadAll(isDraft == "true", int64(offset), int64(limit))
2026-04-01 15:56:05 +02:00
m[col.String()] = append(m[col.String()], s.Data...)
}
o.Data["json"] = map[string]interface{}{
"data": m,
"code": 200,
"err": nil,
}
o.ServeJSON()
}
2026-04-01 15:56:05 +02:00
// @Title Search
// @Description search resources across all types
// @Param type path string true "the type you want to get"
// @Param search path string true "the search you want to get"
2025-01-17 17:03:32 +01:00
// @Param is_draft query string false "draft wished"
// @Success 200 {resource} models.resource
2026-04-01 15:56:05 +02:00
// @router /:type/search/:search [get]
func (o *ResourceController) Search() {
2025-01-17 17:03:32 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
isDraft := o.Ctx.Input.Query("is_draft")
2026-04-01 15:56:05 +02:00
search := o.Ctx.Input.Param(":search")
2026-04-03 14:23:55 +02:00
offset, _ := strconv.Atoi(o.Ctx.Input.Query("offset"))
limit, _ := strconv.Atoi(o.Ctx.Input.Query("limit"))
2026-04-01 15:56:05 +02:00
m := map[string][]utils.ShallowDBObject{}
for _, col := range o.collection(false) {
if m[col.String()] == nil {
m[col.String()] = []utils.ShallowDBObject{}
2024-07-30 10:07:34 +02:00
}
2026-04-03 14:23:55 +02:00
s := oclib.NewRequest(col, user, peerID, groups, nil).Search(nil, search, isDraft == "true", int64(offset), int64(limit))
2026-04-01 15:56:05 +02:00
m[col.String()] = append(m[col.String()], s.Data...)
}
o.Data["json"] = map[string]interface{}{
"data": m,
"code": 200,
"err": nil,
}
o.ServeJSON()
}
// @Title Get
2026-04-01 15:56:05 +02:00
// @Description search resources across all types
// @Param type path string true "the type you want to get"
// @Param id path string true "the id you want to get"
2026-04-01 15:56:05 +02:00
// @Param is_draft query string false "draft wished"
// @Success 200 {resource} models.resource
2026-04-01 15:56:05 +02:00
// @router /:type/:id [get]
func (o *ResourceController) Get() {
2025-01-17 17:03:32 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
2026-04-03 14:23:55 +02:00
fmt.Println(user, groups, peerID)
2026-04-01 15:56:05 +02:00
for _, col := range o.collection(false) {
if d := oclib.NewRequest(col, user, peerID, groups, nil).LoadOne(id); d.Data != nil {
o.Data["json"] = d
break
} else {
o.Data["json"] = d
}
}
o.ServeJSON()
}
// @Title Post
// @Description search resources across all types
// @Param type path string true "the type you want to get"
// @Param data body json true "body for data content (Json format)"
// @Success 200 {resource} models.resource
// @router /:type [post]
func (o *ResourceController) Post() {
libs := o.collection(true)
if len(libs) == 0 {
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": 500,
"err": "not a proper type",
}
}
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
var res map[string]interface{}
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
data := oclib.NewRequest(libs[0], user, peerID, groups, nil).StoreOne(res)
if data.Err == "" {
payload, _ := json.Marshal(data.Data.Serialize(data.Data))
infrastructure.EmitNATS(user, groups, tools.PropalgationMessage{
Action: tools.PB_CREATE,
DataType: libs[0].EnumIndex(),
Payload: payload,
})
}
o.Data["json"] = data
o.ServeJSON()
}
// @Title Put
// @Description search resources across all types
// @Param type path string true "the type you want to get"
// @Param id path string true "the id you want to get"
// @Param data body json true "body for data content (Json format)"
// @Success 200 {resource} models.resource
// @router /:type/:id [put]
func (o *ResourceController) Put() {
libs := o.collection(true)
if len(libs) == 0 {
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": 500,
"err": "not a proper type",
}
}
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
var res map[string]interface{}
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
data := oclib.NewRequest(libs[0], user, peerID, groups, nil).UpdateOne(res, id)
if data.Err == "" {
payload, _ := json.Marshal(data.Data.Serialize(data.Data))
infrastructure.EmitNATS(user, groups, tools.PropalgationMessage{
Action: tools.PB_UPDATE,
DataType: libs[0].EnumIndex(),
Payload: payload,
})
}
o.Data["json"] = data
o.ServeJSON()
}
// @Title Delete
// @Description search resources across all types
// @Param type path string true "the type you want to get"
// @Param id path string true "the id you want to get"
// @Param is_draft query string false "draft wished"
// @Success 200 {resource} models.resource
// @router /:type/:id [delete]
func (o *ResourceController) Delete() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
for _, col := range o.collection(false) {
data := oclib.NewRequest(col, user, peerID, groups, nil).DeleteOne(id)
if data.Err == "" {
o.Data["json"] = data
payload, _ := json.Marshal(data.Data.Serialize(data.Data))
infrastructure.EmitNATS(user, groups, tools.PropalgationMessage{
Action: tools.PB_DELETE,
DataType: col.EnumIndex(),
Payload: payload,
})
break
2024-07-30 10:07:34 +02:00
} else {
2026-04-01 15:56:05 +02:00
o.Data["json"] = data
2024-07-30 10:07:34 +02:00
}
}
o.ServeJSON()
}