Simplify but Complete Catalog
This commit is contained in:
@@ -1,85 +1,221 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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"
|
||||
)
|
||||
|
||||
// Operations about resource
|
||||
// ResourceController aggregates all resource types.
|
||||
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
|
||||
}
|
||||
func resourceTypeEnum(t string, special bool) []oclib.LibDataEnum {
|
||||
e := []oclib.LibDataEnum{}
|
||||
if special && t == "resource" {
|
||||
return e
|
||||
}
|
||||
o.Data["json"] = map[string]interface{}{"data": results, "code": 200, "error": ""}
|
||||
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 Get
|
||||
// @Description find resource by key word
|
||||
// @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 /search/:search [get]
|
||||
// @router /:type/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
|
||||
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.Data["json"] = map[string]interface{}{"data": results, "code": 200, "error": ""}
|
||||
o.ServeJSON()
|
||||
}
|
||||
|
||||
// @Title Get
|
||||
// @Description find resource by id
|
||||
// @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 /:id [get]
|
||||
// @router /:type/: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
|
||||
|
||||
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 {
|
||||
results[resource.String()] = d.Data
|
||||
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.Data["json"] = map[string]interface{}{"data": results, "code": 200, "error": ""}
|
||||
o.ServeJSON()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user