oc-catalog/controllers/schedule.go

164 lines
5.5 KiB
Go

package controllers
import (
"time"
"cloud.o-forge.io/core/oc-catalog/models"
"github.com/beego/beego/v2/core/logs"
beego "github.com/beego/beego/v2/server/web"
)
type ScheduleController struct {
beego.Controller
}
// @Title Create schedule
// @Description Create schedule for a workflow. It will return some future executions just as information
// @Param dcName query string true "Name of the node (oc-catalog) from where the workflow comes."
// @Param workflowName query string true "Workflow Name"
// @Param cron query string true "Cron syntax with year. If no year is specified, will use the current"
// @Param duration query uint true "Duration in seconds"
// @Param startDate query time.Time true "RFC3339 time for startDate"
// @Param stopDate query time.Time true "RFC3339 time for stopDate"
// @Param requirements body models.ExecutionRequirementsModel true "The object content"
// @Success 200 {object} models.ScheduleInfo
// @Failure 403 Authentication issue
// @Failure 400 workflowName not found or empty
// // @Security jwtAPIToken
// @router /book [post]
func (u *ScheduleController) CreateSchedule(dcName, workflowName, cron string, duration uint, startDate, stopDate time.Time, requirements models.ExecutionRequirementsModel) {
// token := u.Ctx.Input.GetData("jwtAPIToken").(string)
//FIXME: Passing a date as "2021-07-15 00:00:00 0000 UTC" break the controller but return 200. Should return 4xx
username := "asd"
nextIters, err := models.CreateScheduleWorkflow(dcName, username, workflowName, cron, duration, startDate, stopDate, requirements)
if err != nil {
u.CustomAbort(400, err.Error())
}
u.Data["json"] = nextIters
u.ServeJSON()
return
}
//TODO: This node corresponds to a unique DC, which it owns. We must restructure the code in order to
// allow a unique DC. And maybe discuss more this point
// @Title Check if schedule can be created in this DC
// @Description Check for availability of this DC
// @Param cron query string true "Cron syntax"
// @Param duration query uint true "Duration in seconds"
// @Param startDate query time.Time true "RFC3339 time for startDate"
// @Param stopDate query time.Time true "RFC3339 time for stopDate"
// @Param requirements body models.ExecutionRequirementsModel true "The object content"
// @Success 200 The schedule can be created
// @Failure 403 Authentication issue
// @Failure 400 Other error. Check the output
// // @Security jwtAPIToken
// @router /check [post]
func (u *ScheduleController) CheckSchedule(cron string, duration uint, startDate, stopDate time.Time, requirements models.ExecutionRequirementsModel) {
// token := u.Ctx.Input.GetData("jwtAPIToken").(string)
// username := "asd"
if cron == "" {
u.CustomAbort(400, "Tasks cronString must not be empty")
}
// Check Dates
if startDate.After(stopDate) || startDate.Equal(stopDate) {
u.CustomAbort(400, "startDate must be before stopDate")
}
if startDate.Before(time.Now().UTC()) {
u.CustomAbort(400, "Current server time ("+time.Now().UTC().String()+") is after the startDate ("+startDate.String()+")")
}
err := models.CheckSchedule(cron, duration, startDate, stopDate, requirements)
if err != nil {
logs.Warning(err)
u.CustomAbort(400, err.Error())
}
// u.Data["json"] = nextIters
// u.ServeJSON()
return
}
// @Title Get schedules
// @Description Get a list of next startDates schedules (inclusive). If timezone is not specified, will assume UTC
// @Param startDate query time.Time true "Start date"
// @Param stopDate query time.Time true "End date"
// @Success 200 {object} []models.ScheduleDB
// @Success 201 Too much elements within the range of dates
// @Failure 403 Authentication issue
// @Failure 400 Other error. Check the output
// // @Security jwtAPIToken
// @router / [get]
func (u *ScheduleController) GetSchedules(startDate time.Time, stopDate time.Time) {
// token := u.Ctx.Input.GetData("jwtAPIToken").(string)
// username := "asd"
data, maxLimit, err := models.GetSchedules(startDate, stopDate)
if err != nil {
logs.Warning(err)
u.CustomAbort(400, err.Error())
}
if maxLimit {
u.Ctx.Output.Status = 201
}
u.Data["json"] = data
u.ServeJSON()
return
}
// @Title Get next schedule
// @Description Give a date, get the next date where there are at least on schedule. If no hours specified, will assume 00:00
// @Param baseDate query time.Time true "Base date"
// @Success 200 {object} *time.Time
// @Failure 403 Authentication issue
// @Failure 400 Other error. Check the output
// // @Security jwtAPIToken
// @router /next [get]
func (u *ScheduleController) GetNextSchedules(baseDate time.Time) {
// token := u.Ctx.Input.GetData("jwtAPIToken").(string)
// username := "asd"
futureDate := models.GetFarSchedules(baseDate, true)
u.Data["json"] = futureDate
u.ServeJSON()
return
}
// @Title Get previous schedule
// @Description Give a date, get the previous date where there are at least on schedule. If no hours specified, will assume 00:00
// @Param baseDate query time.Time true "Base date"
// @Success 200 {object} *time.Time
// @Failure 403 Authentication issue
// @Failure 400 Other error. Check the output
// // @Security jwtAPIToken
// @router /previous [get]
func (u *ScheduleController) GetPreviousSchedules(baseDate time.Time) {
// token := u.Ctx.Input.GetData("jwtAPIToken").(string)
// username := "asd"
futureDate := models.GetFarSchedules(baseDate, false)
u.Data["json"] = futureDate
u.ServeJSON()
return
}