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 { type LiveInterface interface {
utils.DBObject
GetMonitorPath() string
GetResourceAccessor(request *tools.APIRequest) utils.Accessor GetResourceAccessor(request *tools.APIRequest) utils.Accessor
GetResource() resources.ResourceInterface GetResource() resources.ResourceInterface
GetResourceInstance() resources.ResourceInstanceITF GetResourceInstance() resources.ResourceInstanceITF

View File

@ -1,6 +1,8 @@
package live package live
import ( import (
"slices"
"cloud.o-forge.io/core/oc-lib/models/resources" "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/utils"
"cloud.o-forge.io/core/oc-lib/tools" "cloud.o-forge.io/core/oc-lib/tools"
@ -42,6 +44,20 @@ type AbstractLive struct {
ResourcesID []string `json:"resources_id" bson:"resources_id"` 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 { func (r *AbstractLive) GetResourceType() tools.DataType {
return tools.INVALID return tools.INVALID
} }

View File

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