2024-07-19 10:54:58 +02:00
|
|
|
package oclib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-08-12 16:11:25 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-08-12 16:17:47 +02:00
|
|
|
"strings"
|
2024-08-12 16:11:25 +02:00
|
|
|
"time"
|
2024-07-19 10:54:58 +02:00
|
|
|
|
2024-08-13 09:49:42 +02:00
|
|
|
"cloud.o-forge.io/core/oc-lib/models/peer"
|
2024-07-26 10:36:23 +02:00
|
|
|
"cloud.o-forge.io/core/oc-lib/models/resources"
|
2024-08-12 16:11:25 +02:00
|
|
|
"cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
|
2024-07-26 10:36:23 +02:00
|
|
|
"cloud.o-forge.io/core/oc-lib/models/resources/workflow/graph"
|
2024-07-19 10:54:58 +02:00
|
|
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
2024-08-12 16:11:25 +02:00
|
|
|
"cloud.o-forge.io/core/oc-lib/tools"
|
2024-07-19 10:54:58 +02:00
|
|
|
)
|
|
|
|
|
2024-07-26 10:36:23 +02:00
|
|
|
type AbstractWorkflow struct {
|
|
|
|
resources.ResourceSet
|
2024-08-13 14:33:26 +02:00
|
|
|
Graph *graph.Graph `bson:"graph,omitempty" json:"graph,omitempty"`
|
2024-08-21 14:07:22 +02:00
|
|
|
ScheduleActive bool `json:"schedule_active,omitempty" bson:"schedule_active,omitempty"`
|
2024-08-13 14:33:26 +02:00
|
|
|
Schedule *WorkflowSchedule `bson:"schedule,omitempty" json:"schedule,omitempty"`
|
|
|
|
Shared []string `json:"shared,omitempty" bson:"shared,omitempty"`
|
2024-07-26 10:36:23 +02:00
|
|
|
}
|
|
|
|
|
2024-08-12 14:18:13 +02:00
|
|
|
func (w *AbstractWorkflow) isDCLink(link graph.GraphLink) (bool, string) {
|
2024-08-20 14:59:06 +02:00
|
|
|
if d, ok := w.Graph.Items[link.Source.ID]; ok && d.Datacenter != nil {
|
2024-08-20 15:27:12 +02:00
|
|
|
return true, d.Datacenter.UUID
|
2024-07-26 10:36:23 +02:00
|
|
|
}
|
2024-08-20 14:59:06 +02:00
|
|
|
if d, ok := w.Graph.Items[link.Destination.ID]; ok && d.Datacenter != nil {
|
2024-08-20 15:27:12 +02:00
|
|
|
return true, d.Datacenter.UUID
|
2024-08-12 14:18:13 +02:00
|
|
|
}
|
|
|
|
return false, ""
|
2024-07-26 10:36:23 +02:00
|
|
|
}
|
|
|
|
|
2024-07-19 10:54:58 +02:00
|
|
|
type Workflow struct {
|
2024-07-19 13:15:51 +02:00
|
|
|
utils.AbstractObject
|
2024-07-26 10:36:23 +02:00
|
|
|
AbstractWorkflow
|
2024-07-19 10:54:58 +02:00
|
|
|
}
|
|
|
|
|
2024-08-12 16:17:47 +02:00
|
|
|
func (wfa *Workflow) CheckBooking(subPath string) (bool, error) {
|
2024-08-12 14:18:13 +02:00
|
|
|
// check if
|
2024-08-12 16:11:25 +02:00
|
|
|
if wfa.Schedule == nil || wfa.Schedule.Start == nil || wfa.Graph == nil {
|
2024-08-12 14:18:13 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if wfa.Schedule.End == nil {
|
|
|
|
// if no end... then Book like a savage
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
e := *wfa.Schedule.End
|
2024-08-12 16:11:25 +02:00
|
|
|
accessor := (&datacenter.DatacenterResource{}).GetAccessor(nil)
|
|
|
|
for _, link := range wfa.Graph.Links {
|
|
|
|
if ok, dc_id := wfa.isDCLink(link); ok {
|
|
|
|
dc, code, _ := accessor.LoadOne(dc_id)
|
|
|
|
if code != 200 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// CHECK BOOKING
|
2024-08-13 09:49:42 +02:00
|
|
|
peerID := dc.(*datacenter.DatacenterResource).PeerID
|
|
|
|
if peerID == "" {
|
|
|
|
return false, errors.New("no peer id")
|
|
|
|
}
|
|
|
|
p, code, err := (&peer.Peer{}).GetAccessor(nil).LoadOne(peerID)
|
|
|
|
if code != 200 {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
caller := tools.NewHTTPCaller(map[string]map[tools.METHOD]string{})
|
2024-08-21 15:22:13 +02:00
|
|
|
if strings.Contains(p.(*peer.Peer).Url, "localhost") || strings.Contains(p.(*peer.Peer).Url, "127.0.0.1") {
|
|
|
|
subPath = strings.ReplaceAll(subPath, "localhost", utils.BOOKING.API())
|
|
|
|
subPath = strings.ReplaceAll(subPath, "127.0.0.1", utils.BOOKING.API())
|
|
|
|
}
|
2024-08-12 16:17:47 +02:00
|
|
|
subPath = strings.ReplaceAll(subPath, ":start_date", wfa.getFormat(wfa.Schedule.Start))
|
|
|
|
subPath = strings.ReplaceAll(subPath, ":end_date", wfa.getFormat(&e))
|
2024-08-13 09:49:42 +02:00
|
|
|
resp, err := caller.CallGet(p.(*peer.Peer).Url, subPath)
|
2024-08-12 16:11:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
var response map[string]interface{}
|
|
|
|
json.Unmarshal(resp, &response)
|
|
|
|
if code, ok := response["code"]; ok && code != 200 {
|
|
|
|
return false, errors.New(fmt.Sprintf("%v", response["error"]))
|
|
|
|
}
|
|
|
|
}
|
2024-08-12 14:18:13 +02:00
|
|
|
}
|
2024-08-12 16:11:25 +02:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wfa *Workflow) getFormat(date *time.Time) string {
|
|
|
|
month := fmt.Sprintf("%v", date.Month())
|
|
|
|
day := fmt.Sprintf("%v", date.Day())
|
|
|
|
hour := fmt.Sprintf("%v", date.Hour())
|
|
|
|
minute := fmt.Sprintf("%v", date.Minute())
|
|
|
|
second := fmt.Sprintf("%v", date.Second())
|
|
|
|
if len(month) == 1 {
|
|
|
|
month = "0" + month
|
|
|
|
}
|
|
|
|
if len(day) == 1 {
|
|
|
|
day = "0" + day
|
|
|
|
}
|
|
|
|
if len(hour) == 1 {
|
|
|
|
hour = "0" + hour
|
|
|
|
}
|
|
|
|
if len(minute) == 1 {
|
|
|
|
minute = "0" + minute
|
|
|
|
}
|
|
|
|
if len(second) == 1 {
|
|
|
|
second = "0" + second
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%v", date.Year()) + "-" + month + "-" + day + "T" + hour + ":" + minute + ":" + second
|
2024-07-19 10:54:58 +02:00
|
|
|
}
|
|
|
|
|
2024-08-12 14:18:13 +02:00
|
|
|
func (d *Workflow) GetName() string {
|
|
|
|
return d.Name
|
2024-08-08 15:56:48 +02:00
|
|
|
}
|
|
|
|
|
2024-08-12 16:11:25 +02:00
|
|
|
func (d *Workflow) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
|
2024-07-31 10:07:55 +02:00
|
|
|
data := New()
|
2024-08-12 16:11:25 +02:00
|
|
|
data.Init(utils.WORKFLOW, caller)
|
2024-07-19 10:54:58 +02:00
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dma *Workflow) 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 *Workflow) Serialize() map[string]interface{} {
|
|
|
|
var m map[string]interface{}
|
|
|
|
b, err := json.Marshal(dma)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2024-07-24 09:53:30 +02:00
|
|
|
json.Unmarshal(b, &m)
|
2024-07-19 10:54:58 +02:00
|
|
|
return m
|
|
|
|
}
|