Get on Workspace auto load full data

This commit is contained in:
mr 2024-07-26 10:36:23 +02:00
parent d5c5b454f4
commit 6c83e54d37
15 changed files with 129 additions and 70 deletions

View File

@ -55,10 +55,6 @@ func LoadAll(collection LibDataEnum) LibDataShallow {
return LibDataShallow{Data: d, Code: code}
}
func Model(t int) utils.DBObject {
return models.Model(t)
}
func LoadOne(collection LibDataEnum, id string) LibData {
d, code, err := models.Model(collection.EnumIndex()).GetAccessor().LoadOne(id)
if err != nil {

View File

@ -3,12 +3,11 @@ package data
import (
"encoding/json"
resources "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type DataResource struct {
resources.AbstractResource
utils.AbstractResource
Protocols []string `json:"protocol,omitempty" bson:"protocol,omitempty"` //TODO Enum type
DataType string `json:"datatype,omitempty" bson:"datatype,omitempty"`
Example string `json:"example,omitempty" bson:"example,omitempty" description:"base64 encoded data"`

View File

@ -3,7 +3,6 @@ package data
import (
"testing"
resources "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert"
@ -11,7 +10,7 @@ import (
func TestStoreOneData(t *testing.T) {
d := DataResource{DataType: "jpeg", Example: "123456",
AbstractResource: resources.AbstractResource{
AbstractResource: utils.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testData"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
@ -29,7 +28,7 @@ func TestStoreOneData(t *testing.T) {
func TestLoadOneDate(t *testing.T) {
d := DataResource{DataType: "jpeg", Example: "123456",
AbstractResource: resources.AbstractResource{
AbstractResource: utils.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testData"},
Description: "Lorem Ipsum",
Logo: "azerty.com",

View File

@ -3,12 +3,11 @@ package datacenter
import (
"encoding/json"
resources "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type DatacenterResource struct {
resources.AbstractResource
utils.AbstractResource
BookingPrice int `bson:"booking_price,omitempty" json:"booking_price,omitempty"`
CPU *DatacenterCpuModel `bson:"cpu,omitempty" json:"cpu,omitempty"`

View File

@ -3,7 +3,6 @@ package datacenter
import (
"testing"
resources "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert"
@ -11,7 +10,7 @@ import (
func TestStoreOneDatacenter(t *testing.T) {
dc := DatacenterResource{BookingPrice: 123,
AbstractResource: resources.AbstractResource{
AbstractResource: utils.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testDatacenter"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
@ -29,7 +28,7 @@ func TestStoreOneDatacenter(t *testing.T) {
func TestLoadOneDatacenter(t *testing.T) {
dc := DatacenterResource{BookingPrice: 123,
AbstractResource: resources.AbstractResource{
AbstractResource: utils.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testDatacenter"},
Description: "Lorem Ipsum",
Logo: "azerty.com",

View File

@ -3,12 +3,11 @@ package processing
import (
"encoding/json"
resources "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type ProcessingResource struct {
resources.AbstractResource
utils.AbstractResource
CPUs uint `bson:"cp_us,omitempty" json:"cp_us,omitempty"`
GPUs uint `bson:"gp_us,omitempty" json:"gp_us,omitempty"`
RAM uint `bson:"ram,omitempty" json:"ram,omitempty"`

View File

@ -1,7 +1,11 @@
package resources
import (
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/models/resources/data"
"cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
"cloud.o-forge.io/core/oc-lib/models/resources/processing"
"cloud.o-forge.io/core/oc-lib/models/resources/storage"
w "cloud.o-forge.io/core/oc-lib/models/resources/workflow"
)
// AbstractResource is the struct containing all of the attributes commons to all ressources
@ -9,21 +13,16 @@ import (
// Resource is the interface to be implemented by all classes inheriting from Resource to have the same behavior
// http://www.inanzzz.com/index.php/post/wqbs/a-basic-usage-of-int-and-string-enum-types-in-golang
type AbstractResource struct {
utils.AbstractObject
ShortDescription string `json:"short_description,omitempty" bson:"short_description,omitempty" validate:"required"`
Description string `json:"description,omitempty" bson:"description,omitempty"`
Logo string `json:"logo,omitempty" bson:"logo,omitempty" validate:"required"`
Owner string `json:"owner,omitempty" bson:"owner,omitempty" validate:"required"`
OwnerLogo string `json:"owner_logo,omitempty" bson:"owner_logo,omitempty"`
SourceUrl string `json:"source_url,omitempty" bson:"source_url,omitempty" validate:"required"`
Proxy *ResourceProxy `json:"proxy,omitempty" bson:"proxy,omitempty"`
}
type ResourceSet struct {
Datas []string `bson:"datas,omitempty" json:"datas,omitempty"`
Storages []string `bson:"storages,omitempty" json:"storages,omitempty"`
Processings []string `bson:"processing,omitempty" json:"processing,omitempty"`
Datacenters []string `bson:"datacenters,omitempty" json:"datacenters,omitempty"`
Workflows []string `bson:"workflows,omitempty" json:"workflows,omitempty"`
type ResourceProxy struct {
Host string `json:"host,omitempty" bson:"host,omitempty"`
Port int `json:"port,omitempty" bson:"port,omitempty"`
Command string `json:"command,omitempty" bson:"command,omitempty"`
Args []string `json:"args,omitempty" bson:"args,omitempty"`
EnvArgs map[string]interface{} `json:"env_args,omitempty" bson:"env_args,omitempty"`
DataResources []*data.DataResource `bson:"-" json:"data_resources,omitempty"`
StorageResources []*storage.StorageResource `bson:"-" json:"storage_resources,omitempty"`
ProcessingResources []*processing.ProcessingResource `bson:"-" json:"processing_resources,omitempty"`
DatacenterResources []*datacenter.DatacenterResource `bson:"-" json:"datacenter_resources,omitempty"`
WorkflowResources []*w.WorkflowResource `bson:"-" json:"workflow_resources,omitempty"`
}

View File

@ -3,7 +3,6 @@ package storage
import (
"encoding/json"
resources "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
@ -13,7 +12,7 @@ type URL struct {
}
type StorageResource struct {
resources.AbstractResource
utils.AbstractResource
Capacity uint `bson:"capacity,omitempty" json:"capacity,omitempty"`
Url *URL `bson:"url,omitempty" json:"url,omitempty"` // Will allow to select between several protocols

View File

@ -3,7 +3,6 @@ package storage
import (
"testing"
resources "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert"
@ -11,7 +10,7 @@ import (
func TestStoreOneStorage(t *testing.T) {
s := StorageResource{Capacity: 123, Url: &URL{Protocol: "http", Path: "azerty.fr"},
AbstractResource: resources.AbstractResource{
AbstractResource: utils.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testData"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
@ -29,7 +28,7 @@ func TestStoreOneStorage(t *testing.T) {
func TestLoadOneStorage(t *testing.T) {
s := StorageResource{Capacity: 123, Url: &URL{Protocol: "http", Path: "azerty.fr"},
AbstractResource: resources.AbstractResource{
AbstractResource: utils.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testData"},
Description: "Lorem Ipsum",
Logo: "azerty.com",

View File

@ -2,28 +2,12 @@ package oclib
import (
"encoding/json"
"slices"
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/resources/workflow/graph"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type AbstractWorkflow struct {
utils.ResourceSet
Graph *graph.Graph `bson:"graph,omitempty" json:"graph,omitempty"`
Schedule *WorkflowSchedule `bson:"schedule,omitempty" json:"schedule,omitempty"`
}
func (w *AbstractWorkflow) isDCLink(link graph.GraphLink) bool {
if slices.Contains(w.Datacenters, link.Destination.ID) || slices.Contains(w.Datacenters, link.Source.ID) {
return true
}
return false
}
type WorkflowResource struct {
resources.AbstractResource
utils.AbstractResource
WorkflowID string `bson:"workflow_id,omitempty" json:"workflow_id,omitempty"`
}

View File

@ -3,14 +3,13 @@ package oclib
import (
"testing"
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert"
)
func TestStoreOneWorkflow(t *testing.T) {
w := WorkflowResource{AbstractResource: resources.AbstractResource{
w := WorkflowResource{AbstractResource: utils.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
@ -27,7 +26,7 @@ func TestStoreOneWorkflow(t *testing.T) {
}
func TestLoadOneWorkflow(t *testing.T) {
w := WorkflowResource{AbstractResource: resources.AbstractResource{
w := WorkflowResource{AbstractResource: utils.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
Description: "Lorem Ipsum",
Logo: "azerty.com",

View File

@ -10,14 +10,6 @@ import (
var validate = validator.New(validator.WithRequiredStructEnabled())
type ResourceSet struct {
Datas []string `bson:"datas,omitempty" json:"datas,omitempty"`
Storages []string `bson:"storages,omitempty" json:"storages,omitempty"`
Processings []string `bson:"processing,omitempty" json:"processing,omitempty"`
Datacenters []string `bson:"datacenters,omitempty" json:"datacenters,omitempty"`
Workflows []string `bson:"workflows,omitempty" json:"workflows,omitempty"`
}
type AbstractObject struct {
UUID string `json:"id,omitempty" bson:"id,omitempty" validate:"required"`
Name string `json:"name,omitempty" bson:"name,omitempty" validate:"required"`
@ -95,3 +87,21 @@ func (dma *AbstractAccessor) GenericUpdateOne(set DBObject, id string, accessor
}
return accessor.LoadOne(id)
}
type AbstractResource struct {
AbstractObject
ShortDescription string `json:"short_description,omitempty" bson:"short_description,omitempty" validate:"required"`
Description string `json:"description,omitempty" bson:"description,omitempty"`
Logo string `json:"logo,omitempty" bson:"logo,omitempty" validate:"required"`
Owner string `json:"owner,omitempty" bson:"owner,omitempty" validate:"required"`
OwnerLogo string `json:"owner_logo,omitempty" bson:"owner_logo,omitempty"`
SourceUrl string `json:"source_url,omitempty" bson:"source_url,omitempty" validate:"required"`
Proxy *ResourceProxy `json:"proxy,omitempty" bson:"proxy,omitempty"`
}
type ResourceProxy struct {
Host string `json:"host,omitempty" bson:"host,omitempty"`
Port int `json:"port,omitempty" bson:"port,omitempty"`
Command string `json:"command,omitempty" bson:"command,omitempty"`
Args []string `json:"args,omitempty" bson:"args,omitempty"`
EnvArgs map[string]interface{} `json:"env_args,omitempty" bson:"env_args,omitempty"`
}

View File

@ -2,14 +2,29 @@ package oclib
import (
"encoding/json"
"slices"
w "cloud.o-forge.io/core/oc-lib/models/resources/workflow"
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/resources/workflow/graph"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type AbstractWorkflow struct {
resources.ResourceSet
Graph *graph.Graph `bson:"graph,omitempty" json:"graph,omitempty"`
Schedule *WorkflowSchedule `bson:"schedule,omitempty" json:"schedule,omitempty"`
}
func (w *AbstractWorkflow) isDCLink(link graph.GraphLink) bool {
if slices.Contains(w.Datacenters, link.Destination.ID) || slices.Contains(w.Datacenters, link.Source.ID) {
return true
}
return false
}
type Workflow struct {
utils.AbstractObject
w.AbstractWorkflow
AbstractWorkflow
}
func (d *Workflow) GetName() string {

View File

@ -3,13 +3,14 @@ package workspace
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/google/uuid"
)
type Workspace struct {
utils.AbstractObject
utils.ResourceSet
resources.ResourceSet
Active bool `json:"active" bson:"active" default:"false"`
}

View File

@ -2,6 +2,11 @@ package workspace
import (
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/resources/data"
"cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
"cloud.o-forge.io/core/oc-lib/models/resources/processing"
"cloud.o-forge.io/core/oc-lib/models/resources/storage"
w "cloud.o-forge.io/core/oc-lib/models/resources/workflow"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
@ -14,11 +19,23 @@ func (wfa *WorkspaceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, er
}
func (wfa *WorkspaceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
d := set.(*Workspace)
d.DataResources = nil
d.DatacenterResources = nil
d.StorageResources = nil
d.ProcessingResources = nil
d.WorkflowResources = nil
return wfa.GenericUpdateOne(set, id, wfa, &Workspace{})
}
func (wfa *WorkspaceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
d := data.(*Workspace)
d.DataResources = nil
d.DatacenterResources = nil
d.StorageResources = nil
d.ProcessingResources = nil
d.WorkflowResources = nil
return wfa.GenericStoreOne(d, wfa)
}
func (wfa *WorkspaceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
@ -38,6 +55,51 @@ func (wfa *WorkspaceMongoAccessor) LoadOne(id string) (utils.DBObject, int, erro
obj = &Workspace{Active: true}
wfa.UpdateOne(obj, id)
if workflow.Datas != nil && len(workflow.Datas) > 0 {
dataAccessor := &data.DataMongoAccessor{}
for _, id := range workflow.Datas {
d, _, e := dataAccessor.LoadOne(id)
if e == nil {
workflow.DataResources = append(workflow.DataResources, d.(*data.DataResource))
}
}
}
if workflow.Datacenters != nil && len(workflow.Datacenters) > 0 {
dataAccessor := &datacenter.DatacenterMongoAccessor{}
for _, id := range workflow.Datacenters {
d, _, e := dataAccessor.LoadOne(id)
if e == nil {
workflow.DatacenterResources = append(workflow.DatacenterResources, d.(*datacenter.DatacenterResource))
}
}
}
if workflow.Storages != nil && len(workflow.Storages) > 0 {
dataAccessor := &storage.StorageMongoAccessor{}
for _, id := range workflow.Storages {
d, _, e := dataAccessor.LoadOne(id)
if e == nil {
workflow.StorageResources = append(workflow.StorageResources, d.(*storage.StorageResource))
}
}
}
if workflow.Processings != nil && len(workflow.Processings) > 0 {
dataAccessor := &processing.ProcessingMongoAccessor{}
for _, id := range workflow.Processings {
d, _, e := dataAccessor.LoadOne(id)
if e == nil {
workflow.ProcessingResources = append(workflow.ProcessingResources, d.(*processing.ProcessingResource))
}
}
}
if workflow.Workflows != nil && len(workflow.Workflows) > 0 {
dataAccessor := &w.WorkflowResourceMongoAccessor{}
for _, id := range workflow.Workflows {
d, _, e := dataAccessor.LoadOne(id)
if e == nil {
workflow.WorkflowResources = append(workflow.WorkflowResources, d.(*w.WorkflowResource))
}
}
}
return &workflow, 200, nil
}