2024-07-18 11:51:12 +02:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2024-08-05 10:10:58 +02:00
|
|
|
"encoding/json"
|
2024-07-31 10:35:03 +02:00
|
|
|
"errors"
|
2024-07-26 16:41:08 +02:00
|
|
|
|
2024-08-01 09:13:10 +02:00
|
|
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
2024-07-18 13:35:14 +02:00
|
|
|
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
|
|
|
"cloud.o-forge.io/core/oc-lib/logs"
|
2024-08-12 16:11:25 +02:00
|
|
|
"cloud.o-forge.io/core/oc-lib/tools"
|
2024-07-18 18:14:12 +02:00
|
|
|
"github.com/go-playground/validator/v10"
|
2024-07-19 10:54:58 +02:00
|
|
|
"github.com/google/uuid"
|
2024-07-18 11:51:12 +02:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
)
|
|
|
|
|
2024-08-30 14:50:48 +02:00
|
|
|
// single instance of the validator used in every model Struct to validate the fields
|
2024-07-19 10:54:58 +02:00
|
|
|
var validate = validator.New(validator.WithRequiredStructEnabled())
|
|
|
|
|
2024-08-30 14:50:48 +02:00
|
|
|
/*
|
|
|
|
* AbstractObject is a struct that represents the basic fields of an object
|
|
|
|
* it defines the object id and name
|
|
|
|
* every data in base root model should inherit from this struct (only exception is the ResourceModel)
|
|
|
|
*/
|
2024-07-19 10:54:58 +02:00
|
|
|
type AbstractObject struct {
|
2024-07-22 16:39:26 +02:00
|
|
|
UUID string `json:"id,omitempty" bson:"id,omitempty" validate:"required"`
|
|
|
|
Name string `json:"name,omitempty" bson:"name,omitempty" validate:"required"`
|
2024-07-19 10:54:58 +02:00
|
|
|
}
|
|
|
|
|
2024-08-30 14:50:48 +02:00
|
|
|
// GetID returns the id of the object (abstract)
|
2024-07-19 13:27:34 +02:00
|
|
|
func (ao *AbstractObject) GetID() string {
|
|
|
|
return ao.UUID
|
|
|
|
}
|
|
|
|
|
2024-08-30 14:50:48 +02:00
|
|
|
// GetName returns the name of the object (abstract)
|
2024-07-23 11:22:50 +02:00
|
|
|
func (ao *AbstractObject) GetName() string {
|
|
|
|
return ao.Name
|
|
|
|
}
|
|
|
|
|
2024-08-30 14:50:48 +02:00
|
|
|
// GetAccessor returns the accessor of the object (abstract)
|
2024-08-12 16:11:25 +02:00
|
|
|
func (dma *AbstractObject) GetAccessor(caller *tools.HTTPCaller) Accessor {
|
2024-08-05 10:10:58 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-07-19 10:54:58 +02:00
|
|
|
func (r *AbstractObject) GenerateID() {
|
2024-08-13 09:49:42 +02:00
|
|
|
if r.UUID == "" {
|
|
|
|
r.UUID = uuid.New().String()
|
|
|
|
}
|
2024-07-19 10:54:58 +02:00
|
|
|
}
|
2024-07-18 18:14:12 +02:00
|
|
|
|
2024-07-18 11:51:12 +02:00
|
|
|
type AbstractAccessor struct {
|
2024-08-30 14:50:48 +02:00
|
|
|
Logger zerolog.Logger // Logger is the logger of the accessor, it's a specilized logger for the accessor
|
|
|
|
Type string // Type is the data type of the accessor
|
|
|
|
Caller *tools.HTTPCaller // Caller is the http caller of the accessor (optionnal) only need in a peer connection
|
2024-07-18 11:51:12 +02:00
|
|
|
}
|
|
|
|
|
2024-07-18 16:46:54 +02:00
|
|
|
func (dma *AbstractAccessor) GetType() string {
|
|
|
|
return dma.Type
|
|
|
|
}
|
|
|
|
|
2024-08-12 16:11:25 +02:00
|
|
|
func (dma *AbstractAccessor) GetCaller() *tools.HTTPCaller {
|
|
|
|
return dma.Caller
|
|
|
|
}
|
|
|
|
|
2024-08-30 14:50:48 +02:00
|
|
|
// Init initializes the accessor with the data type and the http caller
|
2024-08-12 16:11:25 +02:00
|
|
|
func (dma *AbstractAccessor) Init(t DataType, caller *tools.HTTPCaller) {
|
2024-09-04 10:53:12 +02:00
|
|
|
dma.Logger = logs.CreateLogger(t.String()) // Create a logger with the data type
|
|
|
|
dma.Caller = caller // Set the caller
|
|
|
|
dma.Type = t.String() // Set the data type
|
2024-07-18 11:51:12 +02:00
|
|
|
}
|
2024-07-19 10:54:58 +02:00
|
|
|
|
2024-08-30 14:50:48 +02:00
|
|
|
// GenericLoadOne loads one object from the database (generic)
|
2024-07-19 11:27:58 +02:00
|
|
|
func (wfa *AbstractAccessor) GenericStoreOne(data DBObject, accessor Accessor) (DBObject, int, error) {
|
2024-07-19 11:00:15 +02:00
|
|
|
data.GenerateID()
|
2024-08-01 09:13:10 +02:00
|
|
|
f := dbs.Filters{
|
2024-08-02 14:07:43 +02:00
|
|
|
Or: map[string][]dbs.Filter{
|
|
|
|
"abstractresource.abstractobject.name": {{
|
2024-08-01 09:13:10 +02:00
|
|
|
Operator: dbs.LIKE.String(),
|
|
|
|
Value: data.GetName(),
|
2024-08-02 14:07:43 +02:00
|
|
|
}},
|
|
|
|
"abstractobject.name": {{
|
2024-08-01 09:13:10 +02:00
|
|
|
Operator: dbs.LIKE.String(),
|
|
|
|
Value: data.GetName(),
|
2024-08-02 14:07:43 +02:00
|
|
|
}},
|
2024-08-01 09:13:10 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if cursor, _, _ := accessor.Search(&f, ""); len(cursor) > 0 {
|
2024-07-31 10:35:03 +02:00
|
|
|
return nil, 409, errors.New(accessor.GetType() + " with name " + data.GetName() + " already exists")
|
|
|
|
}
|
2024-07-19 10:54:58 +02:00
|
|
|
err := validate.Struct(data)
|
|
|
|
if err != nil {
|
2024-07-19 11:27:58 +02:00
|
|
|
return nil, 422, err
|
2024-07-19 10:54:58 +02:00
|
|
|
}
|
2024-07-22 14:02:48 +02:00
|
|
|
id, code, err := mongo.MONGOService.StoreOne(data, data.GetID(), wfa.GetType())
|
2024-07-19 10:54:58 +02:00
|
|
|
if err != nil {
|
|
|
|
wfa.Logger.Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
|
2024-07-19 11:27:58 +02:00
|
|
|
return nil, code, err
|
2024-07-19 10:54:58 +02:00
|
|
|
}
|
|
|
|
return accessor.LoadOne(id)
|
|
|
|
}
|
|
|
|
|
2024-08-30 14:50:48 +02:00
|
|
|
// GenericLoadOne loads one object from the database (generic)
|
2024-07-19 11:27:58 +02:00
|
|
|
func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBObject, int, error) {
|
|
|
|
res, code, err := accessor.LoadOne(id)
|
2024-07-18 15:35:30 +02:00
|
|
|
if err != nil {
|
|
|
|
dma.Logger.Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
|
2024-07-19 11:27:58 +02:00
|
|
|
return nil, code, err
|
2024-07-18 15:35:30 +02:00
|
|
|
}
|
2024-07-19 11:27:58 +02:00
|
|
|
_, code, err = mongo.MONGOService.DeleteOne(id, accessor.GetType())
|
2024-07-18 11:51:12 +02:00
|
|
|
if err != nil {
|
|
|
|
dma.Logger.Error().Msg("Could not delete " + id + " to db. Error: " + err.Error())
|
2024-07-19 11:27:58 +02:00
|
|
|
return nil, code, err
|
2024-07-18 11:51:12 +02:00
|
|
|
}
|
2024-07-19 11:27:58 +02:00
|
|
|
return res, 200, nil
|
2024-07-18 11:51:12 +02:00
|
|
|
}
|
|
|
|
|
2024-08-30 14:50:48 +02:00
|
|
|
// GenericLoadOne loads one object from the database (generic)
|
|
|
|
// json expected in entry is a flatted object no need to respect the inheritance hierarchy
|
2024-07-24 14:49:04 +02:00
|
|
|
func (dma *AbstractAccessor) GenericUpdateOne(set DBObject, id string, accessor Accessor, new DBObject) (DBObject, int, error) {
|
2024-07-23 08:40:24 +02:00
|
|
|
r, c, err := accessor.LoadOne(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, c, err
|
|
|
|
}
|
2024-08-30 14:50:48 +02:00
|
|
|
change := set.Serialize() // get the changes
|
|
|
|
loaded := r.Serialize() // get the loaded object
|
2024-07-24 14:49:04 +02:00
|
|
|
|
2024-08-30 14:50:48 +02:00
|
|
|
for k, v := range change { // apply the changes, with a flatten method
|
2024-07-24 14:49:04 +02:00
|
|
|
loaded[k] = v
|
|
|
|
}
|
|
|
|
id, code, err := mongo.MONGOService.UpdateOne(new.Deserialize(loaded), id, accessor.GetType())
|
2024-07-18 11:51:12 +02:00
|
|
|
if err != nil {
|
|
|
|
dma.Logger.Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
|
2024-07-19 11:27:58 +02:00
|
|
|
return nil, code, err
|
2024-07-18 11:51:12 +02:00
|
|
|
}
|
|
|
|
return accessor.LoadOne(id)
|
|
|
|
}
|