2024-08-12 14:18:13 +02:00
|
|
|
package booking
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
|
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
|
|
|
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
|
2024-08-12 16:11:25 +02:00
|
|
|
"cloud.o-forge.io/core/oc-lib/tools"
|
2024-08-12 14:18:13 +02:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Booking struct {
|
|
|
|
workflow_execution.WorkflowExecution
|
|
|
|
DatacenterResourceID string `json:"datacenter_resource_id,omitempty" bson:"datacenter_resource_id,omitempty" validate:"required"`
|
|
|
|
}
|
|
|
|
|
2024-08-22 08:45:26 +02:00
|
|
|
func (wfa *Booking) CheckBooking(id string, start time.Time, end *time.Time) (bool, error) {
|
2024-08-12 14:18:13 +02:00
|
|
|
// check if
|
|
|
|
if end == nil {
|
|
|
|
// if no end... then Book like a savage
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
e := *end
|
2024-08-12 16:11:25 +02:00
|
|
|
accessor := wfa.GetAccessor(nil)
|
2024-08-12 14:18:13 +02:00
|
|
|
res, code, err := accessor.Search(&dbs.Filters{
|
|
|
|
And: map[string][]dbs.Filter{
|
2024-08-22 08:45:26 +02:00
|
|
|
"datacenter_resource_id": {{Operator: dbs.EQUAL.String(), Value: id}},
|
2024-08-12 14:18:13 +02:00
|
|
|
"workflowexecution.state": {{Operator: dbs.EQUAL.String(), Value: workflow_execution.SCHEDULED.EnumIndex()}},
|
|
|
|
"workflowexecution.execution_date": {
|
|
|
|
{Operator: dbs.LTE.String(), Value: primitive.NewDateTimeFromTime(e)},
|
|
|
|
{Operator: dbs.GTE.String(), Value: primitive.NewDateTimeFromTime(start)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, "")
|
|
|
|
if code != 200 {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return len(res) == 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wfa *Booking) ArgoStatusToState(status string) *Booking {
|
|
|
|
wfa.WorkflowExecution.ArgoStatusToState(status)
|
|
|
|
return wfa
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ao *Booking) GetID() string {
|
|
|
|
return ao.UUID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Booking) GenerateID() {
|
|
|
|
r.UUID = uuid.New().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Booking) GetName() string {
|
|
|
|
return d.UUID + "_" + d.ExecDate.String()
|
|
|
|
}
|
|
|
|
|
2024-08-12 16:11:25 +02:00
|
|
|
func (d *Booking) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
|
2024-08-12 14:18:13 +02:00
|
|
|
data := New()
|
2024-08-12 16:11:25 +02:00
|
|
|
data.Init(utils.BOOKING, caller)
|
2024-08-12 14:18:13 +02:00
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dma *Booking) Deserialize(j map[string]interface{}) utils.DBObject {
|
|
|
|
b, err := json.Marshal(j)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
json.Unmarshal(b, dma)
|
|
|
|
return dma
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dma *Booking) Serialize() map[string]interface{} {
|
|
|
|
var m map[string]interface{}
|
|
|
|
b, err := json.Marshal(dma)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
json.Unmarshal(b, &m)
|
|
|
|
return m
|
|
|
|
}
|