Skip to content

Commit

Permalink
Add resource attribute interface (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Feb 13, 2023
1 parent 7a344fd commit 38c6858
Show file tree
Hide file tree
Showing 18 changed files with 681 additions and 27 deletions.
18 changes: 16 additions & 2 deletions resource/schema/bool_attribute.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package schema

import (
"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/defaults"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

// Ensure the implementation satisifies the desired interfaces.
// Ensure the implementation satisfies the desired interfaces.
var (
_ Attribute = BoolAttribute{}
_ fwxschema.AttributeWithBoolDefaultValue = BoolAttribute{}
_ fwxschema.AttributeWithBoolPlanModifiers = BoolAttribute{}
_ fwxschema.AttributeWithBoolValidators = BoolAttribute{}
)
Expand Down Expand Up @@ -135,6 +138,12 @@ type BoolAttribute struct {
//
// Any errors will prevent further execution of this sequence or modifiers.
PlanModifiers []planmodifier.Bool

// Default defines a default value for the attribute. The default value
// handling occurs during the `PlanResourceChange` RPC and modifies the
// Terraform-provided proposed new state before the framework performs
// its check between the proposed new state and prior state.
Default defaults.Bool
}

// ApplyTerraform5AttributePathStep always returns an error as it is not
Expand All @@ -153,6 +162,11 @@ func (a BoolAttribute) BoolValidators() []validator.Bool {
return a.Validators
}

// DefaultValue returns the Default field value.
func (a BoolAttribute) DefaultValue() defaults.Bool {
return a.Default
}

// Equal returns true if the given Attribute is a BoolAttribute
// and all fields are equal.
func (a BoolAttribute) Equal(o fwschema.Attribute) bool {
Expand Down
53 changes: 52 additions & 1 deletion resource/schema/bool_attribute_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package schema_test

import (
"context"
"fmt"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testschema"
testtypes "github.com/hashicorp/terraform-plugin-framework/internal/testing/types"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/defaults"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

func TestBoolAttributeApplyTerraform5AttributePathStep(t *testing.T) {
Expand Down Expand Up @@ -149,6 +153,53 @@ func TestBoolAttributeBoolValidators(t *testing.T) {
}
}

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

opt := cmp.Comparer(func(x, y defaults.Bool) bool {
ctx := context.Background()
req := defaults.BoolRequest{}

xResp := defaults.BoolResponse{}
x.DefaultBool(ctx, req, &xResp)

yResp := defaults.BoolResponse{}
y.DefaultBool(ctx, req, &yResp)

return xResp.PlanValue.Equal(yResp.PlanValue)
})

testCases := map[string]struct {
attribute schema.BoolAttribute
expected defaults.Bool
}{
"no-default": {
attribute: schema.BoolAttribute{},
expected: nil,
},
"default": {
attribute: schema.BoolAttribute{
Default: booldefault.StaticValue(true),
},
expected: booldefault.StaticValue(true),
},
}

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

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

got := testCase.attribute.DefaultValue()

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

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

Expand Down
18 changes: 16 additions & 2 deletions resource/schema/float64_attribute.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package schema

import (
"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/defaults"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

// Ensure the implementation satisifies the desired interfaces.
// Ensure the implementation satisfies the desired interfaces.
var (
_ Attribute = Float64Attribute{}
_ fwxschema.AttributeWithFloat64DefaultValue = Float64Attribute{}
_ fwxschema.AttributeWithFloat64PlanModifiers = Float64Attribute{}
_ fwxschema.AttributeWithFloat64Validators = Float64Attribute{}
)
Expand Down Expand Up @@ -138,6 +141,12 @@ type Float64Attribute struct {
//
// Any errors will prevent further execution of this sequence or modifiers.
PlanModifiers []planmodifier.Float64

// Default defines a default value for the attribute. The default value
// handling occurs during the `PlanResourceChange` RPC and modifies the
// Terraform-provided proposed new state before the framework performs
// its check between the proposed new state and prior state.
Default defaults.Float64
}

// ApplyTerraform5AttributePathStep always returns an error as it is not
Expand All @@ -146,6 +155,11 @@ func (a Float64Attribute) ApplyTerraform5AttributePathStep(step tftypes.Attribut
return a.GetType().ApplyTerraform5AttributePathStep(step)
}

// DefaultValue returns the Default field value.
func (a Float64Attribute) DefaultValue() defaults.Float64 {
return a.Default
}

// Equal returns true if the given Attribute is a Float64Attribute
// and all fields are equal.
func (a Float64Attribute) Equal(o fwschema.Attribute) bool {
Expand Down
53 changes: 52 additions & 1 deletion resource/schema/float64_attribute_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package schema_test

import (
"context"
"fmt"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testschema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/defaults"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/float64default"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

func TestFloat64AttributeApplyTerraform5AttributePathStep(t *testing.T) {
Expand Down Expand Up @@ -80,6 +84,53 @@ func TestFloat64AttributeApplyTerraform5AttributePathStep(t *testing.T) {
}
}

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

opt := cmp.Comparer(func(x, y defaults.Float64) bool {
ctx := context.Background()
req := defaults.Float64Request{}

xResp := defaults.Float64Response{}
x.DefaultFloat64(ctx, req, &xResp)

yResp := defaults.Float64Response{}
y.DefaultFloat64(ctx, req, &yResp)

return xResp.PlanValue.Equal(yResp.PlanValue)
})

testCases := map[string]struct {
attribute schema.Float64Attribute
expected defaults.Float64
}{
"no-default": {
attribute: schema.Float64Attribute{},
expected: nil,
},
"default": {
attribute: schema.Float64Attribute{
Default: float64default.StaticValue(1.2345),
},
expected: float64default.StaticValue(1.2345),
},
}

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

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

got := testCase.attribute.DefaultValue()

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

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

Expand Down
18 changes: 16 additions & 2 deletions resource/schema/int64_attribute.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package schema

import (
"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/defaults"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

// Ensure the implementation satisifies the desired interfaces.
// Ensure the implementation satisfies the desired interfaces.
var (
_ Attribute = Int64Attribute{}
_ fwxschema.AttributeWithInt64DefaultValue = Int64Attribute{}
_ fwxschema.AttributeWithInt64PlanModifiers = Int64Attribute{}
_ fwxschema.AttributeWithInt64Validators = Int64Attribute{}
)
Expand Down Expand Up @@ -138,6 +141,12 @@ type Int64Attribute struct {
//
// Any errors will prevent further execution of this sequence or modifiers.
PlanModifiers []planmodifier.Int64

// Default defines a default value for the attribute. The default value
// handling occurs during the `PlanResourceChange` RPC and modifies the
// Terraform-provided proposed new state before the framework performs
// its check between the proposed new state and prior state.
Default defaults.Int64
}

// ApplyTerraform5AttributePathStep always returns an error as it is not
Expand All @@ -146,6 +155,11 @@ func (a Int64Attribute) ApplyTerraform5AttributePathStep(step tftypes.AttributeP
return a.GetType().ApplyTerraform5AttributePathStep(step)
}

// DefaultValue returns the Default field value.
func (a Int64Attribute) DefaultValue() defaults.Int64 {
return a.Default
}

// Equal returns true if the given Attribute is a Int64Attribute
// and all fields are equal.
func (a Int64Attribute) Equal(o fwschema.Attribute) bool {
Expand Down
53 changes: 52 additions & 1 deletion resource/schema/int64_attribute_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package schema_test

import (
"context"
"fmt"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testschema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/defaults"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

func TestInt64AttributeApplyTerraform5AttributePathStep(t *testing.T) {
Expand Down Expand Up @@ -80,6 +84,53 @@ func TestInt64AttributeApplyTerraform5AttributePathStep(t *testing.T) {
}
}

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

opt := cmp.Comparer(func(x, y defaults.Int64) bool {
ctx := context.Background()
req := defaults.Int64Request{}

xResp := defaults.Int64Response{}
x.DefaultInt64(ctx, req, &xResp)

yResp := defaults.Int64Response{}
y.DefaultInt64(ctx, req, &yResp)

return xResp.PlanValue.Equal(yResp.PlanValue)
})

testCases := map[string]struct {
attribute schema.Int64Attribute
expected defaults.Int64
}{
"no-default": {
attribute: schema.Int64Attribute{},
expected: nil,
},
"default": {
attribute: schema.Int64Attribute{
Default: int64default.StaticValue(12345),
},
expected: int64default.StaticValue(12345),
},
}

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

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

got := testCase.attribute.DefaultValue()

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

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

Expand Down

0 comments on commit 38c6858

Please sign in to comment.