Live x Resource Synergy
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/logs"
|
||||
"cloud.o-forge.io/core/oc-lib/models/live"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
@@ -68,7 +69,12 @@ func (dca *ResourceMongoAccessor[T]) LoadOne(id string) (utils.DBObject, int, er
|
||||
|
||||
func (dca *ResourceMongoAccessor[T]) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, int, error) {
|
||||
if dca.GetType() == tools.COMPUTE_RESOURCE {
|
||||
return nil, 404, errors.New("can't update a non existing computing units resource not reported onto compute units catalog")
|
||||
delete(set, "architecture")
|
||||
delete(set, "infrastructure")
|
||||
} else if dca.GetType() == tools.SERVICE_RESOURCE {
|
||||
delete(set, "infrastructure")
|
||||
} else if dca.GetType() == tools.STORAGE_RESOURCE {
|
||||
delete(set, "storage_type")
|
||||
}
|
||||
return utils.GenericUpdateOne(set, id, dca)
|
||||
}
|
||||
@@ -78,16 +84,66 @@ func (dca *ResourceMongoAccessor[T]) ShouldVerifyAuth() bool {
|
||||
}
|
||||
|
||||
func (dca *ResourceMongoAccessor[T]) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
var i string
|
||||
idsToUpdate := []string{}
|
||||
var a utils.Accessor
|
||||
if dca.GetType() == tools.COMPUTE_RESOURCE {
|
||||
return nil, 404, errors.New("can't create a non existing computing units resource not reported onto compute units catalog")
|
||||
r := data.(*ComputeResource)
|
||||
if len(r.Instances) == 0 {
|
||||
return nil, 404, errors.New("can't create a non existing computing units resource with no instances")
|
||||
}
|
||||
a = live.NewAccessor[*live.LiveDatacenter](tools.LIVE_DATACENTER, &tools.APIRequest{Admin: true})
|
||||
res, _, _ := a.LoadOne(r.Instances[0].GetID())
|
||||
if res == nil {
|
||||
return nil, 404, errors.New("can't create a non existing computing units resource not reported onto compute units catalog")
|
||||
}
|
||||
if !res.(*live.LiveDatacenter).IsCompatible(data.Serialize(data)) {
|
||||
return nil, 404, errors.New("live computing units target is not compatible")
|
||||
}
|
||||
i = res.GetID()
|
||||
idsToUpdate = res.(*live.LiveDatacenter).ResourcesID
|
||||
} else if dca.GetType() == tools.SERVICE_RESOURCE {
|
||||
r := data.(*ServiceResource)
|
||||
if len(r.Instances) == 0 {
|
||||
return nil, 404, errors.New("can't create a non existing service resource with no instances")
|
||||
}
|
||||
a = live.NewAccessor[*live.LiveService](tools.LIVE_SERVICE, &tools.APIRequest{Admin: true})
|
||||
res, _, _ := a.LoadOne(r.Instances[0].GetID())
|
||||
if res == nil {
|
||||
return nil, 404, errors.New("can't create a non existing service resource not reported onto compute units catalog")
|
||||
}
|
||||
if !res.(*live.LiveService).IsCompatible(data.Serialize(data)) {
|
||||
return nil, 404, errors.New("live service target is not compatible")
|
||||
}
|
||||
i = res.GetID()
|
||||
idsToUpdate = res.(*live.LiveService).ResourcesID
|
||||
} else if dca.GetType() == tools.STORAGE_RESOURCE {
|
||||
r := data.(*StorageResource)
|
||||
if len(r.Instances) == 0 {
|
||||
return nil, 404, errors.New("can't create a non existing storage resource with no instances")
|
||||
}
|
||||
a = live.NewAccessor[*live.LiveStorage](tools.LIVE_STORAGE, &tools.APIRequest{Admin: true})
|
||||
res, _, _ := a.LoadOne(r.Instances[0].GetID())
|
||||
if res == nil {
|
||||
return nil, 404, errors.New("can't create a non existing storage resource not reported onto compute units catalog")
|
||||
}
|
||||
if !res.(*live.LiveStorage).IsCompatible(data.Serialize(data)) {
|
||||
return nil, 404, errors.New("live storage target is not compatible")
|
||||
}
|
||||
i = res.GetID()
|
||||
idsToUpdate = res.(*live.LiveStorage).ResourcesID
|
||||
}
|
||||
return utils.GenericStoreOne(data, dca)
|
||||
res, code, err := utils.GenericStoreOne(data, dca)
|
||||
if res != nil && i != "" {
|
||||
idsToUpdate = append(idsToUpdate, res.GetID())
|
||||
a.UpdateOne(map[string]interface{}{
|
||||
"resources_id": idsToUpdate,
|
||||
}, i)
|
||||
}
|
||||
return res, code, err
|
||||
}
|
||||
|
||||
func (dca *ResourceMongoAccessor[T]) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
if dca.GetType() == tools.COMPUTE_RESOURCE {
|
||||
return nil, 404, errors.New("can't copy/publish a non existing computing units resource not reported onto compute units catalog")
|
||||
}
|
||||
return dca.StoreOne(data)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user