From 4fab0cb153c8f9aff2f9eb30cbdb53d09a75228c Mon Sep 17 00:00:00 2001 From: mr Date: Thu, 18 Jul 2024 14:39:54 +0200 Subject: [PATCH] access func --- entrypoint.go | 26 ++++++++++++++++++++--- models/models.go | 14 ++++++------ models/resources/data/data.go | 16 ++++++++++++-- models/resources/datacenter/datacenter.go | 5 +++-- models/resources/processing/processing.go | 16 ++++++++++++-- models/resources/storage/storage.go | 16 ++++++++++++-- models/utils/interfaces.go | 5 ++++- models/workflow/workflow.go | 16 ++++++++++++-- {models/utils => utils}/enums.go | 0 9 files changed, 93 insertions(+), 21 deletions(-) rename {models/utils => utils}/enums.go (100%) diff --git a/entrypoint.go b/entrypoint.go index ce3be1e..d50b0fa 100644 --- a/entrypoint.go +++ b/entrypoint.go @@ -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)) } diff --git a/models/models.go b/models/models.go index ddcecfa..b6a1296 100644 --- a/models/models.go +++ b/models/models.go @@ -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 diff --git a/models/resources/data/data.go b/models/resources/data/data.go index 1c0fe39..9444aef 100644 --- a/models/resources/data/data.go +++ b/models/resources/data/data.go @@ -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{} diff --git a/models/resources/datacenter/datacenter.go b/models/resources/datacenter/datacenter.go index 67d5644..e2a1d0f 100644 --- a/models/resources/datacenter/datacenter.go +++ b/models/resources/datacenter/datacenter.go @@ -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{} diff --git a/models/resources/processing/processing.go b/models/resources/processing/processing.go index 4da2035..1df4cf0 100644 --- a/models/resources/processing/processing.go +++ b/models/resources/processing/processing.go @@ -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{} diff --git a/models/resources/storage/storage.go b/models/resources/storage/storage.go index 1a1a657..bd6f365 100644 --- a/models/resources/storage/storage.go +++ b/models/resources/storage/storage.go @@ -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{} diff --git a/models/utils/interfaces.go b/models/utils/interfaces.go index de92b26..bce57a4 100644 --- a/models/utils/interfaces.go +++ b/models/utils/interfaces.go @@ -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 { diff --git a/models/workflow/workflow.go b/models/workflow/workflow.go index 168ddb9..a78d684 100644 --- a/models/workflow/workflow.go +++ b/models/workflow/workflow.go @@ -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 diff --git a/models/utils/enums.go b/utils/enums.go similarity index 100% rename from models/utils/enums.go rename to utils/enums.go