From 99fbe82a51186174da293d52f9feb78ed9c6216e Mon Sep 17 00:00:00 2001 From: mr Date: Mon, 1 Jun 2026 08:45:50 +0200 Subject: [PATCH] Update Auto Outputs on sourced. --- models/resources/resource_accessor.go | 52 +++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/models/resources/resource_accessor.go b/models/resources/resource_accessor.go index 3c6ee61..220bf08 100755 --- a/models/resources/resource_accessor.go +++ b/models/resources/resource_accessor.go @@ -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/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" @@ -16,6 +17,48 @@ 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{ @@ -76,6 +119,14 @@ func (dca *ResourceMongoAccessor[T]) UpdateOne(set map[string]interface{}, id st } 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) } @@ -134,6 +185,7 @@ func (dca *ResourceMongoAccessor[T]) StoreOne(data utils.DBObject) (utils.DBObje 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())