package resources import ( "errors" "fmt" "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/live" "cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/tools" ) type ResourceMongoAccessor[T ResourceInterface] struct { utils.AbstractAccessor[ResourceInterface] // AbstractAccessor contains the basic fields of an accessor (model, caller) } // New creates a new instance of the computeMongoAccessor func NewAccessor[T ResourceInterface](t tools.DataType, request *tools.APIRequest) *ResourceMongoAccessor[T] { if !slices.Contains([]tools.DataType{ tools.COMPUTE_RESOURCE, tools.STORAGE_RESOURCE, tools.PROCESSING_RESOURCE, tools.SERVICE_RESOURCE, tools.WORKFLOW_RESOURCE, tools.DATA_RESOURCE, tools.NATIVE_TOOL, }, t) { return nil } return &ResourceMongoAccessor[T]{ AbstractAccessor: utils.AbstractAccessor[ResourceInterface]{ Logger: logs.CreateLogger(t.String()), // Create a logger with the data type Request: request, Type: t, New: func() ResourceInterface { switch t { case tools.COMPUTE_RESOURCE: return &ComputeResource{} case tools.STORAGE_RESOURCE: return &StorageResource{} case tools.PROCESSING_RESOURCE: return &ProcessingResource{} case tools.SERVICE_RESOURCE: return &ServiceResource{} case tools.WORKFLOW_RESOURCE: return &WorkflowResource{} case tools.DATA_RESOURCE: return &DataResource{} case tools.NATIVE_TOOL: return &NativeTool{} } return nil }, }, } } /* * Nothing special here, just the basic CRUD operations */ func (dca *ResourceMongoAccessor[T]) LoadOne(id string) (utils.DBObject, int, error) { data, code, err := dca.AbstractAccessor.LoadOne(id) if err == nil { data.(T).VerifyBuy() data.(T).SetAllowedInstances(dca.Request) return data, code, err } return data, code, err } func (dca *ResourceMongoAccessor[T]) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, int, error) { if dca.GetType() == tools.COMPUTE_RESOURCE { 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) } func (dca *ResourceMongoAccessor[T]) ShouldVerifyAuth() bool { return false // TEMP : by pass } 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 { 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 } 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) { return dca.StoreOne(data) } func (wfa *ResourceMongoAccessor[T]) LoadAll(isDraft bool, offset int64, limit int64) ([]utils.ShallowDBObject, int, error) { return utils.GenericLoadAll[T](wfa.GetExec(isDraft), isDraft, wfa, offset, limit) } func (wfa *ResourceMongoAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool, offset int64, limit int64) ([]utils.ShallowDBObject, int, error) { if filters == nil && search == "*" { return utils.GenericLoadAll[T](func(d utils.DBObject) utils.ShallowDBObject { fmt.Println("Search", d) d.(T).VerifyBuy() d.(T).SetAllowedInstances(wfa.Request) fmt.Println("Search2", d) return d }, isDraft, wfa, offset, limit) } return utils.GenericSearch[T](filters, search, wfa.GetObjectFilters(search), func(d utils.DBObject) utils.ShallowDBObject { d.(T).VerifyBuy() d.(T).SetAllowedInstances(wfa.Request) return d }, isDraft, wfa, offset, limit) } func (a *ResourceMongoAccessor[T]) GetExec(isDraft bool) func(utils.DBObject) utils.ShallowDBObject { return func(d utils.DBObject) utils.ShallowDBObject { d.(T).VerifyBuy() d.(T).SetAllowedInstances(a.Request) return d } } func (abs *ResourceMongoAccessor[T]) GetObjectFilters(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 "abstractinstanciatedresource.abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}}, "abstractinstanciatedresource.abstractresource.type": {{Operator: dbs.LIKE.String(), Value: search}}, "abstractinstanciatedresource.abstractresource.short_description": {{Operator: dbs.LIKE.String(), Value: search}}, "abstractinstanciatedresource.abstractresource.description": {{Operator: dbs.LIKE.String(), Value: search}}, "abstractinstanciatedresource.abstractresource.owners.name": {{Operator: dbs.LIKE.String(), Value: search}}, }, } }