package resources import ( "encoding/json" "errors" "fmt" "reflect" "slices" "strings" "cloud.o-forge.io/core/oc-lib/dbs" "cloud.o-forge.io/core/oc-lib/models/common/pricing" "cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/tools" ) /* * DynamicResource is a struct that represents a data resource * it defines the resource data */ type DynamicResource struct { AbstractResource Type tools.DataType `bson:"type,omitempty" json:"type,omitempty"` Filters dbs.Filters `bson:"filters,omitempty" json:"filters,omitempty"` SortRules map[string]string `bson:"rules,omitempty" json:"rules,omitempty"` PeerIds map[int]string `bson:"peer_ids,omitempty" json:"peer_ids,omitempty"` ResourceIds map[int]string `bson:"resource_ids,omitempty" json:"resource_ids,omitempty"` SelectedIndex int `bson:"selected_index,omitempty" json:"selected_index,omitempty"` SelectedPartnershipIndex *int `bson:"selected_partnership_index,omitempty" json:"selected_partnership_index,omitempty"` SelectedBuyingStrategy int `bson:"selected_buying_strategy" json:"selected_buying_strategy,omitempty"` SelectedPricingStrategy int `bson:"selected_pricing_strategy" json:"selected_pricing_strategy,omitempty"` Instances []ResourceInstanceITF `bson:"instances,omitempty" json:"instances,omitempty"` WatchedDynamicResource []string `bson:"watched_dynamic_resource,omitempty" json:"watched_dynamic_resource,omitempty"` } // WorkspaceCandidatesProvider can be set by the workspace package to supply // contextual workspace resources for a given DataType and request without // creating a circular import (workspace → resources → workspace). // When set, SetAllowedInstances uses workspace-scoped resources instead of // the full catalog for requests that carry a username. var WorkspaceCandidatesProvider func(dt tools.DataType, request *tools.APIRequest) []ResourceInterface func (d *DynamicResource) GetAccessor(request *tools.APIRequest) utils.Accessor { return nil } func (d *DynamicResource) SetAllowedInstances(request *tools.APIRequest, instance_id ...string) []ResourceInstanceITF { if WorkspaceCandidatesProvider != nil { candidates := WorkspaceCandidatesProvider(d.Type, request) return d.SetAllowedInstancesFromSet(candidates, request, instance_id...) } d.Instances = []ResourceInstanceITF{} d.sortAndResetInstances() return d.Instances } // SetAllowedInstancesFromSet fills d.Instances from a pre-loaded workspace resource set // instead of querying the catalog. Filters are applied in-memory against the candidates. // Called by WorkspaceResourceSet.Fill so dynamic resources only see workspace-scoped resources. func (d *DynamicResource) SetAllowedInstancesFromSet(candidates []ResourceInterface, request *tools.APIRequest, instance_id ...string) []ResourceInstanceITF { d.Instances = []ResourceInstanceITF{} d.PeerIds = map[int]string{} d.ResourceIds = map[int]string{} for _, res := range candidates { if !d.matchesFilters(res) { continue } for _, i := range res.SetAllowedInstances(request, instance_id...) { d.PeerIds[len(d.Instances)] = res.GetCreatorID() d.ResourceIds[len(d.Instances)] = res.GetID() d.Instances = append(d.Instances, i) } } d.sortAndResetInstances() return d.Instances } func (d *DynamicResource) sortAndResetInstances() { if d.SortRules != nil { sorted := make([]ResourceInstanceITF, len(d.Instances)) copy(sorted, d.Instances) slices.SortStableFunc(sorted, func(a, b ResourceInstanceITF) int { d.SortRules["partnerships"] = "%v not contains 2" return d.compareByRules(a, b, d.SortRules) }) d.Instances = sorted } d.WatchedDynamicResource = []string{} } // matchesFilters applies d.Filters in-memory against a serialized resource. // Keys in d.Filters are JSON tag names; Serialize returns JSON tag names — no bson conversion needed. func (d *DynamicResource) matchesFilters(res ResourceInterface) bool { if len(d.Filters.And) == 0 && len(d.Filters.Or) == 0 { return true } m := res.Serialize(res) for field, fs := range d.Filters.And { vals := nestedVals(m, strings.Split(field, ".")) for _, f := range fs { if !anyMatchesOp(vals, f) { return false } } } if len(d.Filters.Or) > 0 { matched := false for field, fs := range d.Filters.Or { vals := nestedVals(m, strings.Split(field, ".")) for _, f := range fs { if anyMatchesOp(vals, f) { matched = true break } } if matched { break } } if !matched { return false } } return true } // nestedVals navigates a dot-path into m and collects all leaf values. // Arrays at any level are expanded: each element is recursed into. func nestedVals(m map[string]interface{}, path []string) []interface{} { if len(path) == 0 || m == nil { return nil } val, ok := m[path[0]] if !ok { return nil } if len(path) == 1 { if arr, ok := val.([]interface{}); ok { return arr } return []interface{}{val} } rest := path[1:] switch v := val.(type) { case map[string]interface{}: return nestedVals(v, rest) case []interface{}: var out []interface{} for _, elem := range v { if em, ok := elem.(map[string]interface{}); ok { out = append(out, nestedVals(em, rest)...) } } return out } return nil } // anyMatchesOp returns true if at least one value in vals satisfies filter f. func anyMatchesOp(vals []interface{}, f dbs.Filter) bool { if f.Operator == dbs.EXISTS.String() { exists := len(vals) > 0 && vals[0] != nil want := true if b, ok := f.Value.(bool); ok { want = b } return exists == want } if f.Operator == dbs.IN.String() { list, ok := f.Value.([]interface{}) if !ok { return false } for _, v := range vals { sv := fmt.Sprintf("%v", v) for _, item := range list { if sv == fmt.Sprintf("%v", item) { return true } } } return false } for _, v := range vals { if opMatches(v, f) { return true } } return false } func opMatches(val interface{}, f dbs.Filter) bool { switch f.Operator { case dbs.EQUAL.String(): return fmt.Sprintf("%v", val) == fmt.Sprintf("%v", f.Value) case dbs.NOT.String(): return fmt.Sprintf("%v", val) != fmt.Sprintf("%v", f.Value) case dbs.LIKE.String(): return strings.Contains(strings.ToLower(fmt.Sprintf("%v", val)), strings.ToLower(fmt.Sprintf("%v", f.Value))) case dbs.GT.String(), dbs.GTE.String(), dbs.LT.String(), dbs.LTE.String(): return numericCmp(val, f.Value, f.Operator) case dbs.ELEMMATCH.String(): arr, ok := val.([]interface{}) if !ok { return false } sub, ok := f.Value.(map[string]interface{}) if !ok { return false } for _, elem := range arr { em, ok := elem.(map[string]interface{}) if !ok { continue } allOk := true for k, sv := range sub { if fmt.Sprintf("%v", em[k]) != fmt.Sprintf("%v", sv) { allOk = false break } } if allOk { return true } } return false } return false } func numericCmp(a, b interface{}, op string) bool { fa, aOk := toFloat64(a) fb, bOk := toFloat64(b) if !aOk || !bOk { sa, sb := fmt.Sprintf("%v", a), fmt.Sprintf("%v", b) switch op { case dbs.GT.String(): return sa > sb case dbs.GTE.String(): return sa >= sb case dbs.LT.String(): return sa < sb case dbs.LTE.String(): return sa <= sb } return false } switch op { case dbs.GT.String(): return fa > fb case dbs.GTE.String(): return fa >= fb case dbs.LT.String(): return fa < fb case dbs.LTE.String(): return fa <= fb } return false } func toFloat64(v interface{}) (float64, bool) { switch n := v.(type) { case float64: return n, true case float32: return float64(n), true case int: return float64(n), true case int32: return float64(n), true case int64: return float64(n), true } return 0, false } func (d *DynamicResource) AddInstances(instance ResourceInstanceITF) { d.Instances = append(d.Instances, instance) } func (d *DynamicResource) GetSelectedInstance(index *int) ResourceInstanceITF { if len(d.Instances) == 0 { return nil } for i, inst := range d.Instances { if slices.Contains(d.WatchedDynamicResource, inst.GetID()) { continue } d.WatchedDynamicResource = append(d.WatchedDynamicResource, inst.GetID()) d.SelectedIndex = i for i := range inst.GetPartnerships() { fmt.Println(inst.GetProfile(d.PeerIds[i], &i, &d.SelectedBuyingStrategy, &d.SelectedPricingStrategy), d.PeerIds[i], &i, &d.SelectedBuyingStrategy, &d.SelectedPricingStrategy) if inst.GetProfile(d.PeerIds[i], &i, &d.SelectedBuyingStrategy, &d.SelectedPricingStrategy) != nil { d.SelectedPartnershipIndex = &i break } } if d.SelectedPartnershipIndex == nil { i := 0 d.SelectedPartnershipIndex = &i } return inst } return nil } // compareByRules orders instances so those satisfying more sort rules come first. // When both satisfy a rule, the one with the lower first-attribute value wins (ASC strict). // Key format: "attrA" for single-%s rules, "attrA,attrB" for two-%s rules. func (ri *DynamicResource) compareByRules(a, b ResourceInstanceITF, rules map[string]string) int { ma := a.Serialize(a) mb := b.Serialize(b) for attrs, rule := range rules { attrPaths := strings.Split(attrs, ",") aOk, aFirst := ri.ruleMatchesAny(rule, attrPaths, ma) bOk, bFirst := ri.ruleMatchesAny(rule, attrPaths, mb) if aOk && !bOk { return -1 } if !aOk && bOk { return 1 } if aOk && bOk { if aFirst < bFirst { return -1 } if aFirst > bFirst { return 1 } } } return 0 } // ruleMatchesAny checks if any value (or combination for 2-%s rules) satisfies rule. // Arrays at any path level are iterated. Returns (matched, firstMatchingValue). func (ri *DynamicResource) ruleMatchesAny(rule string, attrPaths []string, m map[string]interface{}) (bool, string) { placeholders := strings.Count(rule, "%s") if placeholders == 0 { return false, "" } valsA := ri.getVals(strings.Split(strings.TrimSpace(attrPaths[0]), "."), m) if placeholders == 1 { for _, v := range valsA { if ri.byRules(rule, v) { return true, fmt.Sprintf("%v", v) } } return false, "" } if len(attrPaths) < 2 { return false, "" } valsB := ri.getVals(strings.Split(strings.TrimSpace(attrPaths[1]), "."), m) for _, a := range valsA { for _, b := range valsB { if ri.byRules(rule, a, b) { return true, fmt.Sprintf("%v", a) } } } return false, "" } // getVals navigates attrs into m, collecting all leaf values. // At each level it detects whether the value is a dict (map) or an array and acts accordingly: // - array of maps → recurse into each element with the remaining path // - array of scalars (leaf) → collect all as strings // - map → recurse with the remaining path func (ri *DynamicResource) getVals(attrs []string, m map[string]interface{}) []interface{} { if len(attrs) == 0 { return nil } attr := attrs[0] if attr == "" || m[attr] == nil { return nil } b, err := json.Marshal(m[attr]) if err != nil { return nil } // Leaf level: detect array vs scalar. if len(attrs) == 1 { var arr []interface{} if err := json.Unmarshal(b, &arr); err == nil { results := []interface{}{} for _, v := range arr { results = append(results, fmt.Sprintf("%v", v)) } return results } return []interface{}{m[attr]} } // Intermediate level: detect array of maps vs single map. var arrMaps []map[string]interface{} if err := json.Unmarshal(b, &arrMaps); err == nil { results := []interface{}{} for _, item := range arrMaps { results = append(results, ri.getVals(attrs[1:], item)...) } return results } nm := map[string]interface{}{} if err := json.Unmarshal(b, &nm); err != nil { return nil } return ri.getVals(attrs[1:], nm) } func (ri *DynamicResource) byRules(rule string, vals ...interface{}) bool { if len(vals) == 0 { return false } formatted := fmt.Sprintf(rule, vals...) // hm hm switch { case strings.Contains(rule, "not contains"): a := strings.Split(formatted, " not contains ") if reflect.TypeOf(vals[0]).Kind() == reflect.Map { return vals[0].(map[string]interface{})[fmt.Sprintf("%v", a[1])] != nil } return strings.Contains(a[0], a[1]) case strings.Contains(rule, "contains"): a := strings.Split(formatted, " contains ") if reflect.TypeOf(vals[0]).Kind() == reflect.Map { return vals[0].(map[string]interface{})[fmt.Sprintf("%v", a[1])] != nil } return strings.Contains(a[0], a[1]) case strings.Contains(rule, "<="): a := strings.Split(formatted, " <= ") return len(a) > 1 && a[0] <= a[1] case strings.Contains(rule, ">="): a := strings.Split(formatted, " >= ") return len(a) > 1 && a[0] >= a[1] case strings.Contains(rule, "<>"), strings.Contains(rule, "not like"): if strings.Contains(rule, "<>") { a := strings.Split(formatted, " <> ") return len(a) > 1 && !strings.Contains(a[0], a[1]) && !strings.Contains(a[1], a[0]) } a := strings.Split(formatted, " not like ") return len(a) > 1 && !strings.Contains(a[0], a[1]) && !strings.Contains(a[1], a[0]) case strings.Contains(rule, "<"): a := strings.Split(formatted, " < ") return len(a) > 1 && a[0] < a[1] case strings.Contains(rule, ">"): a := strings.Split(formatted, " > ") return len(a) > 1 && a[0] > a[1] case strings.Contains(rule, "=="): a := strings.Split(formatted, " == ") return len(a) > 1 && a[0] == a[1] case strings.Contains(rule, "!="): a := strings.Split(formatted, " != ") return len(a) > 1 && a[0] != a[1] case strings.Contains(rule, "like"): a := strings.Split(formatted, " like ") return len(a) > 1 && (strings.Contains(a[0], a[1]) || strings.Contains(a[1], a[0])) } return false } func (r *DynamicResource) GetType() string { return tools.DYNAMIC_RESOURCE.String() } func (abs *DynamicResource) ConvertToPricedResource(t tools.DataType, selectedInstance *int, selectedPartnership *int, selectedBuyingStrategy *int, selectedStrategy *int, selectedBookingModeIndex *int, request *tools.APIRequest) (pricing.PricedItemITF, error) { var p pricing.PricedItemITF var err error for _, v := range []tools.DataType{ tools.COMPUTE_RESOURCE, tools.DATA_RESOURCE, tools.STORAGE_RESOURCE, tools.PROCESSING_RESOURCE, tools.WORKFLOW_RESOURCE, } { switch v { case tools.COMPUTE_RESOURCE: if p, err = ConvertToPricedResource[*ComputeResourcePricingProfile](t, selectedInstance, selectedPartnership, selectedBuyingStrategy, selectedStrategy, selectedBookingModeIndex, abs, request); err == nil { return p.(*PricedResource[*ProcessingResourcePricingProfile]), nil } case tools.DATA_RESOURCE: if p, err = ConvertToPricedResource[*DataResourcePricingProfile](t, selectedInstance, selectedPartnership, selectedBuyingStrategy, selectedStrategy, selectedBookingModeIndex, abs, request); err == nil { return p.(*PricedResource[*DataResourcePricingProfile]), nil } case tools.STORAGE_RESOURCE: if p, err = ConvertToPricedResource[*StorageResourcePricingProfile](t, selectedInstance, selectedPartnership, selectedBuyingStrategy, selectedStrategy, selectedBookingModeIndex, abs, request); err == nil { return p.(*PricedResource[*StorageResourcePricingProfile]), nil } case tools.PROCESSING_RESOURCE: if p, err = ConvertToPricedResource[*ProcessingResourcePricingProfile](t, selectedInstance, selectedPartnership, selectedBuyingStrategy, selectedStrategy, selectedBookingModeIndex, abs, request); err == nil { return p.(*PricedResource[*ProcessingResourcePricingProfile]), nil } } } return nil, errors.New("can't convert priced resource") }