deprecated-oc-catalog/controllers/datacenter.go

83 lines
2.0 KiB
Go

package controllers
import (
"cloud.o-forge.io/core/deprecated-oc-catalog/models"
beego "github.com/beego/beego/v2/server/web"
"github.com/go-playground/validator/v10"
)
// DatacenterController operations about datacenters
type DatacenterController struct {
beego.Controller
}
func init() {
validate = validator.New()
}
// @Title Get multiple datacenters by IDs
// @Description Return Datacenter objects if found in the DB. Not found IDs will be ignored
// @Param IDs path []string true "List of datacenter IDs"
// @Success 200 {object} []models.ComputingModel
// @Failure 403 IDs are empty
// @router /multi/:IDs [get]
func (o *DatacenterController) GetMultipleDatacenter(IDs []string) {
if len(IDs) != 0 {
ob, err := models.GetMultipleDatacenter(IDs)
if err != nil {
o.Ctx.Output.SetStatus(500)
} else {
o.Data["json"] = ob
}
} else {
o.Ctx.Output.SetStatus(403)
}
o.ServeJSON()
}
// @Title GetOneDatacenter
// @Description find datacenter by ID
// @Param ID path string true "the ID you want to get"
// @Success 200 {object} models.DatacenterModel
// @Failure 403 ID is empty
// @router /:ID [get]
func (o *DatacenterController) GetOneDatacenter(ID string) {
if ID != "" {
ob, err := models.GetOneDatacenter(ID)
if err != nil {
o.Data["json"] = err.Error()
} else {
o.Data["json"] = ob
}
}
o.ServeJSON()
}
// @Title Create Datacenter
// @Description submit Datacenter object
// @Param body body models.DatacenterNEWModel true "The object content"
// @Success 200 {string} models.DatacenterModel
// @Failure 403 Missing body or fields
// @router / [post]
func (o *DatacenterController) PostDatacenter(body models.DatacenterNEWModel) {
err := validate.Struct(body)
// validationErrors := err.(validator.ValidationErrors)
if err != nil {
o.Data["json"] = err.Error()
o.Ctx.Output.Status = 403
o.ServeJSON()
return
}
ID, err := models.PostOneDatacenter(body)
if err != nil {
o.Ctx.Output.SetStatus(500)
return
}
o.Data["json"] = map[string]string{"ID": ID}
o.ServeJSON()
}