Skip to content

Commit

Permalink
Switching to using native framework path for AtLeastSumOf, AtMostSumO…
Browse files Browse the repository at this point in the history
…f and EqualToSumOf validators (#20)
  • Loading branch information
bendbennett committed Jul 13, 2022
1 parent 4e3474b commit 533f2df
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 123 deletions.
18 changes: 9 additions & 9 deletions int64validator/at_least_sum_of.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ import (
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
)

var _ tfsdk.AttributeValidator = atLeastSumOfValidator{}

// atLeastSumOfValidator validates that an integer Attribute's value is at least the sum of one
// or more integer Attributes.
type atLeastSumOfValidator struct {
attributesToSumPaths []*tftypes.AttributePath
attributesToSumPaths []path.Path
}

// Description describes the validation in plain text formatting.
func (validator atLeastSumOfValidator) Description(_ context.Context) string {
var attributePaths []string
for _, path := range validator.attributesToSumPaths {
attributePaths = append(attributePaths, path.String())
for _, p := range validator.attributesToSumPaths {
attributePaths = append(attributePaths, p.String())
}

return fmt.Sprintf("value must be at least sum of %s", strings.Join(attributePaths, " + "))
Expand All @@ -46,10 +46,10 @@ func (validator atLeastSumOfValidator) Validate(ctx context.Context, request tfs
var sumOfAttribs int64
var numUnknownAttribsToSum int

for _, path := range validator.attributesToSumPaths {
for _, p := range validator.attributesToSumPaths {
var attribToSum types.Int64

response.Diagnostics.Append(request.Config.GetAttribute(ctx, path, &attribToSum)...)
response.Diagnostics.Append(request.Config.GetAttribute(ctx, p, &attribToSum)...)
if response.Diagnostics.HasError() {
return
}
Expand All @@ -72,7 +72,7 @@ func (validator atLeastSumOfValidator) Validate(ctx context.Context, request tfs

if i < sumOfAttribs {

response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic(
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.AttributePath,
validator.Description(ctx),
fmt.Sprintf("%d", i),
Expand All @@ -89,7 +89,7 @@ func (validator atLeastSumOfValidator) Validate(ctx context.Context, request tfs
// - Is exclusively at least the sum of the given attributes.
//
// Null (unconfigured) and unknown (known after apply) values are skipped.
func AtLeastSumOf(attributesToSum ...*tftypes.AttributePath) tfsdk.AttributeValidator {
func AtLeastSumOf(attributesToSum ...path.Path) tfsdk.AttributeValidator {
return atLeastSumOfValidator{
attributesToSumPaths: attributesToSum,
}
Expand Down
65 changes: 33 additions & 32 deletions int64validator/at_least_sum_of_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
Expand All @@ -15,7 +16,7 @@ func TestAtLeastSumOfValidator(t *testing.T) {

type testCase struct {
val attr.Value
attributesToSumPaths []*tftypes.AttributePath
attributesToSumPaths []path.Path
requestConfigRaw map[string]tftypes.Value
expectError bool
}
Expand All @@ -32,9 +33,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
},
"valid integer as Int64 less than sum of attributes": {
val: types.Int64{Value: 10},
attributesToSumPaths: []*tftypes.AttributePath{
tftypes.NewAttributePath().WithAttributeName("one"),
tftypes.NewAttributePath().WithAttributeName("two"),
attributesToSumPaths: []path.Path{
path.Root("one"),
path.Root("two"),
},
requestConfigRaw: map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Number, 15),
Expand All @@ -44,9 +45,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
},
"valid integer as Int64 equal to sum of attributes": {
val: types.Int64{Value: 10},
attributesToSumPaths: []*tftypes.AttributePath{
tftypes.NewAttributePath().WithAttributeName("one"),
tftypes.NewAttributePath().WithAttributeName("two"),
attributesToSumPaths: []path.Path{
path.Root("one"),
path.Root("two"),
},
requestConfigRaw: map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Number, 5),
Expand All @@ -55,9 +56,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
},
"valid integer as Int64 greater than sum of attributes": {
val: types.Int64{Value: 10},
attributesToSumPaths: []*tftypes.AttributePath{
tftypes.NewAttributePath().WithAttributeName("one"),
tftypes.NewAttributePath().WithAttributeName("two"),
attributesToSumPaths: []path.Path{
path.Root("one"),
path.Root("two"),
},
requestConfigRaw: map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Number, 4),
Expand All @@ -66,9 +67,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
},
"valid integer as Int64 greater than sum of attributes, when one summed attribute is null": {
val: types.Int64{Value: 10},
attributesToSumPaths: []*tftypes.AttributePath{
tftypes.NewAttributePath().WithAttributeName("one"),
tftypes.NewAttributePath().WithAttributeName("two"),
attributesToSumPaths: []path.Path{
path.Root("one"),
path.Root("two"),
},
requestConfigRaw: map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Number, nil),
Expand All @@ -77,9 +78,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
},
"valid integer as Int64 does not return error when all attributes are null": {
val: types.Int64{Null: true},
attributesToSumPaths: []*tftypes.AttributePath{
tftypes.NewAttributePath().WithAttributeName("one"),
tftypes.NewAttributePath().WithAttributeName("two"),
attributesToSumPaths: []path.Path{
path.Root("one"),
path.Root("two"),
},
requestConfigRaw: map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Number, nil),
Expand All @@ -88,9 +89,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
},
"valid integer as Int64 returns error when all attributes to sum are null": {
val: types.Int64{Value: -1},
attributesToSumPaths: []*tftypes.AttributePath{
tftypes.NewAttributePath().WithAttributeName("one"),
tftypes.NewAttributePath().WithAttributeName("two"),
attributesToSumPaths: []path.Path{
path.Root("one"),
path.Root("two"),
},
requestConfigRaw: map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Number, nil),
Expand All @@ -100,9 +101,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
},
"valid integer as Int64 greater than sum of attributes, when one summed attribute is unknown": {
val: types.Int64{Value: 10},
attributesToSumPaths: []*tftypes.AttributePath{
tftypes.NewAttributePath().WithAttributeName("one"),
tftypes.NewAttributePath().WithAttributeName("two"),
attributesToSumPaths: []path.Path{
path.Root("one"),
path.Root("two"),
},
requestConfigRaw: map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue),
Expand All @@ -111,9 +112,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
},
"valid integer as Int64 does not return error when all attributes are unknown": {
val: types.Int64{Unknown: true},
attributesToSumPaths: []*tftypes.AttributePath{
tftypes.NewAttributePath().WithAttributeName("one"),
tftypes.NewAttributePath().WithAttributeName("two"),
attributesToSumPaths: []path.Path{
path.Root("one"),
path.Root("two"),
},
requestConfigRaw: map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue),
Expand All @@ -122,9 +123,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
},
"valid integer as Int64 does not return error when all attributes to sum are unknown": {
val: types.Int64{Value: -1},
attributesToSumPaths: []*tftypes.AttributePath{
tftypes.NewAttributePath().WithAttributeName("one"),
tftypes.NewAttributePath().WithAttributeName("two"),
attributesToSumPaths: []path.Path{
path.Root("one"),
path.Root("two"),
},
requestConfigRaw: map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Number, tftypes.UnknownValue),
Expand All @@ -133,9 +134,9 @@ func TestAtLeastSumOfValidator(t *testing.T) {
},
"error when attribute to sum is not Number": {
val: types.Int64{Value: 9},
attributesToSumPaths: []*tftypes.AttributePath{
tftypes.NewAttributePath().WithAttributeName("one"),
tftypes.NewAttributePath().WithAttributeName("two"),
attributesToSumPaths: []path.Path{
path.Root("one"),
path.Root("two"),
},
requestConfigRaw: map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Bool, true),
Expand All @@ -149,7 +150,7 @@ func TestAtLeastSumOfValidator(t *testing.T) {
name, test := name, test
t.Run(name, func(t *testing.T) {
request := tfsdk.ValidateAttributeRequest{
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
AttributePath: path.Root("test"),
AttributeConfig: test.val,
Config: tfsdk.Config{
Raw: tftypes.NewValue(tftypes.Object{}, test.requestConfigRaw),
Expand Down
18 changes: 9 additions & 9 deletions int64validator/at_most_sum_of.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ import (
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
)

var _ tfsdk.AttributeValidator = atMostSumOfValidator{}

// atMostSumOfValidator validates that an integer Attribute's value is at most the sum of one
// or more integer Attributes.
type atMostSumOfValidator struct {
attributesToSumPaths []*tftypes.AttributePath
attributesToSumPaths []path.Path
}

// Description describes the validation in plain text formatting.
func (validator atMostSumOfValidator) Description(_ context.Context) string {
var attributePaths []string
for _, path := range validator.attributesToSumPaths {
attributePaths = append(attributePaths, path.String())
for _, p := range validator.attributesToSumPaths {
attributePaths = append(attributePaths, p.String())
}

return fmt.Sprintf("value must be at most sum of %s", strings.Join(attributePaths, " + "))
Expand All @@ -46,10 +46,10 @@ func (validator atMostSumOfValidator) Validate(ctx context.Context, request tfsd
var sumOfAttribs int64
var numUnknownAttribsToSum int

for _, path := range validator.attributesToSumPaths {
for _, p := range validator.attributesToSumPaths {
var attribToSum types.Int64

response.Diagnostics.Append(request.Config.GetAttribute(ctx, path, &attribToSum)...)
response.Diagnostics.Append(request.Config.GetAttribute(ctx, p, &attribToSum)...)
if response.Diagnostics.HasError() {
return
}
Expand All @@ -72,7 +72,7 @@ func (validator atMostSumOfValidator) Validate(ctx context.Context, request tfsd

if i > sumOfAttribs {

response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic(
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.AttributePath,
validator.Description(ctx),
fmt.Sprintf("%d", i),
Expand All @@ -89,7 +89,7 @@ func (validator atMostSumOfValidator) Validate(ctx context.Context, request tfsd
// - Is exclusively at most the sum of the given attributes.
//
// Null (unconfigured) and unknown (known after apply) values are skipped.
func AtMostSumOf(attributesToSum ...*tftypes.AttributePath) tfsdk.AttributeValidator {
func AtMostSumOf(attributesToSum ...path.Path) tfsdk.AttributeValidator {
return atMostSumOfValidator{
attributesToSumPaths: attributesToSum,
}
Expand Down
Loading

0 comments on commit 533f2df

Please sign in to comment.