103 lines
4.0 KiB
Go
103 lines
4.0 KiB
Go
package resources
|
|
|
|
import (
|
|
"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/resource_model"
|
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
|
"cloud.o-forge.io/core/oc-lib/tools"
|
|
)
|
|
|
|
type resourceMongoAccessor[T ResourceInterface] struct {
|
|
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
|
|
generateData func() utils.DBObject
|
|
}
|
|
|
|
// New creates a new instance of the computeMongoAccessor
|
|
func NewAccessor[T ResourceInterface](t tools.DataType, request *tools.APIRequest, g func() utils.DBObject) *resourceMongoAccessor[T] {
|
|
if !slices.Contains([]tools.DataType{tools.COMPUTE_RESOURCE, tools.STORAGE_RESOURCE, tools.PROCESSING_RESOURCE, tools.WORKFLOW_RESOURCE, tools.DATA_RESOURCE}, t) {
|
|
return nil
|
|
}
|
|
return &resourceMongoAccessor[T]{
|
|
AbstractAccessor: utils.AbstractAccessor{
|
|
ResourceModelAccessor: resource_model.NewAccessor(),
|
|
Logger: logs.CreateLogger(t.String()), // Create a logger with the data type
|
|
Request: request,
|
|
Type: t,
|
|
},
|
|
generateData: g,
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Nothing special here, just the basic CRUD operations
|
|
*/
|
|
|
|
func (dca *resourceMongoAccessor[T]) DeleteOne(id string) (utils.DBObject, int, error) {
|
|
return utils.GenericDeleteOne(id, dca)
|
|
}
|
|
|
|
func (dca *resourceMongoAccessor[T]) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
|
set.(T).SetResourceModel(nil)
|
|
set.(T).Trim()
|
|
return utils.GenericUpdateOne(set, id, dca, dca.generateData())
|
|
}
|
|
|
|
func (dca *resourceMongoAccessor[T]) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
data.(T).SetResourceModel(nil)
|
|
data.(T).Trim()
|
|
return utils.GenericStoreOne(data, dca)
|
|
}
|
|
|
|
func (dca *resourceMongoAccessor[T]) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
|
return dca.StoreOne(data)
|
|
}
|
|
|
|
func (dca *resourceMongoAccessor[T]) LoadOne(id string) (utils.DBObject, int, error) {
|
|
return utils.GenericLoadOne[T](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
|
resources, _, err := dca.ResourceModelAccessor.Search(nil, dca.GetType().String(), false)
|
|
if err == nil && len(resources) > 0 {
|
|
d.(T).SetResourceModel(resources[0].(*resource_model.ResourceModel))
|
|
}
|
|
d.(T).SetAllowedInstances(dca.Request)
|
|
return d, 200, nil
|
|
}, dca)
|
|
}
|
|
|
|
func (wfa *resourceMongoAccessor[T]) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
resources, _, err := wfa.ResourceModelAccessor.Search(nil, wfa.GetType().String(), isDraft)
|
|
return utils.GenericLoadAll[T](func(d utils.DBObject) utils.ShallowDBObject {
|
|
if err == nil && len(resources) > 0 {
|
|
d.(T).SetResourceModel(resources[0].(*resource_model.ResourceModel))
|
|
}
|
|
d.(T).SetAllowedInstances(wfa.Request)
|
|
return d
|
|
}, isDraft, wfa)
|
|
}
|
|
|
|
func (wfa *resourceMongoAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
|
resources, _, err := wfa.ResourceModelAccessor.Search(nil, wfa.GetType().String(), false)
|
|
return utils.GenericSearch[T](filters, search, wfa.getResourceFilter(search),
|
|
func(d utils.DBObject) utils.ShallowDBObject {
|
|
if err == nil && len(resources) > 0 {
|
|
d.(T).SetResourceModel(resources[0].(*resource_model.ResourceModel))
|
|
}
|
|
d.(T).SetAllowedInstances(wfa.Request)
|
|
return d
|
|
}, isDraft, wfa)
|
|
}
|
|
|
|
func (abs *resourceMongoAccessor[T]) getResourceFilter(search string) *dbs.Filters {
|
|
return &dbs.Filters{
|
|
Or: map[string][]dbs.Filter{ // filter by like name, short_description, description, owner, url if no filters are provided
|
|
"abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
|
|
"abstractresource.short_description": {{Operator: dbs.LIKE.String(), Value: search}},
|
|
"abstractresource.description": {{Operator: dbs.LIKE.String(), Value: search}},
|
|
"abstractresource.owner": {{Operator: dbs.LIKE.String(), Value: search}},
|
|
"abstractresource.source_url": {{Operator: dbs.LIKE.String(), Value: search}},
|
|
},
|
|
}
|
|
}
|