glory kube conn

This commit is contained in:
mr
2025-02-14 11:09:31 +01:00
parent a53dbccc23
commit bff909abbb
15 changed files with 650 additions and 283 deletions

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"oc-datacenter/infrastructure"
"time"
oclib "cloud.o-forge.io/core/oc-lib"
@@ -105,6 +106,40 @@ func (o *BookingController) Get() {
o.ServeJSON()
}
// @Title Update
// @Description create computes
// @Param id path string true "the compute id you want to get"
// @Param body body models.compute true "The compute content"
// @Success 200 {compute} models.compute
// @router /:id [put]
func (o *BookingController) Put() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
// store and return Id or post with UUID
var res map[string]interface{}
id := o.Ctx.Input.Param(":id")
book := oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).LoadOne(id)
if book.Code != 200 {
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": book.Code,
"error": book.Err,
}
o.ServeJSON()
return
}
booking := book.Data.(*b.Booking)
if time.Now().After(booking.ExpectedStartDate) {
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).UpdateOne(res, id)
} else {
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": 409,
"error": "booking is not already started",
}
}
o.ServeJSON()
}
// @Title Check
// @Description check booking
// @Param id path string "id of the datacenter"
@@ -156,7 +191,7 @@ func (o *BookingController) Check() {
o.ServeJSON()
}
// @Title Poststatic.
// @Title Post.
// @Description create booking
// @Param booking body string true "the booking you want to post"
// @Param is_draft query string false "draft wished"
@@ -195,11 +230,59 @@ func (o *BookingController) Post() {
oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).DeleteOne(b.GetID())
}
b := oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).StoreOne(resp.Serialize(&resp))
errormsg := ""
if b.Code != 200 {
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": b.Code,
"error": b.Err,
}
o.ServeJSON()
return
}
fmt.Println("there was an error creating the namespace", o.createNamespace(resp.ExecutionsID))
o.Data["json"] = map[string]interface{}{
"data": []interface{}{b},
"code": 200,
"error": errormsg,
"error": "",
}
o.ServeJSON()
}
func (o *BookingController) createNamespace(ns string) error {
/*
* This function is used to create a namespace.
* It takes the following parameters:
* - ns: the namespace you want to create
*/
serv, err := infrastructure.NewService()
if err != nil {
return nil
}
err = serv.CreateNamespace(o.Ctx.Request.Context(), ns)
if err != nil {
return err
}
err = serv.CreateServiceAccount(o.Ctx.Request.Context(), ns)
if err != nil {
return err
}
role := "argo-role"
err = serv.CreateRole(o.Ctx.Request.Context(), ns, role,
[][]string{
{"coordination.k8s.io"},
{""},
{""}},
[][]string{
{"leases"},
{"secrets"},
{"pods"}},
[][]string{
{"get", "create", "update"},
{"get"},
{"patch"}})
if err != nil {
return err
}
fmt.Println("ROLLLLLE BIND")
return serv.CreateRoleBinding(o.Ctx.Request.Context(), ns, "argo-role-binding", role)
}

53
controllers/session.go Normal file
View File

@@ -0,0 +1,53 @@
package controllers
import (
"fmt"
"oc-datacenter/infrastructure"
"strconv"
beego "github.com/beego/beego/v2/server/web"
)
type SessionController struct {
beego.Controller
}
// @Title GetToken
// @Description find booking by id
// @Param id path string "id of the datacenter"
// @Param duration path string "duration of the token"
// @Success 200 {booking} models.booking
// @router /token/:id/:duration [get]
func (o *SessionController) GetToken() {
id := o.Ctx.Input.Param(":id")
durationSTR := o.Ctx.Input.Param(":duration")
duration, err := strconv.Atoi(durationSTR)
if err != nil {
// change code to 400
o.Ctx.Output.SetStatus(400)
o.Data["json"] = map[string]string{"error": "duration invalid"}
o.ServeJSON()
return
}
serv, err := infrastructure.NewService()
if err != nil {
// change code to 500
o.Ctx.Output.SetStatus(500)
o.Data["json"] = map[string]string{"error": err.Error()}
o.ServeJSON()
return
}
fmt.Println("BLAPO", id, duration)
token, err := serv.GetToken(o.Ctx.Request.Context(), id, duration)
if err != nil {
// change code to 500
o.Ctx.Output.SetStatus(500)
o.Data["json"] = map[string]string{"error": err.Error()}
o.ServeJSON()
return
}
o.Data["json"] = token
o.ServeJSON()
}