From 413a5284bb3c7dee9e78a4358fe6d6887f071c27 Mon Sep 17 00:00:00 2001 From: mr Date: Fri, 30 Aug 2024 10:55:35 +0200 Subject: [PATCH] new oclib + comments --- README.md | 2 ++ controllers/booking.go | 31 +++++++++++++++++++++---------- go.mod | 2 +- go.sum | 4 ++++ main.go | 1 + 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5607bd5..e8c8c36 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,5 @@ If default Swagger page is displayed instead of tyour api, change url in swagger url: "swagger.json" +Note on particular process : +- set a bookin delete all related workflow booking before creating new ones. (no update of existing ones) \ No newline at end of file diff --git a/controllers/booking.go b/controllers/booking.go index e5e0049..de00d40 100644 --- a/controllers/booking.go +++ b/controllers/booking.go @@ -4,10 +4,9 @@ import ( "encoding/json" "errors" "time" - "fmt" + oclib "cloud.o-forge.io/core/oc-lib" "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" "cloud.o-forge.io/core/oc-lib/models/workflow_execution" beego "github.com/beego/beego/v2/server/web" @@ -58,7 +57,13 @@ func (o *BookingController) Get() { // @Success 200 {object} models.object // @router /check/:id/:start_date/:end_date [get] 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") 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")) @@ -71,8 +76,8 @@ func (o *BookingController) Check() { "error": errors.New("invalid date format"), } } else { - booking := &b.Booking{} - isAvailable, err2 := booking.CheckBooking(id, date, &date2) + booking := &b.Booking{} // create a new booking object + isAvailable, err2 := booking.CheckBooking(id, date, &date2) // check if the booking is available code := 200 err := "" if !isAvailable { @@ -99,10 +104,15 @@ func (o *BookingController) Check() { // @Success 200 {object} models.object // @router / [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 json.Unmarshal(o.Ctx.Input.CopyBody(10000), &resp) dc_id := resp.ResourceID + // delete all previous bookings res := oclib.Search(&dbs.Filters{And: map[string][]dbs.Filter{ "workflowexecution.workflow_id": {{Operator: dbs.EQUAL.String(), Value: resp.WorkflowID}}, "datacenter_resource_id": {{Operator: dbs.EQUAL.String(), Value: dc_id}}, @@ -116,13 +126,13 @@ func (o *BookingController) Post() { o.ServeJSON() return } - for _, b := range res.Data { + for _, b := range res.Data { // delete all previous bookings oclib.DeleteOne(oclib.LibDataEnum(oclib.BOOKING), b.GetID()) } books := make([]interface{}, 0) errormsg := "" - for _, exec := range resp.Executions { - if ok, _ := (&booking.Booking{}).CheckBooking(dc_id, *exec.ExecDate, exec.EndDate); !ok { + for _, exec := range resp.Executions { // create new bookings + if ok, _ := (&b.Booking{}).CheckBooking(dc_id, *exec.ExecDate, exec.EndDate); !ok { res.Err += " -> the booking from " + exec.ExecDate.String() + " is already taken." o.Data["json"] = map[string]interface{}{ "data": nil, @@ -132,10 +142,11 @@ func (o *BookingController) Post() { o.ServeJSON() return } - new := &booking.Booking{ + new := &b.Booking{ DatacenterResourceID: dc_id, WorkflowExecution: *exec, } + // store the booking b := oclib.StoreOne(oclib.LibDataEnum(oclib.BOOKING), new.Serialize()) if b.Code == 200 { books = append(books, b.Data) diff --git a/go.mod b/go.mod index 5b46ad4..bc0e7c9 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ 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 github.com/beego/bee/v2 v2.1.0 // indirect github.com/beorn7/perks v1.0.1 // indirect diff --git a/go.sum b/go.sum index 2b08d4d..7882524 100644 --- a/go.sum +++ b/go.sum @@ -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-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-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= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= diff --git a/main.go b/main.go index ee59f9e..5282c06 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,7 @@ func main() { 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")) // Normal beego init //if beego.BConfig.RunMode == "dev" {