From 9fef3bdfc36e8614fdbbf7ca6318e18e7c55ba5f Mon Sep 17 00:00:00 2001 From: Ivan Milchev Date: Fri, 16 Feb 2024 18:36:04 +0100 Subject: [PATCH] add more safe guards for merging resources Signed-off-by: Ivan Milchev --- providers-sdk/v1/resources/schema.go | 4 +++- providers/extensible_schema_test.go | 6 ------ providers/runtime.go | 21 +++++++++++++++++---- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/providers-sdk/v1/resources/schema.go b/providers-sdk/v1/resources/schema.go index e6454dbd46..f49c4513ee 100644 --- a/providers-sdk/v1/resources/schema.go +++ b/providers-sdk/v1/resources/schema.go @@ -62,7 +62,9 @@ func (s *Schema) Add(other ResourcesSchema) ResourcesSchema { existing.Fields = map[string]*Field{} } for fk, fv := range v.Fields { - if fExisting, ok := existing.Fields[fk]; ok { + // If the field exists in the current resource, but is from a different provider, + // we store it as an "other" + if fExisting, ok := existing.Fields[fk]; ok && fv.Provider != fExisting.Provider { fExisting.Others = append(fExisting.Others, fv) } else { existing.Fields[fk] = fv diff --git a/providers/extensible_schema_test.go b/providers/extensible_schema_test.go index cc84109475..350354de53 100644 --- a/providers/extensible_schema_test.go +++ b/providers/extensible_schema_test.go @@ -53,10 +53,4 @@ func TestExtensibleSchema(t *testing.T) { _, finfo = s.LookupField("eternity", "v") require.NotNil(t, info) assert.Equal(t, "first", finfo.Provider) - - // s.prioritizeIDs("first") - - _, finfo = s.LookupField("eternity", "iii") - require.NotNil(t, info) - assert.Equal(t, "first", finfo.Provider) } diff --git a/providers/runtime.go b/providers/runtime.go index 535d9fdb92..6b293de147 100644 --- a/providers/runtime.go +++ b/providers/runtime.go @@ -614,10 +614,23 @@ func (r *Runtime) lookupFieldProvider(resource string, field string) (*Connected } // Make sure we grab the field that matches the provider of this runtime (if possible). - for _, f := range fieldInfo.Others { - if f.Provider == r.Provider.Instance.ID { - fieldInfo = f - break + if fieldInfo.Provider != r.Provider.Instance.ID { + for _, rI := range resourceInfo.Others { + // Check if the same field exists in another provider's resource + for _, f := range rI.Fields { + if f.Provider == r.Provider.Instance.ID { + fieldInfo = f + break + } + + // Check if the same field is extended by another provider + for _, otherF := range f.Others { + if otherF.Provider == r.Provider.Instance.ID { + fieldInfo = f + break + } + } + } } }