oc-lib/models/resources/resource.go

96 lines
3.4 KiB
Go
Raw Normal View History

2024-07-18 11:51:12 +02:00
package resources
2024-07-19 10:54:58 +02:00
import (
2024-11-28 11:05:54 +01:00
"cloud.o-forge.io/core/oc-lib/models/resources/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
2024-07-19 10:54:58 +02:00
)
2024-07-18 11:51:12 +02:00
// AbstractResource is the struct containing all of the attributes commons to all ressources
// Resource is the interface to be implemented by all classes inheriting from Resource to have the same behavior
2024-07-23 11:22:50 +02:00
// http://www.inanzzz.com/index.php/post/wqbs/a-basic-usage-of-int-and-string-enum-types-in-golang
2024-11-28 16:49:41 +01:00
type ResourceInterface interface {
utils.DBObject
Trim() *resource_model.AbstractResource
SetResourceModel(model *resource_model.ResourceModel)
}
2024-07-26 10:36:23 +02:00
type ResourceSet struct {
Datas []string `bson:"datas,omitempty" json:"datas,omitempty"`
Storages []string `bson:"storages,omitempty" json:"storages,omitempty"`
2024-07-30 12:08:13 +02:00
Processings []string `bson:"processings,omitempty" json:"processings,omitempty"`
2024-11-28 11:05:54 +01:00
Computes []string `bson:"computes,omitempty" json:"computes,omitempty"`
2024-07-26 10:36:23 +02:00
Workflows []string `bson:"workflows,omitempty" json:"workflows,omitempty"`
2024-07-25 11:46:33 +02:00
2024-11-28 16:49:41 +01:00
DataResources []*DataResource `bson:"-" json:"data_resources,omitempty"`
StorageResources []*StorageResource `bson:"-" json:"storage_resources,omitempty"`
ProcessingResources []*ProcessingResource `bson:"-" json:"processing_resources,omitempty"`
ComputeResources []*ComputeResource `bson:"-" json:"compute_resources,omitempty"`
WorkflowResources []*WorkflowResource `bson:"-" json:"workflow_resources,omitempty"`
}
func (r *ResourceSet) Clear() {
r.DataResources = nil
r.StorageResources = nil
r.ProcessingResources = nil
r.ComputeResources = nil
r.WorkflowResources = nil
2024-07-18 11:51:12 +02:00
}
2024-07-30 12:08:13 +02:00
2024-12-04 11:33:08 +01:00
func (r *ResourceSet) Fill(username string, peerID string, groups []string) {
2024-11-28 11:05:54 +01:00
for k, v := range map[utils.DBObject][]string{
2024-11-28 16:49:41 +01:00
(&DataResource{}): r.Datas,
(&ComputeResource{}): r.Computes,
(&StorageResource{}): r.Storages,
(&ProcessingResource{}): r.Processings,
(&WorkflowResource{}): r.Workflows,
2024-11-28 11:05:54 +01:00
} {
for _, id := range v {
2024-12-04 11:33:08 +01:00
d, _, e := k.GetAccessor(username, peerID, groups, nil).LoadOne(id)
2024-11-28 11:05:54 +01:00
if e == nil {
switch k.(type) {
2024-11-28 16:49:41 +01:00
case *DataResource:
r.DataResources = append(r.DataResources, d.(*DataResource))
case *ComputeResource:
r.ComputeResources = append(r.ComputeResources, d.(*ComputeResource))
case *StorageResource:
r.StorageResources = append(r.StorageResources, d.(*StorageResource))
case *ProcessingResource:
r.ProcessingResources = append(r.ProcessingResources, d.(*ProcessingResource))
case *WorkflowResource:
r.WorkflowResources = append(r.WorkflowResources, d.(*WorkflowResource))
2024-11-28 11:05:54 +01:00
}
}
}
}
}
2024-07-30 12:08:13 +02:00
type ItemResource struct {
2024-11-28 16:49:41 +01:00
Data *DataResource `bson:"data,omitempty" json:"data,omitempty"`
Processing *ProcessingResource `bson:"processing,omitempty" json:"processing,omitempty"`
Storage *StorageResource `bson:"storage,omitempty" json:"storage,omitempty"`
Compute *ComputeResource `bson:"compute,omitempty" json:"compute,omitempty"`
Workflow *WorkflowResource `bson:"workflow,omitempty" json:"workflow,omitempty"`
2024-07-30 12:08:13 +02:00
}
2024-08-01 16:50:16 +02:00
func (i *ItemResource) GetAbstractRessource() *resource_model.AbstractResource {
2024-11-28 11:05:54 +01:00
if i.Data != nil {
2024-08-01 16:50:16 +02:00
return &i.Data.AbstractResource
}
2024-11-28 11:05:54 +01:00
if i.Processing != nil {
2024-08-01 16:50:16 +02:00
return &i.Processing.AbstractResource
}
2024-11-28 11:05:54 +01:00
if i.Storage != nil {
2024-08-01 16:50:16 +02:00
return &i.Storage.AbstractResource
}
2024-11-28 11:05:54 +01:00
if i.Compute != nil {
2024-11-07 11:05:24 +01:00
return &i.Compute.AbstractResource
2024-08-01 16:50:16 +02:00
}
2024-11-28 11:05:54 +01:00
if i.Workflow != nil {
2024-08-01 16:50:16 +02:00
return &i.Workflow.AbstractResource
}
return nil
2024-11-28 11:05:54 +01:00
}