oc-lib/models/utils/abstracts.go

236 lines
7.5 KiB
Go
Raw Normal View History

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-10-02 12:23:22 +02:00
"time"
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/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-11-28 13:19:01 +01:00
mgb "go.mongodb.org/mongo-driver/mongo"
2024-07-18 11:51:12 +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())
/*
* 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-10-02 12:23:22 +02:00
UUID string `json:"id,omitempty" bson:"id,omitempty" validate:"required"`
Name string `json:"name,omitempty" bson:"name,omitempty" validate:"required"`
UpdateDate time.Time `json:"update_date" bson:"update_date"`
LastPeerWriter string `json:"last_peer_writer" bson:"last_peer_writer"`
2024-07-19 10:54:58 +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
}
// 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-10-02 12:23:22 +02:00
func (ao *AbstractObject) UpToDate() {
ao.UpdateDate = time.Now()
2024-10-30 11:17:52 +01:00
// ao.LastPeerWriter, _ = static.GetMyLocalJsonPeer()
2024-10-02 12:23:22 +02:00
}
// GetAccessor returns the accessor of the object (abstract)
2024-11-28 11:05:54 +01:00
func (dma *AbstractObject) GetAccessor(peerID string, groups []string, caller *tools.HTTPCaller) Accessor {
2024-08-05 10:10:58 +02:00
return nil
}
2024-11-28 11:05:54 +01:00
func (ao *AbstractObject) VerifyAuth(peerID string, groups []string) bool {
return true
}
func (dma *AbstractObject) Deserialize(j map[string]interface{}, obj DBObject) DBObject {
2024-08-05 10:10:58 +02:00
b, err := json.Marshal(j)
if err != nil {
return nil
}
2024-11-28 11:05:54 +01:00
json.Unmarshal(b, obj)
return obj
2024-08-05 10:10:58 +02:00
}
2024-11-28 11:05:54 +01:00
func (dma *AbstractObject) Serialize(obj DBObject) map[string]interface{} {
2024-08-05 10:10:58 +02:00
var m map[string]interface{}
2024-11-28 11:05:54 +01:00
b, err := json.Marshal(obj)
2024-08-05 10:10:58 +02:00
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 {
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-11-28 11:05:54 +01:00
PeerID string // PeerID is the id of the peer
Groups []string // Groups is the list of groups that can access the accessor
ResourceModelAccessor Accessor
}
func (dma *AbstractAccessor) VerifyAuth() string {
return ""
2024-07-18 11:51:12 +02:00
}
2024-07-18 16:46:54 +02:00
func (dma *AbstractAccessor) GetType() string {
return dma.Type
}
func (dma *AbstractAccessor) GetCaller() *tools.HTTPCaller {
return dma.Caller
}
// 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)
}
// 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
}
// 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-11-28 11:05:54 +01:00
change := set.Serialize(set) // get the changes
loaded := r.Serialize(r) // get the loaded object
2024-07-24 14:49:04 +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
}
2024-11-28 11:05:54 +01:00
id, code, err := mongo.MONGOService.UpdateOne(new.Deserialize(loaded, new), 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)
}
2024-11-12 12:45:03 +01:00
2024-11-28 11:05:54 +01:00
func GenericLoadOne[T DBObject](id string, f func(DBObject) (DBObject, int, error), wfa Accessor) (DBObject, int, error) {
var data T
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
if !data.VerifyAuth(wfa.GetPeerID(), wfa.GetGroups()) {
return nil, 403, errors.New("You are not allowed to access this collaborative area")
}
if err != nil {
wfa.GetLogger().Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&data)
return f(data)
}
2024-11-28 13:19:01 +01:00
func genericLoadAll[T DBObject](res *mgb.Cursor, code int, err error, f func(DBObject) ShallowDBObject, wfa Accessor) ([]ShallowDBObject, int, error) {
2024-11-28 11:05:54 +01:00
objs := []ShallowDBObject{}
2024-11-28 13:19:01 +01:00
results := []T{}
2024-11-28 11:05:54 +01:00
if err != nil {
wfa.GetLogger().Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
2024-11-28 13:19:01 +01:00
if err = res.All(mongo.MngoCtx, &results); err != nil {
2024-11-28 11:05:54 +01:00
return nil, 404, err
}
for _, r := range results {
if !r.VerifyAuth(wfa.GetPeerID(), wfa.GetGroups()) {
continue
}
2024-11-28 13:19:01 +01:00
objs = append(objs, f(r))
2024-11-28 11:05:54 +01:00
}
return objs, 200, nil
}
2024-11-28 13:19:01 +01:00
func GenericLoadAll[T DBObject](f func(DBObject) ShallowDBObject, wfa Accessor) ([]ShallowDBObject, int, error) {
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
return genericLoadAll[T](res_mongo, code, err, f, wfa)
}
2024-11-28 11:05:54 +01:00
func GenericSearch[T DBObject](filters *dbs.Filters, search string, defaultFilters *dbs.Filters,
2024-11-28 13:19:01 +01:00
f func(DBObject) ShallowDBObject, wfa Accessor) ([]ShallowDBObject, int, error) {
2024-11-28 11:05:54 +01:00
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = defaultFilters
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
2024-11-28 13:19:01 +01:00
return genericLoadAll[T](res_mongo, code, err, f, wfa)
2024-11-28 11:05:54 +01:00
}
2024-11-12 12:45:03 +01: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-11-12 13:36:18 +01:00
func (dma *AbstractAccessor) GenericRawUpdateOne(set DBObject, id string, accessor Accessor) (DBObject, int, error) {
2024-11-12 12:45:03 +01:00
id, code, err := mongo.MONGOService.UpdateOne(set, 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)
}
2024-11-28 11:05:54 +01:00
func (dma *AbstractAccessor) GetPeerID() string {
return dma.PeerID
}
func (dma *AbstractAccessor) GetGroups() []string {
return dma.Groups
}
func (dma *AbstractAccessor) GetLogger() *zerolog.Logger {
return &dma.Logger
}