2024-08-30 09:14:03 +02:00
package workflow
2024-07-19 10:54:58 +02:00
import (
2024-08-12 16:11:25 +02:00
"errors"
2024-07-19 10:54:58 +02:00
2024-12-03 10:57:28 +01:00
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/shallow_collaborative_area"
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-07-19 10:54:58 +02:00
"cloud.o-forge.io/core/oc-lib/models/utils"
2024-11-28 16:49:41 +01:00
"cloud.o-forge.io/core/oc-lib/models/workflow/graph"
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-08-30 14:50:48 +02:00
/ *
* AbstractWorkflow is a struct that represents a workflow for resource or native workflow
* Warning : there is 2 types of workflows , the resource workflow and the native workflow
* native workflow is the one that you create to schedule an execution
* resource workflow is the one that is created to set our native workflow in catalog
* /
2024-07-26 10:36:23 +02:00
type AbstractWorkflow struct {
resources . ResourceSet
2024-08-30 14:50:48 +02:00
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
2024-07-26 10:36:23 +02:00
}
2024-10-04 10:42:30 +02:00
func ( w * AbstractWorkflow ) GetWorkflows ( ) ( list_computings [ ] graph . GraphItem ) {
2024-10-03 17:25:54 +02:00
for _ , item := range w . Graph . Items {
if item . Workflow != nil {
list_computings = append ( list_computings , item )
}
}
return
}
2024-11-28 16:49:41 +01:00
func ( w * AbstractWorkflow ) GetComputeByRelatedProcessing ( processingID string ) [ ] * resources . ComputeResource {
storages := [ ] * resources . ComputeResource { }
2024-11-14 10:02:18 +01:00
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
}
if processingID == nodeID && node != nil { // if the storage is linked to the processing
storages = append ( storages , node )
}
}
return storages
}
2024-11-28 16:49:41 +01:00
func ( w * AbstractWorkflow ) GetStoragesByRelatedProcessing ( processingID string ) [ ] * resources . StorageResource {
storages := [ ] * resources . StorageResource { }
2024-10-10 09:38:27 +02:00
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
}
2024-10-04 10:42:30 +02:00
func ( w * AbstractWorkflow ) GetProcessings ( ) ( list_computings [ ] graph . GraphItem ) {
2024-10-03 17:10:57 +02:00
for _ , item := range w . Graph . Items {
if item . Processing != nil {
list_computings = append ( list_computings , item )
}
}
return
}
2024-11-07 11:05:24 +01:00
// tool function to check if a link is a link between a compute and a resource
2024-08-12 14:18:13 +02:00
func ( w * AbstractWorkflow ) isDCLink ( link graph . GraphLink ) ( bool , string ) {
2024-08-29 11:03:26 +02:00
if w . Graph == nil || w . Graph . Items == nil {
return false , ""
}
2024-11-07 11:05:24 +01:00
if d , ok := w . Graph . Items [ link . Source . ID ] ; ok && d . Compute != nil {
return true , d . Compute . UUID
2024-07-26 10:36:23 +02:00
}
2024-11-07 11:05:24 +01:00
if d , ok := w . Graph . Items [ link . Destination . ID ] ; ok && d . Compute != nil {
return true , d . Compute . UUID
2024-08-12 14:18:13 +02:00
}
return false , ""
2024-07-26 10:36:23 +02:00
}
2024-08-30 14:50:48 +02:00
/ *
* Workflow is a struct that represents a workflow
* it defines the native workflow
* /
2024-07-19 10:54:58 +02:00
type Workflow struct {
2024-08-30 14:50:48 +02:00
utils . AbstractObject // AbstractObject contains the basic fields of an object (id, name)
AbstractWorkflow // AbstractWorkflow contains the basic fields of a workflow
2024-07-19 10:54:58 +02:00
}
2024-12-04 11:33:08 +01:00
func ( ao * Workflow ) VerifyAuth ( username string , peerID string , groups [ ] string ) bool {
2024-12-03 10:57:28 +01:00
isAuthorized := false
if len ( ao . Shared ) > 0 {
for _ , shared := range ao . Shared {
2024-12-04 11:33:08 +01:00
shared , code , _ := shallow_collaborative_area . New ( tools . COLLABORATIVE_AREA , username , peerID , groups , nil ) . LoadOne ( shared )
2024-12-03 10:57:28 +01:00
if code != 200 || shared == nil {
isAuthorized = false
}
2024-12-04 11:33:08 +01:00
isAuthorized = shared . VerifyAuth ( username , peerID , groups )
2024-12-03 10:57:28 +01:00
}
2024-12-03 11:57:51 +01:00
} else {
isAuthorized = true
2024-12-03 10:57:28 +01:00
}
return isAuthorized
}
2024-08-30 14:50:48 +02:00
/ *
* CheckBooking is a function that checks the booking of the workflow on peers ( even ourselves )
* /
2024-08-23 09:53:37 +02:00
func ( wfa * Workflow ) CheckBooking ( caller * tools . HTTPCaller ) ( bool , error ) {
2024-08-12 14:18:13 +02:00
// check if
2024-08-30 14:50:48 +02:00
if wfa . Graph == nil { // no graph no booking
2024-08-12 14:18:13 +02:00
return false , nil
}
2024-12-04 11:33:08 +01:00
accessor := ( & resources . ComputeResource { } ) . GetAccessor ( "" , "" , [ ] string { } , caller )
2024-08-12 16:11:25 +02:00
for _ , link := range wfa . Graph . Links {
2024-11-07 11:05:24 +01:00
if ok , dc_id := wfa . isDCLink ( link ) ; ok { // check if the link is a link between a compute and a resource
2024-08-12 16:11:25 +02:00
dc , code , _ := accessor . LoadOne ( dc_id )
if code != 200 {
continue
}
2024-11-07 11:05:24 +01:00
// CHECK BOOKING ON PEER, compute could be a remote one
2024-11-28 16:49:41 +01:00
peerID := dc . ( * resources . ComputeResource ) . PeerID
2024-08-13 09:49:42 +02:00
if peerID == "" {
return false , errors . New ( "no peer id" )
2024-08-30 14:50:48 +02:00
} // no peer id no booking, we need to know where to book
2024-10-02 10:45:52 +02:00
_ , err := ( & peer . Peer { } ) . LaunchPeerExecution ( peerID , dc_id , tools . BOOKING , tools . GET , nil , caller )
2024-08-12 16:11:25 +02:00
if err != nil {
return false , err
}
}
2024-08-12 14:18:13 +02:00
}
2024-08-12 16:11:25 +02:00
return true , nil
}
2024-12-04 11:33:08 +01:00
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
2024-07-19 10:54:58 +02:00
}