oc-lib/entrypoint.go

419 lines
14 KiB
Go
Raw Normal View History

2024-07-18 14:11:13 +02:00
package oclib
import (
2024-08-21 14:07:22 +02:00
"errors"
"fmt"
2024-08-22 14:29:49 +02:00
"runtime/debug"
2024-08-01 09:13:10 +02:00
"cloud.o-forge.io/core/oc-lib/dbs"
2024-07-18 14:11:13 +02:00
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
2024-07-18 17:55:27 +02:00
"cloud.o-forge.io/core/oc-lib/logs"
2024-07-18 14:11:13 +02:00
"cloud.o-forge.io/core/oc-lib/models"
2024-08-12 12:29:07 +02:00
"cloud.o-forge.io/core/oc-lib/models/peer"
2024-07-30 14:41:33 +02:00
"cloud.o-forge.io/core/oc-lib/models/resource_model"
2024-07-29 08:52:02 +02:00
"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"
2024-07-18 15:35:30 +02:00
"cloud.o-forge.io/core/oc-lib/models/utils"
2024-07-29 08:52:02 +02:00
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"
2024-08-12 12:29:07 +02:00
shared_workspace "cloud.o-forge.io/core/oc-lib/models/workspace/shared"
"cloud.o-forge.io/core/oc-lib/models/workspace/shared/rules/rule"
"cloud.o-forge.io/core/oc-lib/tools"
2024-07-18 18:14:12 +02:00
"github.com/rs/zerolog"
2024-07-18 14:39:54 +02:00
)
2024-08-01 09:13:10 +02:00
type Filters = dbs.Filters
2024-07-18 15:35:30 +02:00
type LibDataEnum int
2024-07-18 15:15:01 +02:00
// init accessible constant to retrieve data from the database
2024-07-18 15:02:39 +02:00
const (
2024-07-22 10:11:28 +02:00
INVALID LibDataEnum = iota
2024-07-22 09:39:42 +02:00
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
2024-07-22 10:11:28 +02:00
WORKFLOW = utils.WORKFLOW
2024-07-25 09:28:55 +02:00
WORKSPACE = utils.WORKSPACE
2024-08-02 17:00:29 +02:00
WORKFLOW_EXECUTION = utils.WORKFLOW_EXECUTION
2024-08-12 12:29:07 +02:00
PEER = utils.PEER
SHARED_WORKSPACE = utils.SHARED_WORKSPACE
RULE = utils.RULE
2024-08-12 14:31:33 +02:00
BOOKING = utils.BOOKING
2024-07-18 15:02:39 +02:00
)
2024-07-18 14:39:54 +02:00
// will turn into standards api hostnames
2024-08-21 15:22:13 +02:00
func (d LibDataEnum) API() string {
return utils.DefaultAPI[d]
}
// will turn into standards name
2024-07-26 14:15:55 +02:00
func (d LibDataEnum) String() string {
return utils.Str[d]
}
// will turn into enum index
2024-07-18 15:35:30 +02:00
func (d LibDataEnum) EnumIndex() int {
2024-07-18 15:15:01 +02:00
return int(d)
}
// model to define the shallow data structure
2024-07-23 11:22:50 +02:00
type LibDataShallow struct {
Data []utils.ShallowDBObject `bson:"data" json:"data"`
Code int `bson:"code" json:"code"`
Err string `bson:"error" json:"error"`
}
// model to define the data structure
2024-07-18 15:35:30 +02:00
type LibData struct {
2024-07-22 09:03:57 +02:00
Data utils.DBObject `bson:"data" json:"data"`
2024-07-22 10:11:28 +02:00
Code int `bson:"code" json:"code"`
Err string `bson:"error" json:"error"`
2024-07-18 15:35:30 +02:00
}
// here is the singleton variable to store the paths that api will use
2024-08-21 16:03:56 +02:00
var paths map[LibDataEnum]string = map[LibDataEnum]string{}
// to get the paths
2024-08-21 16:03:56 +02:00
func GetPaths() map[LibDataEnum]string {
return paths
}
// to get the path
func GetPath(collection LibDataEnum) string {
2024-08-21 16:03:56 +02:00
return paths[collection]
}
// to add the path
func AddPath(collection LibDataEnum, path string) {
paths[collection] = path
}
2024-08-22 13:11:21 +02:00
func Init(appName string, hostname string, port string) {
2024-08-21 14:07:22 +02:00
defer func() {
if r := recover(); r != nil {
2024-08-22 14:29:49 +02:00
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in Init : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
2024-08-22 14:56:32 +02:00
fmt.Printf("Panic recovered in Init : %v - %v\n", r, string(debug.Stack()))
2024-08-21 14:07:22 +02:00
}
}()
logs.SetAppName(appName) // set the app name to the logger to define the main log chan
logs.SetLogger(logs.CreateLogger("main", "")) // create the logger
tools.GetConfig().Host = hostname // set the hostname to the config for inner discovery purpose actually not used
tools.GetConfig().Port = port // set the port to the config for inner discovery purpose actually not used
mongo.MONGOService.Init(models.GetModelsNames(), tools.GetConfig()) // init the mongo service
/*
Here we will check if the resource model is already stored in the database
If not we will store it
Resource model is the model that will define the structure of the resources
*/
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
2024-07-30 14:41:33 +02:00
for _, model := range []string{utils.DATA_RESOURCE.String(), utils.PROCESSING_RESOURCE.String(), utils.STORAGE_RESOURCE.String(), utils.DATACENTER_RESOURCE.String(), utils.WORKFLOW_RESOURCE.String()} {
2024-08-01 09:13:10 +02:00
data, code, _ := accessor.Search(nil, model)
2024-07-30 14:41:33 +02:00
if code == 404 || len(data) == 0 {
m := map[string]resource_model.Model{}
// TODO Specify the model for each resource
// for now only processing is specified here (not an elegant way)
2024-07-30 14:41:33 +02:00
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,
}
m["execute"] = resource_model.Model{
Type: "map[int]map[string]string",
ReadOnly: false,
}
2024-07-30 14:41:33 +02:00
}
accessor.StoreOne(&resource_model.ResourceModel{
ResourceType: model,
Model: m,
})
}
}
2024-07-18 14:11:13 +02:00
}
// GetLogger returns the main logger
2024-07-18 18:14:12 +02:00
func GetLogger() zerolog.Logger {
2024-07-19 09:32:58 +02:00
return logs.GetLogger()
2024-07-18 17:59:25 +02:00
}
/*
* Search will search for the data in the database
* @param filters *dbs.Filters
* @param word string
* @param collection LibDataEnum
* @param c ...*tools.HTTPCaller
* @return data LibDataShallow
*/
2024-08-21 14:07:22 +02:00
func Search(filters *dbs.Filters, word string, collection LibDataEnum, c ...*tools.HTTPCaller) (data LibDataShallow) {
defer func() { // recover the panic
2024-08-21 14:07:22 +02:00
if r := recover(); r != nil {
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in Search : "+fmt.Sprintf("%v", r)))
2024-08-22 14:29:49 +02:00
data = LibDataShallow{Data: nil, Code: 500, Err: "Panic recovered in LoadAll : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
2024-08-21 14:07:22 +02:00
}
}()
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
d, code, err := models.Model(collection.EnumIndex()).GetAccessor(caller).Search(filters, word)
2024-07-26 13:45:10 +02:00
if err != nil {
2024-08-21 14:07:22 +02:00
data = LibDataShallow{Data: d, Code: code, Err: err.Error()}
return
2024-07-26 13:45:10 +02:00
}
2024-08-21 14:07:22 +02:00
data = LibDataShallow{Data: d, Code: code}
return
2024-07-26 13:45:10 +02:00
}
/*
* LoadAll will load all the data from the database
* @param collection LibDataEnum
* @param c ...*tools.HTTPCaller
* @return data LibDataShallow
*/
2024-08-21 14:07:22 +02:00
func LoadAll(collection LibDataEnum, c ...*tools.HTTPCaller) (data LibDataShallow) {
defer func() { // recover the panic
2024-08-21 14:07:22 +02:00
if r := recover(); r != nil {
2024-08-22 14:29:49 +02:00
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in LoadAll : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
data = LibDataShallow{Data: nil, Code: 500, Err: "Panic recovered in LoadAll : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
2024-08-21 14:07:22 +02:00
}
}()
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
d, code, err := models.Model(collection.EnumIndex()).GetAccessor(caller).LoadAll()
2024-07-23 11:22:50 +02:00
if err != nil {
2024-08-21 14:07:22 +02:00
data = LibDataShallow{Data: d, Code: code, Err: err.Error()}
return
2024-07-23 11:22:50 +02:00
}
2024-08-21 14:07:22 +02:00
data = LibDataShallow{Data: d, Code: code}
return
2024-07-23 11:22:50 +02:00
}
/*
* LoadOne will load one data from the database
* @param collection LibDataEnum
* @param id string
* @param c ...*tools.HTTPCaller
* @return data LibData
*/
2024-08-21 14:07:22 +02:00
func LoadOne(collection LibDataEnum, id string, c ...*tools.HTTPCaller) (data LibData) {
defer func() { // recover the panic
2024-08-21 14:07:22 +02:00
if r := recover(); r != nil {
2024-08-22 14:29:49 +02:00
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in LoadOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
data = LibData{Data: nil, Code: 500, Err: "Panic recovered in LoadOne : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
2024-08-21 14:07:22 +02:00
}
}()
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
d, code, err := models.Model(collection.EnumIndex()).GetAccessor(caller).LoadOne(id)
2024-07-22 15:18:59 +02:00
if err != nil {
2024-08-21 14:07:22 +02:00
data = LibData{Data: d, Code: code, Err: err.Error()}
return
2024-07-22 15:18:59 +02:00
}
2024-08-21 14:07:22 +02:00
data = LibData{Data: d, Code: code}
return
2024-07-18 14:39:54 +02:00
}
/*
* UpdateOne will update one data from the database
* @param collection LibDataEnum
* @param set map[string]interface{}
* @param id string
* @param c ...*tools.HTTPCaller
* @return data LibData
*/
2024-08-21 14:07:22 +02:00
func UpdateOne(collection LibDataEnum, set map[string]interface{}, id string, c ...*tools.HTTPCaller) (data LibData) {
defer func() { // recover the panic
2024-08-21 14:07:22 +02:00
if r := recover(); r != nil {
2024-08-22 14:29:49 +02:00
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in UpdateOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
data = LibData{Data: nil, Code: 500, Err: "Panic recovered in UpdateOne : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
2024-08-21 14:07:22 +02:00
}
}()
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
2024-07-22 15:18:59 +02:00
model := models.Model(collection.EnumIndex())
d, code, err := model.GetAccessor(caller).UpdateOne(model.Deserialize(set), id)
2024-07-22 15:18:59 +02:00
if err != nil {
2024-08-21 14:07:22 +02:00
data = LibData{Data: d, Code: code, Err: err.Error()}
return
2024-07-22 15:18:59 +02:00
}
2024-08-21 14:07:22 +02:00
data = LibData{Data: d, Code: code}
return
2024-07-18 14:39:54 +02:00
}
/*
* DeleteOne will delete one data from the database
* @param collection LibDataEnum
* @param id string
* @param c ...*tools.HTTPCaller
* @return data LibData
*/
2024-08-21 14:07:22 +02:00
func DeleteOne(collection LibDataEnum, id string, c ...*tools.HTTPCaller) (data LibData) {
defer func() { // recover the panic
2024-08-21 14:07:22 +02:00
if r := recover(); r != nil {
2024-08-22 14:29:49 +02:00
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in DeleteOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
data = LibData{Data: nil, Code: 500, Err: "Panic recovered in DeleteOne : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
2024-08-21 14:07:22 +02:00
}
}()
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
d, code, err := models.Model(collection.EnumIndex()).GetAccessor(caller).DeleteOne(id)
2024-07-22 15:18:59 +02:00
if err != nil {
2024-08-21 14:07:22 +02:00
data = LibData{Data: d, Code: code, Err: err.Error()}
return
2024-07-22 15:18:59 +02:00
}
2024-08-21 14:07:22 +02:00
data = LibData{Data: d, Code: code}
return
2024-07-19 13:41:10 +02:00
}
/*
* StoreOne will store one data from the database
* @param collection LibDataEnum
* @param object map[string]interface{}
* @param c ...*tools.HTTPCaller
* @return data LibData
*/
2024-08-21 14:07:22 +02:00
func StoreOne(collection LibDataEnum, object map[string]interface{}, c ...*tools.HTTPCaller) (data LibData) {
defer func() { // recover the panic
2024-08-21 14:07:22 +02:00
if r := recover(); r != nil {
2024-08-22 14:29:49 +02:00
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in StoreOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
data = LibData{Data: nil, Code: 500, Err: "Panic recovered in StoreOne : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
2024-08-21 14:07:22 +02:00
}
}()
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
2024-07-18 17:55:27 +02:00
model := models.Model(collection.EnumIndex())
d, code, err := model.GetAccessor(caller).StoreOne(model.Deserialize(object))
2024-07-22 15:18:59 +02:00
if err != nil {
2024-08-21 14:07:22 +02:00
data = LibData{Data: d, Code: code, Err: err.Error()}
return
2024-07-22 15:18:59 +02:00
}
2024-08-21 14:07:22 +02:00
data = LibData{Data: d, Code: code}
return
2024-07-18 14:11:13 +02:00
}
2024-07-22 09:16:59 +02:00
/*
* CopyOne will copy one data from the database
* @param collection LibDataEnum
* @param object map[string]interface{}
* @param c ...*tools.HTTPCaller
* @return data LibData
*/
2024-08-21 14:07:22 +02:00
func CopyOne(collection LibDataEnum, object map[string]interface{}, c ...*tools.HTTPCaller) (data LibData) {
defer func() { // recover the panic
2024-08-21 14:07:22 +02:00
if r := recover(); r != nil {
2024-08-22 14:29:49 +02:00
tools.UncatchedError = append(tools.UncatchedError, errors.New("Panic recovered in CopyOne : "+fmt.Sprintf("%v", r)+" - "+string(debug.Stack())))
data = LibData{Data: nil, Code: 500, Err: "Panic recovered in UpdateOne : " + fmt.Sprintf("%v", r) + " - " + string(debug.Stack())}
2024-08-21 14:07:22 +02:00
}
}()
var caller *tools.HTTPCaller // define the caller
if len(c) > 0 {
caller = c[0]
}
2024-07-22 09:16:59 +02:00
model := models.Model(collection.EnumIndex())
d, code, err := model.GetAccessor(caller).CopyOne(model.Deserialize(object))
2024-07-22 15:18:59 +02:00
if err != nil {
2024-08-21 14:07:22 +02:00
data = LibData{Data: d, Code: code, Err: err.Error()}
return
2024-07-22 15:18:59 +02:00
}
2024-08-21 14:07:22 +02:00
data = LibData{Data: d, Code: code}
return
2024-07-22 10:11:28 +02:00
}
2024-07-29 08:52:02 +02:00
// ================ CAST ========================= //
func (l *LibData) ToDataResource() *data.DataResource {
if l.Data.GetAccessor(nil).GetType() == utils.DATA_RESOURCE.String() {
2024-07-29 08:52:02 +02:00
return l.Data.(*data.DataResource)
}
return nil
}
func (l *LibData) ToDatacenterResource() *datacenter.DatacenterResource {
if l.Data != nil && l.Data.GetAccessor(nil).GetType() == utils.DATACENTER_RESOURCE.String() {
2024-07-29 08:52:02 +02:00
return l.Data.(*datacenter.DatacenterResource)
}
return nil
}
func (l *LibData) ToStorageResource() *storage.StorageResource {
if l.Data.GetAccessor(nil).GetType() == utils.STORAGE_RESOURCE.String() {
2024-07-29 08:52:02 +02:00
return l.Data.(*storage.StorageResource)
}
return nil
}
func (l *LibData) ToProcessingResource() *processing.ProcessingResource {
if l.Data.GetAccessor(nil).GetType() == utils.PROCESSING_RESOURCE.String() {
2024-07-29 08:52:02 +02:00
return l.Data.(*processing.ProcessingResource)
}
return nil
}
func (l *LibData) ToWorkflowResource() *w.WorkflowResource {
if l.Data.GetAccessor(nil).GetType() == utils.WORKFLOW_RESOURCE.String() {
2024-07-29 08:52:02 +02:00
return l.Data.(*w.WorkflowResource)
}
return nil
}
2024-08-12 12:29:07 +02:00
func (l *LibData) ToPeer() *peer.Peer {
if l.Data.GetAccessor(nil).GetType() == utils.PEER.String() {
2024-08-12 12:29:07 +02:00
return l.Data.(*peer.Peer)
}
return nil
}
2024-07-29 08:52:02 +02:00
func (l *LibData) ToWorkflow() *w2.Workflow {
if l.Data.GetAccessor(nil).GetType() == utils.WORKFLOW.String() {
2024-07-29 08:52:02 +02:00
return l.Data.(*w2.Workflow)
}
return nil
}
func (l *LibData) ToWorkspace() *workspace.Workspace {
if l.Data.GetAccessor(nil).GetType() == utils.WORKSPACE.String() {
2024-07-29 08:52:02 +02:00
return l.Data.(*workspace.Workspace)
}
return nil
}
2024-08-12 12:29:07 +02:00
func (l *LibData) ToSharedWorkspace() *shared_workspace.SharedWorkspace {
if l.Data.GetAccessor(nil).GetType() == utils.SHARED_WORKSPACE.String() {
2024-08-12 12:29:07 +02:00
return l.Data.(*shared_workspace.SharedWorkspace)
}
return nil
}
func (l *LibData) ToRule() *rule.Rule {
if l.Data.GetAccessor(nil).GetType() == utils.SHARED_WORKSPACE.String() {
2024-08-12 12:29:07 +02:00
return l.Data.(*rule.Rule)
}
return nil
}
2024-07-29 08:52:02 +02:00
func (l *LibData) ToWorkflowExecution() *workflow_execution.WorkflowExecution {
if l.Data.GetAccessor(nil).GetType() == utils.WORKFLOW_EXECUTION.String() {
2024-07-29 08:52:02 +02:00
return l.Data.(*workflow_execution.WorkflowExecution)
}
return nil
}