massive draft for payment process (UNCOMPLETE)
This commit is contained in:
@@ -2,8 +2,10 @@ package workflow
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/shallow_collaborative_area"
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||
"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/utils"
|
||||
@@ -19,29 +21,61 @@ import (
|
||||
*/
|
||||
type AbstractWorkflow struct {
|
||||
resources.ResourceSet
|
||||
Graph *graph.Graph `bson:"graph,omitempty" json:"graph,omitempty"` // Graph UI & logic representation of the workflow
|
||||
ScheduleActive bool `json:"schedule_active" bson:"schedule_active"` // ScheduleActive is a flag that indicates if the schedule is active, if not the workflow is not scheduled and no execution or booking will be set
|
||||
Schedule *WorkflowSchedule `bson:"schedule,omitempty" json:"schedule,omitempty"` // Schedule is the schedule of the workflow
|
||||
Shared []string `json:"shared,omitempty" bson:"shared,omitempty"` // Shared is the ID of the shared workflow
|
||||
Graph *graph.Graph `bson:"graph,omitempty" json:"graph,omitempty"` // Graph UI & logic representation of the workflow
|
||||
ScheduleActive bool `json:"schedule_active" bson:"schedule_active"` // ScheduleActive is a flag that indicates if the schedule is active, if not the workflow is not scheduled and no execution or booking will be set
|
||||
// Schedule *WorkflowSchedule `bson:"schedule,omitempty" json:"schedule,omitempty"` // Schedule is the schedule of the workflow
|
||||
Shared []string `json:"shared,omitempty" bson:"shared,omitempty"` // Shared is the ID of the shared workflow
|
||||
}
|
||||
|
||||
func (w *AbstractWorkflow) GetWorkflows() (list_computings []graph.GraphItem) {
|
||||
func (d *Workflow) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return NewAccessor(request) // Create a new instance of the accessor
|
||||
}
|
||||
|
||||
func (w *AbstractWorkflow) GetGraphItems(f func(item graph.GraphItem) bool) (list_datas []graph.GraphItem) {
|
||||
for _, item := range w.Graph.Items {
|
||||
if item.Workflow != nil {
|
||||
list_computings = append(list_computings, item)
|
||||
if f(item) {
|
||||
list_datas = append(list_datas, item)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (w *AbstractWorkflow) GetComputeByRelatedProcessing(processingID string) []*resources.ComputeResource {
|
||||
storages := []*resources.ComputeResource{}
|
||||
func (w *AbstractWorkflow) GetResources(f func(item graph.GraphItem) bool) map[string]resources.ShallowResourceInterface {
|
||||
list_datas := map[string]resources.ShallowResourceInterface{}
|
||||
for _, item := range w.Graph.Items {
|
||||
if f(item) {
|
||||
res := item.GetResource()
|
||||
list_datas[res.GetID()] = res
|
||||
}
|
||||
}
|
||||
return list_datas
|
||||
}
|
||||
|
||||
func (w *AbstractWorkflow) GetPricedItem(f func(item graph.GraphItem) bool) map[string]pricing.PricedItemITF {
|
||||
list_datas := map[string]pricing.PricedItemITF{}
|
||||
for _, item := range w.Graph.Items {
|
||||
if f(item) {
|
||||
res := item.GetResource()
|
||||
ord := item.GetPricedItem()
|
||||
list_datas[res.GetID()] = ord
|
||||
}
|
||||
}
|
||||
return list_datas
|
||||
}
|
||||
|
||||
func (w *AbstractWorkflow) GetByRelatedProcessing(processingID string, g func(item graph.GraphItem) bool) []resources.ShallowResourceInterface {
|
||||
storages := []resources.ShallowResourceInterface{}
|
||||
for _, link := range w.Graph.Links {
|
||||
nodeID := link.Destination.ID // we considers that the processing is the destination
|
||||
node := w.Graph.Items[link.Source.ID].Compute // we are looking for the storage as source
|
||||
if node == nil { // if the source is not a storage, we consider that the destination is the storage
|
||||
nodeID = link.Source.ID // and the processing is the source
|
||||
node = w.Graph.Items[link.Destination.ID].Compute // we are looking for the storage as destination
|
||||
nodeID := link.Destination.ID
|
||||
var node resources.ShallowResourceInterface
|
||||
if g(w.Graph.Items[link.Source.ID]) {
|
||||
item := w.Graph.Items[link.Source.ID]
|
||||
node = item.GetResource()
|
||||
}
|
||||
if node == nil && g(w.Graph.Items[link.Destination.ID]) { // if the source is not a storage, we consider that the destination is the storage
|
||||
nodeID = link.Source.ID
|
||||
item := w.Graph.Items[link.Destination.ID] // and the processing is the source
|
||||
node = item.GetResource() // we are looking for the storage as destination
|
||||
}
|
||||
if processingID == nodeID && node != nil { // if the storage is linked to the processing
|
||||
storages = append(storages, node)
|
||||
@@ -50,43 +84,24 @@ func (w *AbstractWorkflow) GetComputeByRelatedProcessing(processingID string) []
|
||||
return storages
|
||||
}
|
||||
|
||||
func (w *AbstractWorkflow) GetStoragesByRelatedProcessing(processingID string) []*resources.StorageResource {
|
||||
storages := []*resources.StorageResource{}
|
||||
for _, link := range w.Graph.Links {
|
||||
nodeID := link.Destination.ID // we considers that the processing is the destination
|
||||
node := w.Graph.Items[link.Source.ID].Storage // we are looking for the storage as source
|
||||
if node == nil { // if the source is not a storage, we consider that the destination is the storage
|
||||
nodeID = link.Source.ID // and the processing is the source
|
||||
node = w.Graph.Items[link.Destination.ID].Storage // we are looking for the storage as destination
|
||||
}
|
||||
if processingID == nodeID && node != nil { // if the storage is linked to the processing
|
||||
storages = append(storages, node)
|
||||
}
|
||||
}
|
||||
return storages
|
||||
func (wf *AbstractWorkflow) IsProcessing(item graph.GraphItem) bool {
|
||||
return item.Processing != nil
|
||||
}
|
||||
|
||||
func (w *AbstractWorkflow) GetProcessings() (list_computings []graph.GraphItem) {
|
||||
for _, item := range w.Graph.Items {
|
||||
if item.Processing != nil {
|
||||
list_computings = append(list_computings, item)
|
||||
}
|
||||
}
|
||||
return
|
||||
func (wf *AbstractWorkflow) IsCompute(item graph.GraphItem) bool {
|
||||
return item.Compute != nil
|
||||
}
|
||||
|
||||
// tool function to check if a link is a link between a compute and a resource
|
||||
func (w *AbstractWorkflow) isDCLink(link graph.GraphLink) (bool, string) {
|
||||
if w.Graph == nil || w.Graph.Items == nil {
|
||||
return false, ""
|
||||
}
|
||||
if d, ok := w.Graph.Items[link.Source.ID]; ok && d.Compute != nil {
|
||||
return true, d.Compute.UUID
|
||||
}
|
||||
if d, ok := w.Graph.Items[link.Destination.ID]; ok && d.Compute != nil {
|
||||
return true, d.Compute.UUID
|
||||
}
|
||||
return false, ""
|
||||
func (wf *AbstractWorkflow) IsData(item graph.GraphItem) bool {
|
||||
return item.Data != nil
|
||||
}
|
||||
|
||||
func (wf *AbstractWorkflow) IsStorage(item graph.GraphItem) bool {
|
||||
return item.Storage != nil
|
||||
}
|
||||
|
||||
func (wf *AbstractWorkflow) IsWorkflow(item graph.GraphItem) bool {
|
||||
return item.Workflow != nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -98,18 +113,51 @@ type Workflow struct {
|
||||
AbstractWorkflow // AbstractWorkflow contains the basic fields of a workflow
|
||||
}
|
||||
|
||||
func (ao *Workflow) VerifyAuth(username string, peerID string, groups []string) bool {
|
||||
func (w *Workflow) GetNearestStart(start time.Time) float64 {
|
||||
near := float64(10000000000)
|
||||
for _, item := range w.Graph.Items {
|
||||
if item.GetResource().GetLocationStart() == nil {
|
||||
continue
|
||||
}
|
||||
newS := item.GetResource().GetLocationStart()
|
||||
if newS.Sub(start).Seconds() < near {
|
||||
near = newS.Sub(start).Seconds()
|
||||
}
|
||||
// get the nearest start from start var
|
||||
}
|
||||
return near
|
||||
}
|
||||
|
||||
func (w *Workflow) GetLongestTime(end *time.Time) float64 {
|
||||
if end == nil {
|
||||
return -1
|
||||
}
|
||||
longestTime := float64(0)
|
||||
for _, item := range w.GetGraphItems(w.IsProcessing) {
|
||||
if item.GetResource().GetLocationEnd() == nil {
|
||||
continue
|
||||
}
|
||||
newS := item.GetResource().GetLocationEnd()
|
||||
if longestTime < newS.Sub(*end).Seconds() {
|
||||
longestTime = newS.Sub(*end).Seconds()
|
||||
}
|
||||
// get the nearest start from start var
|
||||
}
|
||||
return longestTime
|
||||
}
|
||||
|
||||
func (ao *Workflow) VerifyAuth(request *tools.APIRequest) bool {
|
||||
isAuthorized := false
|
||||
if len(ao.Shared) > 0 {
|
||||
for _, shared := range ao.Shared {
|
||||
shared, code, _ := shallow_collaborative_area.New(tools.COLLABORATIVE_AREA, username, peerID, groups, nil).LoadOne(shared)
|
||||
shared, code, _ := shallow_collaborative_area.NewAccessor(request).LoadOne(shared)
|
||||
if code != 200 || shared == nil {
|
||||
isAuthorized = false
|
||||
}
|
||||
isAuthorized = shared.VerifyAuth(username, peerID, groups)
|
||||
isAuthorized = shared.VerifyAuth(request)
|
||||
}
|
||||
}
|
||||
return ao.AbstractObject.VerifyAuth(username, peerID, groups) || isAuthorized
|
||||
return ao.AbstractObject.VerifyAuth(request) || isAuthorized
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -120,19 +168,19 @@ func (wfa *Workflow) CheckBooking(caller *tools.HTTPCaller) (bool, error) {
|
||||
if wfa.Graph == nil { // no graph no booking
|
||||
return false, nil
|
||||
}
|
||||
accessor := (&resources.ComputeResource{}).GetAccessor("", "", []string{}, caller)
|
||||
accessor := (&resources.ComputeResource{}).GetAccessor(&tools.APIRequest{Caller: caller})
|
||||
for _, link := range wfa.Graph.Links {
|
||||
if ok, dc_id := wfa.isDCLink(link); ok { // check if the link is a link between a compute and a resource
|
||||
dc, code, _ := accessor.LoadOne(dc_id)
|
||||
if ok, compute_id := link.IsComputeLink(*wfa.Graph); ok { // check if the link is a link between a compute and a resource
|
||||
compute, code, _ := accessor.LoadOne(compute_id)
|
||||
if code != 200 {
|
||||
continue
|
||||
}
|
||||
// CHECK BOOKING ON PEER, compute could be a remote one
|
||||
peerID := dc.(*resources.ComputeResource).PeerID
|
||||
peerID := compute.(*resources.ComputeResource).CreatorID
|
||||
if peerID == "" {
|
||||
return false, errors.New("no peer id")
|
||||
} // no peer id no booking, we need to know where to book
|
||||
_, err := (&peer.Peer{}).LaunchPeerExecution(peerID, dc_id, tools.BOOKING, tools.GET, nil, caller)
|
||||
_, err := (&peer.Peer{}).LaunchPeerExecution(peerID, compute_id, tools.BOOKING, tools.GET, nil, caller)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@@ -140,7 +188,3 @@ func (wfa *Workflow) CheckBooking(caller *tools.HTTPCaller) (bool, error) {
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (d *Workflow) GetAccessor(username string, peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor {
|
||||
return New(tools.WORKFLOW, username, peerID, groups, caller) // Create a new instance of the accessor
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user