access func

This commit is contained in:
mr 2024-07-18 14:39:54 +02:00
parent 7f1f85c00e
commit 4fab0cb153
9 changed files with 93 additions and 21 deletions

View File

@ -3,13 +3,33 @@ package oclib
import (
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/utils"
)
type LibData resources.ResourceType
const (
WORKFLOW LibData = 5
)
func Init() {
mongo.MONGOService.Init(models.GetModelsNames(), GetConfig())
}
func Model(name string) utils.DBObject {
return models.Model(name)
func LoadOne(name string, driver utils.Driver, id string) interface{} {
return models.Model(name).GetAccessor(driver).LoadOne(id)
}
func UpdateOne(name string, driver utils.Driver, set map[string]interface{}, id string) interface{} {
return models.Model(name).GetAccessor(driver).UpdateOne(set, id)
}
func DeleteOne(name string, driver utils.Driver, id string) interface{} {
return models.Model(name).GetAccessor(driver).DeleteOne(id)
}
func StoreOne(name string, driver utils.Driver, object map[string]interface{}) interface{} {
model := models.Model(name)
return model.GetAccessor(driver).StoreOne(model.Deserialize(object))
}

View File

@ -11,18 +11,18 @@ import (
w "cloud.o-forge.io/core/oc-lib/models/workflow"
)
var models = map[string]utils.DBObject{
w.WORKFLOW: &w.Workflow{},
r.ToString(r.DATA): &d.Data{},
r.ToString(r.DATACENTER): &dc.Datacenter{},
r.ToString(r.STORAGE): &s.Storage{},
r.ToString(r.PROCESSING): &p.Processing{},
var models = map[string]func() utils.DBObject{
w.WORKFLOW: func() utils.DBObject { return &w.Workflow{} },
r.ToString(r.DATA): func() utils.DBObject { return &d.Data{} },
r.ToString(r.DATACENTER): func() utils.DBObject { return &dc.Datacenter{} },
r.ToString(r.STORAGE): func() utils.DBObject { return &s.Storage{} },
r.ToString(r.PROCESSING): func() utils.DBObject { return &p.Processing{} },
}
func Model(model string) utils.DBObject {
log := logs.CreateLogger("oclib", "")
if _, ok := models[model]; ok {
return models[model]
return models[model]()
}
log.Error().Msg("Can't find model " + model + ".")
return nil

View File

@ -1,8 +1,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"
tool "cloud.o-forge.io/core/oc-lib/utils"
)
type Data struct {
@ -12,14 +15,23 @@ type Data struct {
Example string `json:"example" bson:"example" required:"true" validate:"required" description:"base64 encoded data"`
}
func (dma *Data) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (d *Data) GetType() resources.ResourceType {
return resources.DATA
}
func (d *Data) GetAccessor(driver utils.Driver) utils.Accessor {
func (d *Data) GetAccessor(driver tool.Driver) utils.Accessor {
var data utils.Accessor
switch driver {
case utils.MONGO:
case tool.MONGO:
data = &DataMongoAccessor{}
default:
data = &DataMongoAccessor{}

View File

@ -3,6 +3,7 @@ package datacenter
import (
resources "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
tool "cloud.o-forge.io/core/oc-lib/utils"
)
type Datacenter struct {
@ -39,10 +40,10 @@ func (d *Datacenter) GetType() resources.ResourceType {
return resources.DATACENTER
}
func (d *Datacenter) GetAccessor(driver utils.Driver) utils.Accessor {
func (d *Datacenter) GetAccessor(driver tool.Driver) utils.Accessor {
var data utils.Accessor
switch driver {
case utils.MONGO:
case tool.MONGO:
data = &DatacenterMongoAccessor{}
default:
data = &DatacenterMongoAccessor{}

View File

@ -1,8 +1,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"
tool "cloud.o-forge.io/core/oc-lib/utils"
)
type Processing struct {
@ -29,14 +32,23 @@ type ExecutionRequirementsModel struct {
DiskIO string `bson:"disk_io,omitempty" json:"disk_io,omitempty"`
}
func (dma *Processing) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (p *Processing) GetType() resources.ResourceType {
return resources.PROCESSING
}
func (d *Processing) GetAccessor(driver utils.Driver) utils.Accessor {
func (d *Processing) GetAccessor(driver tool.Driver) utils.Accessor {
var data utils.Accessor
switch driver {
case utils.MONGO:
case tool.MONGO:
data = &ProcessingMongoAccessor{}
default:
data = &ProcessingMongoAccessor{}

View File

@ -1,8 +1,11 @@
package storage
import (
"encoding/json"
resources "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
tool "cloud.o-forge.io/core/oc-lib/utils"
)
type URL struct {
@ -22,14 +25,23 @@ type Storage struct {
BookingPrice uint `bson:"booking_price,omitempty" json:"booking_price,omitempty"`
}
func (dma *Storage) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (s *Storage) GetType() resources.ResourceType {
return resources.STORAGE
}
func (d *Storage) GetAccessor(driver utils.Driver) utils.Accessor {
func (d *Storage) GetAccessor(driver tool.Driver) utils.Accessor {
var data utils.Accessor
switch driver {
case utils.MONGO:
case tool.MONGO:
data = &StorageMongoAccessor{}
default:
data = &StorageMongoAccessor{}

View File

@ -1,8 +1,11 @@
package utils
import "cloud.o-forge.io/core/oc-lib/utils"
type DBObject interface {
GetName() string
GetAccessor(driver Driver) Accessor
Deserialize(j map[string]interface{}) DBObject
GetAccessor(driver utils.Driver) Accessor
}
type Accessor interface {

View File

@ -1,6 +1,8 @@
package oclib
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/resources/data"
"cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
@ -8,6 +10,7 @@ import (
"cloud.o-forge.io/core/oc-lib/models/resources/storage"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/models/workflow/graph"
tool "cloud.o-forge.io/core/oc-lib/utils"
)
const WORKFLOW = "workflow"
@ -22,10 +25,10 @@ type Workflow struct {
Schedule WorkflowSchedule `bson:"schedule,omitempty" json:"schedule,omitempty"`
}
func (d *Workflow) GetAccessor(driver utils.Driver) utils.Accessor {
func (d *Workflow) GetAccessor(driver tool.Driver) utils.Accessor {
var data utils.Accessor
switch driver {
case utils.MONGO:
case tool.MONGO:
data = &WorkflowMongoAccessor{}
default:
data = &WorkflowMongoAccessor{}
@ -34,6 +37,15 @@ func (d *Workflow) GetAccessor(driver utils.Driver) utils.Accessor {
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 (w *Workflow) isDCLink(link graph.GraphLink) bool {
if _, exists := w.Datacenters[link.Destination.ID]; exists {
return true