Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mgyucht committed Dec 11, 2024
1 parent 35b132b commit d16ec81
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/providers/pluginfw/tfschema/customizable_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions internal/providers/pluginfw/tfschema/customizable_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (a ListNestedAttributeBuilder) SetReadOnly() AttributeBuilder {
a.Computed = true
a.Optional = false
a.Required = false
a.NestedObject.SetReadOnly()
return a
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit d16ec81

Please sign in to comment.