implement remote call for remote action

This commit is contained in:
mr
2024-08-12 16:11:25 +02:00
parent 4575f9ad3f
commit 2ac24779cd
26 changed files with 312 additions and 144 deletions

View File

@@ -2,14 +2,16 @@ package oclib
import (
"encoding/json"
"errors"
"fmt"
"slices"
"time"
"cloud.o-forge.io/core/oc-lib/dbs"
"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/models/workflow_execution"
"go.mongodb.org/mongo-driver/bson/primitive"
"cloud.o-forge.io/core/oc-lib/tools"
)
type AbstractWorkflow struct {
@@ -34,9 +36,9 @@ type Workflow struct {
AbstractWorkflow
}
func (wfa *Workflow) CheckBooking() (bool, error) {
func (wfa *Workflow) CheckBooking(caller *tools.HTTPCaller) (bool, error) {
// check if
if wfa.Schedule == nil || wfa.Schedule.Start == nil {
if wfa.Schedule == nil || wfa.Schedule.Start == nil || wfa.Graph == nil {
return false, nil
}
if wfa.Schedule.End == nil {
@@ -44,29 +46,60 @@ func (wfa *Workflow) CheckBooking() (bool, error) {
return true, nil
}
e := *wfa.Schedule.End
accessor := wfa.GetAccessor()
res, code, err := accessor.Search(&dbs.Filters{
And: map[string][]dbs.Filter{
"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(*wfa.Schedule.Start)},
},
},
}, "")
if code != 200 {
return false, err
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
url := dc.(*datacenter.DatacenterResource).SourceUrl
resp, err := caller.CallGet(url, caller.OriginSubPath+"/"+wfa.getFormat(wfa.Schedule.Start)+"/"+wfa.getFormat(&e))
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 len(res) == 0, nil
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() utils.Accessor {
func (d *Workflow) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
data := New()
data.SetLogger(utils.WORKFLOW)
data.Init(utils.WORKFLOW, caller)
return data
}

View File

@@ -1,12 +1,13 @@
package oclib
import (
"encoding/json"
"errors"
"fmt"
"strings"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/booking"
"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/utils"
@@ -80,10 +81,9 @@ func (wfa *workflowMongoAccessor) DeleteOne(id string) (utils.DBObject, int, err
return wfa.GenericDeleteOne(id, wfa)
}
func (wfa *workflowMongoAccessor) book(id string, realData *Workflow, execs []*workflow_execution.WorkflowExecution) []*booking.Booking {
books := []*booking.Booking{}
func (wfa *workflowMongoAccessor) book(id string, realData *Workflow, execs []*workflow_execution.WorkflowExecution) error {
if realData.Schedule == nil {
return books
return nil
}
res, _, _ := wfa.LoadOne(id)
r := res.(*Workflow)
@@ -92,46 +92,30 @@ func (wfa *workflowMongoAccessor) book(id string, realData *Workflow, execs []*w
g = realData.Graph
}
if g != nil && g.Links != nil && len(g.Links) > 0 {
bookAccessor := (&booking.Booking{}).GetAccessor()
accessor := (&datacenter.DatacenterResource{}).GetAccessor()
accessor := (&datacenter.DatacenterResource{}).GetAccessor(nil)
for _, link := range g.Links {
if ok, dc_id := realData.isDCLink(link); ok {
_, code, _ := accessor.LoadOne(dc_id)
dc, code, _ := accessor.LoadOne(dc_id)
if code != 200 {
continue
}
// CHECK BOOKING
// dc.(*datacenter.DatacenterResource).SourceUrl should get source url... but it's not implemented
res, code, _ := bookAccessor.Search(&dbs.Filters{And: map[string][]dbs.Filter{
"peer_id": {{Operator: dbs.EQUAL.String(), Value: "my_peer"}}, // peer is always the same for the moment
"datacenter_resource_id": {{Operator: dbs.EQUAL.String(), Value: dc_id}},
}}, "")
if code != 200 {
continue
url := dc.(*datacenter.DatacenterResource).SourceUrl
resp, err := wfa.Caller.CallPost(url, wfa.Caller.OriginSubPath, (&workflow_execution.WorkflowExecutions{
Executions: execs,
}).Serialize())
if err != nil {
return err
}
for _, b := range res {
bookAccessor.DeleteOne(b.GetID())
}
for _, exec := range execs {
if ok, err := (&booking.Booking{}).CheckBooking(*exec.ExecDate, exec.EndDate); !ok {
if err != nil {
return books
}
return books
}
b, code, _ := bookAccessor.StoreOne(&booking.Booking{
PeerID: "my_peer",
DatacenterResourceID: dc_id,
WorkflowExecution: *exec,
})
if code == 200 {
books = append(books, b.(*booking.Booking))
}
var response map[string]interface{}
json.Unmarshal(resp, &response)
if code, ok := response["code"]; ok && code != 200 {
return errors.New(fmt.Sprintf("%v", response["error"]))
}
}
}
}
return books
return nil
}
func (wfa *workflowMongoAccessor) execution(id string, realData *Workflow, delete bool) (int, error) {
@@ -143,15 +127,14 @@ func (wfa *workflowMongoAccessor) execution(id string, realData *Workflow, delet
if r.Schedule != nil && r.Schedule.Start == realData.Schedule.Start && r.Schedule.End == realData.Schedule.End && r.Schedule.Cron == realData.Schedule.Cron {
return 200, nil
}
accessor := (&workflow_execution.WorkflowExecution{}).GetAccessor()
accessor := (&workflow_execution.WorkflowExecution{}).GetAccessor(nil)
execs, err := wfa.getExecutions(id, realData)
for _, exec := range execs {
if ok, err := (&booking.Booking{}).CheckBooking(*exec.ExecDate, exec.EndDate); !ok {
if err != nil {
return 500, err
}
return 409, errors.New("the booking from " + exec.ExecDate.String() + " is already taken.")
}
if err != nil {
return 422, err
}
err = wfa.book(id, realData, execs)
if err != nil {
return 409, err
}
if delete {
mongo.MONGOService.DeleteMultiple(map[string]interface{}{
@@ -169,7 +152,6 @@ func (wfa *workflowMongoAccessor) execution(id string, realData *Workflow, delet
} else {
return 422, err
}
wfa.book(id, realData, execs)
return 200, nil
}
@@ -202,7 +184,7 @@ func (wfa *workflowMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject,
}
func (wfa *workflowMongoAccessor) execute(workflow *Workflow) {
accessor := (&workspace.Workspace{}).GetAccessor()
accessor := (&workspace.Workspace{}).GetAccessor(nil)
filters := &dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractobject.name": {{dbs.LIKE.String(), workflow.Name + "_workspace"}},