123 lines
4.3 KiB
Go
123 lines
4.3 KiB
Go
|
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"
|
||
|
)
|
||
|
|
||
|
type computeUnitsMongoAccessor[T LiveInterface] struct {
|
||
|
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
||
|
}
|
||
|
|
||
|
// New creates a new instance of the computeUnitsMongoAccessor
|
||
|
func NewAccessor[T LiveInterface](request *tools.APIRequest) *computeUnitsMongoAccessor[T] {
|
||
|
return &computeUnitsMongoAccessor[T]{
|
||
|
AbstractAccessor: utils.AbstractAccessor{
|
||
|
Logger: logs.CreateLogger(tools.LIVE_DATACENTER.String()), // Create a logger with the data type
|
||
|
Request: request,
|
||
|
Type: tools.LIVE_DATACENTER,
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Nothing special here, just the basic CRUD operations
|
||
|
*/
|
||
|
func (a *computeUnitsMongoAccessor[T]) DeleteOne(id string) (utils.DBObject, int, error) {
|
||
|
return utils.GenericDeleteOne(id, a)
|
||
|
}
|
||
|
|
||
|
func (a *computeUnitsMongoAccessor[T]) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||
|
// should verify if a source is existing...
|
||
|
return utils.GenericUpdateOne(set, id, a, &LiveDatacenter{})
|
||
|
}
|
||
|
|
||
|
func (a *computeUnitsMongoAccessor[T]) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||
|
return utils.GenericStoreOne(data.(*LiveDatacenter), a)
|
||
|
}
|
||
|
|
||
|
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")
|
||
|
}
|
||
|
datacenterUnits := data.(*LiveDatacenter)
|
||
|
if datacenterUnits.MonitorPath == "" || datacenterUnits.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 {
|
||
|
return nil, code, err
|
||
|
} else {
|
||
|
datacenterUnits = res.(*LiveDatacenter)
|
||
|
}
|
||
|
resAccess := datacenterUnits.GetResourceAccessor(a.Request)
|
||
|
instance := datacenterUnits.GetResourceInstance()
|
||
|
b, _ := json.Marshal(datacenterUnits)
|
||
|
json.Unmarshal(b, instance)
|
||
|
|
||
|
if len(datacenterUnits.ResourcesID) > 0 {
|
||
|
for _, r := range datacenterUnits.ResourcesID {
|
||
|
// 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)
|
||
|
}
|
||
|
if datacenterUnits.GetID() != "" {
|
||
|
return a.LoadOne(datacenterUnits.GetID())
|
||
|
} else {
|
||
|
return a.StoreOne(datacenterUnits)
|
||
|
}
|
||
|
} else {
|
||
|
r := datacenterUnits.GetResource()
|
||
|
b, _ := json.Marshal(datacenterUnits)
|
||
|
json.Unmarshal(b, &r)
|
||
|
datacenterUnits.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())
|
||
|
} else {
|
||
|
return a.StoreOne(datacenterUnits)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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 d, 200, nil
|
||
|
}, a)
|
||
|
}
|
||
|
|
||
|
func (a *computeUnitsMongoAccessor[T]) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||
|
return utils.GenericLoadAll[*LiveDatacenter](a.getExec(), isDraft, a)
|
||
|
}
|
||
|
|
||
|
func (a *computeUnitsMongoAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||
|
return utils.GenericSearch[*LiveDatacenter](filters, search, (&LiveDatacenter{}).GetObjectFilters(search), a.getExec(), isDraft, a)
|
||
|
}
|
||
|
|
||
|
func (a *computeUnitsMongoAccessor[T]) getExec() func(utils.DBObject) utils.ShallowDBObject {
|
||
|
return func(d utils.DBObject) utils.ShallowDBObject {
|
||
|
return d
|
||
|
}
|
||
|
}
|