Skip to content

Commit 8e3117a

Browse files
authored
[Internal] Remove unused configuration from blocks (#4283)
## Changes Currently, our schema builder elements in the plugin framework components allow you to set Computed/Optional/Required/Sensitive fields on Block schema elements. However, these are simply dropped when converting to actual schema elements: only attributes have these fields. This PR moves the SetComputed/SetOptional/SetRequired/SetSensitive/SetReadOnly methods to the AttributeBuilder interface and panics if you attempt to set them on a block. ## Tests Added a unit test to verify that these methods panic when called on an element that is converted into a block.
1 parent 00eac36 commit 8e3117a

15 files changed

+151
-170
lines changed

internal/providers/pluginfw/tfschema/attribute_builder.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@ import (
1010
// This common interface prevents us from keeping two copies of StructToSchema and CustomizableSchema.
1111
type AttributeBuilder interface {
1212
BaseSchemaBuilder
13+
14+
// SetOptional sets the attribute as optional in the schema. This does not affect whether the attribute is computed.
15+
// It fails if the attribute is already optional.
16+
SetOptional() AttributeBuilder
17+
18+
// SetRequired sets the attribute as required in the schema. This does not affect whether the attribute is computed.
19+
// It fails if the attribute is already required.
20+
SetRequired() AttributeBuilder
21+
22+
// SetSensitive sets the attribute as sensitive in the schema. It fails if the attribute is already sensitive.
23+
SetSensitive() AttributeBuilder
24+
25+
// SetComputed sets the attribute as computed in the schema. It fails if the attribute is already computed.
26+
SetComputed() AttributeBuilder
27+
28+
// Sets the attribute as read-only in the schema, i.e. computed and neither optional or required. It fails if the
29+
// attribute is already read-only.
30+
SetReadOnly() AttributeBuilder
31+
1332
BuildDataSourceAttribute() dataschema.Attribute
1433
BuildResourceAttribute() schema.Attribute
1534
}

internal/providers/pluginfw/tfschema/base_schema_builder.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,5 @@ package tfschema
33
// BaseSchemaBuilder is the common interface for all blocks and attributes, it can be used to build data source and resource.
44
// Both AttributeBuilder and BlockBuilder extend this interface.
55
type BaseSchemaBuilder interface {
6-
SetOptional() BaseSchemaBuilder
7-
SetRequired() BaseSchemaBuilder
8-
SetSensitive() BaseSchemaBuilder
9-
SetComputed() BaseSchemaBuilder
10-
SetReadOnly() BaseSchemaBuilder
116
SetDeprecated(string) BaseSchemaBuilder
127
}

internal/providers/pluginfw/tfschema/bool_attribute.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (a BoolAttributeBuilder) BuildResourceAttribute() schema.Attribute {
4040
}
4141
}
4242

43-
func (a BoolAttributeBuilder) SetOptional() BaseSchemaBuilder {
43+
func (a BoolAttributeBuilder) SetOptional() AttributeBuilder {
4444
if a.Optional && !a.Required {
4545
panic("attribute is already optional")
4646
}
@@ -49,7 +49,7 @@ func (a BoolAttributeBuilder) SetOptional() BaseSchemaBuilder {
4949
return a
5050
}
5151

52-
func (a BoolAttributeBuilder) SetRequired() BaseSchemaBuilder {
52+
func (a BoolAttributeBuilder) SetRequired() AttributeBuilder {
5353
if !a.Optional && a.Required {
5454
panic("attribute is already required")
5555
}
@@ -58,23 +58,23 @@ func (a BoolAttributeBuilder) SetRequired() BaseSchemaBuilder {
5858
return a
5959
}
6060

61-
func (a BoolAttributeBuilder) SetSensitive() BaseSchemaBuilder {
61+
func (a BoolAttributeBuilder) SetSensitive() AttributeBuilder {
6262
if a.Sensitive {
6363
panic("attribute is already sensitive")
6464
}
6565
a.Sensitive = true
6666
return a
6767
}
6868

69-
func (a BoolAttributeBuilder) SetComputed() BaseSchemaBuilder {
69+
func (a BoolAttributeBuilder) SetComputed() AttributeBuilder {
7070
if a.Computed {
7171
panic("attribute is already computed")
7272
}
7373
a.Computed = true
7474
return a
7575
}
7676

77-
func (a BoolAttributeBuilder) SetReadOnly() BaseSchemaBuilder {
77+
func (a BoolAttributeBuilder) SetReadOnly() AttributeBuilder {
7878
if a.Computed && !a.Optional && !a.Required {
7979
panic("attribute is already read only")
8080
}
@@ -89,12 +89,12 @@ func (a BoolAttributeBuilder) SetDeprecated(msg string) BaseSchemaBuilder {
8989
return a
9090
}
9191

92-
func (a BoolAttributeBuilder) AddValidator(v validator.Bool) BaseSchemaBuilder {
92+
func (a BoolAttributeBuilder) AddValidator(v validator.Bool) AttributeBuilder {
9393
a.Validators = append(a.Validators, v)
9494
return a
9595
}
9696

97-
func (a BoolAttributeBuilder) AddPlanModifier(v planmodifier.Bool) BaseSchemaBuilder {
97+
func (a BoolAttributeBuilder) AddPlanModifier(v planmodifier.Bool) AttributeBuilder {
9898
a.PlanModifiers = append(a.PlanModifiers, v)
9999
return a
100100
}

internal/providers/pluginfw/tfschema/customizable_schema.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ type CustomizableSchema struct {
1616

1717
// ConstructCustomizableSchema constructs a CustomizableSchema given a NestedBlockObject.
1818
func ConstructCustomizableSchema(nestedObject NestedBlockObject) *CustomizableSchema {
19-
attr := AttributeBuilder(SingleNestedBlockBuilder{NestedObject: nestedObject})
20-
return &CustomizableSchema{attr: attr}
19+
return &CustomizableSchema{attr: SingleNestedBlockBuilder{NestedObject: nestedObject}}
2120
}
2221

2322
// ToAttributeMap converts CustomizableSchema into BaseSchemaBuilder.
@@ -116,7 +115,12 @@ func (s *CustomizableSchema) AddPlanModifier(v any, path ...string) *Customizabl
116115

117116
func (s *CustomizableSchema) SetOptional(path ...string) *CustomizableSchema {
118117
cb := func(attr BaseSchemaBuilder) BaseSchemaBuilder {
119-
return attr.SetOptional()
118+
switch a := attr.(type) {
119+
case AttributeBuilder:
120+
return a.SetOptional()
121+
default:
122+
panic(fmt.Errorf("SetOptional called on invalid attribute type: %s. %s", reflect.TypeOf(attr).String(), common.TerraformBugErrorMessage))
123+
}
120124
}
121125

122126
navigateSchemaWithCallback(&s.attr, cb, path...)
@@ -126,7 +130,12 @@ func (s *CustomizableSchema) SetOptional(path ...string) *CustomizableSchema {
126130

127131
func (s *CustomizableSchema) SetRequired(path ...string) *CustomizableSchema {
128132
cb := func(attr BaseSchemaBuilder) BaseSchemaBuilder {
129-
return attr.SetRequired()
133+
switch a := attr.(type) {
134+
case AttributeBuilder:
135+
return a.SetRequired()
136+
default:
137+
panic(fmt.Errorf("SetRequired called on invalid attribute type: %s. %s", reflect.TypeOf(attr).String(), common.TerraformBugErrorMessage))
138+
}
130139
}
131140

132141
navigateSchemaWithCallback(&s.attr, cb, path...)
@@ -136,7 +145,12 @@ func (s *CustomizableSchema) SetRequired(path ...string) *CustomizableSchema {
136145

137146
func (s *CustomizableSchema) SetSensitive(path ...string) *CustomizableSchema {
138147
cb := func(attr BaseSchemaBuilder) BaseSchemaBuilder {
139-
return attr.SetSensitive()
148+
switch a := attr.(type) {
149+
case AttributeBuilder:
150+
return a.SetSensitive()
151+
default:
152+
panic(fmt.Errorf("SetSensitive called on invalid attribute type: %s. %s", reflect.TypeOf(attr).String(), common.TerraformBugErrorMessage))
153+
}
140154
}
141155

142156
navigateSchemaWithCallback(&s.attr, cb, path...)
@@ -155,7 +169,12 @@ func (s *CustomizableSchema) SetDeprecated(msg string, path ...string) *Customiz
155169

156170
func (s *CustomizableSchema) SetComputed(path ...string) *CustomizableSchema {
157171
cb := func(attr BaseSchemaBuilder) BaseSchemaBuilder {
158-
return attr.SetComputed()
172+
switch a := attr.(type) {
173+
case AttributeBuilder:
174+
return a.SetComputed()
175+
default:
176+
panic(fmt.Errorf("SetComputed called on invalid attribute type: %s. %s", reflect.TypeOf(attr).String(), common.TerraformBugErrorMessage))
177+
}
159178
}
160179

161180
navigateSchemaWithCallback(&s.attr, cb, path...)
@@ -167,7 +186,12 @@ func (s *CustomizableSchema) SetComputed(path ...string) *CustomizableSchema {
167186
// by the platform.
168187
func (s *CustomizableSchema) SetReadOnly(path ...string) *CustomizableSchema {
169188
cb := func(attr BaseSchemaBuilder) BaseSchemaBuilder {
170-
return attr.SetReadOnly()
189+
switch a := attr.(type) {
190+
case AttributeBuilder:
191+
return a.SetReadOnly()
192+
default:
193+
panic(fmt.Errorf("SetReadOnly called on invalid attribute type: %s. %s", reflect.TypeOf(attr).String(), common.TerraformBugErrorMessage))
194+
}
171195
}
172196

173197
navigateSchemaWithCallback(&s.attr, cb, path...)

internal/providers/pluginfw/tfschema/customizable_schema_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,48 @@ func TestCustomizeSchemaObjectTypeValidatorAdded(t *testing.T) {
130130

131131
assert.True(t, len(scm.Blocks["nested_slice_object"].(schema.ListNestedBlock).Validators) == 1)
132132
}
133+
134+
func TestCustomizeSchema_SetRequired_PanicOnBlock(t *testing.T) {
135+
assert.Panics(t, func() {
136+
_ = ResourceStructToSchema(TestTfSdk{}, func(c CustomizableSchema) CustomizableSchema {
137+
c.SetRequired("nested")
138+
return c
139+
})
140+
})
141+
}
142+
143+
func TestCustomizeSchema_SetOptional_PanicOnBlock(t *testing.T) {
144+
assert.Panics(t, func() {
145+
_ = ResourceStructToSchema(TestTfSdk{}, func(c CustomizableSchema) CustomizableSchema {
146+
c.SetOptional("nested")
147+
return c
148+
})
149+
})
150+
}
151+
152+
func TestCustomizeSchema_SetSensitive_PanicOnBlock(t *testing.T) {
153+
assert.Panics(t, func() {
154+
_ = ResourceStructToSchema(TestTfSdk{}, func(c CustomizableSchema) CustomizableSchema {
155+
c.SetSensitive("nested")
156+
return c
157+
})
158+
})
159+
}
160+
161+
func TestCustomizeSchema_SetReadOnly_PanicOnBlock(t *testing.T) {
162+
assert.Panics(t, func() {
163+
_ = ResourceStructToSchema(TestTfSdk{}, func(c CustomizableSchema) CustomizableSchema {
164+
c.SetReadOnly("nested")
165+
return c
166+
})
167+
})
168+
}
169+
170+
func TestCustomizeSchema_SetComputed_PanicOnBlock(t *testing.T) {
171+
assert.Panics(t, func() {
172+
_ = ResourceStructToSchema(TestTfSdk{}, func(c CustomizableSchema) CustomizableSchema {
173+
c.SetComputed("nested")
174+
return c
175+
})
176+
})
177+
}

internal/providers/pluginfw/tfschema/float64_attribute.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (a Float64AttributeBuilder) BuildResourceAttribute() schema.Attribute {
4040
}
4141
}
4242

43-
func (a Float64AttributeBuilder) SetOptional() BaseSchemaBuilder {
43+
func (a Float64AttributeBuilder) SetOptional() AttributeBuilder {
4444
if a.Optional && !a.Required {
4545
panic("attribute is already optional")
4646
}
@@ -49,7 +49,7 @@ func (a Float64AttributeBuilder) SetOptional() BaseSchemaBuilder {
4949
return a
5050
}
5151

52-
func (a Float64AttributeBuilder) SetRequired() BaseSchemaBuilder {
52+
func (a Float64AttributeBuilder) SetRequired() AttributeBuilder {
5353
if !a.Optional && a.Required {
5454
panic("attribute is already required")
5555
}
@@ -58,23 +58,23 @@ func (a Float64AttributeBuilder) SetRequired() BaseSchemaBuilder {
5858
return a
5959
}
6060

61-
func (a Float64AttributeBuilder) SetSensitive() BaseSchemaBuilder {
61+
func (a Float64AttributeBuilder) SetSensitive() AttributeBuilder {
6262
if a.Sensitive {
6363
panic("attribute is already sensitive")
6464
}
6565
a.Sensitive = true
6666
return a
6767
}
6868

69-
func (a Float64AttributeBuilder) SetComputed() BaseSchemaBuilder {
69+
func (a Float64AttributeBuilder) SetComputed() AttributeBuilder {
7070
if a.Computed {
7171
panic("attribute is already computed")
7272
}
7373
a.Computed = true
7474
return a
7575
}
7676

77-
func (a Float64AttributeBuilder) SetReadOnly() BaseSchemaBuilder {
77+
func (a Float64AttributeBuilder) SetReadOnly() AttributeBuilder {
7878
if a.Computed && !a.Optional && !a.Required {
7979
panic("attribute is already read only")
8080
}
@@ -89,12 +89,12 @@ func (a Float64AttributeBuilder) SetDeprecated(msg string) BaseSchemaBuilder {
8989
return a
9090
}
9191

92-
func (a Float64AttributeBuilder) AddValidator(v validator.Float64) BaseSchemaBuilder {
92+
func (a Float64AttributeBuilder) AddValidator(v validator.Float64) AttributeBuilder {
9393
a.Validators = append(a.Validators, v)
9494
return a
9595
}
9696

97-
func (a Float64AttributeBuilder) AddPlanModifier(v planmodifier.Float64) BaseSchemaBuilder {
97+
func (a Float64AttributeBuilder) AddPlanModifier(v planmodifier.Float64) AttributeBuilder {
9898
a.PlanModifiers = append(a.PlanModifiers, v)
9999
return a
100100
}

internal/providers/pluginfw/tfschema/int64_attribute.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (a Int64AttributeBuilder) BuildResourceAttribute() schema.Attribute {
4040
}
4141
}
4242

43-
func (a Int64AttributeBuilder) SetOptional() BaseSchemaBuilder {
43+
func (a Int64AttributeBuilder) SetOptional() AttributeBuilder {
4444
if a.Optional && !a.Required {
4545
panic("attribute is already optional")
4646
}
@@ -49,7 +49,7 @@ func (a Int64AttributeBuilder) SetOptional() BaseSchemaBuilder {
4949
return a
5050
}
5151

52-
func (a Int64AttributeBuilder) SetRequired() BaseSchemaBuilder {
52+
func (a Int64AttributeBuilder) SetRequired() AttributeBuilder {
5353
if !a.Optional && a.Required {
5454
panic("attribute is already required")
5555
}
@@ -58,23 +58,23 @@ func (a Int64AttributeBuilder) SetRequired() BaseSchemaBuilder {
5858
return a
5959
}
6060

61-
func (a Int64AttributeBuilder) SetSensitive() BaseSchemaBuilder {
61+
func (a Int64AttributeBuilder) SetSensitive() AttributeBuilder {
6262
if a.Sensitive {
6363
panic("attribute is already sensitive")
6464
}
6565
a.Sensitive = true
6666
return a
6767
}
6868

69-
func (a Int64AttributeBuilder) SetComputed() BaseSchemaBuilder {
69+
func (a Int64AttributeBuilder) SetComputed() AttributeBuilder {
7070
if a.Computed {
7171
panic("attribute is already computed")
7272
}
7373
a.Computed = true
7474
return a
7575
}
7676

77-
func (a Int64AttributeBuilder) SetReadOnly() BaseSchemaBuilder {
77+
func (a Int64AttributeBuilder) SetReadOnly() AttributeBuilder {
7878
if a.Computed && !a.Optional && !a.Required {
7979
panic("attribute is already read only")
8080
}
@@ -89,12 +89,12 @@ func (a Int64AttributeBuilder) SetDeprecated(msg string) BaseSchemaBuilder {
8989
return a
9090
}
9191

92-
func (a Int64AttributeBuilder) AddValidator(v validator.Int64) BaseSchemaBuilder {
92+
func (a Int64AttributeBuilder) AddValidator(v validator.Int64) AttributeBuilder {
9393
a.Validators = append(a.Validators, v)
9494
return a
9595
}
9696

97-
func (a Int64AttributeBuilder) AddPlanModifier(v planmodifier.Int64) BaseSchemaBuilder {
97+
func (a Int64AttributeBuilder) AddPlanModifier(v planmodifier.Int64) AttributeBuilder {
9898
a.PlanModifiers = append(a.PlanModifiers, v)
9999
return a
100100
}

0 commit comments

Comments
 (0)