working oc-datacenter

This commit is contained in:
mr
2025-01-17 17:23:29 +01:00
parent 0a9cc39669
commit ea69be8df1
6 changed files with 242 additions and 45 deletions

View File

@@ -8,8 +8,8 @@ import (
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"
"go.mongodb.org/mongo-driver/bson/primitive"
)
@@ -19,10 +19,40 @@ type BookingController struct {
beego.Controller
}
// @Title Search
// @Description search bookings by execution
// @Param id path string true "id execution"
// @Param is_draft query string false "draft wished"
// @Success 200 {workspace} models.workspace
// @router /search/execution/:id [get]
func (o *BookingController) ExecutionSearch() {
/*
* This is a sample of how to use the search function
* The search function is used to search for data in the database
* The search function takes in a filter and a data type
* The filter is a struct that contains the search parameters
* The data type is an enum that specifies the type of data to search for
* The search function returns a list of data that matches the filter
* The data is then returned as a json object
*/
// store and return Id or post with UUID
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
isDraft := o.Ctx.Input.Query("is_draft")
f := dbs.Filters{
Or: map[string][]dbs.Filter{ // filter by name if no filters are provided
"execution_id": {{Operator: dbs.EQUAL.String(), Value: id}},
},
}
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).Search(&f, "", isDraft == "true")
o.ServeJSON()
}
// @Title Search
// @Description search bookings
// @Param start_date path string true "the word search you want to get"
// @Param end_date path string true "the word search you want to get"
// @Param is_draft query string false "draft wished"
// @Success 200 {workspace} models.workspace
// @router /search/:start_date/:end_date [get]
func (o *BookingController) Search() {
@@ -36,25 +66,30 @@ func (o *BookingController) Search() {
* The data is then returned as a json object
*/
// store and return Id or post with UUID
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
start_date, _ := time.Parse("2006-01-02", o.Ctx.Input.Param(":start_date"))
end_date, _ := time.Parse("2006-01-02", o.Ctx.Input.Param(":end_date"))
isDraft := o.Ctx.Input.Query("is_draft")
sd := primitive.NewDateTimeFromTime(start_date)
ed := primitive.NewDateTimeFromTime(end_date)
f := dbs.Filters{
And: map[string][]dbs.Filter{
"workflowexecution.execution_date": {{Operator: "gte", Value: sd}, {Operator: "lte", Value: ed}},
"execution_date": {{Operator: "gte", Value: sd}, {Operator: "lte", Value: ed}},
},
}
o.Data["json"] = oclib.Search(&f, "", oclib.LibDataEnum(oclib.BOOKING))
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).Search(&f, "", isDraft == "true")
o.ServeJSON()
}
// @Title GetAll
// @Description find booking by id
// @Param is_draft query string false "draft wished"
// @Success 200 {booking} models.booking
// @router / [get]
func (o *BookingController) GetAll() {
o.Data["json"] = oclib.LoadAll(oclib.LibDataEnum(oclib.BOOKING))
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
isDraft := o.Ctx.Input.Query("is_draft")
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).LoadAll(isDraft == "true")
o.ServeJSON()
}
@@ -64,8 +99,9 @@ func (o *BookingController) GetAll() {
// @Success 200 {booking} models.booking
// @router /:id [get]
func (o *BookingController) Get() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
o.Data["json"] = oclib.LoadOne(oclib.LibDataEnum(oclib.BOOKING), id)
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).LoadOne(id)
o.ServeJSON()
}
@@ -74,6 +110,7 @@ func (o *BookingController) Get() {
// @Param id path string "id of the datacenter"
// @Param start_date path string "the booking start date" format "2006-01-02T15:04:05"
// @Param end_date path string "the booking end date" format "2006-01-02T15:04:05"
// @Param is_draft query string false "draft wished"
// @Success 200 {object} models.object
// @router /check/:id/:start_date/:end_date [get]
func (o *BookingController) Check() {
@@ -81,7 +118,7 @@ func (o *BookingController) Check() {
* 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
* - start_date: the start date of the booking/search/execution/:id
* - end_date: the end date of the booking
*/
id := o.Ctx.Input.Param(":id")
@@ -96,8 +133,8 @@ func (o *BookingController) Check() {
"error": errors.New("invalid date format"),
}
} else {
booking := &b.Booking{} // create a new booking object
isAvailable, err2 := booking.CheckBooking(id, date, &date2) // check if the booking is available
booking := &b.Booking{} // create a new booking object
isAvailable, err2 := booking.Check(id, date, &date2, 1) // check if the booking is available
fmt.Println(isAvailable, err2)
code := 200
err := ""
@@ -119,9 +156,10 @@ func (o *BookingController) Check() {
o.ServeJSON()
}
// @Title Post
// @Title Poststatic.
// @Description create booking
// @Param booking body string true "the booking you want to post"
// @Param is_draft query string false "draft wished"
// @Success 200 {object} models.object
// @router / [post]
func (o *BookingController) Post() {
@@ -133,15 +171,16 @@ func (o *BookingController) Post() {
* - datacenter_resource_id: the id of the datacenter
* - workflow_execution: the workflow execution
*/
var resp workflow_execution.WorkflowExecutions
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &resp)
var resp booking.Booking
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
json.Unmarshal(o.Ctx.Input.CopyBody(100000), &resp)
dc_id := resp.ResourceID
// delete all previous bookings
fmt.Println("Deleting previous bookings", resp)
res := oclib.Search(&dbs.Filters{And: map[string][]dbs.Filter{
"workflowexecution.workflow_id": {{Operator: dbs.EQUAL.String(), Value: resp.WorkflowID}},
"compute_resource_id": {{Operator: dbs.EQUAL.String(), Value: dc_id}},
}}, "", oclib.LibDataEnum(oclib.BOOKING))
isDraft := o.Ctx.Input.Query("is_draft")
res := oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).Search(&dbs.Filters{And: map[string][]dbs.Filter{
"workflow_id": {{Operator: dbs.EQUAL.String(), Value: resp.WorkflowID}},
"resource_id": {{Operator: dbs.EQUAL.String(), Value: dc_id}},
}}, "", isDraft == "true")
if res.Code != 200 {
o.Data["json"] = map[string]interface{}{
"data": nil,
@@ -152,35 +191,12 @@ func (o *BookingController) Post() {
return
}
for _, b := range res.Data { // delete all previous bookings
oclib.DeleteOne(oclib.LibDataEnum(oclib.BOOKING), b.GetID())
oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).DeleteOne(b.GetID())
}
books := make([]interface{}, 0)
b := oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).StoreOne(resp.Serialize(&resp))
errormsg := ""
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,
"code": res.Code,
"error": res.Err + " -> the booking from " + exec.ExecDate.String() + " is already taken.",
}
o.ServeJSON()
return
}
new := &b.Booking{
ComputeResourceID: 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)
} else {
errormsg += " -> " + b.Err
}
}
o.Data["json"] = map[string]interface{}{
"data": books,
"data": []interface{}{b},
"code": 200,
"error": errormsg,
}