2025-02-05 08:41:16 +01:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
|
|
import (
|
2026-04-10 15:20:41 +02:00
|
|
|
"encoding/json"
|
2026-04-09 07:49:35 +02:00
|
|
|
"fmt"
|
2026-02-25 09:08:40 +01:00
|
|
|
"net/http"
|
|
|
|
|
"oc-datacenter/infrastructure/monitor"
|
2026-04-09 07:49:35 +02:00
|
|
|
"strconv"
|
2026-02-25 09:08:40 +01:00
|
|
|
"time"
|
|
|
|
|
|
2025-02-05 08:41:16 +01:00
|
|
|
oclib "cloud.o-forge.io/core/oc-lib"
|
|
|
|
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
2026-04-10 15:20:41 +02:00
|
|
|
"cloud.o-forge.io/core/oc-lib/models/live"
|
2026-04-09 07:49:35 +02:00
|
|
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
2025-02-05 08:41:16 +01:00
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
2026-02-25 09:08:40 +01:00
|
|
|
"github.com/gorilla/websocket"
|
2025-02-05 08:41:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Operations about workspace
|
|
|
|
|
type DatacenterController struct {
|
|
|
|
|
beego.Controller
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 07:49:35 +02:00
|
|
|
func resourceTypeEnum(t string, special bool) []oclib.LibDataEnum {
|
|
|
|
|
e := []oclib.LibDataEnum{}
|
|
|
|
|
if special && t == "resource" {
|
|
|
|
|
return e
|
|
|
|
|
}
|
|
|
|
|
if t == "storage" || t == "live" {
|
|
|
|
|
e = append(e, oclib.LibDataEnum(oclib.LIVE_STORAGE))
|
|
|
|
|
}
|
|
|
|
|
if t == "datacenter" || t == "live" {
|
|
|
|
|
e = append(e, oclib.LibDataEnum(oclib.LIVE_DATACENTER))
|
|
|
|
|
}
|
|
|
|
|
return e
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (o *DatacenterController) 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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @Title Search
|
|
|
|
|
// @Description search datacenter
|
|
|
|
|
// @Param type path string true "the type you want to get"
|
|
|
|
|
// @Param search path string true "the word search you want to get"
|
|
|
|
|
// @Param is_draft query string false "draft wished"
|
|
|
|
|
// @Param offset query string false
|
|
|
|
|
// @Param limit query string false
|
|
|
|
|
// @Success 200 {workspace} models.workspace
|
|
|
|
|
// @router /:type/search/:search [get]
|
|
|
|
|
func (o *DatacenterController) Search() {
|
|
|
|
|
/*
|
|
|
|
|
* This is a sample of how to use the search function
|
|
|
|
|
* The search function is used to search for data in the database
|
|
|
|
|
* The search function takes in a filter and a data type
|
|
|
|
|
* The filter is a struct that contains the search parameters
|
|
|
|
|
* The data type is an enum that specifies the type of data to search for
|
|
|
|
|
* The search function returns a list of data that matches the filter
|
|
|
|
|
* The data is then returned as a json object
|
|
|
|
|
*/
|
|
|
|
|
// store and return Id or post with UUID
|
|
|
|
|
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
|
|
|
|
offset, _ := strconv.Atoi(o.Ctx.Input.Query("offset"))
|
|
|
|
|
limit, _ := strconv.Atoi(o.Ctx.Input.Query("limit"))
|
|
|
|
|
search := o.Ctx.Input.Param(":search")
|
|
|
|
|
if search == "*" {
|
|
|
|
|
search = ""
|
|
|
|
|
}
|
|
|
|
|
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).Search(&dbs.Filters{
|
|
|
|
|
Or: map[string][]dbs.Filter{
|
|
|
|
|
// "abstractlive.abstractobject.creator_id": {{Operator: dbs.EQUAL.String(), Value: peerID}},
|
|
|
|
|
"abstractlive.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
|
|
|
|
|
},
|
|
|
|
|
}, "", isDraft == "true", int64(offset), int64(limit))
|
|
|
|
|
fmt.Println(s)
|
|
|
|
|
m[col.String()] = append(m[col.String()], s.Data...)
|
|
|
|
|
}
|
|
|
|
|
o.Data["json"] = map[string]interface{}{
|
|
|
|
|
"data": m,
|
|
|
|
|
"code": 200,
|
|
|
|
|
"err": nil,
|
|
|
|
|
}
|
|
|
|
|
o.ServeJSON()
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-05 08:41:16 +01:00
|
|
|
// @Title GetAll
|
|
|
|
|
// @Description find booking by id
|
2026-04-09 07:49:35 +02:00
|
|
|
// @Param type path string true "the word type you want to get"
|
2025-02-05 08:41:16 +01:00
|
|
|
// @Param is_draft query string false "draft wished"
|
2026-04-09 07:49:35 +02:00
|
|
|
// @Param offset query string false
|
|
|
|
|
// @Param limit query string false
|
2025-02-05 08:41:16 +01:00
|
|
|
// @Success 200 {booking} models.booking
|
|
|
|
|
// @router / [get]
|
|
|
|
|
func (o *DatacenterController) GetAll() {
|
|
|
|
|
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
|
|
|
|
isDraft := o.Ctx.Input.Query("is_draft")
|
2026-04-09 07:49:35 +02:00
|
|
|
offset, _ := strconv.Atoi(o.Ctx.Input.Query("offset"))
|
|
|
|
|
limit, _ := strconv.Atoi(o.Ctx.Input.Query("limit"))
|
|
|
|
|
m := map[string][]utils.ShallowDBObject{}
|
|
|
|
|
for _, col := range o.collection(false) {
|
|
|
|
|
if m[col.String()] == nil {
|
|
|
|
|
m[col.String()] = []utils.ShallowDBObject{}
|
|
|
|
|
}
|
|
|
|
|
s := oclib.NewRequest(oclib.LibDataEnum(col), user, peerID, groups, nil).LoadAll(isDraft == "true", int64(offset), int64(limit))
|
|
|
|
|
fmt.Println(s)
|
|
|
|
|
m[col.String()] = append(m[col.String()], s.Data...)
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(m)
|
|
|
|
|
o.Data["json"] = map[string]interface{}{
|
|
|
|
|
"data": m,
|
|
|
|
|
"code": 200,
|
|
|
|
|
"err": nil,
|
2025-02-05 08:41:16 +01:00
|
|
|
}
|
|
|
|
|
o.ServeJSON()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @Title Get
|
|
|
|
|
// @Description find booking by id
|
|
|
|
|
// @Param id path string true "the id you want to get"
|
2026-04-09 07:49:35 +02:00
|
|
|
// @Param type path string true "the word type you want to get"
|
2025-02-05 08:41:16 +01:00
|
|
|
// @Param is_draft query string false "draft wished"
|
|
|
|
|
// @Success 200 {booking} models.booking
|
2026-04-09 07:49:35 +02:00
|
|
|
// @router /:type/:id [get]
|
2025-02-05 08:41:16 +01:00
|
|
|
func (o *DatacenterController) Get() {
|
|
|
|
|
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
|
|
|
|
id := o.Ctx.Input.Param(":id")
|
2026-04-09 07:49:35 +02:00
|
|
|
for _, col := range o.collection(false) {
|
|
|
|
|
data := oclib.NewRequest(col, user, peerID, groups, nil).LoadOne(id)
|
|
|
|
|
o.Data["json"] = data
|
|
|
|
|
if data.Data != nil {
|
|
|
|
|
break
|
2025-02-05 08:41:16 +01:00
|
|
|
}
|
2026-04-09 07:49:35 +02:00
|
|
|
}
|
|
|
|
|
o.ServeJSON()
|
|
|
|
|
}
|
2025-02-05 08:41:16 +01:00
|
|
|
|
2026-04-09 07:49:35 +02:00
|
|
|
// @Title Delete
|
|
|
|
|
// @Description find booking by id
|
|
|
|
|
// @Param id path string true "the id you want to get"
|
|
|
|
|
// @Param type path string true "the word type you want to get"
|
|
|
|
|
// @Param is_draft query string false "draft wished"
|
|
|
|
|
// @Success 200 {booking} models.booking
|
|
|
|
|
// @router /:type/:id [delete]
|
|
|
|
|
func (o *DatacenterController) 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)
|
|
|
|
|
o.Data["json"] = data
|
|
|
|
|
if data.Data != nil {
|
|
|
|
|
break
|
2025-02-05 08:41:16 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
o.ServeJSON()
|
|
|
|
|
}
|
2026-02-25 09:08:40 +01:00
|
|
|
|
2026-04-10 15:20:41 +02:00
|
|
|
func GetResource(typ oclib.LibDataEnum) interface{} {
|
|
|
|
|
if typ == oclib.LibDataEnum(oclib.LIVE_DATACENTER) {
|
|
|
|
|
return &live.LiveDatacenter{}
|
|
|
|
|
}
|
|
|
|
|
if typ == oclib.LibDataEnum(oclib.LIVE_STORAGE) {
|
|
|
|
|
return &live.LiveStorage{}
|
|
|
|
|
}
|
|
|
|
|
return &live.AbstractLive{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @Title Search
|
|
|
|
|
// @Description search workspace
|
|
|
|
|
// @Param is_draft query string false
|
|
|
|
|
// @Param offset query string false
|
|
|
|
|
// @Param limit query string false
|
|
|
|
|
// @Param data body json true "body for data content (Json format)"
|
|
|
|
|
// @Success 200 {workspace} models.workspace
|
|
|
|
|
// @router /:type/extended/search [post]
|
|
|
|
|
func (o *DatacenterController) SearchExtended() {
|
|
|
|
|
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
|
|
|
|
// store and return Id or post with UUIDLibDataEnum
|
|
|
|
|
isDraft := o.Ctx.Input.Query("is_draft")
|
|
|
|
|
offset, _ := strconv.Atoi(o.Ctx.Input.Query("offset"))
|
|
|
|
|
limit, _ := strconv.Atoi(o.Ctx.Input.Query("limit"))
|
|
|
|
|
var res map[string]interface{}
|
|
|
|
|
json.Unmarshal(o.Ctx.Input.CopyBody(100000), &res)
|
|
|
|
|
m := map[string][]utils.ShallowDBObject{}
|
|
|
|
|
for _, col := range o.collection(false) {
|
|
|
|
|
if m[col.String()] == nil {
|
|
|
|
|
m[col.String()] = []utils.ShallowDBObject{}
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(res, oclib.FiltersFromFlatMap(res, GetResource(col)))
|
|
|
|
|
|
|
|
|
|
s := oclib.NewRequest(col, user, peerID, groups, nil).Search(
|
|
|
|
|
oclib.FiltersFromFlatMap(res, GetResource(col)), "", isDraft == "true", int64(offset), int64(limit))
|
|
|
|
|
m[col.String()] = append(m[col.String()], s.Data...)
|
|
|
|
|
}
|
|
|
|
|
o.Data["json"] = map[string]interface{}{
|
|
|
|
|
"data": m,
|
|
|
|
|
"code": 200,
|
|
|
|
|
"err": nil,
|
|
|
|
|
}
|
|
|
|
|
o.ServeJSON()
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 09:08:40 +01:00
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
|
CheckOrigin: func(r *http.Request) bool { return true }, // allow all origins
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @Title Log
|
|
|
|
|
// @Description find booking by id
|
|
|
|
|
// @Param id path string true "the id you want to get"
|
|
|
|
|
// @Success 200 {booking} models.booking
|
2026-04-09 07:49:35 +02:00
|
|
|
// @router /logs/:id [get]
|
2026-02-25 09:08:40 +01:00
|
|
|
func (o *DatacenterController) Log() {
|
|
|
|
|
// user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
|
|
|
|
id := o.Ctx.Input.Param(":id")
|
|
|
|
|
conn, err := upgrader.Upgrade(o.Ctx.ResponseWriter, o.Ctx.Request, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
o.Ctx.WriteString("WebSocket upgrade failed: " + err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer conn.Close()
|
|
|
|
|
monitors, err := monitor.NewMonitorService()
|
|
|
|
|
if err != nil {
|
|
|
|
|
o.Ctx.WriteString("Monitor service unavailable: " + err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx := monitor.StreamRegistry.Register(id)
|
|
|
|
|
monitors.Stream(ctx, id, 1*time.Second, conn)
|
|
|
|
|
}
|
2026-04-10 15:20:41 +02:00
|
|
|
|
|
|
|
|
// @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 *DatacenterController) 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(100000), &res)
|
|
|
|
|
o.Data["json"] = oclib.NewRequest(libs[0], user, peerID, groups, nil).StoreOne(res)
|
|
|
|
|
o.ServeJSON()
|
|
|
|
|
}
|