222 lines
6.7 KiB
Go
Executable File
222 lines
6.7 KiB
Go
Executable File
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"oc-catalog/infrastructure"
|
|
|
|
oclib "cloud.o-forge.io/core/oc-lib"
|
|
"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"
|
|
)
|
|
|
|
// ResourceController aggregates all resource types.
|
|
type ResourceController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
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
|
|
// @Description list all resources across all types
|
|
// @Param type path string true "the type you want to get"
|
|
// @Param is_draft query string false "draft wished"
|
|
// @Success 200 {resource} models.resource
|
|
// @router /:type [get]
|
|
func (o *ResourceController) GetAll() {
|
|
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
|
isDraft := o.Ctx.Input.Query("is_draft")
|
|
|
|
m := map[string][]utils.ShallowDBObject{}
|
|
for _, col := range o.collection(false) {
|
|
if m[col.String()] == nil {
|
|
m[col.String()] = []utils.ShallowDBObject{}
|
|
}
|
|
s := oclib.NewRequest(col, user, peerID, groups, nil).LoadAll(isDraft == "true")
|
|
m[col.String()] = append(m[col.String()], s.Data...)
|
|
}
|
|
o.Data["json"] = map[string]interface{}{
|
|
"data": m,
|
|
"code": 200,
|
|
"err": nil,
|
|
}
|
|
o.ServeJSON()
|
|
}
|
|
|
|
// @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"
|
|
// @Param is_draft query string false "draft wished"
|
|
// @Success 200 {resource} models.resource
|
|
// @router /:type/search/:search [get]
|
|
func (o *ResourceController) Search() {
|
|
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
|
isDraft := o.Ctx.Input.Query("is_draft")
|
|
search := o.Ctx.Input.Param(":search")
|
|
|
|
m := map[string][]utils.ShallowDBObject{}
|
|
for _, col := range o.collection(false) {
|
|
if m[col.String()] == nil {
|
|
m[col.String()] = []utils.ShallowDBObject{}
|
|
}
|
|
s := oclib.NewRequest(col, user, peerID, groups, nil).Search(nil, search, isDraft == "true")
|
|
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
|
|
// @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 [get]
|
|
func (o *ResourceController) Get() {
|
|
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
|
id := o.Ctx.Input.Param(":id")
|
|
|
|
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
|
|
} else {
|
|
o.Data["json"] = data
|
|
}
|
|
}
|
|
o.ServeJSON()
|
|
}
|