new oclib + comments

This commit is contained in:
mr 2024-08-30 10:55:35 +02:00
parent a07db36d99
commit 413a5284bb
5 changed files with 29 additions and 11 deletions

View File

@ -11,3 +11,5 @@ If default Swagger page is displayed instead of tyour api, change url in swagger
url: "swagger.json" url: "swagger.json"
Note on particular process :
- set a bookin delete all related workflow booking before creating new ones. (no update of existing ones)

View File

@ -4,10 +4,9 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"time" "time"
"fmt"
oclib "cloud.o-forge.io/core/oc-lib" oclib "cloud.o-forge.io/core/oc-lib"
"cloud.o-forge.io/core/oc-lib/dbs" "cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/models/booking"
b "cloud.o-forge.io/core/oc-lib/models/booking" b "cloud.o-forge.io/core/oc-lib/models/booking"
"cloud.o-forge.io/core/oc-lib/models/workflow_execution" "cloud.o-forge.io/core/oc-lib/models/workflow_execution"
beego "github.com/beego/beego/v2/server/web" beego "github.com/beego/beego/v2/server/web"
@ -58,7 +57,13 @@ func (o *BookingController) Get() {
// @Success 200 {object} models.object // @Success 200 {object} models.object
// @router /check/:id/:start_date/:end_date [get] // @router /check/:id/:start_date/:end_date [get]
func (o *BookingController) Check() { func (o *BookingController) Check() {
// store and return Id or post with UUID /*
* This function is used to check if a booking is available for a specific datacenter.
* It takes the following parameters:
* - id: the id of the datacenter
* - start_date: the start date of the booking
* - end_date: the end date of the booking
*/
id := o.Ctx.Input.Param(":id") id := o.Ctx.Input.Param(":id")
date, err := time.Parse("2006-01-02T15:04:05", o.Ctx.Input.Param(":start_date")) date, err := time.Parse("2006-01-02T15:04:05", o.Ctx.Input.Param(":start_date"))
date2, err2 := time.Parse("2006-01-02T15:04:05", o.Ctx.Input.Param(":end_date")) date2, err2 := time.Parse("2006-01-02T15:04:05", o.Ctx.Input.Param(":end_date"))
@ -71,8 +76,8 @@ func (o *BookingController) Check() {
"error": errors.New("invalid date format"), "error": errors.New("invalid date format"),
} }
} else { } else {
booking := &b.Booking{} booking := &b.Booking{} // create a new booking object
isAvailable, err2 := booking.CheckBooking(id, date, &date2) isAvailable, err2 := booking.CheckBooking(id, date, &date2) // check if the booking is available
code := 200 code := 200
err := "" err := ""
if !isAvailable { if !isAvailable {
@ -99,10 +104,15 @@ func (o *BookingController) Check() {
// @Success 200 {object} models.object // @Success 200 {object} models.object
// @router / [post] // @router / [post]
func (o *BookingController) Post() { func (o *BookingController) Post() {
// TODO retrieve objects in body DON'T FORGET TO CHECK IF THE OBJECT IS VALID /*
* This function is used to create a booking.
* It takes the following parameters:
* - booking: the booking you want to post
*/
var resp workflow_execution.WorkflowExecutions var resp workflow_execution.WorkflowExecutions
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &resp) json.Unmarshal(o.Ctx.Input.CopyBody(10000), &resp)
dc_id := resp.ResourceID dc_id := resp.ResourceID
// delete all previous bookings
res := oclib.Search(&dbs.Filters{And: map[string][]dbs.Filter{ res := oclib.Search(&dbs.Filters{And: map[string][]dbs.Filter{
"workflowexecution.workflow_id": {{Operator: dbs.EQUAL.String(), Value: resp.WorkflowID}}, "workflowexecution.workflow_id": {{Operator: dbs.EQUAL.String(), Value: resp.WorkflowID}},
"datacenter_resource_id": {{Operator: dbs.EQUAL.String(), Value: dc_id}}, "datacenter_resource_id": {{Operator: dbs.EQUAL.String(), Value: dc_id}},
@ -116,13 +126,13 @@ func (o *BookingController) Post() {
o.ServeJSON() o.ServeJSON()
return return
} }
for _, b := range res.Data { for _, b := range res.Data { // delete all previous bookings
oclib.DeleteOne(oclib.LibDataEnum(oclib.BOOKING), b.GetID()) oclib.DeleteOne(oclib.LibDataEnum(oclib.BOOKING), b.GetID())
} }
books := make([]interface{}, 0) books := make([]interface{}, 0)
errormsg := "" errormsg := ""
for _, exec := range resp.Executions { for _, exec := range resp.Executions { // create new bookings
if ok, _ := (&booking.Booking{}).CheckBooking(dc_id, *exec.ExecDate, exec.EndDate); !ok { if ok, _ := (&b.Booking{}).CheckBooking(dc_id, *exec.ExecDate, exec.EndDate); !ok {
res.Err += " -> the booking from " + exec.ExecDate.String() + " is already taken." res.Err += " -> the booking from " + exec.ExecDate.String() + " is already taken."
o.Data["json"] = map[string]interface{}{ o.Data["json"] = map[string]interface{}{
"data": nil, "data": nil,
@ -132,10 +142,11 @@ func (o *BookingController) Post() {
o.ServeJSON() o.ServeJSON()
return return
} }
new := &booking.Booking{ new := &b.Booking{
DatacenterResourceID: dc_id, DatacenterResourceID: dc_id,
WorkflowExecution: *exec, WorkflowExecution: *exec,
} }
// store the booking
b := oclib.StoreOne(oclib.LibDataEnum(oclib.BOOKING), new.Serialize()) b := oclib.StoreOne(oclib.LibDataEnum(oclib.BOOKING), new.Serialize())
if b.Code == 200 { if b.Code == 200 {
books = append(books, b.Data) books = append(books, b.Data)

2
go.mod
View File

@ -11,7 +11,7 @@ require (
) )
require ( require (
cloud.o-forge.io/core/oc-lib v0.0.0-20240823210057-8e80fa88beeb // indirect cloud.o-forge.io/core/oc-lib v0.0.0-20240830071403-db78c70dc349 // indirect
filippo.io/edwards25519 v1.1.0 // indirect filippo.io/edwards25519 v1.1.0 // indirect
github.com/beego/bee/v2 v2.1.0 // indirect github.com/beego/bee/v2 v2.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect github.com/beorn7/perks v1.0.1 // indirect

4
go.sum
View File

@ -124,6 +124,10 @@ cloud.o-forge.io/core/oc-lib v0.0.0-20240822065024-fb80e05d6a7b h1:A7NBwTXoHTg/o
cloud.o-forge.io/core/oc-lib v0.0.0-20240822065024-fb80e05d6a7b/go.mod h1:1hhYh5QWAbYw9cKplQ0ZD9PMgU8t6gPqiYF8sldv1HU= cloud.o-forge.io/core/oc-lib v0.0.0-20240822065024-fb80e05d6a7b/go.mod h1:1hhYh5QWAbYw9cKplQ0ZD9PMgU8t6gPqiYF8sldv1HU=
cloud.o-forge.io/core/oc-lib v0.0.0-20240823210057-8e80fa88beeb h1:7/GQRm485qUHpuJnW1QayD0jk4RTmf5PAOqM0xBwM7E= cloud.o-forge.io/core/oc-lib v0.0.0-20240823210057-8e80fa88beeb h1:7/GQRm485qUHpuJnW1QayD0jk4RTmf5PAOqM0xBwM7E=
cloud.o-forge.io/core/oc-lib v0.0.0-20240823210057-8e80fa88beeb/go.mod h1:1hhYh5QWAbYw9cKplQ0ZD9PMgU8t6gPqiYF8sldv1HU= cloud.o-forge.io/core/oc-lib v0.0.0-20240823210057-8e80fa88beeb/go.mod h1:1hhYh5QWAbYw9cKplQ0ZD9PMgU8t6gPqiYF8sldv1HU=
cloud.o-forge.io/core/oc-lib v0.0.0-20240828114159-ede91cde4583 h1:5DVFZLJ4tySspQwk0H1HRR+arWSRsYsujP4N8zxvy2Q=
cloud.o-forge.io/core/oc-lib v0.0.0-20240828114159-ede91cde4583/go.mod h1:1hhYh5QWAbYw9cKplQ0ZD9PMgU8t6gPqiYF8sldv1HU=
cloud.o-forge.io/core/oc-lib v0.0.0-20240830071403-db78c70dc349 h1:bEIY1lCsA78/mJqFE0gV6likAv5ZifH3RMnLJxiSk3o=
cloud.o-forge.io/core/oc-lib v0.0.0-20240830071403-db78c70dc349/go.mod h1:FIJD0taWLJ5pjQLJ6sfE2KlTkvbmk5SMcyrxdjsaVz0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=

View File

@ -29,6 +29,7 @@ func main() {
o.GetStringDefault("MONGO_DATABASE", "DC_myDC"), o.GetStringDefault("MONGO_DATABASE", "DC_myDC"),
"", "",
) )
// Init the OC library, hostname & port are used for discovery purpose, it's the hostname and port of the service
oclib.Init("oc-datacenter", o.GetStringDefault("HOSTNAME", "localhost"), o.GetStringDefault("PORT", "8092")) oclib.Init("oc-datacenter", o.GetStringDefault("HOSTNAME", "localhost"), o.GetStringDefault("PORT", "8092"))
// Normal beego init // Normal beego init
//if beego.BConfig.RunMode == "dev" { //if beego.BConfig.RunMode == "dev" {