corrected live accessor

This commit is contained in:
mr 2025-06-26 15:57:49 +02:00
parent e600fedcab
commit af0d7807bc
3 changed files with 44 additions and 31 deletions

View File

@ -7,6 +7,8 @@ import (
)
type LiveInterface interface {
utils.DBObject
GetMonitorPath() string
GetResourceAccessor(request *tools.APIRequest) utils.Accessor
GetResource() resources.ResourceInterface
GetResourceInstance() resources.ResourceInstanceITF

View File

@ -1,6 +1,8 @@
package live
import (
"slices"
"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/tools"
@ -42,6 +44,20 @@ type AbstractLive struct {
ResourcesID []string `json:"resources_id" bson:"resources_id"`
}
func (d *AbstractLive) GetMonitorPath() string {
return d.MonitorPath
}
func (d *AbstractLive) GetResourcesID() []string {
return d.ResourcesID
}
func (d *AbstractLive) SetResourcesID(id string) {
if !slices.Contains(d.ResourcesID, id) {
d.ResourcesID = append(d.ResourcesID, id)
}
}
func (r *AbstractLive) GetResourceType() tools.DataType {
return tools.INVALID
}

View File

@ -3,11 +3,9 @@ package live
import (
"encoding/json"
"errors"
"slices"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/logs"
"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/tools"
)
@ -48,67 +46,64 @@ func (a *computeUnitsMongoAccessor[T]) CopyOne(data utils.DBObject) (utils.DBObj
if data.IsDrafted() {
return nil, 422, errors.New("can't publish a drafted compute units")
}
datacenterUnits := data.(*LiveDatacenter)
if datacenterUnits.MonitorPath == "" || datacenterUnits.GetID() != "" {
live := data.(T)
if live.GetMonitorPath() == "" || live.GetID() != "" {
return nil, 422, errors.New("publishing is only allowed is it can be monitored and be accessible")
}
if res, code, err := a.LoadOne(datacenterUnits.GetID()); err != nil {
if res, code, err := a.LoadOne(live.GetID()); err != nil {
return nil, code, err
} else {
datacenterUnits = res.(*LiveDatacenter)
live = res.(T)
}
resAccess := datacenterUnits.GetResourceAccessor(a.Request)
instance := datacenterUnits.GetResourceInstance()
b, _ := json.Marshal(datacenterUnits)
resAccess := live.GetResourceAccessor(a.Request)
instance := live.GetResourceInstance()
b, _ := json.Marshal(live)
json.Unmarshal(b, instance)
if len(datacenterUnits.ResourcesID) > 0 {
for _, r := range datacenterUnits.ResourcesID {
if len(live.GetResourcesID()) > 0 {
for _, r := range live.GetResourcesID() {
// TODO dependent of a existing resource
res, code, err := resAccess.LoadOne(r)
if err == nil {
return nil, code, err
}
existingComputeResource := res.(*resources.ComputeResource)
if existingComputeResource.Architecture != datacenterUnits.Architecture || existingComputeResource.Infrastructure != datacenterUnits.Infrastructure {
return nil, 422, errors.New("should be same architecture & infrastructure from the resource")
}
datacenterUnits.SetResourceInstance(existingComputeResource, instance)
resAccess.UpdateOne(existingComputeResource, existingComputeResource.UUID)
existingResource := live.GetResource()
b, _ := json.Marshal(res)
json.Unmarshal(b, existingResource)
live.SetResourceInstance(existingResource, instance)
resAccess.UpdateOne(existingResource, existingResource.GetID())
}
if datacenterUnits.GetID() != "" {
return a.LoadOne(datacenterUnits.GetID())
if live.GetID() != "" {
return a.LoadOne(live.GetID())
} else {
return a.StoreOne(datacenterUnits)
return a.StoreOne(live)
}
} else {
r := datacenterUnits.GetResource()
b, _ := json.Marshal(datacenterUnits)
r := live.GetResource()
b, _ := json.Marshal(live)
json.Unmarshal(b, &r)
datacenterUnits.SetResourceInstance(r, instance)
live.SetResourceInstance(r, instance)
res, code, err := resAccess.StoreOne(r)
if err != nil {
return nil, code, err
}
if !slices.Contains(datacenterUnits.ResourcesID, res.GetID()) {
datacenterUnits.ResourcesID = append(datacenterUnits.ResourcesID, res.GetID())
}
if datacenterUnits.GetID() != "" {
return a.UpdateOne(datacenterUnits, datacenterUnits.GetID())
live.SetResourcesID(res.GetID())
if live.GetID() != "" {
return a.UpdateOne(live, live.GetID())
} else {
return a.StoreOne(datacenterUnits)
return a.StoreOne(live)
}
}
}
func (a *computeUnitsMongoAccessor[T]) LoadOne(id string) (utils.DBObject, int, error) {
return utils.GenericLoadOne[*LiveDatacenter](id, func(d utils.DBObject) (utils.DBObject, int, error) {
return utils.GenericLoadOne[T](id, func(d utils.DBObject) (utils.DBObject, int, error) {
return d, 200, nil
}, a)
}
func (a *computeUnitsMongoAccessor[T]) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
return utils.GenericLoadAll[*LiveDatacenter](a.getExec(), isDraft, a)
return utils.GenericLoadAll[T](a.getExec(), isDraft, a)
}
func (a *computeUnitsMongoAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {