137 lines
3.6 KiB
Go
137 lines
3.6 KiB
Go
package oclib
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"cloud.o-forge.io/core/oc-lib/models/peer"
|
|
"cloud.o-forge.io/core/oc-lib/models/resources"
|
|
"cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
|
|
"cloud.o-forge.io/core/oc-lib/models/resources/workflow/graph"
|
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
|
"cloud.o-forge.io/core/oc-lib/tools"
|
|
)
|
|
|
|
type AbstractWorkflow struct {
|
|
resources.ResourceSet
|
|
Graph *graph.Graph `bson:"graph,omitempty" json:"graph,omitempty"`
|
|
ScheduleActive bool `bson:"schedule_active,omitempty" json:"schedule_active,omitempty"`
|
|
Schedule *WorkflowSchedule `bson:"schedule,omitempty" json:"schedule,omitempty"`
|
|
Shared []string `json:"shared,omitempty" bson:"shared,omitempty"`
|
|
}
|
|
|
|
func (w *AbstractWorkflow) isDCLink(link graph.GraphLink) (bool, string) {
|
|
if slices.Contains(w.Datacenters, link.Source.ID) {
|
|
return true, link.Source.ID
|
|
}
|
|
if slices.Contains(w.Datacenters, link.Destination.ID) {
|
|
return true, link.Destination.ID
|
|
}
|
|
return false, ""
|
|
}
|
|
|
|
type Workflow struct {
|
|
utils.AbstractObject
|
|
AbstractWorkflow
|
|
}
|
|
|
|
func (wfa *Workflow) CheckBooking(subPath string) (bool, error) {
|
|
// check if
|
|
if wfa.Schedule == nil || wfa.Schedule.Start == nil || wfa.Graph == nil {
|
|
return false, nil
|
|
}
|
|
if wfa.Schedule.End == nil {
|
|
// if no end... then Book like a savage
|
|
return true, nil
|
|
}
|
|
e := *wfa.Schedule.End
|
|
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
|
|
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{})
|
|
subPath = strings.ReplaceAll(subPath, ":start_date", wfa.getFormat(wfa.Schedule.Start))
|
|
subPath = strings.ReplaceAll(subPath, ":end_date", wfa.getFormat(&e))
|
|
resp, err := caller.CallGet(p.(*peer.Peer).Url, subPath)
|
|
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"]))
|
|
}
|
|
}
|
|
}
|
|
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
|
|
}
|
|
|
|
func (d *Workflow) GetName() string {
|
|
return d.Name
|
|
}
|
|
|
|
func (d *Workflow) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
|
|
data := New()
|
|
data.Init(utils.WORKFLOW, caller)
|
|
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
|
|
}
|
|
json.Unmarshal(b, &m)
|
|
return m
|
|
}
|