163 Commits

Author SHA1 Message Date
mr
29fcd0e34a Merge branch 'master' of https://cloud.o-forge.io/core/oc-lib 2024-08-06 08:42:53 +02:00
mr
d2484e84f0 Add a Name to Workflow Execution 2024-08-06 08:40:05 +02:00
pb
17f62b6495 add labels to lokiwritter after initialisation 2024-08-05 15:47:53 +02:00
mr
dd5f8f5b2d kick out print 2024-08-05 10:10:58 +02:00
mr
1e8cb98098 kick out print 2024-08-05 08:50:20 +02:00
mr
e390dfa8a9 exec access 2024-08-05 08:28:37 +02:00
mr
9abc3a0d99 exec access 2024-08-05 08:25:05 +02:00
mr
1f09e48abf exec access 2024-08-05 08:15:43 +02:00
mr
0b5df45a02 exec access 2024-08-02 17:38:51 +02:00
mr
e320efd70e exec access 2024-08-02 17:00:29 +02:00
mr
caf0c1afcb mongo and or 2024-08-02 16:45:45 +02:00
mr
51541010e2 mongo and or 2024-08-02 16:39:10 +02:00
mr
89f14d99b8 mongo and or 2024-08-02 16:09:17 +02:00
mr
2525cc7783 mongo and or 2024-08-02 16:02:23 +02:00
mr
09b67b26b0 mongo and or 2024-08-02 15:45:57 +02:00
mr
24a277333d mongo and or 2024-08-02 15:36:52 +02:00
mr
efa73d8a45 mongo stability debug + zoom 2024-08-02 15:06:40 +02:00
mr
fa52c71e93 logs update 2024-08-02 14:07:43 +02:00
mr
e23756eba6 logs update 2024-08-02 13:13:04 +02:00
mr
e9dd17ae87 dbs and 2024-08-02 13:10:27 +02:00
mr
d0e2778246 nil except 2024-08-02 11:43:59 +02:00
mr
9da3a7c3e6 nil except 2024-08-02 11:17:04 +02:00
mr
ed4cb943a8 nil except 2024-08-02 11:04:18 +02:00
pb
b125c9be01 updated RAM attribute 2024-08-01 16:50:26 +02:00
pb
7d93c60e36 Added method item to resource 2024-08-01 16:50:16 +02:00
mr
c3e6f04dd8 nil except 2024-08-01 11:33:27 +02:00
mr
69d53f9d4d mongo procedure 2024-08-01 11:31:50 +02:00
mr
dbbda34117 pointer of pointer is kicked out 2024-08-01 11:19:14 +02:00
mr
b0f2cf46eb error 2024-08-01 11:11:41 +02:00
mr
0256266107 error 2024-08-01 11:09:39 +02:00
mr
b0f5c78e55 default bson 2024-08-01 10:03:28 +02:00
mr
27327019be logs 2024-08-01 10:02:40 +02:00
mr
1f8aefb7d8 logs 2024-08-01 09:55:59 +02:00
mr
745e175da4 COMPLEX SEARCH 2024-08-01 09:34:44 +02:00
mr
924a688a9d COMPLEX SEARCH 2024-08-01 09:13:10 +02:00
mr
ad455e0e3a nice shortcut on resource 2024-07-31 16:56:30 +02:00
mr
51f3a320b3 nullable itemresource 2024-07-31 16:21:19 +02:00
mr
c4448fd3af mongo unfatal 2024-07-31 16:06:47 +02:00
mr
78bef28a21 duplicate key 2024-07-31 14:30:52 +02:00
mr
63236362ca optionnal vars failed 2024-07-31 11:50:57 +02:00
mr
2795c924f7 optionnal vars failed 2024-07-31 11:47:47 +02:00
mr
1ff4cb08d1 activation workspace 2024-07-31 11:34:13 +02:00
mr
4f27aefc4b activation workspace 2024-07-31 11:29:21 +02:00
mr
e311553eb1 precise workspace flow 2024-07-31 11:23:59 +02:00
mr
274ce57f5b private accessor 2024-07-31 10:36:57 +02:00
mr
00fe19083b private accessor 2024-07-31 10:35:03 +02:00
mr
7ae1399a9a private accessor 2024-07-31 10:07:55 +02:00
mr
d6a2a416c1 debug automation workspace 2024-07-31 09:52:57 +02:00
mr
511e0c3f31 debug automation workspace 2024-07-31 08:57:20 +02:00
mr
19381d6438 debug automation workspace 2024-07-31 08:36:16 +02:00
mr
18d54cdb8f debug automation workspace 2024-07-30 16:30:44 +02:00
mr
b06193add2 rename arguments in args 2024-07-30 14:41:33 +02:00
mr
86c3e83eda missing collection resource model 2024-07-30 14:40:44 +02:00
mr
e784216584 Resource Model Apparel 2024-07-30 14:08:27 +02:00
mr
5405e91167 copy resource in workflow 2024-07-30 12:08:13 +02:00
mr
250fefd0d8 search is debug nested object included 2024-07-30 09:27:52 +02:00
mr
6ac855394b workin exucution scheduler 2024-07-30 09:20:33 +02:00
mr
4730d5b4d4 test on nested search 2024-07-30 08:59:31 +02:00
mr
5ab011844d repair accessor call 2024-07-30 08:52:14 +02:00
mr
0e96b5c01a logs on search 2024-07-30 08:39:28 +02:00
mr
50cecc40c4 dbs logs 2024-07-30 08:33:26 +02:00
mr
dc30412f2e debugging workflow_execution + search invest 2024-07-29 17:05:39 +02:00
mr
3e72510d53 search is not searchin 2024-07-29 16:23:52 +02:00
mr
beeac40a10 debug logs 2024-07-29 16:04:09 +02:00
mr
2f6fab2f7b missing context on search 2024-07-29 15:53:15 +02:00
mr
d77a403150 missing context on search 2024-07-29 15:22:14 +02:00
mr
8c3b92143e maximum log 2024-07-29 15:07:59 +02:00
mr
e2f722e17b set up print 2024-07-29 14:19:48 +02:00
mr
956af8f5f5 set up print 2024-07-29 13:36:25 +02:00
mr
c7f65914bb cast at root lib 2024-07-29 08:52:02 +02:00
mr
ff286583aa DATA misdirect 2024-07-26 16:47:11 +02:00
mr
fd64d423be models 2024-07-26 16:41:08 +02:00
mr
87b7cda0ce adjust model 2024-07-26 15:19:36 +02:00
mr
eb1417853a acces to string on enum 2024-07-26 14:15:55 +02:00
mr
8c97fca96c adding search func 2024-07-26 13:45:10 +02:00
mr
6c83e54d37 Get on Workspace auto load full data 2024-07-26 10:36:23 +02:00
mr
d5c5b454f4 not a proper type ref o nworkspace 2024-07-26 09:51:54 +02:00
mr
5ad4d523ab not a proper type ref o nworkspace 2024-07-26 08:58:59 +02:00
mr
1ca983db36 adding workspace to catalog 2024-07-26 08:27:09 +02:00
pb
2891dc8a68 adding push path to the url contacted by lokiwritter.Write 2024-07-25 12:35:14 +02:00
mr
6f6cb7c489 resource schedule init 2024-07-25 11:52:05 +02:00
mr
94b45387fb resource push 2024-07-25 11:46:33 +02:00
mr
094ae0a7f0 Adding Workspace Logic 2024-07-25 09:28:55 +02:00
mr
51e94e73e5 id in json 2024-07-24 17:08:30 +02:00
mr
357d79e68b Update Resource 2024-07-24 14:49:04 +02:00
mr
ac2111a365 Update loggon 2024-07-24 14:23:47 +02:00
mr
637b2c977d Serialize failures 2024-07-24 09:53:30 +02:00
mr
c5f4427f9f Update still not workin 2024-07-24 09:27:03 +02:00
mr
045f3471ec Execution missing to catalog 2024-07-24 08:58:40 +02:00
mr
bde70dec9b Debugging Put / GET ALL 2024-07-24 08:35:41 +02:00
mr
b775298bb0 LoadAll evolve 2024-07-23 18:06:54 +02:00
mr
aa7ce56174 rm env 2024-07-23 17:45:47 +02:00
mr
00f25b48c0 Execution 2024-07-23 16:14:46 +02:00
mr
806f5d0f20 datacenter 2024-07-23 14:57:16 +02:00
mr
8fb3e35142 no workflow 2024-07-23 14:38:47 +02:00
mr
0b410b8580 color value 2024-07-23 11:49:11 +02:00
mr
94cd62acfe Shallow Get All 2024-07-23 11:22:50 +02:00
mr
a687206a1b correction struct graph 2024-07-23 09:46:27 +02:00
mr
635b1d4ed4 color is an hex 2024-07-23 09:22:40 +02:00
mr
88c8feb7d8 missing bson model on graph 2024-07-23 09:15:42 +02:00
mr
febabe67e7 Not a proper WF model 2024-07-23 08:59:39 +02:00
mr
0c825b65b0 Update flatten object 2024-07-23 08:40:24 +02:00
mr
a6f2556050 log 2024-07-22 16:57:54 +02:00
mr
e44a6b65b7 omitempty 2024-07-22 16:39:26 +02:00
mr
ebdbd97ce6 update set 2024-07-22 16:12:38 +02:00
mr
4a90d3379c update fails 2024-07-22 15:18:59 +02:00
mr
7a49f6b957 t 2024-07-22 14:46:49 +02:00
mr
b1c3c056df another test 2024-07-22 14:06:53 +02:00
mr
9f17532646 insert uuid 2024-07-22 14:02:48 +02:00
mr
f7dfbe91dd another test flat in base 2024-07-22 13:26:59 +02:00
mr
b1293af229 test 2024-07-22 12:51:43 +02:00
mr
6696b0faac deserialize 2024-07-22 11:50:14 +02:00
mr
a5741d5831 omitempty is not null 2024-07-22 10:57:00 +02:00
mr
bbe4b3eee7 omitempty is not null 2024-07-22 10:31:05 +02:00
mr
7f294374e8 error adjust 2024-07-22 10:11:28 +02:00
mr
df556f7c7a apropriate db 2024-07-22 09:39:42 +02:00
mr
742bcd1acf config pb 2024-07-22 09:16:59 +02:00
mr
53811538c0 config pb 2024-07-22 09:03:57 +02:00
mr
070f2d063e deserialize test 2024-07-19 15:20:03 +02:00
mr
e89b5aae84 test copy 2024-07-19 13:41:10 +02:00
mr
3f9814e649 test * 2024-07-19 13:27:34 +02:00
mr
cdc077c59e test * 2024-07-19 13:15:51 +02:00
mr
5c44b46bcb test * 2024-07-19 12:57:25 +02:00
mr
d28662d70b add with http code 2024-07-19 11:27:58 +02:00
mr
650168f39c resource as resource named1 2024-07-19 11:03:56 +02:00
mr
20913f056b resource as resource named1 2024-07-19 11:00:15 +02:00
mr
2a9a784ec1 resource as resource named 2024-07-19 10:54:58 +02:00
mr
218714683b validator err 2024-07-19 09:43:18 +02:00
mr
694957a483 validator err test 2024-07-19 09:41:13 +02:00
mr
c494dba5ab validator not work 2024-07-19 09:36:46 +02:00
mr
e838474835 validator use test 2024-07-19 09:32:58 +02:00
mr
071a49cd81 validator2 2024-07-18 18:28:36 +02:00
mr
4f4e2949d6 validator 2024-07-18 18:14:12 +02:00
mr
cc4cc49617 main logger 2024-07-18 17:59:25 +02:00
mr
0a441cd3df main logger 2024-07-18 17:55:27 +02:00
mr
3e9eae007b apropriate db 2024-07-18 16:46:54 +02:00
mr
d273fdc57f config pb 2024-07-18 16:18:27 +02:00
mr
8eb88dcd82 config pb 2024-07-18 16:14:39 +02:00
mr
e3a7703bc5 config pb 2024-07-18 16:09:08 +02:00
mr
08100eb57f determine returnal 2024-07-18 15:35:30 +02:00
mr
6d104288b7 accessor 2024-07-18 15:15:01 +02:00
mr
33c284cd4c accessor 2024-07-18 15:04:12 +02:00
mr
e345204851 accessor 2024-07-18 15:02:39 +02:00
mr
4fab0cb153 access func 2024-07-18 14:39:54 +02:00
mr
7f1f85c00e accessor 2024-07-18 14:11:13 +02:00
mr
2aa9054148 missing conf 2024-07-18 13:58:49 +02:00
mr
c41eeb969a configuration 2024-07-18 13:55:38 +02:00
mr
9bc77b6699 config 2024-07-18 13:43:41 +02:00
mr
1999213f8b module name 2024-07-18 13:35:14 +02:00
7d5bdadbef rename 2024-07-18 13:16:53 +02:00
mr
1dfff4635e init 2024-07-18 12:05:32 +02:00
mr
85fc43368b lib conf 2024-07-18 11:56:54 +02:00
mr
29a75bced9 organize + graph 2024-07-18 11:51:12 +02:00
pb
2eb74da9d2 packaged every model 2024-07-17 18:02:30 +02:00
pb
2684703da5 organised in packages 2024-07-17 17:48:20 +02:00
pb
02b0a2a595 rewrote the mongo client handler 2024-07-17 17:20:25 +02:00
pb
90e9bcf378 improve testing 2024-07-17 17:19:42 +02:00
pb
4fd22dc131 tests for each model 2024-07-17 17:19:03 +02:00
pb
562720089f Accessor for each ressource to access DB 2024-07-17 17:18:27 +02:00
pb
3732187678 Improved models' structure 2024-07-17 17:17:37 +02:00
pb
f0eec45836 mongo 2024-07-16 11:05:11 +02:00
pb
034a81cedb First batch 2024-07-16 11:05:11 +02:00
pb
5ba33d3131 New libs 2024-07-16 11:05:11 +02:00
57 changed files with 3189 additions and 69 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
out/*

View File

@@ -1 +0,0 @@
package oclib

42
conf.go Normal file
View File

@@ -0,0 +1,42 @@
package oclib
import "sync"
// ===================================================
// This class has to be updated everytime
// a new configuration variable is defined
// in a componant that imports oc-lib
// ===================================================
type Config struct {
MongoUrl string
MongoDatabase string
}
func (c Config) GetUrl() string {
return c.MongoUrl
}
func (c Config) GetDatabase() string {
return c.MongoDatabase
}
var instance *Config
var once sync.Once
func GetConfig() *Config {
once.Do(func() {
instance = &Config{}
})
return instance
}
func SetConfig(url string, database string) *Config {
once.Do(func() {
instance = &Config{
MongoUrl: url,
MongoDatabase: database,
}
})
return instance
}

View File

@@ -1,5 +0,0 @@
package oclib
type Data struct {
Resource
}

View File

@@ -1 +0,0 @@
package oclib

View File

@@ -1 +0,0 @@
package oclib

128
dbs/dbs.go Normal file
View File

@@ -0,0 +1,128 @@
package dbs
import (
"fmt"
"strings"
"go.mongodb.org/mongo-driver/bson"
)
type Operator int
const (
LIKE Operator = iota
EXISTS
IN
GTE
LTE
LT
GT
EQUAL
)
var str = [...]string{
"like",
"exists",
"in",
"gte",
"lte",
"lt",
"gt",
"equal",
}
func (m Operator) String() string {
return str[m]
}
func (m Operator) ToMongoEOperator(k string, value interface{}) bson.E {
switch m {
case LIKE:
return bson.E{Key: k, Value: bson.M{"$regex": ToValueOperator(StringToOperator(m.String()), value)}}
case EXISTS:
return bson.E{Key: k, Value: bson.M{"$exists": ToValueOperator(StringToOperator(m.String()), value)}}
case IN:
return bson.E{Key: k, Value: bson.M{"$in": ToValueOperator(StringToOperator(m.String()), value)}}
case GTE:
return bson.E{Key: k, Value: bson.M{"$gte": ToValueOperator(StringToOperator(m.String()), value)}}
case GT:
return bson.E{Key: k, Value: bson.M{"$gt": ToValueOperator(StringToOperator(m.String()), value)}}
case LTE:
return bson.E{Key: k, Value: bson.M{"$lte": ToValueOperator(StringToOperator(m.String()), value)}}
case LT:
return bson.E{Key: k, Value: bson.M{"$lt": ToValueOperator(StringToOperator(m.String()), value)}}
case EQUAL:
return bson.E{Key: k, Value: value}
default:
return bson.E{Key: k, Value: bson.M{"$regex": ToValueOperator(StringToOperator(m.String()), value)}}
}
}
func (m Operator) ToMongoOperator(k string, value interface{}) bson.M {
switch m {
case LIKE:
return bson.M{k: bson.M{"$regex": ToValueOperator(StringToOperator(m.String()), value)}}
case EXISTS:
return bson.M{k: bson.M{"$exists": ToValueOperator(StringToOperator(m.String()), value)}}
case IN:
return bson.M{k: bson.M{"$in": ToValueOperator(StringToOperator(m.String()), value)}}
case GTE:
return bson.M{k: bson.M{"$gte": ToValueOperator(StringToOperator(m.String()), value)}}
case GT:
return bson.M{k: bson.M{"$gt": ToValueOperator(StringToOperator(m.String()), value)}}
case LTE:
return bson.M{k: bson.M{"$lte": ToValueOperator(StringToOperator(m.String()), value)}}
case LT:
return bson.M{k: bson.M{"$lt": ToValueOperator(StringToOperator(m.String()), value)}}
case EQUAL:
return bson.M{k: value}
default:
return bson.M{k: bson.M{"$regex": ToValueOperator(StringToOperator(m.String()), value)}}
}
}
func StringToOperator(s string) Operator {
for i, v := range str {
if v == s {
return Operator(i)
}
}
return LIKE
}
func ToValueOperator(operator Operator, value interface{}) interface{} {
if strings.TrimSpace(fmt.Sprintf("%v", value)) == "*" {
value = ""
}
if operator == LIKE {
return "(?i).*" + strings.TrimSpace(fmt.Sprintf("%v", value)) + ".*"
}
return value
}
type Filters struct {
And map[string][]Filter `json:"and"`
Or map[string][]Filter `json:"or"`
}
type Filter struct {
Operator string `json:"operator,omitempty"`
Value interface{} `json:"value,omitempty"`
}
type Input = map[string]interface{}
func InputToBson(i Input, isUpdate bool) bson.D {
input := bson.D{}
for k, v := range i {
if k == "id" {
input = append(input, bson.E{Key: "_id", Value: v})
} else {
input = append(input, bson.E{Key: k, Value: v})
}
}
if isUpdate {
return bson.D{{Key: "$set", Value: input}}
}
return input
}

339
dbs/mongo/mongo.go Normal file
View File

@@ -0,0 +1,339 @@
package mongo
import (
"context"
"errors"
"time"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/logs"
"github.com/rs/zerolog"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
mngoClient *mongo.Client
mngoDB *mongo.Database
MngoCtx context.Context
cancel context.CancelFunc
isConnected bool
existingCollections []string
mngoCollections []string
mngoConfig MongoConf
ResourceMap map[string]interface{}
)
var MONGOService = MongoDB{}
type MongoConf interface {
GetUrl() string
GetDatabase() string
}
type MongoDB struct {
Logger zerolog.Logger
}
func (m *MongoDB) Init(collections []string, config MongoConf) {
// var baseConfig string
isConnected = false
m.Logger = logs.GetLogger()
ResourceMap = make(map[string]interface{})
m.Logger.Info().Msg("Connecting to" + config.GetUrl())
mngoCollections = collections
mngoConfig = config
if err := m.createClient(config.GetUrl()); err != nil {
m.Logger.Error().Msg(err.Error())
}
}
func (m *MongoDB) createClient(MongoURL string) error {
var err error
// Allows us to use marshal and unmarshall with results of FindOne() and others
bsonOpts := &options.BSONOptions{
UseJSONStructTags: true,
NilSliceAsEmpty: true,
}
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
clientOptions := options.Client().ApplyURI(MongoURL).SetBSONOptions(bsonOpts)
// Ping the primary
if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil || mngoClient == nil {
mngoClient = nil
isConnected = false
return errors.New("Mongodb connect " + MongoURL + ":" + err.Error())
}
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err = mngoClient.Ping(MngoCtx, nil); err != nil {
mngoClient = nil
isConnected = false
return errors.New("Mongodb ping " + MongoURL + ":" + err.Error())
}
if !isConnected && mngoClient != nil {
m.Logger.Info().Msg("Connecting mongo client to db " + mngoConfig.GetDatabase())
m.prepareDB(mngoCollections, mngoConfig)
m.Logger.Info().Msg("Database is READY")
}
return nil
}
func (m *MongoDB) prepareDB(list_collection []string, config MongoConf) {
var err error
mngoDB = mngoClient.Database(config.GetDatabase())
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
existingCollections, err = mngoDB.ListCollectionNames(MngoCtx, bson.D{})
if err != nil {
m.Logger.Fatal().Msg("Error contacting MongoDB\n" + err.Error())
}
collectionMap := make(map[string]bool)
for _, name := range existingCollections {
collectionMap[name] = true
}
// Only do the collection definition process if it doesn't already exists
// we add the collection to the collection map from mongo/mongo_utils to provide faster access to the collection
for _, collection_name := range list_collection {
new_collection := mngoDB.Collection(collection_name)
if _, exists := collectionMap[collection_name]; !exists {
m.createCollection(collection_name, new_collection)
} else {
CollectionMap[collection_name] = new_collection
}
}
isConnected = true
}
// Creates the collection with index specified in mongo/mongo_collections
// or use the basic collection creation function
func (m *MongoDB) createCollection(collection_name string, new_collection *mongo.Collection) {
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var err error
CollectionMap[collection_name] = new_collection
_, exists := IndexesMap[collection_name]
if exists {
if _, err = new_collection.Indexes().CreateMany(MngoCtx, IndexesMap[collection_name]); err != nil {
var cmdErr mongo.CommandError
if errors.As(err, &cmdErr) && cmdErr.Code != 85 {
m.Logger.Fatal().Msg("Error creating indexes for " + collection_name + " collection : \n" + err.Error())
panic(err)
} else if !errors.As(err, &cmdErr) {
m.Logger.Fatal().Msg("Unexpected error: " + err.Error())
panic(err)
}
}
} else {
mngoDB.CreateCollection(MngoCtx, collection_name)
}
}
func (m *MongoDB) DeleteOne(id string, collection_name string) (int64, int, error) {
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return 0, 503, err
}
filter := bson.M{"_id": id}
targetDBCollection := CollectionMap[collection_name]
opts := options.Delete().SetHint(bson.D{{Key: "_id", Value: 1}})
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
result, err := targetDBCollection.DeleteOne(MngoCtx, filter, opts)
if err != nil {
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
return 0, 404, err
}
return result.DeletedCount, 200, nil
}
func (m *MongoDB) DeleteMultiple(f map[string]interface{}, collection_name string) (int64, int, error) {
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return 0, 503, err
}
filter := bson.D{}
for k, v := range f {
filter = append(filter, bson.E{Key: k, Value: v})
}
targetDBCollection := CollectionMap[collection_name]
opts := options.Delete().SetHint(bson.D{{Key: "_id", Value: 1}})
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
result, err := targetDBCollection.DeleteMany(MngoCtx, filter, opts)
if err != nil {
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
return 0, 404, err
}
return result.DeletedCount, 200, nil
}
func (m *MongoDB) UpdateMultiple(set interface{}, filter map[string]interface{}, collection_name string) (int64, int, error) {
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return 0, 503, err
}
var doc map[string]interface{}
b, _ := bson.Marshal(set)
bson.Unmarshal(b, &doc)
f := bson.D{}
for k, v := range filter {
f = append(f, bson.E{Key: k, Value: v})
}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := targetDBCollection.UpdateMany(MngoCtx, f, dbs.InputToBson(doc, true))
if err != nil {
m.Logger.Error().Msg("Couldn't update resource: " + err.Error())
return 0, 404, err
}
return res.UpsertedCount, 200, nil
}
func (m *MongoDB) UpdateOne(set interface{}, id string, collection_name string) (string, int, error) {
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return "", 503, err
}
var doc map[string]interface{}
b, _ := bson.Marshal(set)
bson.Unmarshal(b, &doc)
filter := bson.M{"_id": id}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, err := targetDBCollection.UpdateOne(MngoCtx, filter, dbs.InputToBson(doc, true))
if err != nil {
m.Logger.Error().Msg("Couldn't update resource: " + err.Error())
return "", 404, err
}
return id, 200, nil
}
func (m *MongoDB) StoreOne(obj interface{}, id string, collection_name string) (string, int, error) {
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return "", 503, err
}
var doc map[string]interface{}
b, _ := bson.Marshal(obj)
bson.Unmarshal(b, &doc)
doc["_id"] = id
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, err := targetDBCollection.InsertOne(MngoCtx, doc)
if err != nil {
m.Logger.Error().Msg("Couldn't insert resource: " + err.Error())
return "", 409, err
}
return id, 200, nil
}
func (m *MongoDB) LoadOne(id string, collection_name string) (*mongo.SingleResult, int, error) {
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return nil, 503, err
}
filter := bson.M{"_id": id}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res := targetDBCollection.FindOne(MngoCtx, filter)
if res.Err() != nil {
m.Logger.Error().Msg("Couldn't find resource " + id + ". Error : " + res.Err().Error())
err := res.Err()
return nil, 404, err
}
return res, 200, nil
}
func (m *MongoDB) Search(filters *dbs.Filters, collection_name string) (*mongo.Cursor, int, error) {
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return nil, 503, err
}
opts := options.Find()
opts.SetLimit(100)
targetDBCollection := CollectionMap[collection_name]
orList := bson.A{}
andList := bson.A{}
f := bson.D{}
if filters != nil {
for k, filter := range filters.Or {
for _, ff := range filter {
orList = append(orList, dbs.StringToOperator(ff.Operator).ToMongoOperator(k, ff.Value))
}
}
for k, filter := range filters.And {
for _, ff := range filter {
andList = append(andList, dbs.StringToOperator(ff.Operator).ToMongoOperator(k, ff.Value))
}
}
if len(orList) > 0 && len(andList) == 0 {
f = bson.D{{"$or", orList}}
} else {
if len(orList) > 0 {
andList = append(andList, bson.M{"$or": orList})
}
f = bson.D{{"$and", andList}}
}
}
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if cursor, err := targetDBCollection.Find(
MngoCtx,
f,
opts,
); err != nil {
return nil, 404, err
} else {
return cursor, 200, nil
}
}
func (m *MongoDB) LoadFilter(filter map[string]interface{}, collection_name string) (*mongo.Cursor, int, error) {
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return nil, 503, err
}
f := bson.D{}
for k, v := range filter {
f = append(f, bson.E{Key: k, Value: v})
}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := targetDBCollection.Find(MngoCtx, f)
if err != nil {
m.Logger.Error().Msg("Couldn't find any resources. Error : " + err.Error())
return nil, 404, err
}
return res, 200, nil
}
func (m *MongoDB) LoadAll(collection_name string) (*mongo.Cursor, int, error) {
if err := m.createClient(mngoConfig.GetUrl()); err != nil {
return nil, 503, err
}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := targetDBCollection.Find(MngoCtx, bson.D{})
if err != nil {
m.Logger.Error().Msg("Couldn't find any resources. Error : " + err.Error())
return nil, 404, err
}
return res, 200, nil
}

55
dbs/mongo/mongo_utils.go Normal file
View File

@@ -0,0 +1,55 @@
package mongo
import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
// Will store the created collection object for a faster access
var CollectionMap map[string]*mongo.Collection
var IndexesMap map[string][]mongo.IndexModel
func init() {
CollectionMap = make(map[string]*mongo.Collection)
IndexesMap = make(map[string][]mongo.IndexModel)
IndexesMap["data"] = append(IndexesMap["data"], mongo.IndexModel{Keys: bson.D{
{Key: "description", Value: "text"},
{Key: "example", Value: "text"}},
})
IndexesMap["datacenter"] = append(IndexesMap["datacenter"], mongo.IndexModel{Keys: bson.D{
{Key: "description", Value: "text"},
{Key: "example", Value: "text"},
{Key: "owner", Value: "text"}},
})
IndexesMap["storage"] = append(IndexesMap["storage"], mongo.IndexModel{Keys: bson.D{
{Key: "description", Value: "text"},
{Key: "example", Value: "text"}},
})
IndexesMap["processing"] = append(IndexesMap["processing"], mongo.IndexModel{Keys: bson.D{
{Key: "description", Value: "text"},
{Key: "example", Value: "text"},
{Key: "owner", Value: "text"},
},
})
IndexesMap["workflow"] = append(IndexesMap["workflow"], mongo.IndexModel{Keys: bson.D{
{Key: "description", Value: "text"},
{Key: "example", Value: "text"},
{Key: "owner", Value: "text"},
},
})
}
func GetObjIDFromString(id string) interface{} {
objectID, err := primitive.ObjectIDFromHex(id)
if err == nil {
return objectID
}
return id
}

View File

@@ -2,21 +2,21 @@
abstract Ressource {
+id: int
+UUID: int
+name: string
+icon: string
+description: string
+graphic: GraphicElement
+element: Data/Processing/Storage/Workflow/Datacenter
+element: DataResource/ProcessingResource/StorageResource/Workflow/DatacenterResource
}
class Data {
+id: int
class DataResource {
+UUID: int
+name: string
}
class Processing {
+id: int
class ProcessingResource {
+UUID: int
+name: string
+container: string
+command: int
@@ -24,84 +24,84 @@ class Processing {
}
class Storage {
+id: int
class StorageResource {
+UUID: int
+name: string
+url: string
+capacity: int
}
class Datacenter {
+id: int
class DatacenterResource {
+UUID: int
+name: string
}
class Workflow {
+id: int
+UUID: int
+name: string
}
class ResourceSet {
+id: int
+UUID: int
+name: string
+ressources: Ressource[]
}
class WorkflowSchedule {
+id: int
+UUID: int
+start: date
+end: date
+cron : string
}
class Graph {
+id: int
+ressources: map[GraphicElement.ID]Ressource
+UUID: int
+ressources: map[GraphicElement.UUID]Ressource
+links: Link[]
}
class Link {
+id: int
+source: GraphicElement.ID
+target: GraphicElement.ID
+UUID: int
+source: GraphicElement.UUID
+target: GraphicElement.UUID
+graphic: GraphicLink
}
class GraphicLink {
+id: int
+UUID: int
+startXY: coord
+endXY: coord
+style: string
}
class GraphicElement {
+id: int
+UUID: int
+style: string
+xy: coord
}
class Calendar {
+id: int
+UUID: int
+name: string
+workflows: Workflow[]
+owner: string
}
class UserWorkflows {
+id: int
+UUID: int
+user: string
+workflows: Workflow[]
}
class DatacenterWorkflows {
+id: int
+datacenter: Datacenter
+UUID: int
+datacenter: DatacenterResource
+workflows: Workflow[]
}
class Graph {
+id: int
+UUID: int
+graph: Graph
+workflows: Workflow[]
}
@@ -112,16 +112,16 @@ UserWorkflows "1" o-- "0..*" Workflow
DatacenterWorkflows "1" o-- "0..*" Workflow
Ressource <|-- Data
Ressource <|-- Processing
Ressource <|-- Storage
Ressource <|-- Datacenter
Ressource <|-- DataResource
Ressource <|-- ProcessingResource
Ressource <|-- StorageResource
Ressource <|-- DatacenterResource
Ressource <|-- Workflow
ResourceSet "1" o-- "0..*" Ressource
Workflow "1" o-- "0..*" ResourceSet
Workflow "1" o-- "0..*" WorkflowSchedule
Workflow "1" o-- "0..1" WorkflowSchedule
Workflow "1" o-- "0..*" Graph
Graph "1" o-- "0..*" Resources

Binary file not shown.

Before

Width:  |  Height:  |  Size: 89 KiB

After

Width:  |  Height:  |  Size: 102 KiB

202
entrypoint.go Normal file
View File

@@ -0,0 +1,202 @@
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/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"
"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
)
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) 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) ToWorkflowExecution() *workflow_execution.WorkflowExecution {
if l.Data.GetAccessor().GetType() == utils.WORKSPACE.String() {
return l.Data.(*workflow_execution.WorkflowExecution)
}
return nil
}

36
go.mod
View File

@@ -1,11 +1,39 @@
module oc-lib
module cloud.o-forge.io/core/oc-lib
go 1.22.0
require github.com/rs/zerolog v1.33.0
require (
github.com/go-playground/validator/v10 v10.22.0
github.com/google/uuid v1.6.0
github.com/rs/zerolog v1.33.0
github.com/stretchr/testify v1.9.0
)
require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
golang.org/x/sys v0.12.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
go.mongodb.org/mongo-driver v1.16.0
golang.org/x/sys v0.22.0 // indirect
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/vk496/cron v1.2.0
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

86
go.sum
View File

@@ -1,15 +1,97 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I=
github.com/gabriel-vasile/mimetype v1.4.4/go.mod h1:JwLei5XPtWdGiMFB5Pjle1oEeoSeEuJfJE+TtfvdB/s=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.22.0 h1:k6HsTZ0sTnROkhS//R0O+55JgM8C4Bx7ia+JlgcnOao=
github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
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/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/vk496/cron v1.2.0 h1:fDxb4qNi6Rmxh3h9snW1sKJ0nHgjpg3fYc0Oq+igbvk=
github.com/vk496/cron v1.2.0/go.mod h1:f8lpm+SIXbjvujp8Dix4S2B+GGva/q0yrRPQ8hwTtOc=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76 h1:tBiBTKHnIjovYoLX/TPkcf+OjqqKGQrPtGT3Foz+Pgo=
github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76/go.mod h1:SQliXeA7Dhkt//vS29v3zpbEwoa+zb2Cn5xj5uO4K5U=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/mongo-driver v1.16.0 h1:tpRsfBJMROVHKpdGyc1BBEzzjDUWjItxbVSZ8Ls4BQ4=
go.mongodb.org/mongo-driver v1.16.0/go.mod h1:oB6AhJQvFQL4LEHyXi6aJzQJtBiTQHiAd83l0GdFaiw=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -1 +0,0 @@
package oclib

View File

@@ -1,7 +1,6 @@
package oclib
package logs
import (
"oc-lib/logs"
"os"
"runtime"
"time"
@@ -10,24 +9,41 @@ import (
)
var logger zerolog.Logger
// CreateLogger
var appname string
// logs.CreateLogger
// Create a new logger
// Parameters:
// - appname: string : the name of the application using oclib
// - url: string : the url of a loki logger, console log only if ""
// Returns:
// - zerolog.Logger : the logger that will log for the library and the app
func CreateLogger(appname string, url string) zerolog.Logger {
func SetAppName(name string) {
appname = name
}
func GetAppName() string {
return appname
}
func GetLogger() zerolog.Logger {
return logger
}
func SetLogger(l zerolog.Logger) {
logger = l
}
func CreateLogger(funcName string, url string) zerolog.Logger {
if url != "" {
labels := map[string]string{
"app": "app",
"app": appname,
"code": "go",
"platform": runtime.GOOS,
"function": funcName,
}
lokiWriter := logs.NewLokiWriter(url, labels)
lokiWriter := NewLokiWriter(url, labels)
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}

View File

@@ -55,6 +55,15 @@ func (w *LokiWriter) Write(p []byte) (n int, err error) {
}
labels["level"] = level
// Add label that have been added to the event
for k,v := range(event){
if k != "level" && k != "time" && k != "message"{
data := v.(map[string]string)
labels[k] = data["data"]
}
}
// Format the timestamp in nanoseconds
timestamp := fmt.Sprintf("%d000000", time.Now().UnixNano()/int64(time.Millisecond))
@@ -76,7 +85,7 @@ func (w *LokiWriter) Write(p []byte) (n int, err error) {
//fmt.Printf("Sending payload to Loki: %s\n", string(payloadBytes))
req, err := http.NewRequest("POST", w.url, bytes.NewReader(payloadBytes))
req, err := http.NewRequest("POST", w.url + "/loki/api/v1/push", bytes.NewReader(payloadBytes))
if err != nil {
return 0, fmt.Errorf("failed to create HTTP request: %w", err)
}

45
models/models.go Normal file
View File

@@ -0,0 +1,45 @@
package models
import (
"cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
d "cloud.o-forge.io/core/oc-lib/models/resources/data"
dc "cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
p "cloud.o-forge.io/core/oc-lib/models/resources/processing"
s "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"
w3 "cloud.o-forge.io/core/oc-lib/models/workspace"
)
var models = map[string]func() utils.DBObject{
utils.WORKFLOW_RESOURCE.String(): func() utils.DBObject { return &w.WorkflowResource{} },
utils.DATA_RESOURCE.String(): func() utils.DBObject { return &d.DataResource{} },
utils.DATACENTER_RESOURCE.String(): func() utils.DBObject { return &dc.DatacenterResource{} },
utils.STORAGE_RESOURCE.String(): func() utils.DBObject { return &s.StorageResource{} },
utils.PROCESSING_RESOURCE.String(): func() utils.DBObject { return &p.ProcessingResource{} },
utils.WORKFLOW.String(): func() utils.DBObject { return &w2.Workflow{} },
utils.WORKFLOW_EXECUTION.String(): func() utils.DBObject { return &workflow_execution.WorkflowExecution{} },
utils.WORKSPACE.String(): func() utils.DBObject { return &w3.Workspace{} },
utils.RESOURCE_MODEL.String(): func() utils.DBObject { return &resource_model.ResourceModel{} },
}
func Model(model int) utils.DBObject {
log := logs.GetLogger()
if _, ok := models[utils.FromInt(model)]; ok {
return models[utils.FromInt(model)]()
}
log.Error().Msg("Can't find model " + utils.FromInt(model) + ".")
return nil
}
func GetModelsNames() []string {
names := []string{}
for name := range models {
names = append(names, name)
}
return names
}

View File

@@ -0,0 +1,108 @@
package resource_model
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/google/uuid"
)
type AbstractResource struct {
utils.AbstractObject
ShortDescription string `json:"short_description,omitempty" bson:"short_description,omitempty" validate:"required"`
Description string `json:"description,omitempty" bson:"description,omitempty"`
Logo string `json:"logo,omitempty" bson:"logo,omitempty" validate:"required"`
Owner string `json:"owner,omitempty" bson:"owner,omitempty" validate:"required"`
OwnerLogo string `json:"owner_logo,omitempty" bson:"owner_logo,omitempty"`
SourceUrl string `json:"source_url,omitempty" bson:"source_url,omitempty" validate:"required"`
Price string `json:"price,omitempty" bson:"price,omitempty"`
License string `json:"license,omitempty" bson:"license,omitempty"`
ResourceModel *ResourceModel `json:"resource_model,omitempty" bson:"resource_model,omitempty"`
}
func (abs *AbstractResource) GetModelValue(key string) interface{} {
if abs.ResourceModel == nil || abs.ResourceModel.Model == nil {
return nil
}
if _, ok := abs.ResourceModel.Model[key]; !ok {
return nil
}
return abs.ResourceModel.Model[key].Value
}
func (abs *AbstractResource) GetModelType(key string) interface{} {
if abs.ResourceModel == nil || abs.ResourceModel.Model == nil {
return nil
}
if _, ok := abs.ResourceModel.Model[key]; !ok {
return nil
}
return abs.ResourceModel.Model[key].Type
}
func (abs *AbstractResource) GetModelKeys() []string {
keys := make([]string, 0)
for k := range abs.ResourceModel.Model {
keys = append(keys, k)
}
return keys
}
func (abs *AbstractResource) GetModelReadOnly(key string) interface{} {
if abs.ResourceModel == nil || abs.ResourceModel.Model == nil {
return nil
}
if _, ok := abs.ResourceModel.Model[key]; !ok {
return nil
}
return abs.ResourceModel.Model[key].ReadOnly
}
type Model struct {
Type string `json:"type,omitempty" bson:"type,omitempty"`
Value interface{} `json:"value,omitempty" bson:"value,omitempty"`
ReadOnly bool `json:"readonly,omitempty" bson:"readonly,omitempty"`
}
type ResourceModel struct {
UUID string `json:"id,omitempty" bson:"id,omitempty" validate:"required"`
ResourceType string `json:"resource_type,omitempty" bson:"resource_type,omitempty" validate:"required"`
Model map[string]Model `json:"model,omitempty" bson:"model,omitempty"`
}
func (ao *ResourceModel) GetID() string {
return ao.UUID
}
func (r *ResourceModel) GenerateID() {
r.UUID = uuid.New().String()
}
func (d *ResourceModel) GetName() string {
return d.UUID
}
func (d *ResourceModel) GetAccessor() utils.Accessor {
data := &ResourceModelMongoAccessor{}
data.SetLogger(utils.RESOURCE_MODEL)
return data
}
func (dma *ResourceModel) 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 *ResourceModel) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}

View File

@@ -0,0 +1,79 @@
package resource_model
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/models/utils"
)
type ResourceModelMongoAccessor struct {
utils.AbstractAccessor
}
func (wfa *ResourceModelMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return wfa.GenericDeleteOne(id, wfa)
}
func (wfa *ResourceModelMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
return wfa.GenericUpdateOne(set, id, wfa, &ResourceModel{})
}
func (wfa *ResourceModelMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *ResourceModelMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *ResourceModelMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var workflow ResourceModel
res_mongo, code, 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, code, err
}
res_mongo.Decode(&workflow)
return &workflow, 200, nil
}
func (wfa ResourceModelMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []ResourceModel
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}
func (wfa *ResourceModelMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{
"resource_type": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []ResourceModel
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}

View File

@@ -0,0 +1,40 @@
package data
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type DataResource struct {
resource_model.AbstractResource
Protocols []string `json:"protocol,omitempty" bson:"protocol,omitempty"` //TODO Enum type
DataType string `json:"datatype,omitempty" bson:"datatype,omitempty"`
Example string `json:"example,omitempty" bson:"example,omitempty" description:"base64 encoded data"`
}
func (dma *DataResource) 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 *DataResource) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}
func (d *DataResource) GetAccessor() utils.Accessor {
data := New()
data.SetLogger(utils.DATA_RESOURCE)
return data
}

View File

@@ -0,0 +1,105 @@
package data
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/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type dataMongoAccessor struct {
utils.AbstractAccessor
}
func New() *dataMongoAccessor {
return &dataMongoAccessor{}
}
func (dma *dataMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return dma.GenericDeleteOne(id, dma)
}
func (dma *dataMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
set.(*DataResource).ResourceModel = nil
return dma.GenericUpdateOne(set, id, dma, &DataResource{})
}
func (dma *dataMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
data.(*DataResource).ResourceModel = nil
return dma.GenericStoreOne(data, dma)
}
func (dma *dataMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return dma.GenericStoreOne(data, dma)
}
func (dma *dataMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var data DataResource
res_mongo, code, err := mongo.MONGOService.LoadOne(id, dma.GetType())
if err != nil {
dma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&data)
accessor := (&resource_model.ResourceModel{}).GetAccessor()
resources, _, err := accessor.Search(nil, dma.GetType())
if err == nil && len(resources) > 0 {
data.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
return &data, 200, nil
}
func (wfa dataMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []DataResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor()
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource)
}
return objs, 200, nil
}
func (wfa *dataMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.short_description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.owner": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.source_url": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []DataResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor()
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource)
}
return objs, 200, nil
}

View File

@@ -0,0 +1,45 @@
package data
import (
"testing"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert"
)
func TestStoreOneData(t *testing.T) {
d := DataResource{DataType: "jpeg", Example: "123456",
AbstractResource: resource_model.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testData"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
dma := New()
id, _, _ := dma.StoreOne(&d)
assert.NotEmpty(t, id)
}
func TestLoadOneDate(t *testing.T) {
d := DataResource{DataType: "jpeg", Example: "123456",
AbstractResource: resource_model.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testData"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
dma := New()
new_d, _, _ := dma.StoreOne(&d)
assert.Equal(t, d, new_d)
}

View File

@@ -0,0 +1,60 @@
package datacenter
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type DatacenterResource struct {
resource_model.AbstractResource
CPUs []*CPU `bson:"cpus,omitempty" json:"cpus,omitempty"`
RAM *RAM `bson:"ram,omitempty" json:"ram,omitempty"`
GPUs []*GPU `bson:"gpus,omitempty" json:"gpus,omitempty"`
}
func (dma *DatacenterResource) 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 *DatacenterResource) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}
func (d *DatacenterResource) GetAccessor() utils.Accessor {
data := New()
data.SetLogger(utils.DATACENTER_RESOURCE)
return data
}
type CPU struct {
Cores uint `bson:"cores,omitempty" json:"cores,omitempty"` //TODO: validate
Architecture string `bson:"architecture,omitempty" json:"architecture,omitempty"` //TOOD: enum
Shared bool `bson:"shared,omitempty" json:"shared,omitempty"`
MinimumMemory uint `bson:"minimum_memory,omitempty" json:"minimum_memory,omitempty"`
Platform string `bson:"platform,omitempty" json:"platform,omitempty"`
}
type RAM struct {
Size uint `bson:"size,omitempty" json:"size,omitempty" description:"Units in MB"`
Ecc bool `bson:"ecc,omitempty" json:"ecc,omitempty"`
}
type GPU struct {
CudaCores uint `bson:"cuda_cores,omitempty" json:"cuda_cores,omitempty"`
Model string `bson:"model,omitempty" json:"model,omitempty"`
Memory uint `bson:"memory,omitempty" json:"memory,omitempty" description:"Units in MB"`
TensorCores uint `bson:"tensor_cores,omitempty" json:"tensor_cores,omitempty"`
}

View File

@@ -0,0 +1,107 @@
package datacenter
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/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type datacenterMongoAccessor struct {
utils.AbstractAccessor
}
func New() *datacenterMongoAccessor {
return &datacenterMongoAccessor{}
}
func (dca *datacenterMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return dca.GenericDeleteOne(id, dca)
}
func (dca *datacenterMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
set.(*DatacenterResource).ResourceModel = nil
return dca.GenericUpdateOne(set, id, dca, &DatacenterResource{})
}
func (dca *datacenterMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
data.(*DatacenterResource).ResourceModel = nil
return dca.GenericStoreOne(data, dca)
}
func (dca *datacenterMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return dca.GenericStoreOne(data, dca)
}
func (dca *datacenterMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var datacenter DatacenterResource
res_mongo, code, err := mongo.MONGOService.LoadOne(id, dca.GetType())
if err != nil {
dca.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&datacenter)
accessor := (&resource_model.ResourceModel{}).GetAccessor()
resources, _, err := accessor.Search(nil, dca.GetType())
if err == nil && len(resources) > 0 {
datacenter.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
return &datacenter, 200, nil
}
func (wfa datacenterMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []DatacenterResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor()
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource)
}
return objs, 200, nil
}
func (wfa *datacenterMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.short_description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.owner": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.source_url": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []DatacenterResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor()
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource)
}
return objs, 200, nil
}

View File

@@ -0,0 +1,46 @@
package datacenter
import (
"testing"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert"
)
func TestStoreOneDatacenter(t *testing.T) {
dc := DatacenterResource{
AbstractResource: resource_model.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testDatacenter"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
dcma := New()
id, _, _ := dcma.StoreOne(&dc)
assert.NotEmpty(t, id)
}
func TestLoadOneDatacenter(t *testing.T) {
dc := DatacenterResource{
AbstractResource: resource_model.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testDatacenter"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
dcma := New()
new_dc, _, _ := dcma.StoreOne(&dc)
assert.Equal(t, dc, new_dc)
}

View File

@@ -0,0 +1,45 @@
package processing
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/resources/datacenter"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type ProcessingResource struct {
resource_model.AbstractResource
CPUs []*datacenter.CPU `bson:"cpus,omitempty" json:"cp_us,omitempty"`
GPUs []*datacenter.GPU `bson:"gpus,omitempty" json:"gp_us,omitempty"`
RAM *datacenter.RAM `bson:"ram,omitempty" json:"ram,omitempty"`
Storage uint `bson:"storage,omitempty" json:"storage,omitempty"`
Parallel bool `bson:"parallel,omitempty" json:"parallel,omitempty"`
ScalingModel uint `bson:"scaling_model,omitempty" json:"scaling_model,omitempty"`
DiskIO string `bson:"disk_io,omitempty" json:"disk_io,omitempty"`
}
func (dma *ProcessingResource) 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 *ProcessingResource) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}
func (d *ProcessingResource) GetAccessor() utils.Accessor {
data := New()
data.SetLogger(utils.PROCESSING_RESOURCE)
return data
}

View File

@@ -0,0 +1,108 @@
package processing
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/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type processingMongoAccessor struct {
utils.AbstractAccessor
}
func New() *processingMongoAccessor {
return &processingMongoAccessor{}
}
func (pma *processingMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return pma.GenericDeleteOne(id, pma)
}
func (pma *processingMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
set.(*ProcessingResource).ResourceModel = nil
return pma.GenericUpdateOne(set, id, pma, &ProcessingResource{})
}
func (pma *processingMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
data.(*ProcessingResource).ResourceModel = nil
return pma.GenericStoreOne(data, pma)
}
func (pma *processingMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return pma.GenericStoreOne(data, pma)
}
func (pma *processingMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var processing ProcessingResource
res_mongo, code, err := mongo.MONGOService.LoadOne(id, pma.GetType())
if err != nil {
pma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&processing)
accessor := (&resource_model.ResourceModel{}).GetAccessor()
resources, _, err := accessor.Search(nil, pma.GetType())
if err == nil && len(resources) > 0 {
processing.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
return &processing, 200, nil
}
func (wfa processingMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []ProcessingResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor()
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource)
}
return objs, 200, nil
}
func (wfa *processingMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.short_description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.owner": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.source_url": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []ProcessingResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor()
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource)
}
return objs, 200, nil
}

View File

@@ -0,0 +1,38 @@
package processing
/*
func TestStoreOneProcessing(t *testing.T) {
p := ProcessingResource{Container: "totoCont",
AbstractResource: resources.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testData"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
sma := ProcessingMongoAccessor{}
id, _, _ := sma.StoreOne(&p)
assert.NotEmpty(t, id)
}
func TestLoadOneProcessing(t *testing.T) {
p := ProcessingResource{Container: "totoCont",
AbstractResource: resources.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testData"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
sma := ProcessingMongoAccessor{}
new_s, _, _ := sma.StoreOne(&p)
assert.Equal(t, p, new_s)
}
*/

View File

@@ -0,0 +1,58 @@
package resources
import (
"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"
)
// 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
// http://www.inanzzz.com/index.php/post/wqbs/a-basic-usage-of-int-and-string-enum-types-in-golang
type ResourceSet struct {
Datas []string `bson:"datas,omitempty" json:"datas,omitempty"`
Storages []string `bson:"storages,omitempty" json:"storages,omitempty"`
Processings []string `bson:"processings,omitempty" json:"processings,omitempty"`
Datacenters []string `bson:"datacenters,omitempty" json:"datacenters,omitempty"`
Workflows []string `bson:"workflows,omitempty" json:"workflows,omitempty"`
DataResources []*data.DataResource `bson:"-" json:"data_resources,omitempty"`
StorageResources []*storage.StorageResource `bson:"-" json:"storage_resources,omitempty"`
ProcessingResources []*processing.ProcessingResource `bson:"-" json:"processing_resources,omitempty"`
DatacenterResources []*datacenter.DatacenterResource `bson:"-" json:"datacenter_resources,omitempty"`
WorkflowResources []*w.WorkflowResource `bson:"-" json:"workflow_resources,omitempty"`
}
type ItemResource struct {
Data *data.DataResource `bson:"data,omitempty" json:"data,omitempty"`
Processing *processing.ProcessingResource `bson:"processing,omitempty" json:"processing,omitempty"`
Storage *storage.StorageResource `bson:"storage,omitempty" json:"storage,omitempty"`
Datacenter *datacenter.DatacenterResource `bson:"datacenter,omitempty" json:"datacenter,omitempty"`
Workflow *w.WorkflowResource `bson:"workflow,omitempty" json:"workflow,omitempty"`
}
func (i *ItemResource) GetAbstractRessource() *resource_model.AbstractResource {
if(i.Data != nil){
return &i.Data.AbstractResource
}
if(i.Processing != nil){
return &i.Processing.AbstractResource
}
if(i.Storage != nil){
return &i.Storage.AbstractResource
}
if(i.Datacenter != nil){
return &i.Datacenter.AbstractResource
}
if(i.Workflow != nil){
return &i.Workflow.AbstractResource
}
return nil
}

View File

@@ -0,0 +1,50 @@
package storage
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type URL struct {
Protocol string `bson:"protocol,omitempty" json:"protocol,omitempty"`
Path string `bson:"path,omitempty" json:"path,omitempty"`
}
type StorageResource struct {
resource_model.AbstractResource
Acronym string `bson:"acronym,omitempty" json:"acronym,omitempty"`
Type string `bson:"type,omitempty" json:"type,omitempty"`
Size uint `bson:"size,omitempty" json:"size,omitempty"`
Url *URL `bson:"url,omitempty" json:"url,omitempty"` // Will allow to select between several protocols
Encryption bool `bson:"encryption,omitempty" json:"encryption,omitempty"`
Redundancy string `bson:"redundancy,omitempty" json:"redundancy,omitempty"`
Throughput string `bson:"throughput,omitempty" json:"throughput,omitempty"`
}
func (dma *StorageResource) 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 *StorageResource) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}
func (d *StorageResource) GetAccessor() utils.Accessor {
data := New()
data.SetLogger(utils.STORAGE_RESOURCE)
return data
}

View File

@@ -0,0 +1,108 @@
package storage
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/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type storageMongoAccessor struct {
utils.AbstractAccessor
}
func New() *storageMongoAccessor {
return &storageMongoAccessor{}
}
func (sma *storageMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return sma.GenericDeleteOne(id, sma)
}
func (sma *storageMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
set.(*StorageResource).ResourceModel = nil
return sma.GenericUpdateOne(set, id, sma, &StorageResource{})
}
func (sma *storageMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
data.(*StorageResource).ResourceModel = nil
return sma.GenericStoreOne(data, sma)
}
func (sma *storageMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return sma.GenericStoreOne(data, sma)
}
func (sma *storageMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var storage StorageResource
res_mongo, code, err := mongo.MONGOService.LoadOne(id, sma.GetType())
if err != nil {
sma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&storage)
accessor := (&resource_model.ResourceModel{}).GetAccessor()
resources, _, err := accessor.Search(nil, sma.GetType())
if err == nil && len(resources) > 0 {
storage.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
return &storage, 200, nil
}
func (wfa storageMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []StorageResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor()
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource)
}
return objs, 200, nil
}
func (wfa *storageMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.short_description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.owner": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.source_url": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []StorageResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor()
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource)
}
return objs, 200, nil
}

View File

@@ -0,0 +1,46 @@
package storage
import (
"testing"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert"
)
func TestStoreOneStorage(t *testing.T) {
s := StorageResource{Size: 123, Url: &URL{Protocol: "http", Path: "azerty.fr"},
AbstractResource: resource_model.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testData"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
sma := New()
id, _, _ := sma.StoreOne(&s)
assert.NotEmpty(t, id)
}
func TestLoadOneStorage(t *testing.T) {
s := StorageResource{Size: 123, Url: &URL{Protocol: "http", Path: "azerty.fr"},
AbstractResource: resource_model.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testData"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
sma := New()
new_s, _, _ := sma.StoreOne(&s)
assert.Equal(t, s, new_s)
}

View File

@@ -0,0 +1,44 @@
package graph
import "cloud.o-forge.io/core/oc-lib/models/resources"
type Graph struct {
Zoom float64 `bson:"zoom" json:"zoom" validate:"required"`
Items map[string]GraphItem `bson:"items" json:"items" default:"{}" validate:"required"`
Links []GraphLink `bson:"links" json:"links" default:"{}" validate:"required"`
}
type GraphItem struct {
ID string `bson:"id" json:"id" validate:"required"`
Width float64 `bson:"width" json:"width" validate:"required"`
Height float64 `bson:"height" json:"height" validate:"required"`
Position Position `bson:"position" json:"position" validate:"required"`
resources.ItemResource
}
type GraphLink struct {
Source Position `bson:"source" json:"source" validate:"required"`
Destination Position `bson:"destination" json:"destination" validate:"required"`
Style GraphLinkStyle `bson:"style" json:"style" validate:"required"`
}
type GraphLinkStyle struct {
Color int64 `bson:"color" json:"color" validate:"required"`
Stroke float64 `bson:"stroke" json:"stroke" validate:"required"`
Tension float64 `bson:"tension" json:"tension"`
HeadRadius float64 `bson:"head_radius" json:"head_radius"`
DashWidth float64 `bson:"dash_width" json:"dash_width"`
DashSpace float64 `bson:"dash_space" json:"dash_space"`
EndArrow Position `bson:"end_arrow" json:"end_arrow"`
StartArrow Position `bson:"start_arrow" json:"start_arrow"`
ArrowStyle int64 `bson:"arrow_style" json:"arrow_style" validate:"required"`
ArrowDirection int64 `bson:"arrow_direction" json:"arrow_direction" validate:"required"`
StartArrowWidth float64 `bson:"start_arrow_width" json:"start_arrow_width"`
EndArrowWidth float64 `bson:"end_arrow_width" json:"end_arrow_width"`
}
type Position struct {
ID string `json:"id" bson:"id" validate:"required"`
X float64 `json:"x" bson:"x" validate:"required"`
Y float64 `json:"y" bson:"y" validate:"required"`
}

View File

@@ -0,0 +1,38 @@
package oclib
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type WorkflowResource struct {
resource_model.AbstractResource
WorkflowID string `bson:"workflow_id,omitempty" json:"workflow_id,omitempty"`
}
func (d *WorkflowResource) GetAccessor() utils.Accessor {
data := New()
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, &m)
return m
}

View File

@@ -0,0 +1,112 @@
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/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type workflowResourceMongoAccessor struct {
utils.AbstractAccessor
}
func New() *workflowResourceMongoAccessor {
return &workflowResourceMongoAccessor{}
}
func (wfa *workflowResourceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return wfa.GenericDeleteOne(id, wfa)
}
func (wfa *workflowResourceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
set.(*WorkflowResource).ResourceModel = nil
return wfa.GenericUpdateOne(set, id, wfa, &WorkflowResource{})
}
func (wfa *workflowResourceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
data.(*WorkflowResource).ResourceModel = nil
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *workflowResourceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
res, _, _ := wfa.LoadOne(data.GetID())
data.(*WorkflowResource).WorkflowID = data.GetID()
if res == nil {
return wfa.GenericStoreOne(data, wfa)
} else {
data.(*WorkflowResource).UUID = res.GetID()
return wfa.GenericUpdateOne(data, res.GetID(), wfa, &WorkflowResource{})
}
}
func (wfa *workflowResourceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var workflow WorkflowResource
res_mongo, code, 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, code, err
}
res_mongo.Decode(&workflow)
accessor := (&resource_model.ResourceModel{}).GetAccessor()
resources, _, err := accessor.Search(nil, wfa.GetType())
if err == nil && len(resources) > 0 {
workflow.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
return &workflow, 200, nil
}
func (wfa workflowResourceMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []WorkflowResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor()
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource)
}
return objs, 200, nil
}
func (wfa *workflowResourceMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.short_description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.description": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.owner": {{Operator: dbs.LIKE.String(), Value: search}},
"abstractresource.source_url": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []WorkflowResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
accessor := (&resource_model.ResourceModel{}).GetAccessor()
resources, _, err := accessor.Search(nil, wfa.GetType())
for _, r := range results {
if err == nil && len(resources) > 0 {
r.ResourceModel = resources[0].(*resource_model.ResourceModel)
}
objs = append(objs, &r.AbstractResource)
}
return objs, 200, nil
}

View File

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

View File

@@ -0,0 +1,43 @@
package oclib
import (
"testing"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/stretchr/testify/assert"
)
func TestStoreOneWorkflow(t *testing.T) {
w := WorkflowResource{AbstractResource: resource_model.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
wma := New()
id, _, _ := wma.StoreOne(&w)
assert.NotEmpty(t, id)
}
func TestLoadOneWorkflow(t *testing.T) {
w := WorkflowResource{AbstractResource: resource_model.AbstractResource{
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
Description: "Lorem Ipsum",
Logo: "azerty.com",
Owner: "toto",
OwnerLogo: "totoLogo",
SourceUrl: "azerty.fr",
},
}
wma := New()
new_w, _, _ := wma.StoreOne(&w)
assert.Equal(t, w, new_w)
}

131
models/utils/abstracts.go Normal file
View File

@@ -0,0 +1,131 @@
package utils
import (
"encoding/json"
"errors"
"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"
"github.com/go-playground/validator/v10"
"github.com/google/uuid"
"github.com/rs/zerolog"
)
var validate = validator.New(validator.WithRequiredStructEnabled())
type AbstractObject struct {
UUID string `json:"id,omitempty" bson:"id,omitempty" validate:"required"`
Name string `json:"name,omitempty" bson:"name,omitempty" validate:"required"`
}
func (ao *AbstractObject) GetID() string {
return ao.UUID
}
func (ao *AbstractObject) GetName() string {
return ao.Name
}
func (dma *AbstractObject) GetAccessor() Accessor {
return nil
}
func (dma *AbstractObject) Deserialize(j map[string]interface{}) DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *AbstractObject) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}
func (r *AbstractObject) GenerateID() {
r.UUID = uuid.New().String()
}
type AbstractAccessor struct {
Logger zerolog.Logger
Type string
}
func (dma *AbstractAccessor) GetType() string {
return dma.Type
}
func (dma *AbstractAccessor) SetLogger(t DataType) {
dma.Logger = logs.CreateLogger(t.String(), "")
dma.Type = t.String()
}
func (wfa *AbstractAccessor) GenericStoreOne(data DBObject, accessor Accessor) (DBObject, int, error) {
data.GenerateID()
f := dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractresource.abstractobject.name": {{
Operator: dbs.LIKE.String(),
Value: data.GetName(),
}},
"abstractobject.name": {{
Operator: dbs.LIKE.String(),
Value: data.GetName(),
}},
},
}
if cursor, _, _ := accessor.Search(&f, ""); len(cursor) > 0 {
return nil, 409, errors.New(accessor.GetType() + " with name " + data.GetName() + " already exists")
}
err := validate.Struct(data)
if err != nil {
return nil, 422, err
}
id, code, err := mongo.MONGOService.StoreOne(data, data.GetID(), wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
return nil, code, err
}
return accessor.LoadOne(id)
}
func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBObject, int, error) {
res, code, err := accessor.LoadOne(id)
if err != nil {
dma.Logger.Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
return nil, code, err
}
_, code, err = mongo.MONGOService.DeleteOne(id, accessor.GetType())
if err != nil {
dma.Logger.Error().Msg("Could not delete " + id + " to db. Error: " + err.Error())
return nil, code, err
}
return res, 200, nil
}
func (dma *AbstractAccessor) GenericUpdateOne(set DBObject, id string, accessor Accessor, new DBObject) (DBObject, int, error) {
r, c, err := accessor.LoadOne(id)
if err != nil {
return nil, c, err
}
change := set.Serialize()
loaded := r.Serialize()
for k, v := range change {
loaded[k] = v
}
id, code, err := mongo.MONGOService.UpdateOne(new.Deserialize(loaded), id, accessor.GetType())
if err != nil {
dma.Logger.Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
return nil, code, err
}
return accessor.LoadOne(id)
}

8
models/utils/common.go Normal file
View File

@@ -0,0 +1,8 @@
package utils
/*
type Price struct {
Price float64 `json:"price,omitempty" bson:"price,omitempty"`
Currency string `json:"currency,omitempty" bson:"currency,omitempty"`
}
*/

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

@@ -0,0 +1,42 @@
package utils
type DataType int
const (
INVALID DataType = iota
DATA_RESOURCE
PROCESSING_RESOURCE
STORAGE_RESOURCE
DATACENTER_RESOURCE
WORKFLOW_RESOURCE
WORKFLOW
WORKFLOW_EXECUTION
WORKSPACE
RESOURCE_MODEL
)
var Str = [...]string{
"invalid",
"data_resource",
"processing_resource",
"storage_resource",
"datacenter_resource",
"workflow_resource",
"workflow",
"workflow_execution",
"workspace",
"resource_model",
}
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

@@ -0,0 +1,32 @@
package utils
import "cloud.o-forge.io/core/oc-lib/dbs"
type ShallowDBObject interface {
GenerateID()
GetID() string
GetName() string
Deserialize(j map[string]interface{}) DBObject
Serialize() map[string]interface{}
}
type DBObject interface {
GenerateID()
GetID() string
GetName() string
Deserialize(j map[string]interface{}) DBObject
Serialize() map[string]interface{}
GetAccessor() Accessor
}
type Accessor interface {
SetLogger(t DataType)
GetType() string
Search(filters *dbs.Filters, search string) ([]ShallowDBObject, int, error)
LoadAll() ([]ShallowDBObject, int, error)
LoadOne(id string) (DBObject, int, error)
DeleteOne(id string) (DBObject, int, error)
CopyOne(data DBObject) (DBObject, int, error)
StoreOne(data DBObject) (DBObject, int, error)
UpdateOne(set DBObject, id string) (DBObject, int, error)
}

View File

@@ -0,0 +1,57 @@
package oclib
import (
"encoding/json"
"slices"
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/resources/workflow/graph"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type AbstractWorkflow struct {
resources.ResourceSet
Graph *graph.Graph `bson:"graph,omitempty" json:"graph,omitempty"`
Schedule *WorkflowSchedule `bson:"schedule,omitempty" json:"schedule,omitempty"`
}
func (w *AbstractWorkflow) isDCLink(link graph.GraphLink) bool {
if slices.Contains(w.Datacenters, link.Destination.ID) || slices.Contains(w.Datacenters, link.Source.ID) {
return true
}
return false
}
type Workflow struct {
utils.AbstractObject
AbstractWorkflow
}
func (d *Workflow) GetName() string {
return d.Name
}
func (d *Workflow) GetAccessor() utils.Accessor {
data := New()
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, &m)
return m
}

View File

@@ -0,0 +1,199 @@
package oclib
import (
"errors"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"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/workflow_execution"
"cloud.o-forge.io/core/oc-lib/models/workspace"
"github.com/vk496/cron"
)
type workflowMongoAccessor struct {
utils.AbstractAccessor
}
func New() *workflowMongoAccessor {
return &workflowMongoAccessor{}
}
func (wfa *workflowMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return wfa.GenericDeleteOne(id, wfa)
}
func (wfa *workflowMongoAccessor) execution(id string, realData *Workflow, delete bool) (int, error) {
if realData.Schedule == nil {
return 200, nil
}
res, _, _ := wfa.LoadOne(id)
r := res.(*Workflow)
if r.Schedule.Start == realData.Schedule.Start && r.Schedule.End == realData.Schedule.End && r.Schedule.Cron == realData.Schedule.Cron {
return 200, nil
}
if delete {
mongo.MONGOService.DeleteMultiple(map[string]interface{}{
"workflow_id": realData.UUID,
}, utils.WORKFLOW_EXECUTION.String())
}
if realData.Schedule != nil {
accessor := (&workflow_execution.WorkflowExecution{}).GetAccessor()
if realData.Schedule.Start == nil || realData.Schedule.Start.IsZero() {
return 422, errors.New("should get a start date on the scheduler.")
}
if realData.Schedule.End != nil && realData.Schedule.End.IsZero() {
realData.Schedule.End = nil
}
if len(realData.Schedule.Cron) > 0 {
if realData.Schedule.End == nil {
return 422, errors.New("a cron task should got a end date.")
}
c, err := cron.Parse(realData.Schedule.Cron)
if err != nil {
return 422, errors.New("Bad cron message: " + err.Error())
}
for s := c.Next(*realData.Schedule.Start); !s.IsZero() && s.Before(*realData.Schedule.End); s = c.Next(s) {
obj := &workflow_execution.WorkflowExecution{
AbstractObject: utils.AbstractObject{
Name: realData.Schedule.Name,
},
ExecDate: &s,
EndDate: realData.Schedule.End,
State: 1,
WorkflowID: realData.UUID,
}
accessor.StoreOne(obj)
}
} else {
obj := &workflow_execution.WorkflowExecution{
AbstractObject: utils.AbstractObject{
Name: realData.Schedule.Name,
},
ExecDate: realData.Schedule.Start,
EndDate: realData.Schedule.End,
State: 1,
WorkflowID: realData.UUID,
}
accessor.StoreOne(obj)
}
}
return 200, nil
}
func (wfa *workflowMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
if code, err := wfa.execution(id, set.(*Workflow), true); err != nil {
return nil, code, err
}
res, code, err := wfa.GenericUpdateOne(set, id, wfa, &Workflow{})
if code != 200 {
return nil, code, err
}
wfa.execute(res.(*Workflow))
return res, code, err
}
func (wfa *workflowMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
res, code, err := wfa.GenericStoreOne(data, wfa)
if err != nil {
return nil, code, err
}
if code, err := wfa.execution(res.GetID(), res.(*Workflow), false); err != nil {
return nil, code, err
}
wfa.execute(res.(*Workflow))
return res, code, err
}
func (wfa *workflowMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *workflowMongoAccessor) execute(workflow *Workflow) {
accessor := (&workspace.Workspace{}).GetAccessor()
filters := &dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractobject.name": {{dbs.LIKE.String(), workflow.Name + "_workspace"}},
},
}
resource, _, err := accessor.Search(filters, "")
if err == nil && len(resource) > 0 {
accessor.UpdateOne(&workspace.Workspace{
Active: true,
ResourceSet: resources.ResourceSet{
Datas: workflow.Datas,
Processings: workflow.Processings,
Storages: workflow.Storages,
Workflows: workflow.Workflows,
Datacenters: workflow.Datacenters,
},
}, resource[0].GetID())
} else {
accessor.StoreOne(&workspace.Workspace{
Active: true,
AbstractObject: utils.AbstractObject{Name: workflow.Name + "_workspace"},
ResourceSet: resources.ResourceSet{
Datas: workflow.Datas,
Processings: workflow.Processings,
Storages: workflow.Storages,
Workflows: workflow.Workflows,
Datacenters: workflow.Datacenters,
},
})
}
}
func (wfa *workflowMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var workflow Workflow
res_mongo, code, 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, code, err
}
res_mongo.Decode(&workflow)
wfa.execute(&workflow)
return &workflow, 200, nil
}
func (wfa workflowMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []Workflow
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r.AbstractObject)
}
return objs, 200, nil
}
func (wfa *workflowMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []Workflow
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}

View File

@@ -0,0 +1,16 @@
package oclib
import "time"
type WorkflowSchedule struct {
Id string `json:"id"`
Name string `json:"name" bson:"name" validate:"required"`
Start *time.Time `json:"start" bson:"start" validate:"required"`
End *time.Time `json:"end,omitempty" bson:"end,omitempty"`
Cron string `json:"cron,omitempty" bson:"cron,omitempty"`
}
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 := New()
id, _, _ := wma.StoreOne(&w)
assert.NotEmpty(t, id)
}
func TestLoadOneWorkflow(t *testing.T) {
w := Workflow{
AbstractObject: utils.AbstractObject{Name: "testWorkflow"},
}
wma := New()
new_w, _, _ := wma.StoreOne(&w)
assert.Equal(t, w, new_w)
}

View File

@@ -0,0 +1,83 @@
package workflow_execution
import (
"encoding/json"
"time"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/google/uuid"
)
type ScheduledType int
const (
SCHEDULED ScheduledType = iota + 1
STARTED
FAILURE
SUCCESS
)
var str = [...]string{
"scheduled",
"started",
"failure",
"success",
}
func FromInt(i int) string {
return str[i]
}
func (d ScheduledType) String() string {
return str[d]
}
// EnumIndex - Creating common behavior-give the type a EnumIndex functio
func (d ScheduledType) EnumIndex() int {
return int(d)
}
type WorkflowExecution struct {
utils.AbstractObject
ExecDate *time.Time `json:"execution_date,omitempty" bson:"execution_date,omitempty" validate:"required"`
EndDate *time.Time `json:"end_date,omitempty" bson:"end_date,omitempty"`
State int64 `json:"state,omitempty" bson:"state,omitempty"`
WorkflowID string `json:"workflow_id" bson:"workflow_id,omitempty"`
}
func (ao *WorkflowExecution) GetID() string {
return ao.UUID
}
func (r *WorkflowExecution) GenerateID() {
r.UUID = uuid.New().String()
}
func (d *WorkflowExecution) GetName() string {
return d.UUID + "_" + d.ExecDate.String()
}
func (d *WorkflowExecution) GetAccessor() utils.Accessor {
data := New()
data.SetLogger(utils.WORKFLOW_EXECUTION)
return data
}
func (dma *WorkflowExecution) 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 *WorkflowExecution) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}

View File

@@ -0,0 +1,84 @@
package workflow_execution
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/models/utils"
)
type workflowExecutionMongoAccessor struct {
utils.AbstractAccessor
}
func New() *workflowExecutionMongoAccessor {
return &workflowExecutionMongoAccessor{}
}
func (wfa *workflowExecutionMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return wfa.GenericDeleteOne(id, wfa)
}
func (wfa *workflowExecutionMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
return wfa.GenericUpdateOne(set, id, wfa, &WorkflowExecution{})
}
func (wfa *workflowExecutionMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *workflowExecutionMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *workflowExecutionMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var workflow WorkflowExecution
res_mongo, code, 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, code, err
}
res_mongo.Decode(&workflow)
return &workflow, 200, nil
}
func (wfa workflowExecutionMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []WorkflowExecution
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r.AbstractObject)
}
return objs, 200, nil
}
func (wfa *workflowExecutionMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []WorkflowExecution
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}

View File

@@ -0,0 +1,53 @@
package workspace
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/google/uuid"
)
type Workspace struct {
utils.AbstractObject
resources.ResourceSet
IsContextual bool `json:"is_contextual" bson:"is_contextual" default:"false"`
Active bool `json:"active" bson:"active" default:"false"`
}
func (ao *Workspace) GetID() string {
return ao.UUID
}
func (r *Workspace) GenerateID() {
r.UUID = uuid.New().String()
}
func (d *Workspace) GetName() string {
return d.Name
}
func (d *Workspace) GetAccessor() utils.Accessor {
data := New()
data.SetLogger(utils.WORKSPACE)
return data
}
func (dma *Workspace) 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 *Workspace) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}

View File

@@ -0,0 +1,161 @@
package workspace
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/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"
)
type workspaceMongoAccessor struct {
utils.AbstractAccessor
}
func New() *workspaceMongoAccessor {
return &workspaceMongoAccessor{}
}
func (wfa *workspaceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return wfa.GenericDeleteOne(id, wfa)
}
func (wfa *workspaceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
d := set.(*Workspace)
d.DataResources = nil
d.DatacenterResources = nil
d.StorageResources = nil
d.ProcessingResources = nil
d.WorkflowResources = nil
if d.Active {
res, _, err := wfa.LoadAll()
if err == nil {
for _, r := range res {
if r.GetID() != id {
r.(*Workspace).Active = false
wfa.UpdateOne(r.(*Workspace), r.GetID())
}
}
}
}
return wfa.GenericUpdateOne(set, id, wfa, &Workspace{})
}
func (wfa *workspaceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
d := data.(*Workspace)
d.DataResources = nil
d.DatacenterResources = nil
d.StorageResources = nil
d.ProcessingResources = nil
d.WorkflowResources = nil
return wfa.GenericStoreOne(d, wfa)
}
func (wfa *workspaceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *workspaceMongoAccessor) fill(workflow *Workspace) *Workspace {
if workflow.Datas != nil && len(workflow.Datas) > 0 {
dataAccessor := (&data.DataResource{}).GetAccessor()
for _, id := range workflow.Datas {
d, _, e := dataAccessor.LoadOne(id)
if e == nil {
workflow.DataResources = append(workflow.DataResources, d.(*data.DataResource))
}
}
}
if workflow.Datacenters != nil && len(workflow.Datacenters) > 0 {
dataAccessor := (&datacenter.DatacenterResource{}).GetAccessor()
for _, id := range workflow.Datacenters {
d, _, e := dataAccessor.LoadOne(id)
if e == nil {
workflow.DatacenterResources = append(workflow.DatacenterResources, d.(*datacenter.DatacenterResource))
}
}
}
if workflow.Storages != nil && len(workflow.Storages) > 0 {
dataAccessor := (&storage.StorageResource{}).GetAccessor()
for _, id := range workflow.Storages {
d, _, e := dataAccessor.LoadOne(id)
if e == nil {
workflow.StorageResources = append(workflow.StorageResources, d.(*storage.StorageResource))
}
}
}
if workflow.Processings != nil && len(workflow.Processings) > 0 {
dataAccessor := (&processing.ProcessingResource{}).GetAccessor()
for _, id := range workflow.Processings {
d, _, e := dataAccessor.LoadOne(id)
if e == nil {
workflow.ProcessingResources = append(workflow.ProcessingResources, d.(*processing.ProcessingResource))
}
}
}
if workflow.Workflows != nil && len(workflow.Workflows) > 0 {
dataAccessor := (&w.WorkflowResource{}).GetAccessor()
for _, id := range workflow.Workflows {
d, _, e := dataAccessor.LoadOne(id)
if e == nil {
workflow.WorkflowResources = append(workflow.WorkflowResources, d.(*w.WorkflowResource))
}
}
}
return workflow
}
func (wfa *workspaceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var workflow Workspace
res_mongo, code, 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, code, err
}
res_mongo.Decode(&workflow)
return wfa.fill(&workflow), 200, nil
}
func (wfa workspaceMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []Workspace
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, wfa.fill(&r))
}
return objs, 200, nil
}
func (wfa *workspaceMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []Workspace
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, wfa.fill(&r))
}
return objs, 200, nil
}

View File

@@ -1,12 +0,0 @@
package oclib
type Resource struct {
Id string
Name string
ShortDescription string
Description string
Logo string
Owner string
OwnerLogo string
SourceUrl string
}

View File

@@ -1 +0,0 @@
package oclib

View File

@@ -1 +0,0 @@
package oclib

1
tests/config.go Normal file
View File

@@ -0,0 +1 @@
package tests

6
tests/oclib_conf.json Normal file
View File

@@ -0,0 +1,6 @@
{
"DB_URL_LOCAL" : "mongodb://127.0.0.1:27017",
"DB_URL_DOCKER": "mongodb://mongo:27017/",
"DBPOINT" : "oclib_tests",
"DCNAME" : "testDC"
}

View File

@@ -1 +0,0 @@
package oclib

View File

@@ -1 +0,0 @@
package oclib