oc-catalog/controllers/resource.go

110 lines
3.4 KiB
Go
Raw Normal View History

package controllers
import (
oclib "cloud.o-forge.io/core/oc-lib"
beego "github.com/beego/beego/v2/server/web"
)
// Operations about resource
type ResourceController struct {
beego.Controller
}
// @Title GetAll
// @Description find resource by id
// @Success 200 {resource} models.resource
// @router / [get]
func (o *ResourceController) GetAll() {
results := map[string]interface{}{}
2024-10-15 12:13:20 +02:00
for _, resource := range []oclib.LibDataEnum{
oclib.LibDataEnum(oclib.DATA_RESOURCE),
2024-11-07 13:42:19 +01:00
oclib.LibDataEnum(oclib.COMPUTE_RESOURCE),
2024-10-15 12:13:20 +02:00
oclib.LibDataEnum(oclib.STORAGE_RESOURCE),
oclib.LibDataEnum(oclib.PROCESSING_RESOURCE),
oclib.LibDataEnum(oclib.WORKFLOW_RESOURCE)} {
2024-07-30 10:07:34 +02:00
d := oclib.LoadAll(resource)
if d.Code != 200 || len(d.Data) == 0 {
results[resource.String()] = []interface{}{}
} else {
2024-07-30 15:56:12 +02:00
results[resource.String()] = d.Data
2024-07-30 10:07:34 +02:00
}
}
2024-07-30 15:56:12 +02:00
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"
// @Success 200 {resource} models.resource
2024-07-30 10:07:34 +02:00
// @router /search/:search [get]
func (o *ResourceController) Search() {
search := o.Ctx.Input.Param(":search")
results := map[string]interface{}{}
2024-10-15 12:13:20 +02:00
for _, resource := range []oclib.LibDataEnum{
oclib.LibDataEnum(oclib.DATA_RESOURCE),
2024-11-07 13:42:19 +01:00
oclib.LibDataEnum(oclib.COMPUTE_RESOURCE),
2024-10-15 12:13:20 +02:00
oclib.LibDataEnum(oclib.STORAGE_RESOURCE),
oclib.LibDataEnum(oclib.PROCESSING_RESOURCE),
oclib.LibDataEnum(oclib.WORKFLOW_RESOURCE)} {
2024-08-01 09:27:38 +02:00
d := oclib.Search(nil, search, resource)
2024-07-30 10:07:34 +02:00
if d.Code != 200 || len(d.Data) == 0 {
results[resource.String()] = []interface{}{}
} else {
2024-07-30 15:56:12 +02:00
results[resource.String()] = d.Data
2024-07-30 10:07:34 +02:00
}
}
2024-07-30 15:56:12 +02:00
o.Data["json"] = map[string]interface{}{"data": results, "code": 200, "error": ""}
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() {
id := o.Ctx.Input.Param(":id")
results := map[string]interface{}{}
2024-10-15 12:13:20 +02:00
for _, resource := range []oclib.LibDataEnum{
oclib.LibDataEnum(oclib.DATA_RESOURCE),
2024-11-07 13:42:19 +01:00
oclib.LibDataEnum(oclib.COMPUTE_RESOURCE),
2024-10-15 12:13:20 +02:00
oclib.LibDataEnum(oclib.STORAGE_RESOURCE),
oclib.LibDataEnum(oclib.PROCESSING_RESOURCE),
oclib.LibDataEnum(oclib.WORKFLOW_RESOURCE)} {
2024-07-30 10:07:34 +02:00
d := oclib.LoadOne(resource, id)
if d.Code != 200 {
results[resource.String()] = nil
} else {
2024-07-30 15:56:12 +02:00
results[resource.String()] = d.Data
2024-07-30 10:07:34 +02:00
}
}
2024-07-30 15:56:12 +02:00
o.Data["json"] = map[string]interface{}{"data": results, "code": 200, "error": ""}
o.ServeJSON()
}
// @Title Delete
// @Description delete the resource
// @Param id path string true "The id you want to delete"DeleteOne
// @Success 200 {resource} delete success!
// @router /:id [delete]
func (o *ResourceController) Delete() {
id := o.Ctx.Input.Param(":id")
results := map[string]interface{}{}
2024-10-15 12:13:20 +02:00
for _, resource := range []oclib.LibDataEnum{
oclib.LibDataEnum(oclib.DATA_RESOURCE),
2024-11-07 13:42:19 +01:00
oclib.LibDataEnum(oclib.COMPUTE_RESOURCE),
2024-10-15 12:13:20 +02:00
oclib.LibDataEnum(oclib.STORAGE_RESOURCE),
oclib.LibDataEnum(oclib.PROCESSING_RESOURCE),
oclib.LibDataEnum(oclib.WORKFLOW_RESOURCE)} {
2024-07-30 10:07:34 +02:00
d := oclib.DeleteOne(resource, id)
if d.Code != 200 {
results[resource.String()] = nil
} else {
2024-07-30 15:56:12 +02:00
results[resource.String()] = d.Data
2024-07-30 10:07:34 +02:00
}
}
2024-07-30 15:56:12 +02:00
o.Data["json"] = map[string]interface{}{"data": results, "code": 200, "error": ""}
o.ServeJSON()
}