118 lines
3.9 KiB
Go
118 lines
3.9 KiB
Go
package controllers
|
|
|
|
import (
|
|
"net/http"
|
|
"oc-datacenter/infrastructure/monitor"
|
|
"time"
|
|
|
|
oclib "cloud.o-forge.io/core/oc-lib"
|
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
// Operations about workspace
|
|
type DatacenterController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
// @Title GetAll
|
|
// @Description find booking by id
|
|
// @Param is_draft query string false "draft wished"
|
|
// @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")
|
|
storages := oclib.NewRequest(oclib.LibDataEnum(oclib.LIVE_STORAGE), user, peerID, groups, nil).Search(&dbs.Filters{
|
|
Or: map[string][]dbs.Filter{
|
|
"abstractinstanciatedresource.abstractresource.abstractobject.creator_id": {{Operator: dbs.EQUAL.String(), Value: peerID}},
|
|
},
|
|
}, "", isDraft == "true")
|
|
computes := oclib.NewRequest(oclib.LibDataEnum(oclib.LIVE_DATACENTER), user, peerID, groups, nil).Search(&dbs.Filters{
|
|
Or: map[string][]dbs.Filter{
|
|
"abstractinstanciatedresource.abstractresource.abstractobject.creator_id": {{Operator: dbs.EQUAL.String(), Value: peerID}},
|
|
},
|
|
}, "", isDraft == "true")
|
|
storages.Data = append(storages.Data, computes.Data...)
|
|
if storages.Err != "" {
|
|
storages.Err += " - " + computes.Err
|
|
}
|
|
o.Data["json"] = storages
|
|
o.ServeJSON()
|
|
}
|
|
|
|
// @Title Get
|
|
// @Description find booking by id
|
|
// @Param id path string true "the id you want to get"
|
|
// @Param is_draft query string false "draft wished"
|
|
// @Success 200 {booking} models.booking
|
|
// @router /:id [get]
|
|
func (o *DatacenterController) Get() {
|
|
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
|
|
isDraft := o.Ctx.Input.Query("is_draft")
|
|
id := o.Ctx.Input.Param(":id")
|
|
storages := oclib.NewRequest(oclib.LibDataEnum(oclib.LIVE_STORAGE), user, peerID, groups, nil).Search(&dbs.Filters{
|
|
Or: map[string][]dbs.Filter{
|
|
"abstractinstanciatedresource.abstractresource.abstractobject.id": {{Operator: dbs.EQUAL.String(), Value: id}},
|
|
"abstractinstanciatedresource.abstractresource.abstractobject.creator_id": {{Operator: dbs.EQUAL.String(), Value: peerID}},
|
|
},
|
|
}, "", isDraft == "true")
|
|
if len(storages.Data) == 0 {
|
|
computes := oclib.NewRequest(oclib.LibDataEnum(oclib.LIVE_DATACENTER), user, peerID, groups, nil).Search(&dbs.Filters{
|
|
Or: map[string][]dbs.Filter{
|
|
"abstractinstanciatedresource.abstractresource.abstractobject.id": {{Operator: dbs.EQUAL.String(), Value: id}},
|
|
"abstractinstanciatedresource.abstractresource.abstractobject.creator_id": {{Operator: dbs.EQUAL.String(), Value: peerID}},
|
|
},
|
|
}, "", isDraft == "true")
|
|
if len(computes.Data) == 0 {
|
|
o.Data["json"] = map[string]interface{}{
|
|
"data": nil,
|
|
"code": computes.Code,
|
|
"err": computes.Err,
|
|
}
|
|
} else {
|
|
o.Data["json"] = map[string]interface{}{
|
|
"data": computes.Data[0],
|
|
"code": computes.Code,
|
|
"err": computes.Err,
|
|
}
|
|
}
|
|
|
|
} else {
|
|
o.Data["json"] = map[string]interface{}{
|
|
"data": storages.Data[0],
|
|
"code": storages.Code,
|
|
"err": storages.Err,
|
|
}
|
|
}
|
|
o.ServeJSON()
|
|
}
|
|
|
|
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
|
|
// @router /:id [get]
|
|
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)
|
|
}
|