Skip to content

Commit

Permalink
all: Remove remaining Block-based MaxItems and MinItems functionality
Browse files Browse the repository at this point in the history
Reference: #437

Block MaxItems and MinItems support, while defined in the Terraform protocol, is intentially not included in the newer schema handling. Terraform can only perform limited static analysis of blocks and errors generated occur before the provider is called for configuration validation, which means that practitioners do not get all configuration errors at the same time. Provider developers can implement validators to achieve the same validation functionality, for example using the `listvalidator.Size*` and `setvalidator.Size*` validators in the terraform-plugin-framework-validators Go module.
  • Loading branch information
bflad committed Dec 13, 2022
1 parent c7608ec commit 003c041
Show file tree
Hide file tree
Showing 32 changed files with 7 additions and 1,169 deletions.
16 changes: 0 additions & 16 deletions datasource/schema/list_nested_block.go
Expand Up @@ -158,22 +158,6 @@ func (b ListNestedBlock) GetMarkdownDescription() string {
return b.MarkdownDescription
}

// GetMaxItems always returns 0.
//
// Deprecated: This method will be removed in the future. Use validators
// instead.
func (b ListNestedBlock) GetMaxItems() int64 {
return 0
}

// GetMinItems always returns 0.
//
// Deprecated: This method will be removed in the future. Use validators
// instead.
func (b ListNestedBlock) GetMinItems() int64 {
return 0
}

// GetNestedObject returns the NestedObject field value.
func (b ListNestedBlock) GetNestedObject() fwschema.NestedBlockObject {
return b.NestedObject
Expand Down
68 changes: 0 additions & 68 deletions datasource/schema/list_nested_block_test.go
Expand Up @@ -297,74 +297,6 @@ func TestListNestedBlockGetMarkdownDescription(t *testing.T) {
}
}

func TestListNestedBlockGetMaxItems(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
block schema.ListNestedBlock
expected int64
}{
"0": {
block: schema.ListNestedBlock{
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"testattr": schema.StringAttribute{},
},
},
},
expected: 0,
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.block.GetMaxItems()

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}

func TestListNestedBlockGetMinItems(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
block schema.ListNestedBlock
expected int64
}{
"0": {
block: schema.ListNestedBlock{
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"testattr": schema.StringAttribute{},
},
},
},
expected: 0,
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.block.GetMinItems()

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}

func TestListNestedBlockGetNestedObject(t *testing.T) {
t.Parallel()

Expand Down
16 changes: 0 additions & 16 deletions datasource/schema/set_nested_block.go
Expand Up @@ -158,22 +158,6 @@ func (b SetNestedBlock) GetMarkdownDescription() string {
return b.MarkdownDescription
}

// GetMaxItems always returns 0.
//
// Deprecated: This method will be removed in the future. Use validators
// instead.
func (b SetNestedBlock) GetMaxItems() int64 {
return 0
}

// GetMinItems always returns 0.
//
// Deprecated: This method will be removed in the future. Use validators
// instead.
func (b SetNestedBlock) GetMinItems() int64 {
return 0
}

// GetNestedObject returns the NestedObject field value.
func (b SetNestedBlock) GetNestedObject() fwschema.NestedBlockObject {
return b.NestedObject
Expand Down
68 changes: 0 additions & 68 deletions datasource/schema/set_nested_block_test.go
Expand Up @@ -297,74 +297,6 @@ func TestSetNestedBlockGetMarkdownDescription(t *testing.T) {
}
}

func TestSetNestedBlockGetMaxItems(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
block schema.SetNestedBlock
expected int64
}{
"0": {
block: schema.SetNestedBlock{
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"testattr": schema.StringAttribute{},
},
},
},
expected: 0,
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.block.GetMaxItems()

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}

func TestSetNestedBlockGetMinItems(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
block schema.SetNestedBlock
expected int64
}{
"0": {
block: schema.SetNestedBlock{
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"testattr": schema.StringAttribute{},
},
},
},
expected: 0,
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.block.GetMinItems()

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}

func TestSetNestedBlockGetNestedObject(t *testing.T) {
t.Parallel()

Expand Down
14 changes: 0 additions & 14 deletions datasource/schema/single_nested_block.go
Expand Up @@ -167,20 +167,6 @@ func (b SingleNestedBlock) GetMarkdownDescription() string {
return b.MarkdownDescription
}

// GetMaxItems always returns 0.
//
// Deprecated: This method will be removed in the future.
func (b SingleNestedBlock) GetMaxItems() int64 {
return 0
}

// GetMinItems always returns 0.
//
// Deprecated: This method will be removed in the future.
func (b SingleNestedBlock) GetMinItems() int64 {
return 0
}

// GetNestedObject returns a generated NestedBlockObject from the
// Attributes, CustomType, and Validators field values.
func (b SingleNestedBlock) GetNestedObject() fwschema.NestedBlockObject {
Expand Down
64 changes: 0 additions & 64 deletions datasource/schema/single_nested_block_test.go
Expand Up @@ -297,70 +297,6 @@ func TestSingleNestedBlockGetMarkdownDescription(t *testing.T) {
}
}

func TestSingleNestedBlockGetMaxItems(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
block schema.SingleNestedBlock
expected int64
}{
"0": {
block: schema.SingleNestedBlock{
Attributes: map[string]schema.Attribute{
"testattr": schema.StringAttribute{},
},
},
expected: 0,
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.block.GetMaxItems()

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}

func TestSingleNestedBlockGetMinItems(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
block schema.SingleNestedBlock
expected int64
}{
"0": {
block: schema.SingleNestedBlock{
Attributes: map[string]schema.Attribute{
"testattr": schema.StringAttribute{},
},
},
expected: 0,
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.block.GetMinItems()

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}

func TestSingleNestedBlockGetNestedObject(t *testing.T) {
t.Parallel()

Expand Down
25 changes: 7 additions & 18 deletions internal/fwschema/block.go
Expand Up @@ -13,6 +13,13 @@ import (
// Refer to the internal/fwschema/fwxschema package for optional interfaces
// that define framework-specific functionality, such a plan modification and
// validation.
//
// Note that MaxItems and MinItems support, while defined in the Terraform
// protocol, is intentially not present. Terraform can only perform limited
// static analysis of blocks and errors generated occur before the provider
// is called for configuration validation, which means that practitioners do
// not get all configuration errors at the same time. Provider developers can
// implement validators to achieve the same validation functionality.
type Block interface {
// Implementations should include the tftypes.AttributePathStepper
// interface methods for proper path and data handling.
Expand All @@ -37,16 +44,6 @@ type Block interface {
// name.
GetMarkdownDescription() string

// GetMaxItems should return the max items of a block. This is named
// differently than MaxItems to prevent a conflict with the tfsdk.Block
// field name.
GetMaxItems() int64

// GetMinItems should return the min items of a block. This is named
// differently than MinItems to prevent a conflict with the tfsdk.Block
// field name.
GetMinItems() int64

// GetNestedObject should return the object underneath the block.
// For single nesting mode, the NestedBlockObject can be generated from
// the Block.
Expand Down Expand Up @@ -81,13 +78,5 @@ func BlocksEqual(a, b Block) bool {
return false
}

if a.GetMaxItems() != b.GetMaxItems() {
return false
}

if a.GetMinItems() != b.GetMinItems() {
return false
}

return true
}

0 comments on commit 003c041

Please sign in to comment.