Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Internal] Remove unused configuration from blocks #4283

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions internal/providers/pluginfw/tfschema/attribute_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ import (
// This common interface prevents us from keeping two copies of StructToSchema and CustomizableSchema.
type AttributeBuilder interface {
BaseSchemaBuilder

// SetOptional sets the attribute as optional in the schema. This does not affect whether the attribute is computed.
// It fails if the attribute is already optional.
SetOptional() BaseSchemaBuilder

// SetRequired sets the attribute as required in the schema. This does not affect whether the attribute is computed.
// It fails if the attribute is already required.
SetRequired() BaseSchemaBuilder

// SetSensitive sets the attribute as sensitive in the schema. It fails if the attribute is already sensitive.
SetSensitive() BaseSchemaBuilder

// SetComputed sets the attribute as computed in the schema. It fails if the attribute is already computed.
SetComputed() BaseSchemaBuilder

// Sets the attribute as read-only in the schema, i.e. computed and neither optional or required. It fails if the
// attribute is already read-only.
SetReadOnly() BaseSchemaBuilder

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these return BaseSchemaBuilder or AttributeBuilder?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, they can return AttributeBuilder.

BuildDataSourceAttribute() dataschema.Attribute
BuildResourceAttribute() schema.Attribute
}
Expand Down
5 changes: 0 additions & 5 deletions internal/providers/pluginfw/tfschema/base_schema_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,5 @@ package tfschema
// BaseSchemaBuilder is the common interface for all blocks and attributes, it can be used to build data source and resource.
// Both AttributeBuilder and BlockBuilder extend this interface.
type BaseSchemaBuilder interface {
SetOptional() BaseSchemaBuilder
SetRequired() BaseSchemaBuilder
SetSensitive() BaseSchemaBuilder
SetComputed() BaseSchemaBuilder
SetReadOnly() BaseSchemaBuilder
SetDeprecated(string) BaseSchemaBuilder
}
38 changes: 31 additions & 7 deletions internal/providers/pluginfw/tfschema/customizable_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ type CustomizableSchema struct {

// ConstructCustomizableSchema constructs a CustomizableSchema given a NestedBlockObject.
func ConstructCustomizableSchema(nestedObject NestedBlockObject) *CustomizableSchema {
attr := AttributeBuilder(SingleNestedBlockBuilder{NestedObject: nestedObject})
return &CustomizableSchema{attr: attr}
return &CustomizableSchema{attr: SingleNestedBlockBuilder{NestedObject: nestedObject}}
}

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

func (s *CustomizableSchema) SetOptional(path ...string) *CustomizableSchema {
cb := func(attr BaseSchemaBuilder) BaseSchemaBuilder {
return attr.SetOptional()
switch a := attr.(type) {
case AttributeBuilder:
return a.SetOptional()
default:
panic(fmt.Errorf("SetOptional called on invalid attribute type: %s. %s", reflect.TypeOf(attr).String(), common.TerraformBugErrorMessage))
}
}

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

func (s *CustomizableSchema) SetRequired(path ...string) *CustomizableSchema {
cb := func(attr BaseSchemaBuilder) BaseSchemaBuilder {
return attr.SetRequired()
switch a := attr.(type) {
case AttributeBuilder:
return a.SetRequired()
default:
panic(fmt.Errorf("SetRequired called on invalid attribute type: %s. %s", reflect.TypeOf(attr).String(), common.TerraformBugErrorMessage))
}
}

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

func (s *CustomizableSchema) SetSensitive(path ...string) *CustomizableSchema {
cb := func(attr BaseSchemaBuilder) BaseSchemaBuilder {
return attr.SetSensitive()
switch a := attr.(type) {
case AttributeBuilder:
return a.SetSensitive()
default:
panic(fmt.Errorf("SetSensitive called on invalid attribute type: %s. %s", reflect.TypeOf(attr).String(), common.TerraformBugErrorMessage))
}
}

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

func (s *CustomizableSchema) SetComputed(path ...string) *CustomizableSchema {
cb := func(attr BaseSchemaBuilder) BaseSchemaBuilder {
return attr.SetComputed()
switch a := attr.(type) {
case AttributeBuilder:
return a.SetComputed()
default:
panic(fmt.Errorf("SetComputed called on invalid attribute type: %s. %s", reflect.TypeOf(attr).String(), common.TerraformBugErrorMessage))
}
}

navigateSchemaWithCallback(&s.attr, cb, path...)
Expand All @@ -167,7 +186,12 @@ func (s *CustomizableSchema) SetComputed(path ...string) *CustomizableSchema {
// by the platform.
func (s *CustomizableSchema) SetReadOnly(path ...string) *CustomizableSchema {
cb := func(attr BaseSchemaBuilder) BaseSchemaBuilder {
return attr.SetReadOnly()
switch a := attr.(type) {
case AttributeBuilder:
return a.SetReadOnly()
default:
panic(fmt.Errorf("SetReadOnly called on invalid attribute type: %s. %s", reflect.TypeOf(attr).String(), common.TerraformBugErrorMessage))
}
}

navigateSchemaWithCallback(&s.attr, cb, path...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,48 @@ func TestCustomizeSchemaObjectTypeValidatorAdded(t *testing.T) {

assert.True(t, len(scm.Blocks["nested_slice_object"].(schema.ListNestedBlock).Validators) == 1)
}

func TestCustomizeSchema_SetRequired_PanicOnBlock(t *testing.T) {
assert.Panics(t, func() {
_ = ResourceStructToSchema(TestTfSdk{}, func(c CustomizableSchema) CustomizableSchema {
c.SetRequired("nested")
return c
})
})
}

func TestCustomizeSchema_SetOptional_PanicOnBlock(t *testing.T) {
assert.Panics(t, func() {
_ = ResourceStructToSchema(TestTfSdk{}, func(c CustomizableSchema) CustomizableSchema {
c.SetOptional("nested")
return c
})
})
}

func TestCustomizeSchema_SetSensitive_PanicOnBlock(t *testing.T) {
assert.Panics(t, func() {
_ = ResourceStructToSchema(TestTfSdk{}, func(c CustomizableSchema) CustomizableSchema {
c.SetSensitive("nested")
return c
})
})
}

func TestCustomizeSchema_SetReadOnly_PanicOnBlock(t *testing.T) {
assert.Panics(t, func() {
_ = ResourceStructToSchema(TestTfSdk{}, func(c CustomizableSchema) CustomizableSchema {
c.SetReadOnly("nested")
return c
})
})
}

func TestCustomizeSchema_SetComputed_PanicOnBlock(t *testing.T) {
assert.Panics(t, func() {
_ = ResourceStructToSchema(TestTfSdk{}, func(c CustomizableSchema) CustomizableSchema {
c.SetComputed("nested")
return c
})
})
}
48 changes: 0 additions & 48 deletions internal/providers/pluginfw/tfschema/list_nested_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import (
// To be compatible with our sdkv2 schema, all struct types in the gosdk are represented with this type.
type ListNestedBlockBuilder struct {
NestedObject NestedBlockObject
Optional bool
Required bool
Sensitive bool
Computed bool
DeprecationMessage string
Validators []validator.List
PlanModifiers []planmodifier.List
Expand All @@ -37,50 +33,6 @@ func (a ListNestedBlockBuilder) BuildResourceBlock() schema.Block {
}
}

func (a ListNestedBlockBuilder) SetOptional() BaseSchemaBuilder {
if a.Optional && !a.Required {
panic("attribute is already optional")
}
a.Optional = true
a.Required = false
return a
}

func (a ListNestedBlockBuilder) SetRequired() BaseSchemaBuilder {
if !a.Optional && a.Required {
panic("attribute is already required")
}
a.Optional = false
a.Required = true
return a
}

func (a ListNestedBlockBuilder) SetSensitive() BaseSchemaBuilder {
if a.Sensitive {
panic("attribute is already sensitive")
}
a.Sensitive = true
return a
}

func (a ListNestedBlockBuilder) SetComputed() BaseSchemaBuilder {
if a.Computed {
panic("attribute is already computed")
}
a.Computed = true
return a
}

func (a ListNestedBlockBuilder) SetReadOnly() BaseSchemaBuilder {
if a.Computed && !a.Optional && !a.Required {
panic("attribute is already read only")
}
a.Computed = true
a.Optional = false
a.Required = false
return a
}

func (a ListNestedBlockBuilder) SetDeprecated(msg string) BaseSchemaBuilder {
a.DeprecationMessage = msg
return a
Expand Down
48 changes: 0 additions & 48 deletions internal/providers/pluginfw/tfschema/single_nested_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import (
// SingleNestedBlockBuilder represents a single nested complex (non-primitive) type.
type SingleNestedBlockBuilder struct {
NestedObject NestedBlockObject
Optional bool
Required bool
Sensitive bool
Computed bool
DeprecationMessage string
Validators []validator.Object
PlanModifiers []planmodifier.Object
Expand Down Expand Up @@ -49,50 +45,6 @@ func (a SingleNestedBlockBuilder) BuildResourceBlock() schema.Block {
}
}

func (a SingleNestedBlockBuilder) SetOptional() BaseSchemaBuilder {
if a.Optional && !a.Required {
panic("attribute is already optional")
}
a.Optional = true
a.Required = false
return a
}

func (a SingleNestedBlockBuilder) SetRequired() BaseSchemaBuilder {
if !a.Optional && a.Required {
panic("attribute is already required")
}
a.Optional = false
a.Required = true
return a
}

func (a SingleNestedBlockBuilder) SetSensitive() BaseSchemaBuilder {
if a.Sensitive {
panic("attribute is already sensitive")
}
a.Sensitive = true
return a
}

func (a SingleNestedBlockBuilder) SetComputed() BaseSchemaBuilder {
if a.Computed {
panic("attribute is already computed")
}
a.Computed = true
return a
}

func (a SingleNestedBlockBuilder) SetReadOnly() BaseSchemaBuilder {
if a.Computed && !a.Optional && !a.Required {
panic("attribute is already read only")
}
a.Computed = true
a.Optional = false
a.Required = false
return a
}

func (a SingleNestedBlockBuilder) SetDeprecated(msg string) BaseSchemaBuilder {
a.DeprecationMessage = msg
return a
Expand Down
6 changes: 0 additions & 6 deletions internal/providers/pluginfw/tfschema/struct_to_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ func typeToSchema(v reflect.Value) NestedBlockObject {
Attributes: nestedScm.Attributes,
Blocks: nestedScm.Blocks,
},
Optional: structTag.optional,
Required: !structTag.optional,
Computed: structTag.computed,
Validators: validators,
}
}
Expand Down Expand Up @@ -188,9 +185,6 @@ func typeToSchema(v reflect.Value) NestedBlockObject {
nestedScm := typeToSchema(sv)
scmBlock[fieldName] = ListNestedBlockBuilder{
NestedObject: nestedScm,
Optional: structTag.optional,
Required: !structTag.optional,
Computed: structTag.computed,
}
}
} else {
Expand Down
Loading