resource as resource named

This commit is contained in:
mr 2024-07-19 10:54:58 +02:00
parent 218714683b
commit 2a9a784ec1
28 changed files with 381 additions and 284 deletions

View File

@ -7,15 +7,15 @@ abstract Ressource {
+icon: string +icon: string
+description: string +description: string
+graphic: GraphicElement +graphic: GraphicElement
+element: Data/Processing/Storage/Workflow/Datacenter +element: DataResource/ProcessingResource/StorageResource/Workflow/DatacenterResource
} }
class Data { class DataResource {
+UUID: int +UUID: int
+name: string +name: string
} }
class Processing { class ProcessingResource {
+UUID: int +UUID: int
+name: string +name: string
+container: string +container: string
@ -24,14 +24,14 @@ class Processing {
} }
class Storage { class StorageResource {
+UUID: int +UUID: int
+name: string +name: string
+url: string +url: string
+capacity: int +capacity: int
} }
class Datacenter { class DatacenterResource {
+UUID: int +UUID: int
+name: string +name: string
@ -96,7 +96,7 @@ class UserWorkflows {
class DatacenterWorkflows { class DatacenterWorkflows {
+UUID: int +UUID: int
+datacenter: Datacenter +datacenter: DatacenterResource
+workflows: Workflow[] +workflows: Workflow[]
} }
@ -112,10 +112,10 @@ UserWorkflows "1" o-- "0..*" Workflow
DatacenterWorkflows "1" o-- "0..*" Workflow DatacenterWorkflows "1" o-- "0..*" Workflow
Ressource <|-- Data Ressource <|-- DataResource
Ressource <|-- Processing Ressource <|-- ProcessingResource
Ressource <|-- Storage Ressource <|-- StorageResource
Ressource <|-- Datacenter Ressource <|-- DatacenterResource
Ressource <|-- Workflow Ressource <|-- Workflow
ResourceSet "1" o-- "0..*" Ressource ResourceSet "1" o-- "0..*" Ressource

View File

@ -4,7 +4,6 @@ import (
"cloud.o-forge.io/core/oc-lib/dbs/mongo" "cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs" "cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/models" "cloud.o-forge.io/core/oc-lib/models"
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/rs/zerolog" "github.com/rs/zerolog"
) )
@ -13,11 +12,12 @@ type LibDataEnum int
const ( const (
INVALID LibDataEnum = iota INVALID LibDataEnum = iota
DATA = resources.DATA DATA_RESOURCE = utils.DATA_RESOURCE
PROCESSING = resources.PROCESSING PROCESSING_RESOURCE = utils.PROCESSING_RESOURCE
STORAGE = resources.STORAGE STORAGE_RESOURCE = utils.STORAGE_RESOURCE
DATACENTER = resources.DATACENTER DATACENTER_RESOURCE = utils.DATACENTER_RESOURCE
WORKFLOW = resources.WORKFLOW WORKFLOW_RESOURCE = utils.WORKFLOW_RESOURCE
WORKFLOW = utils.WORKFLOW
) )
func (d LibDataEnum) EnumIndex() int { func (d LibDataEnum) EnumIndex() int {
@ -25,7 +25,7 @@ func (d LibDataEnum) EnumIndex() int {
} }
type LibData struct { type LibData struct {
Data utils.DBObject `bson:"data" json:"data"` DataResource utils.DBObject `bson:"data" json:"data"`
Err error `bson:"error" json:"error"` Err error `bson:"error" json:"error"`
} }
@ -41,23 +41,23 @@ func GetLogger() zerolog.Logger {
func LoadOne(collection LibDataEnum, id string) LibData { func LoadOne(collection LibDataEnum, id string) LibData {
d, err := models.Model(collection.EnumIndex()).GetAccessor().LoadOne(id) d, err := models.Model(collection.EnumIndex()).GetAccessor().LoadOne(id)
return LibData{Data: d, Err: err} return LibData{DataResource: d, Err: err}
} }
func UpdateOne(collection LibDataEnum, set map[string]interface{}, id string) LibData { func UpdateOne(collection LibDataEnum, set map[string]interface{}, id string) LibData {
model := models.Model(collection.EnumIndex()) model := models.Model(collection.EnumIndex())
set = model.Deserialize(set).Serialize() set = model.Deserialize(set).Serialize()
d, err := model.GetAccessor().UpdateOne(set, id) d, err := model.GetAccessor().UpdateOne(set, id)
return LibData{Data: d, Err: err} return LibData{DataResource: d, Err: err}
} }
func DeleteOne(collection LibDataEnum, id string) LibData { func DeleteOne(collection LibDataEnum, id string) LibData {
d, err := models.Model(collection.EnumIndex()).GetAccessor().DeleteOne(id) d, err := models.Model(collection.EnumIndex()).GetAccessor().DeleteOne(id)
return LibData{Data: d, Err: err} return LibData{DataResource: d, Err: err}
} }
func StoreOne(collection LibDataEnum, object map[string]interface{}) LibData { func StoreOne(collection LibDataEnum, object map[string]interface{}) LibData {
model := models.Model(collection.EnumIndex()) model := models.Model(collection.EnumIndex())
d, err := model.GetAccessor().StoreOne(model.Deserialize(object)) d, err := model.GetAccessor().StoreOne(model.Deserialize(object))
return LibData{Data: d, Err: err} return LibData{DataResource: d, Err: err}
} }

1
go.mod
View File

@ -23,6 +23,7 @@ require (
github.com/go-playground/validator/v10 v10.22.0 // indirect github.com/go-playground/validator/v10 v10.22.0 // indirect
github.com/go-stack/stack v1.8.1 // indirect github.com/go-stack/stack v1.8.1 // indirect
github.com/golang/snappy v0.0.4 // indirect github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect github.com/klauspost/compress v1.17.9 // indirect
github.com/leodido/go-urn v1.4.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect

2
go.sum
View File

@ -50,6 +50,8 @@ github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=

View File

@ -2,29 +2,31 @@ package models
import ( import (
"cloud.o-forge.io/core/oc-lib/logs" "cloud.o-forge.io/core/oc-lib/logs"
r "cloud.o-forge.io/core/oc-lib/models/resources"
d "cloud.o-forge.io/core/oc-lib/models/resources/data" d "cloud.o-forge.io/core/oc-lib/models/resources/data"
dc "cloud.o-forge.io/core/oc-lib/models/resources/datacenter" dc "cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
p "cloud.o-forge.io/core/oc-lib/models/resources/processing" p "cloud.o-forge.io/core/oc-lib/models/resources/processing"
s "cloud.o-forge.io/core/oc-lib/models/resources/storage" s "cloud.o-forge.io/core/oc-lib/models/resources/storage"
w "cloud.o-forge.io/core/oc-lib/models/resources/workflow" w "cloud.o-forge.io/core/oc-lib/models/resources/workflow"
"cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/models/utils"
w2 "cloud.o-forge.io/core/oc-lib/models/workflow"
) )
var models = map[string]func() utils.DBObject{ var models = map[string]func() utils.DBObject{
r.WORKFLOW.String(): func() utils.DBObject { return &w.Workflow{} }, utils.WORKFLOW_RESOURCE.String(): func() utils.DBObject { return &w.WorkflowResource{} },
r.DATA.String(): func() utils.DBObject { return &d.Data{} }, utils.DATA_RESOURCE.String(): func() utils.DBObject { return &d.DataResource{} },
r.DATACENTER.String(): func() utils.DBObject { return &dc.Datacenter{} }, utils.DATACENTER_RESOURCE.String(): func() utils.DBObject { return &dc.DatacenterResource{} },
r.STORAGE.String(): func() utils.DBObject { return &s.Storage{} }, utils.STORAGE_RESOURCE.String(): func() utils.DBObject { return &s.StorageResource{} },
r.PROCESSING.String(): func() utils.DBObject { return &p.Processing{} }, utils.PROCESSING_RESOURCE.String(): func() utils.DBObject { return &p.ProcessingResource{} },
utils.WORKFLOW.String(): func() utils.DBObject { return &w2.Workflow{} },
} }
func Model(model int) utils.DBObject { func Model(model int) utils.DBObject {
log := logs.GetLogger() log := logs.GetLogger()
if _, ok := models[r.FromInt(model)]; ok { if _, ok := models[utils.FromInt(model)]; ok {
return models[r.FromInt(model)]() return models[utils.FromInt(model)]()
} }
log.Error().Msg("Can't find model " + r.FromInt(model) + ".") log.Error().Msg("Can't find model " + utils.FromInt(model) + ".")
return nil return nil
} }

View File

@ -7,14 +7,14 @@ import (
"cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/models/utils"
) )
type Data struct { type DataResource struct {
resources.AbstractResource resources.AbstractResource
Protocols []string `json:"protocol,omitempty" bson:"protocol,omitempty"` //TODO Enum type Protocols []string `json:"protocol,omitempty" bson:"protocol,omitempty"` //TODO Enum type
DataType string `json:"datatype" required:"true" bson:"datatype"` DataType string `json:"datatype" required:"true" bson:"datatype"`
Example string `json:"example" bson:"example" required:"true" validate:"required" description:"base64 encoded data"` Example string `json:"example" bson:"example" required:"true" validate:"required" description:"base64 encoded data"`
} }
func (dma *Data) Deserialize(j map[string]interface{}) utils.DBObject { func (dma *DataResource) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j) b, err := json.Marshal(j)
if err != nil { if err != nil {
return nil return nil
@ -23,7 +23,7 @@ func (dma *Data) Deserialize(j map[string]interface{}) utils.DBObject {
return dma return dma
} }
func (dma *Data) Serialize() map[string]interface{} { func (dma *DataResource) Serialize() map[string]interface{} {
var m map[string]interface{} var m map[string]interface{}
b, err := json.Marshal(dma) b, err := json.Marshal(dma)
if err != nil { if err != nil {
@ -33,12 +33,8 @@ func (dma *Data) Serialize() map[string]interface{} {
return m return m
} }
func (d *Data) GetType() resources.ResourceType { func (d *DataResource) GetAccessor() utils.Accessor {
return resources.DATA
}
func (d *Data) GetAccessor() utils.Accessor {
data := &DataMongoAccessor{} data := &DataMongoAccessor{}
data.SetLogger(resources.DATA) data.SetLogger(utils.DATACENTER_RESOURCE)
return data return data
} }

View File

@ -18,20 +18,11 @@ func (dma *DataMongoAccessor) UpdateOne(set map[string]interface{}, id string) (
} }
func (dma *DataMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) { func (dma *DataMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
err := utils.Validate.Struct(data) return dma.GenericStoreOne(data, dma)
if err != nil {
return nil, err
}
id, err := mongo.MONGOService.StoreOne(data.(*Data), dma.GetType())
if err != nil {
dma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return nil, err
}
return dma.LoadOne(id)
} }
func (dma *DataMongoAccessor) LoadOne(id string) (utils.DBObject, error) { func (dma *DataMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
var data Data var data DataResource
res_mongo, err := mongo.MONGOService.LoadOne(id, dma.GetType()) res_mongo, err := mongo.MONGOService.LoadOne(id, dma.GetType())
if err != nil { if err != nil {
dma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error()) dma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())

View File

@ -4,15 +4,15 @@ import (
"testing" "testing"
resources "cloud.o-forge.io/core/oc-lib/models/resources" resources "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestStoreOneData(t *testing.T) { func TestStoreOneData(t *testing.T) {
d := Data{DataType: "jpeg", Example: "123456", d := DataResource{DataType: "jpeg", Example: "123456",
AbstractResource: resources.AbstractResource{ AbstractResource: resources.AbstractResource{
Uuid: "123", AbstractObject: utils.AbstractObject{Name: "testData"},
Name: "testData",
Description: "Lorem Ipsum", Description: "Lorem Ipsum",
Logo: "azerty.com", Logo: "azerty.com",
Owner: "toto", Owner: "toto",
@ -28,10 +28,9 @@ func TestStoreOneData(t *testing.T) {
} }
func TestLoadOneDate(t *testing.T) { func TestLoadOneDate(t *testing.T) {
d := Data{DataType: "jpeg", Example: "123456", d := DataResource{DataType: "jpeg", Example: "123456",
AbstractResource: resources.AbstractResource{ AbstractResource: resources.AbstractResource{
Uuid: "123", AbstractObject: utils.AbstractObject{Name: "testData"},
Name: "testData",
Description: "Lorem Ipsum", Description: "Lorem Ipsum",
Logo: "azerty.com", Logo: "azerty.com",
Owner: "toto", Owner: "toto",

View File

@ -7,7 +7,7 @@ import (
"cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/models/utils"
) )
type Datacenter struct { type DatacenterResource struct {
resources.AbstractResource resources.AbstractResource
Owner string `bson:"owner" json:"owner" required:"true"` Owner string `bson:"owner" json:"owner" required:"true"`
BookingPrice int `bson:"booking_price" json:"booking_price" required:"true"` BookingPrice int `bson:"booking_price" json:"booking_price" required:"true"`
@ -37,7 +37,7 @@ type DatacenterGpuModel struct {
TensorCores uint `bson:"tensor_cores,omitempty" json:"tensor_cores,omitempty"` TensorCores uint `bson:"tensor_cores,omitempty" json:"tensor_cores,omitempty"`
} }
func (dma *Datacenter) Deserialize(j map[string]interface{}) utils.DBObject { func (dma *DatacenterResource) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j) b, err := json.Marshal(j)
if err != nil { if err != nil {
return nil return nil
@ -46,7 +46,7 @@ func (dma *Datacenter) Deserialize(j map[string]interface{}) utils.DBObject {
return dma return dma
} }
func (dma *Datacenter) Serialize() map[string]interface{} { func (dma *DatacenterResource) Serialize() map[string]interface{} {
var m map[string]interface{} var m map[string]interface{}
b, err := json.Marshal(dma) b, err := json.Marshal(dma)
if err != nil { if err != nil {
@ -56,12 +56,8 @@ func (dma *Datacenter) Serialize() map[string]interface{} {
return m return m
} }
func (d *Datacenter) GetType() resources.ResourceType { func (d *DatacenterResource) GetAccessor() utils.Accessor {
return resources.DATACENTER
}
func (d *Datacenter) GetAccessor() utils.Accessor {
data := &DatacenterMongoAccessor{} data := &DatacenterMongoAccessor{}
data.SetLogger(resources.DATACENTER) data.SetLogger(utils.DATACENTER_RESOURCE)
return data return data
} }

View File

@ -18,20 +18,11 @@ func (dca *DatacenterMongoAccessor) UpdateOne(set map[string]interface{}, id str
} }
func (dca *DatacenterMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) { func (dca *DatacenterMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
err := utils.Validate.Struct(data) return dca.GenericStoreOne(data, dca)
if err != nil {
return nil, err
}
id, err := mongo.MONGOService.StoreOne(data.(*Datacenter), dca.GetType())
if err != nil {
dca.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return nil, err
}
return dca.LoadOne(id)
} }
func (dca *DatacenterMongoAccessor) LoadOne(id string) (utils.DBObject, error) { func (dca *DatacenterMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
var datacenter Datacenter var datacenter DatacenterResource
res_mongo, err := mongo.MONGOService.LoadOne(id, dca.GetType()) res_mongo, err := mongo.MONGOService.LoadOne(id, dca.GetType())
if err != nil { if err != nil {

View File

@ -4,15 +4,15 @@ import (
"testing" "testing"
resources "cloud.o-forge.io/core/oc-lib/models/resources" resources "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestStoreOneDatacenter(t *testing.T) { func TestStoreOneDatacenter(t *testing.T) {
dc := Datacenter{Owner: "toto", BookingPrice: 123, dc := DatacenterResource{Owner: "toto", BookingPrice: 123,
AbstractResource: resources.AbstractResource{ AbstractResource: resources.AbstractResource{
Uuid: "123", AbstractObject: utils.AbstractObject{Name: "testDatacenter"},
Name: "testDatacenter",
Description: "Lorem Ipsum", Description: "Lorem Ipsum",
Logo: "azerty.com", Logo: "azerty.com",
Owner: "toto", Owner: "toto",
@ -28,10 +28,9 @@ func TestStoreOneDatacenter(t *testing.T) {
} }
func TestLoadOneDatacenter(t *testing.T) { func TestLoadOneDatacenter(t *testing.T) {
dc := Datacenter{Owner: "toto", BookingPrice: 123, dc := DatacenterResource{Owner: "toto", BookingPrice: 123,
AbstractResource: resources.AbstractResource{ AbstractResource: resources.AbstractResource{
Uuid: "123", AbstractObject: utils.AbstractObject{Name: "testDatacenter"},
Name: "testDatacenter",
Description: "Lorem Ipsum", Description: "Lorem Ipsum",
Logo: "azerty.com", Logo: "azerty.com",
Owner: "toto", Owner: "toto",

View File

@ -7,7 +7,7 @@ import (
"cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/models/utils"
) )
type Processing struct { type ProcessingResource struct {
resources.AbstractResource resources.AbstractResource
Container string `bson:"container,omitempty" json:"container,omitempty"` // We could create a specific class for container, that could check if the name exists/is available Container string `bson:"container,omitempty" json:"container,omitempty"` // We could create a specific class for container, that could check if the name exists/is available
Repository string `bson:"repository,omitempty" json:"repository,omitempty"` // Indicate where to find the container image => Could add a struct handling authentication to the repo Repository string `bson:"repository,omitempty" json:"repository,omitempty"` // Indicate where to find the container image => Could add a struct handling authentication to the repo
@ -31,7 +31,7 @@ type ExecutionRequirementsModel struct {
DiskIO string `bson:"disk_io,omitempty" json:"disk_io,omitempty"` DiskIO string `bson:"disk_io,omitempty" json:"disk_io,omitempty"`
} }
func (dma *Processing) Deserialize(j map[string]interface{}) utils.DBObject { func (dma *ProcessingResource) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j) b, err := json.Marshal(j)
if err != nil { if err != nil {
return nil return nil
@ -40,7 +40,7 @@ func (dma *Processing) Deserialize(j map[string]interface{}) utils.DBObject {
return dma return dma
} }
func (dma *Processing) Serialize() map[string]interface{} { func (dma *ProcessingResource) Serialize() map[string]interface{} {
var m map[string]interface{} var m map[string]interface{}
b, err := json.Marshal(dma) b, err := json.Marshal(dma)
if err != nil { if err != nil {
@ -50,12 +50,8 @@ func (dma *Processing) Serialize() map[string]interface{} {
return m return m
} }
func (p *Processing) GetType() resources.ResourceType { func (d *ProcessingResource) GetAccessor() utils.Accessor {
return resources.PROCESSING
}
func (d *Processing) GetAccessor() utils.Accessor {
data := &ProcessingMongoAccessor{} data := &ProcessingMongoAccessor{}
data.SetLogger(resources.PROCESSING) data.SetLogger(utils.PROCESSING_RESOURCE)
return data return data
} }

View File

@ -18,21 +18,12 @@ func (pma *ProcessingMongoAccessor) UpdateOne(set map[string]interface{}, id str
} }
func (pma *ProcessingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) { func (pma *ProcessingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
err := utils.Validate.Struct(data) return pma.GenericStoreOne(data, pma)
if err != nil {
return nil, err
}
id, err := mongo.MONGOService.StoreOne(data.(*Processing), pma.GetType())
if err != nil {
pma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return nil, err
}
return pma.LoadOne(id)
} }
func (pma *ProcessingMongoAccessor) LoadOne(id string) (utils.DBObject, error) { func (pma *ProcessingMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
var processing Processing var processing ProcessingResource
res_mongo, err := mongo.MONGOService.LoadOne(id, pma.GetType()) res_mongo, err := mongo.MONGOService.LoadOne(id, pma.GetType())
if err != nil { if err != nil {

View File

@ -4,15 +4,15 @@ import (
"testing" "testing"
resources "cloud.o-forge.io/core/oc-lib/models/resources" resources "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestStoreOneProcessing(t *testing.T) { func TestStoreOneProcessing(t *testing.T) {
p := Processing{Container: "totoCont", p := ProcessingResource{Container: "totoCont",
AbstractResource: resources.AbstractResource{ AbstractResource: resources.AbstractResource{
Uuid: "123", AbstractObject: utils.AbstractObject{Name: "testData"},
Name: "testData",
Description: "Lorem Ipsum", Description: "Lorem Ipsum",
Logo: "azerty.com", Logo: "azerty.com",
Owner: "toto", Owner: "toto",
@ -28,10 +28,9 @@ func TestStoreOneProcessing(t *testing.T) {
} }
func TestLoadOneProcessing(t *testing.T) { func TestLoadOneProcessing(t *testing.T) {
p := Processing{Container: "totoCont", p := ProcessingResource{Container: "totoCont",
AbstractResource: resources.AbstractResource{ AbstractResource: resources.AbstractResource{
Uuid: "123", AbstractObject: utils.AbstractObject{Name: "testData"},
Name: "testData",
Description: "Lorem Ipsum", Description: "Lorem Ipsum",
Logo: "azerty.com", Logo: "azerty.com",
Owner: "toto", Owner: "toto",

View File

@ -1,51 +1,21 @@
package resources package resources
import (
"cloud.o-forge.io/core/oc-lib/models/utils"
)
// AbstractResource is the struct containing all of the attributes commons to all ressources // 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 // 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 //http://www.inanzzz.com/index.php/post/wqbs/a-basic-usage-of-int-and-string-enum-types-in-golang
type ResourceType int
const (
INVALID ResourceType = iota
DATA
PROCESSING
STORAGE
DATACENTER
WORKFLOW
)
var str = [...]string{
"invalid",
"data",
"processing",
"storage",
"datacenter",
"workflow",
}
func FromInt(i int) string {
return str[i]
}
func (d ResourceType) String() string {
return str[d]
}
// EnumIndex - Creating common behavior-give the type a EnumIndex functio
func (d ResourceType) EnumIndex() int {
return int(d)
}
type Resource interface { type Resource interface {
GetType() ResourceType GetType() utils.DataType
} }
type AbstractResource struct { type AbstractResource struct {
Uuid string `json:"uuid" required:"true" bson:"uuid" validate:"required"` utils.AbstractObject
Name string `json:"name" required:"true" bson:"name" validate:"required"`
ShortDescription string `json:"short_description" required:"true" bson:"short_description" validate:"required"` ShortDescription string `json:"short_description" required:"true" bson:"short_description" validate:"required"`
Description string `json:"description,omitempty" bson:"description"` Description string `json:"description,omitempty" bson:"description"`
Logo string `json:"logo" required:"true" bson:"logo" validate:"required"` Logo string `json:"logo" required:"true" bson:"logo" validate:"required"`
@ -55,7 +25,7 @@ type AbstractResource struct {
} }
func (r *AbstractResource) GetID() string { func (r *AbstractResource) GetID() string {
return r.Uuid return r.UUID
} }
func (r *AbstractResource) GetName() string { func (r *AbstractResource) GetName() string {

View File

@ -12,7 +12,7 @@ type URL struct {
Path string `bson:"path" json:"path"` Path string `bson:"path" json:"path"`
} }
type Storage struct { type StorageResource struct {
resources.AbstractResource resources.AbstractResource
Capacity uint `bson:"capacity,omitempty" json:"capacity,omitempty"` Capacity uint `bson:"capacity,omitempty" json:"capacity,omitempty"`
@ -24,7 +24,7 @@ type Storage struct {
BookingPrice uint `bson:"booking_price,omitempty" json:"booking_price,omitempty"` BookingPrice uint `bson:"booking_price,omitempty" json:"booking_price,omitempty"`
} }
func (dma *Storage) Deserialize(j map[string]interface{}) utils.DBObject { func (dma *StorageResource) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j) b, err := json.Marshal(j)
if err != nil { if err != nil {
return nil return nil
@ -33,7 +33,7 @@ func (dma *Storage) Deserialize(j map[string]interface{}) utils.DBObject {
return dma return dma
} }
func (dma *Storage) Serialize() map[string]interface{} { func (dma *StorageResource) Serialize() map[string]interface{} {
var m map[string]interface{} var m map[string]interface{}
b, err := json.Marshal(dma) b, err := json.Marshal(dma)
if err != nil { if err != nil {
@ -43,8 +43,8 @@ func (dma *Storage) Serialize() map[string]interface{} {
return m return m
} }
func (d *Storage) GetAccessor() utils.Accessor { func (d *StorageResource) GetAccessor() utils.Accessor {
data := &StorageMongoAccessor{} data := &StorageMongoAccessor{}
data.SetLogger(resources.STORAGE) data.SetLogger(utils.STORAGE_RESOURCE)
return data return data
} }

View File

@ -18,21 +18,12 @@ func (sma *StorageMongoAccessor) UpdateOne(set map[string]interface{}, id string
} }
func (sma *StorageMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) { func (sma *StorageMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
err := utils.Validate.Struct(data) return sma.GenericStoreOne(data, sma)
if err != nil {
return nil, err
}
id, err := mongo.MONGOService.StoreOne(data.(*Storage), sma.GetType())
if err != nil {
sma.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return nil, err
}
return sma.LoadOne(id)
} }
func (sma *StorageMongoAccessor) LoadOne(id string) (utils.DBObject, error) { func (sma *StorageMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
var storage Storage var storage StorageResource
res_mongo, err := mongo.MONGOService.LoadOne(id, sma.GetType()) res_mongo, err := mongo.MONGOService.LoadOne(id, sma.GetType())
if err != nil { if err != nil {

View File

@ -4,15 +4,15 @@ import (
"testing" "testing"
resources "cloud.o-forge.io/core/oc-lib/models/resources" resources "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestStoreOneStorage(t *testing.T) { func TestStoreOneStorage(t *testing.T) {
s := Storage{Capacity: 123, Url: URL{Protocol: "http", Path: "azerty.fr"}, s := StorageResource{Capacity: 123, Url: URL{Protocol: "http", Path: "azerty.fr"},
AbstractResource: resources.AbstractResource{ AbstractResource: resources.AbstractResource{
Uuid: "123", AbstractObject: utils.AbstractObject{Name: "testData"},
Name: "testData",
Description: "Lorem Ipsum", Description: "Lorem Ipsum",
Logo: "azerty.com", Logo: "azerty.com",
Owner: "toto", Owner: "toto",
@ -28,10 +28,9 @@ func TestStoreOneStorage(t *testing.T) {
} }
func TestLoadOneStorage(t *testing.T) { func TestLoadOneStorage(t *testing.T) {
s := Storage{Capacity: 123, Url: URL{Protocol: "http", Path: "azerty.fr"}, s := StorageResource{Capacity: 123, Url: URL{Protocol: "http", Path: "azerty.fr"},
AbstractResource: resources.AbstractResource{ AbstractResource: resources.AbstractResource{
Uuid: "123", AbstractObject: utils.AbstractObject{Name: "testData"},
Name: "testData",
Description: "Lorem Ipsum", Description: "Lorem Ipsum",
Logo: "azerty.com", Logo: "azerty.com",
Owner: "toto", Owner: "toto",

View File

@ -12,42 +12,17 @@ import (
"cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/models/utils"
) )
type Workflow struct { type AbstractWorkflow struct {
resources.AbstractResource
Graph graph.Graph `bson:"graph,omitempty" json:"graph,omitempty"` Graph graph.Graph `bson:"graph,omitempty" json:"graph,omitempty"`
Datas map[string]data.Data `bson:"datas,omitempty" json:"datas,omitempty"` Datas map[string]data.DataResource `bson:"datas,omitempty" json:"datas,omitempty"`
Storages map[string]storage.Storage `bson:"storages,omitempty" json:"storages,omitempty"` Storages map[string]storage.StorageResource `bson:"storages,omitempty" json:"storages,omitempty"`
Processing map[string]processing.Processing `bson:"processing,omitempty" json:"processing,omitempty"` ProcessingResource map[string]processing.ProcessingResource `bson:"processing,omitempty" json:"processing,omitempty"`
Datacenters map[string]datacenter.Datacenter `bson:"datacenters,omitempty" json:"datacenters,omitempty"` Datacenters map[string]datacenter.DatacenterResource `bson:"datacenters,omitempty" json:"datacenters,omitempty"`
Workflows map[string]WorkflowResource `bson:"workflows,omitempty" json:"workflows,omitempty"`
Schedule WorkflowSchedule `bson:"schedule,omitempty" json:"schedule,omitempty"` Schedule WorkflowSchedule `bson:"schedule,omitempty" json:"schedule,omitempty"`
} }
func (d *Workflow) GetAccessor() utils.Accessor { func (w *AbstractWorkflow) isDCLink(link graph.GraphLink) bool {
data := &WorkflowMongoAccessor{}
data.SetLogger(resources.WORKFLOW)
return data
}
func (dma *Workflow) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *Workflow) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return m
}
func (w *Workflow) isDCLink(link graph.GraphLink) bool {
if _, exists := w.Datacenters[link.Destination.ID]; exists { if _, exists := w.Datacenters[link.Destination.ID]; exists {
return true return true
} else if _, exists := w.Datacenters[link.Source.ID]; exists { } else if _, exists := w.Datacenters[link.Source.ID]; exists {
@ -56,3 +31,34 @@ func (w *Workflow) isDCLink(link graph.GraphLink) bool {
return false return false
} }
type WorkflowResource struct {
resources.AbstractResource
AbstractWorkflow
WorkflowID string `bson:"workflow_id,omitempty" json:"workflow_id,omitempty"`
}
func (d *WorkflowResource) GetAccessor() utils.Accessor {
data := &WorkflowResourceMongoAccessor{}
data.SetLogger(utils.WORKFLOW_RESOURCE)
return data
}
func (dma *WorkflowResource) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *WorkflowResource) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return m
}

View File

@ -5,34 +5,24 @@ import (
"cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/models/utils"
) )
type WorkflowMongoAccessor struct { type WorkflowResourceMongoAccessor struct {
utils.AbstractAccessor utils.AbstractAccessor
} }
func (wfa *WorkflowMongoAccessor) DeleteOne(id string) (utils.DBObject, error) { func (wfa *WorkflowResourceMongoAccessor) DeleteOne(id string) (utils.DBObject, error) {
return wfa.GenericDeleteOne(id, wfa) return wfa.GenericDeleteOne(id, wfa)
} }
func (wfa *WorkflowMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) { func (wfa *WorkflowResourceMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) {
return wfa.GenericUpdateOne(set, id, wfa) return wfa.GenericUpdateOne(set, id, wfa)
} }
func (wfa *WorkflowMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) { func (wfa *WorkflowResourceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
err := utils.Validate.Struct(data) return wfa.GenericStoreOne(data, wfa)
wfa.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
if err != nil {
return nil, err
}
id, err := mongo.MONGOService.StoreOne(data.(*Workflow), wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return nil, err
}
return wfa.LoadOne(id)
} }
func (wfa *WorkflowMongoAccessor) LoadOne(id string) (utils.DBObject, error) { func (wfa *WorkflowResourceMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
var workflow Workflow var workflow WorkflowResource
res_mongo, err := mongo.MONGOService.LoadOne(id, wfa.GetType()) res_mongo, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
if err != nil { if err != nil {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error()) wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())

View File

@ -4,14 +4,14 @@ import (
"testing" "testing"
"cloud.o-forge.io/core/oc-lib/models/resources" "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestStoreOneWorkflow(t *testing.T) { func TestStoreOneWorkflow(t *testing.T) {
w := Workflow{AbstractResource: resources.AbstractResource{ w := WorkflowResource{AbstractResource: resources.AbstractResource{
Uuid: "123", AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
Name: "testWorkflow",
Description: "Lorem Ipsum", Description: "Lorem Ipsum",
Logo: "azerty.com", Logo: "azerty.com",
Owner: "toto", Owner: "toto",
@ -20,16 +20,15 @@ func TestStoreOneWorkflow(t *testing.T) {
}, },
} }
wma := WorkflowMongoAccessor{} wma := WorkflowResourceMongoAccessor{}
id, _ := wma.StoreOne(&w) id, _ := wma.StoreOne(&w)
assert.NotEmpty(t, id) assert.NotEmpty(t, id)
} }
func TestLoadOneWorkflow(t *testing.T) { func TestLoadOneWorkflow(t *testing.T) {
w := Workflow{AbstractResource: resources.AbstractResource{ w := WorkflowResource{AbstractResource: resources.AbstractResource{
Uuid: "123", AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
Name: "testWorkflow",
Description: "Lorem Ipsum", Description: "Lorem Ipsum",
Logo: "azerty.com", Logo: "azerty.com",
Owner: "toto", Owner: "toto",
@ -38,7 +37,7 @@ func TestLoadOneWorkflow(t *testing.T) {
}, },
} }
wma := WorkflowMongoAccessor{} wma := WorkflowResourceMongoAccessor{}
new_w, _ := wma.StoreOne(&w) new_w, _ := wma.StoreOne(&w)
assert.Equal(t, w, new_w) assert.Equal(t, w, new_w)
} }

View File

@ -3,12 +3,21 @@ package utils
import ( import (
"cloud.o-forge.io/core/oc-lib/dbs/mongo" "cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs" "cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/models/resources"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
"github.com/google/uuid"
"github.com/rs/zerolog" "github.com/rs/zerolog"
) )
var Validate = validator.New(validator.WithRequiredStructEnabled()) var validate = validator.New(validator.WithRequiredStructEnabled())
type AbstractObject struct {
UUID string `json:"id" required:"true" bson:"_id" validate:"required"`
Name string `json:"name" required:"true" bson:"name" validate:"required"`
}
func (r *AbstractObject) GenerateID() {
r.UUID = uuid.New().String()
}
type AbstractAccessor struct { type AbstractAccessor struct {
Logger zerolog.Logger Logger zerolog.Logger
@ -19,10 +28,26 @@ func (dma *AbstractAccessor) GetType() string {
return dma.Type return dma.Type
} }
func (dma *AbstractAccessor) SetLogger(t resources.ResourceType) { func (dma *AbstractAccessor) SetLogger(t DataType) {
dma.Logger = logs.CreateLogger(t.String(), "") dma.Logger = logs.CreateLogger(t.String(), "")
dma.Type = t.String() dma.Type = t.String()
} }
func (wfa *AbstractAccessor) GenericStoreOne(data DBObject, accessor Accessor) (DBObject, error) {
err := validate.Struct(data)
wfa.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
if err != nil {
return nil, err
}
data.GenerateID()
id, err := mongo.MONGOService.StoreOne(data, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return nil, err
}
return accessor.LoadOne(id)
}
func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBObject, error) { func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBObject, error) {
res, err := accessor.LoadOne(id) res, err := accessor.LoadOne(id)
if err != nil { if err != nil {

36
models/utils/enums.go Normal file
View File

@ -0,0 +1,36 @@
package utils
type DataType int
const (
INVALID DataType = iota
DATA_RESOURCE
PROCESSING_RESOURCE
STORAGE_RESOURCE
DATACENTER_RESOURCE
WORKFLOW_RESOURCE
WORKFLOW
)
var str = [...]string{
"invalid",
"data_resource",
"processing_resource",
"storage_resource",
"datacenter_resource",
"workflow_resource",
"workflow",
}
func FromInt(i int) string {
return str[i]
}
func (d DataType) String() string {
return str[d]
}
// EnumIndex - Creating common behavior-give the type a EnumIndex functio
func (d DataType) EnumIndex() int {
return int(d)
}

View File

@ -1,8 +1,7 @@
package utils package utils
import "cloud.o-forge.io/core/oc-lib/models/resources"
type DBObject interface { type DBObject interface {
GenerateID()
GetName() string GetName() string
Deserialize(j map[string]interface{}) DBObject Deserialize(j map[string]interface{}) DBObject
Serialize() map[string]interface{} Serialize() map[string]interface{}
@ -10,7 +9,7 @@ type DBObject interface {
} }
type Accessor interface { type Accessor interface {
SetLogger(t resources.ResourceType) SetLogger(t DataType)
GetType() string GetType() string
LoadOne(id string) (DBObject, error) LoadOne(id string) (DBObject, error)
DeleteOne(id string) (DBObject, error) DeleteOne(id string) (DBObject, error)

View File

@ -0,0 +1,42 @@
package oclib
import (
"encoding/json"
w "cloud.o-forge.io/core/oc-lib/models/resources/workflow"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type Workflow struct {
utils.AbstractObject
w.AbstractWorkflow
}
func (d *Workflow) GetName() string {
return d.Name
}
func (d *Workflow) GetAccessor() utils.Accessor {
data := &WorkflowMongoAccessor{}
data.SetLogger(utils.WORKFLOW)
return data
}
func (dma *Workflow) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *Workflow) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return m
}

View File

@ -0,0 +1,33 @@
package oclib
import (
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type WorkflowMongoAccessor struct {
utils.AbstractAccessor
}
func (wfa *WorkflowMongoAccessor) DeleteOne(id string) (utils.DBObject, error) {
return wfa.GenericDeleteOne(id, wfa)
}
func (wfa *WorkflowMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, error) {
return wfa.GenericUpdateOne(set, id, wfa)
}
func (wfa *WorkflowMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, error) {
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *WorkflowMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
var workflow Workflow
res_mongo, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, err
}
res_mongo.Decode(&workflow)
return &workflow, nil
}

View File

@ -0,0 +1,15 @@
package oclib
import "time"
type WorkflowSchedule struct {
Id string `json:"id"`
Start time.Time
End time.Time
Cron string
}
func (ws *WorkflowSchedule) GetAllDates() (timetable []time.Time){
// Return all the execution time generated by the Cron
return
}

View File

@ -0,0 +1,29 @@
package oclib
import (
"testing"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert"
)
func TestStoreOneWorkflow(t *testing.T) {
w := Workflow{
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
}
wma := WorkflowMongoAccessor{}
id, _ := wma.StoreOne(&w)
assert.NotEmpty(t, id)
}
func TestLoadOneWorkflow(t *testing.T) {
w := Workflow{
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
}
wma := WorkflowMongoAccessor{}
new_w, _ := wma.StoreOne(&w)
assert.Equal(t, w, new_w)
}