From d16ec817ff81ae885876ac52c74cc036a391ad86 Mon Sep 17 00:00:00 2001 From: Miles Yucht Date: Wed, 11 Dec 2024 19:34:12 +0100 Subject: [PATCH] fixes --- .../pluginfw/tfschema/customizable_schema.go | 2 ++ .../tfschema/customizable_schema_test.go | 22 +++++++++++++++++++ .../tfschema/list_nested_attribute.go | 1 + .../tfschema/nested_attribute_object.go | 6 +++++ 4 files changed, 31 insertions(+) diff --git a/internal/providers/pluginfw/tfschema/customizable_schema.go b/internal/providers/pluginfw/tfschema/customizable_schema.go index b7051b4502..b2837371a5 100644 --- a/internal/providers/pluginfw/tfschema/customizable_schema.go +++ b/internal/providers/pluginfw/tfschema/customizable_schema.go @@ -61,6 +61,8 @@ func (s *CustomizableSchema) AddValidator(v any, path ...string) *CustomizableSc return a.AddValidator(v.(validator.List)) case ListNestedAttributeBuilder: return a.AddValidator(v.(validator.List)) + case ListNestedBlockBuilder: + return a.AddValidator(v.(validator.List)) case MapAttributeBuilder: return a.AddValidator(v.(validator.Map)) case MapNestedAttributeBuilder: diff --git a/internal/providers/pluginfw/tfschema/customizable_schema_test.go b/internal/providers/pluginfw/tfschema/customizable_schema_test.go index 6de3c00021..0b6fc49ebd 100644 --- a/internal/providers/pluginfw/tfschema/customizable_schema_test.go +++ b/internal/providers/pluginfw/tfschema/customizable_schema_test.go @@ -115,6 +115,28 @@ func TestCustomizeSchemaSetReadOnly(t *testing.T) { assert.True(t, scm.Attributes["map"].IsComputed()) } +type testTfSdkListNestedAttribute struct { + List types.List `tfsdk:"list"` +} + +func (testTfSdkListNestedAttribute) GetComplexFieldTypes(context.Context) map[string]reflect.Type { + return map[string]reflect.Type{ + "list": reflect.TypeOf(NestedTfSdk{}), + } +} + +func TestCustomizeSchemaSetReadOnly_RecursivelySetsFieldsOfListNestedAttributes(t *testing.T) { + scm := ResourceStructToSchema(context.Background(), testTfSdkListNestedAttribute{}, func(c CustomizableSchema) CustomizableSchema { + c.ConvertToAttribute("list").SetReadOnly("list") + return c + }) + for _, field := range []string{"name", "enabled"} { + assert.True(t, !scm.Attributes["list"].(schema.ListNestedAttribute).NestedObject.Attributes[field].IsOptional()) + assert.True(t, !scm.Attributes["list"].(schema.ListNestedAttribute).NestedObject.Attributes[field].IsRequired()) + assert.True(t, scm.Attributes["list"].(schema.ListNestedAttribute).NestedObject.Attributes[field].IsComputed()) + } +} + func TestCustomizeSchemaAddValidator(t *testing.T) { scm := ResourceStructToSchema(context.Background(), TestTfSdk{}, func(c CustomizableSchema) CustomizableSchema { c.AddValidator(stringLengthBetweenValidator{}, "description") diff --git a/internal/providers/pluginfw/tfschema/list_nested_attribute.go b/internal/providers/pluginfw/tfschema/list_nested_attribute.go index 9cd882990c..adc97a7ed8 100644 --- a/internal/providers/pluginfw/tfschema/list_nested_attribute.go +++ b/internal/providers/pluginfw/tfschema/list_nested_attribute.go @@ -85,6 +85,7 @@ func (a ListNestedAttributeBuilder) SetReadOnly() AttributeBuilder { a.Computed = true a.Optional = false a.Required = false + a.NestedObject.SetReadOnly() return a } diff --git a/internal/providers/pluginfw/tfschema/nested_attribute_object.go b/internal/providers/pluginfw/tfschema/nested_attribute_object.go index bf9f639231..f12702f7b9 100644 --- a/internal/providers/pluginfw/tfschema/nested_attribute_object.go +++ b/internal/providers/pluginfw/tfschema/nested_attribute_object.go @@ -25,3 +25,9 @@ func (a NestedAttributeObject) BuildResourceAttribute() schema.NestedAttributeOb Attributes: resourceAttributes, } } + +func (a NestedAttributeObject) SetReadOnly() { + for attr, attrV := range a.Attributes { + a.Attributes[attr] = attrV.SetReadOnly() + } +}