package resources import ( "encoding/json" "errors" "fmt" "slices" "cloud.o-forge.io/core/oc-lib/config" "cloud.o-forge.io/core/oc-lib/dbs" "cloud.o-forge.io/core/oc-lib/logs" "cloud.o-forge.io/core/oc-lib/models/common/models" "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) } func sourceFromAccess(access *ResourceAccess) string { if access == nil { return "" } if access.Container != nil && access.Container.Source != "" { return access.Container.Source } if access.Source != nil && access.Source.Source != "" { return access.Source.Source } return "" } func upsertSourceParam(outputs []models.Param, source string) []models.Param { for i, p := range outputs { if p.Attr == "source" { outputs[i].Value = source return outputs } } return append(outputs, models.Param{Attr: "source", Value: source, Readonly: true}) } func applyAccessSourceOutput(data utils.DBObject) { switch r := data.(type) { case *ProcessingResource: for _, inst := range r.Instances { if src := sourceFromAccess(inst.Access); src != "" { r.Outputs = upsertSourceParam(r.Outputs, src) return } } case *DataResource: for _, inst := range r.Instances { if src := sourceFromAccess(inst.Access); src != "" { r.Outputs = upsertSourceParam(r.Outputs, src) return } } } } // 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 } var workspaceResourceTypes = []tools.DataType{ tools.COMPUTE_RESOURCE, tools.DATA_RESOURCE, tools.PROCESSING_RESOURCE, tools.STORAGE_RESOURCE, tools.WORKFLOW_RESOURCE, tools.SERVICE_RESOURCE, } func emitResourceNATS(method tools.NATSMethod, dt tools.DataType, payload []byte) { if !slices.Contains(workspaceResourceTypes, dt) { return } tools.NewNATSCaller().SetNATSPub(method, tools.NATSResponse{ FromApp: config.GetAppName(), Datatype: dt, Method: int(method), Payload: payload, }) } func (dca *ResourceMongoAccessor[T]) DeleteOne(id string) (utils.DBObject, int, error) { data, code, err := dca.AbstractAccessor.LoadOne(id) if err != nil { return data, code, err } res, code, err := dca.AbstractAccessor.DeleteOne(id) if err == nil && data != nil { b, _ := json.Marshal(data) go emitResourceNATS(tools.REMOVE_RESOURCE, dca.GetType(), b) } return res, 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") } if dca.GetType() == tools.PROCESSING_RESOURCE || dca.GetType() == tools.DATA_RESOURCE { if merged, _, _, err := utils.ModelGenericUpdateOne(set, id, dca); err == nil { applyAccessSourceOutput(merged) if serialized := merged.Serialize(merged); serialized != nil { set["outputs"] = serialized["outputs"] } } } 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()) fmt.Println(res, 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 service 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 storage 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 } applyAccessSourceOutput(data) 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) } if err == nil && res != nil { b, _ := json.Marshal(res) go emitResourceNATS(tools.CREATE_RESOURCE, dca.GetType(), b) } return res, code, err } // PurgedResourcePayload holds a silently-deleted resource's type and serialized payload. type PurgedResourcePayload struct { DT tools.DataType Payload []byte } // purgeByType searches and silently deletes all resources of type T created by creatorID. // Uses AbstractAccessor.DeleteOne directly to bypass the NATS-emitting override. func purgeByType[T ResourceInterface](dt tools.DataType, creatorID string) []PurgedResourcePayload { a := NewAccessor[T](dt, nil) if a == nil { return nil } res, _, _ := a.Search(&dbs.Filters{ And: map[string][]dbs.Filter{ "creator_id": {{Operator: dbs.EQUAL.String(), Value: creatorID}}, }, }, "", false, 0, 10000) var result []PurgedResourcePayload for _, item := range res { b, err := json.Marshal(item) if err != nil { continue } a.AbstractAccessor.DeleteOne(item.GetID()) result = append(result, PurgedResourcePayload{DT: dt, Payload: b}) } return result } // PurgeCreatorResources deletes all catalog resources created by creatorPeerID from // the DB without emitting NATS. Used for non-blacklist peer privilege downgrades where // workspace state should be left untouched. func PurgeCreatorResources(creatorPeerID string) []PurgedResourcePayload { var result []PurgedResourcePayload result = append(result, purgeByType[*ComputeResource](tools.COMPUTE_RESOURCE, creatorPeerID)...) result = append(result, purgeByType[*DataResource](tools.DATA_RESOURCE, creatorPeerID)...) result = append(result, purgeByType[*ProcessingResource](tools.PROCESSING_RESOURCE, creatorPeerID)...) result = append(result, purgeByType[*StorageResource](tools.STORAGE_RESOURCE, creatorPeerID)...) result = append(result, purgeByType[*WorkflowResource](tools.WORKFLOW_RESOURCE, creatorPeerID)...) result = append(result, purgeByType[*ServiceResource](tools.SERVICE_RESOURCE, creatorPeerID)...) return result } // FilterMapFromResourcePayload deserializes a resource payload by DataType, zeros out // the AbstractInstanciatedResource (and its AbstractResource / Instances sub-fields), // then marshals back to get only the concrete type's own JSON fields. // Returns nil for WORKFLOW_RESOURCE and unknown types. // JSON keys only — not BSON paths. func FilterMapFromResourcePayload(dt tools.DataType, payload []byte) map[string]interface{} { var m map[string]interface{} switch dt { case tools.COMPUTE_RESOURCE: var r ComputeResource if json.Unmarshal(payload, &r) != nil { return nil } r.AbstractInstanciatedResource = AbstractInstanciatedResource[*ComputeResourceInstance]{} b, _ := json.Marshal(r) json.Unmarshal(b, &m) case tools.DATA_RESOURCE: var r DataResource if json.Unmarshal(payload, &r) != nil { return nil } r.AbstractInstanciatedResource = AbstractInstanciatedResource[*DataInstance]{} b, _ := json.Marshal(r) json.Unmarshal(b, &m) case tools.PROCESSING_RESOURCE: var r ProcessingResource if json.Unmarshal(payload, &r) != nil { return nil } r.AbstractInstanciatedResource = AbstractInstanciatedResource[*ProcessingInstance]{} b, _ := json.Marshal(r) json.Unmarshal(b, &m) case tools.STORAGE_RESOURCE: var r StorageResource if json.Unmarshal(payload, &r) != nil { return nil } r.AbstractInstanciatedResource = AbstractInstanciatedResource[*StorageResourceInstance]{} b, _ := json.Marshal(r) json.Unmarshal(b, &m) case tools.SERVICE_RESOURCE: var r ServiceResource if json.Unmarshal(payload, &r) != nil { return nil } r.AbstractInstanciatedResource = AbstractInstanciatedResource[*ServiceInstance]{} b, _ := json.Marshal(r) json.Unmarshal(b, &m) case tools.WORKFLOW_RESOURCE: var r WorkflowResource if json.Unmarshal(payload, &r) != nil { return nil } r.AbstractResource = AbstractResource{} b, _ := json.Marshal(r) json.Unmarshal(b, &m) default: return nil } return m } 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}}, }, } }