Files
oc-lib/models/live/live_mongo_accessor.go

93 lines
2.6 KiB
Go
Raw Normal View History

2025-06-24 11:29:04 +02:00
package live
import (
"encoding/json"
"errors"
"cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
)
type computeUnitsMongoAccessor[T LiveInterface] struct {
2026-02-18 12:24:19 +01:00
utils.AbstractAccessor[LiveInterface] // AbstractAccessor contains the basic fields of an accessor (model, caller)
2025-06-24 11:29:04 +02:00
}
// New creates a new instance of the computeUnitsMongoAccessor
2025-06-26 15:59:21 +02:00
func NewAccessor[T LiveInterface](t tools.DataType, request *tools.APIRequest) *computeUnitsMongoAccessor[T] {
2025-06-24 11:29:04 +02:00
return &computeUnitsMongoAccessor[T]{
2026-02-18 12:24:19 +01:00
AbstractAccessor: utils.AbstractAccessor[LiveInterface]{
2025-06-26 15:59:21 +02:00
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
2025-06-24 11:29:04 +02:00
Request: request,
2025-06-26 15:59:21 +02:00
Type: t,
2026-02-18 12:24:19 +01:00
New: func() LiveInterface {
switch t {
case tools.LIVE_DATACENTER:
return &LiveDatacenter{}
case tools.LIVE_STORAGE:
return &LiveStorage{}
}
return &LiveDatacenter{}
},
2025-06-24 11:29:04 +02:00
},
}
}
/*
* Nothing special here, just the basic CRUD operations
*/
func (a *computeUnitsMongoAccessor[T]) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
// is a publisher... that become a resources.
if data.IsDrafted() {
return nil, 422, errors.New("can't publish a drafted compute units")
}
2025-06-26 15:57:49 +02:00
live := data.(T)
if live.GetMonitorPath() == "" || live.GetID() != "" {
2025-06-24 11:29:04 +02:00
return nil, 422, errors.New("publishing is only allowed is it can be monitored and be accessible")
}
2025-06-26 15:57:49 +02:00
if res, code, err := a.LoadOne(live.GetID()); err != nil {
2025-06-24 11:29:04 +02:00
return nil, code, err
} else {
2025-06-26 15:57:49 +02:00
live = res.(T)
2025-06-24 11:29:04 +02:00
}
2025-06-26 15:57:49 +02:00
resAccess := live.GetResourceAccessor(a.Request)
instance := live.GetResourceInstance()
b, _ := json.Marshal(live)
2025-06-24 11:29:04 +02:00
json.Unmarshal(b, instance)
2025-06-26 15:57:49 +02:00
if len(live.GetResourcesID()) > 0 {
for _, r := range live.GetResourcesID() {
2025-06-24 11:29:04 +02:00
// TODO dependent of a existing resource
res, code, err := resAccess.LoadOne(r)
if err == nil {
return nil, code, err
}
2025-06-26 15:57:49 +02:00
existingResource := live.GetResource()
b, _ := json.Marshal(res)
json.Unmarshal(b, existingResource)
live.SetResourceInstance(existingResource, instance)
resAccess.UpdateOne(existingResource, existingResource.GetID())
2025-06-24 11:29:04 +02:00
}
2025-06-26 15:57:49 +02:00
if live.GetID() != "" {
return a.LoadOne(live.GetID())
2025-06-24 11:29:04 +02:00
} else {
2025-06-26 15:57:49 +02:00
return a.StoreOne(live)
2025-06-24 11:29:04 +02:00
}
} else {
2025-06-26 15:57:49 +02:00
r := live.GetResource()
b, _ := json.Marshal(live)
2025-06-24 11:29:04 +02:00
json.Unmarshal(b, &r)
2025-06-26 15:57:49 +02:00
live.SetResourceInstance(r, instance)
2025-06-24 11:29:04 +02:00
res, code, err := resAccess.StoreOne(r)
if err != nil {
return nil, code, err
}
2025-06-26 15:57:49 +02:00
live.SetResourcesID(res.GetID())
if live.GetID() != "" {
return a.UpdateOne(live, live.GetID())
2025-06-24 11:29:04 +02:00
} else {
2025-06-26 15:57:49 +02:00
return a.StoreOne(live)
2025-06-24 11:29:04 +02:00
}
}
}