Live x Resource Synergy
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
package live
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/logs"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
@@ -32,67 +29,11 @@ func NewAccessor[T LiveInterface](t tools.DataType, request *tools.APIRequest) *
|
||||
}
|
||||
return &LiveDatacenter{}
|
||||
},
|
||||
NotImplemented: []string{"CopyOne"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Nothing special here, just the basic CRUD operations
|
||||
*/
|
||||
func (a *liveMongoAccessor[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")
|
||||
}
|
||||
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(live.GetID()); err != nil {
|
||||
return nil, code, err
|
||||
} else {
|
||||
live = res.(T)
|
||||
}
|
||||
resAccess := live.GetResourceAccessor(a.Request)
|
||||
instance := live.GetResourceInstance()
|
||||
b, _ := json.Marshal(live)
|
||||
json.Unmarshal(b, instance)
|
||||
|
||||
if len(live.GetResourcesID()) > 0 {
|
||||
for _, r := range live.GetResourcesID() {
|
||||
res, code, err := resAccess.LoadOne(r)
|
||||
if err == nil {
|
||||
return nil, code, err
|
||||
}
|
||||
existingResource := live.GetResource()
|
||||
b, _ := json.Marshal(res)
|
||||
json.Unmarshal(b, existingResource)
|
||||
live.SetResourceInstance(existingResource, instance)
|
||||
resAccess.UpdateOne(existingResource.Serialize(existingResource), existingResource.GetID())
|
||||
}
|
||||
if live.GetID() != "" {
|
||||
return a.LoadOne(live.GetID())
|
||||
} else {
|
||||
return a.StoreOne(live)
|
||||
}
|
||||
} else {
|
||||
r := live.GetResource()
|
||||
b, _ := json.Marshal(live)
|
||||
json.Unmarshal(b, &r)
|
||||
live.SetResourceInstance(r, instance)
|
||||
res, code, err := utils.GenericStoreOne(r, resAccess)
|
||||
if err != nil {
|
||||
return nil, code, err
|
||||
}
|
||||
live.SetResourcesID(res.GetID())
|
||||
if live.GetID() != "" {
|
||||
return a.UpdateOne(live.Serialize(live), live.GetID())
|
||||
} else {
|
||||
return a.StoreOne(live)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (wfa *liveMongoAccessor[T]) LoadAll(isDraft bool, offset int64, limit int64) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericLoadAll[T](wfa.GetExec(isDraft), isDraft, wfa, offset, limit)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user