230 lines
7.2 KiB
Go
230 lines
7.2 KiB
Go
package oclib
|
|
|
|
import (
|
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
|
"cloud.o-forge.io/core/oc-lib/logs"
|
|
"cloud.o-forge.io/core/oc-lib/models"
|
|
"cloud.o-forge.io/core/oc-lib/models/peer"
|
|
"cloud.o-forge.io/core/oc-lib/models/resource_model"
|
|
"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"
|
|
w2 "cloud.o-forge.io/core/oc-lib/models/workflow"
|
|
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
|
|
"cloud.o-forge.io/core/oc-lib/models/workspace"
|
|
shared_workspace "cloud.o-forge.io/core/oc-lib/models/workspace/shared"
|
|
"cloud.o-forge.io/core/oc-lib/models/workspace/shared/rules/rule"
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
type Filters = dbs.Filters
|
|
|
|
type LibDataEnum int
|
|
|
|
const (
|
|
INVALID LibDataEnum = iota
|
|
DATA_RESOURCE = utils.DATA_RESOURCE
|
|
PROCESSING_RESOURCE = utils.PROCESSING_RESOURCE
|
|
STORAGE_RESOURCE = utils.STORAGE_RESOURCE
|
|
DATACENTER_RESOURCE = utils.DATACENTER_RESOURCE
|
|
WORKFLOW_RESOURCE = utils.WORKFLOW_RESOURCE
|
|
WORKFLOW = utils.WORKFLOW
|
|
WORKSPACE = utils.WORKSPACE
|
|
WORKFLOW_EXECUTION = utils.WORKFLOW_EXECUTION
|
|
PEER = utils.PEER
|
|
SHARED_WORKSPACE = utils.SHARED_WORKSPACE
|
|
RULE = utils.RULE
|
|
)
|
|
|
|
func (d LibDataEnum) String() string {
|
|
return utils.Str[d]
|
|
}
|
|
|
|
func (d LibDataEnum) EnumIndex() int {
|
|
return int(d)
|
|
}
|
|
|
|
type LibDataShallow struct {
|
|
Data []utils.ShallowDBObject `bson:"data" json:"data"`
|
|
Code int `bson:"code" json:"code"`
|
|
Err string `bson:"error" json:"error"`
|
|
}
|
|
|
|
type LibData struct {
|
|
Data utils.DBObject `bson:"data" json:"data"`
|
|
Code int `bson:"code" json:"code"`
|
|
Err string `bson:"error" json:"error"`
|
|
}
|
|
|
|
func Init(appName string) {
|
|
logs.SetAppName(appName)
|
|
logs.SetLogger(logs.CreateLogger("main", ""))
|
|
mongo.MONGOService.Init(models.GetModelsNames(), GetConfig())
|
|
accessor := (&resource_model.ResourceModel{}).GetAccessor()
|
|
for _, model := range []string{utils.DATA_RESOURCE.String(), utils.PROCESSING_RESOURCE.String(), utils.STORAGE_RESOURCE.String(), utils.DATACENTER_RESOURCE.String(), utils.WORKFLOW_RESOURCE.String()} {
|
|
data, code, _ := accessor.Search(nil, model)
|
|
if code == 404 || len(data) == 0 {
|
|
m := map[string]resource_model.Model{}
|
|
if model == utils.PROCESSING_RESOURCE.String() {
|
|
m["image"] = resource_model.Model{
|
|
Type: "string",
|
|
ReadOnly: false,
|
|
}
|
|
m["command"] = resource_model.Model{
|
|
Type: "string",
|
|
ReadOnly: false,
|
|
}
|
|
m["args"] = resource_model.Model{
|
|
Type: "string",
|
|
ReadOnly: false,
|
|
}
|
|
}
|
|
accessor.StoreOne(&resource_model.ResourceModel{
|
|
ResourceType: model,
|
|
Model: m,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func GetLogger() zerolog.Logger {
|
|
return logs.GetLogger()
|
|
}
|
|
|
|
func Search(filters *dbs.Filters, word string, collection LibDataEnum) LibDataShallow {
|
|
d, code, err := models.Model(collection.EnumIndex()).GetAccessor().Search(filters, word)
|
|
if err != nil {
|
|
return LibDataShallow{Data: d, Code: code, Err: err.Error()}
|
|
}
|
|
return LibDataShallow{Data: d, Code: code}
|
|
}
|
|
|
|
func LoadAll(collection LibDataEnum) LibDataShallow {
|
|
d, code, err := models.Model(collection.EnumIndex()).GetAccessor().LoadAll()
|
|
if err != nil {
|
|
return LibDataShallow{Data: d, Code: code, Err: err.Error()}
|
|
}
|
|
return LibDataShallow{Data: d, Code: code}
|
|
}
|
|
|
|
func LoadOne(collection LibDataEnum, id string) LibData {
|
|
d, code, err := models.Model(collection.EnumIndex()).GetAccessor().LoadOne(id)
|
|
if err != nil {
|
|
return LibData{Data: d, Code: code, Err: err.Error()}
|
|
}
|
|
return LibData{Data: d, Code: code}
|
|
}
|
|
|
|
func UpdateOne(collection LibDataEnum, set map[string]interface{}, id string) LibData {
|
|
model := models.Model(collection.EnumIndex())
|
|
d, code, err := model.GetAccessor().UpdateOne(model.Deserialize(set), id)
|
|
if err != nil {
|
|
return LibData{Data: d, Code: code, Err: err.Error()}
|
|
}
|
|
return LibData{Data: d, Code: code}
|
|
}
|
|
|
|
func DeleteOne(collection LibDataEnum, id string) LibData {
|
|
d, code, err := models.Model(collection.EnumIndex()).GetAccessor().DeleteOne(id)
|
|
if err != nil {
|
|
return LibData{Data: d, Code: code, Err: err.Error()}
|
|
}
|
|
return LibData{Data: d, Code: code}
|
|
}
|
|
|
|
func StoreOne(collection LibDataEnum, object map[string]interface{}) LibData {
|
|
model := models.Model(collection.EnumIndex())
|
|
d, code, err := model.GetAccessor().StoreOne(model.Deserialize(object))
|
|
if err != nil {
|
|
return LibData{Data: d, Code: code, Err: err.Error()}
|
|
}
|
|
return LibData{Data: d, Code: code}
|
|
}
|
|
|
|
func CopyOne(collection LibDataEnum, object map[string]interface{}) LibData {
|
|
model := models.Model(collection.EnumIndex())
|
|
d, code, err := model.GetAccessor().CopyOne(model.Deserialize(object))
|
|
if err != nil {
|
|
return LibData{Data: d, Code: code, Err: err.Error()}
|
|
}
|
|
return LibData{Data: d, Code: code}
|
|
}
|
|
|
|
// ================ CAST ========================= //
|
|
|
|
func (l *LibData) ToDataResource() *data.DataResource {
|
|
if l.Data.GetAccessor().GetType() == utils.DATA_RESOURCE.String() {
|
|
return l.Data.(*data.DataResource)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (l *LibData) ToDatacenterResource() *datacenter.DatacenterResource {
|
|
if l.Data != nil && l.Data.GetAccessor().GetType() == utils.DATACENTER_RESOURCE.String() {
|
|
return l.Data.(*datacenter.DatacenterResource)
|
|
}
|
|
return nil
|
|
}
|
|
func (l *LibData) ToStorageResource() *storage.StorageResource {
|
|
if l.Data.GetAccessor().GetType() == utils.STORAGE_RESOURCE.String() {
|
|
return l.Data.(*storage.StorageResource)
|
|
}
|
|
return nil
|
|
}
|
|
func (l *LibData) ToProcessingResource() *processing.ProcessingResource {
|
|
if l.Data.GetAccessor().GetType() == utils.PROCESSING_RESOURCE.String() {
|
|
return l.Data.(*processing.ProcessingResource)
|
|
}
|
|
return nil
|
|
}
|
|
func (l *LibData) ToWorkflowResource() *w.WorkflowResource {
|
|
if l.Data.GetAccessor().GetType() == utils.WORKFLOW_RESOURCE.String() {
|
|
return l.Data.(*w.WorkflowResource)
|
|
}
|
|
return nil
|
|
}
|
|
func (l *LibData) ToPeer() *peer.Peer {
|
|
if l.Data.GetAccessor().GetType() == utils.PEER.String() {
|
|
return l.Data.(*peer.Peer)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (l *LibData) ToWorkflow() *w2.Workflow {
|
|
if l.Data.GetAccessor().GetType() == utils.WORKFLOW.String() {
|
|
return l.Data.(*w2.Workflow)
|
|
}
|
|
return nil
|
|
}
|
|
func (l *LibData) ToWorkspace() *workspace.Workspace {
|
|
if l.Data.GetAccessor().GetType() == utils.WORKSPACE.String() {
|
|
return l.Data.(*workspace.Workspace)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (l *LibData) ToSharedWorkspace() *shared_workspace.SharedWorkspace {
|
|
if l.Data.GetAccessor().GetType() == utils.SHARED_WORKSPACE.String() {
|
|
return l.Data.(*shared_workspace.SharedWorkspace)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (l *LibData) ToRule() *rule.Rule {
|
|
if l.Data.GetAccessor().GetType() == utils.SHARED_WORKSPACE.String() {
|
|
return l.Data.(*rule.Rule)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (l *LibData) ToWorkflowExecution() *workflow_execution.WorkflowExecution {
|
|
if l.Data.GetAccessor().GetType() == utils.WORKFLOW_EXECUTION.String() {
|
|
return l.Data.(*workflow_execution.WorkflowExecution)
|
|
}
|
|
return nil
|
|
}
|