Skip to content

Commit 2cc6fbf

Browse files
committed
updated to use new interface
1 parent c832d5d commit 2cc6fbf

File tree

5 files changed

+99
-8
lines changed

5 files changed

+99
-8
lines changed

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ require (
1010
github.com/hashicorp/terraform-plugin-go v0.25.0
1111
)
1212

13+
replace github.com/hashicorp/terraform-plugin-go => /Users/austin.valle/code/terraform-plugin-go
14+
15+
replace github.com/hashicorp/terraform-plugin-framework => /Users/austin.valle/code/terraform-plugin-framework
16+
1317
require (
1418
github.com/fatih/color v1.13.0 // indirect
1519
github.com/hashicorp/go-hclog v1.5.0 // indirect

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
77
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
88
github.com/hashicorp/go-hclog v1.5.0 h1:bI2ocEMgcVlz55Oj1xZNBsVi900c7II+fWDyV9o+13c=
99
github.com/hashicorp/go-hclog v1.5.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
10-
github.com/hashicorp/terraform-plugin-framework v1.13.0 h1:8OTG4+oZUfKgnfTdPTJwZ532Bh2BobF4H+yBiYJ/scw=
11-
github.com/hashicorp/terraform-plugin-framework v1.13.0/go.mod h1:j64rwMGpgM3NYXTKuxrCnyubQb/4VKldEKlcG8cvmjU=
12-
github.com/hashicorp/terraform-plugin-go v0.25.0 h1:oi13cx7xXA6QciMcpcFi/rwA974rdTxjqEhXJjbAyks=
13-
github.com/hashicorp/terraform-plugin-go v0.25.0/go.mod h1:+SYagMYadJP86Kvn+TGeV+ofr/R3g4/If0O5sO96MVw=
1410
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
1511
github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow=
1612
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=

int64validator/at_most.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99

10+
"github.com/hashicorp/terraform-plugin-framework/diag"
1011
"github.com/hashicorp/terraform-plugin-framework/function"
1112
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1213

@@ -30,7 +31,28 @@ func (validator atMostValidator) MarkdownDescription(ctx context.Context) string
3031
}
3132

3233
func (v atMostValidator) ValidateInt64(ctx context.Context, request validator.Int64Request, response *validator.Int64Response) {
33-
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
34+
if request.ConfigValue.IsNull() {
35+
return
36+
}
37+
38+
if request.ConfigValue.IsUnknown() {
39+
// Check if there is a lower bound refinement, and if that lower bound indicates the eventual value will be invalid
40+
if lowerRefn, ok := request.ConfigValue.LowerBoundRefinement(); ok {
41+
if lowerRefn.IsInclusive() && lowerRefn.LowerBound() > v.max {
42+
response.Diagnostics.Append(diag.NewAttributeErrorDiagnostic(
43+
request.Path,
44+
"Invalid Attribute Value",
45+
// TODO: improve error messaging?
46+
fmt.Sprintf("Attribute %s %s, got an unknown value that will be greater than: %d", request.Path, v.Description(ctx), lowerRefn.LowerBound()),
47+
))
48+
} else if !lowerRefn.IsInclusive() && lowerRefn.LowerBound() >= v.max {
49+
response.Diagnostics.Append(diag.NewAttributeErrorDiagnostic(
50+
request.Path,
51+
"Invalid Attribute Value",
52+
fmt.Sprintf("Attribute %s %s, got an unknown value that will be at least: %d", request.Path, v.Description(ctx), lowerRefn.LowerBound()),
53+
))
54+
}
55+
}
3456
return
3557
}
3658

@@ -44,7 +66,25 @@ func (v atMostValidator) ValidateInt64(ctx context.Context, request validator.In
4466
}
4567

4668
func (v atMostValidator) ValidateParameterInt64(ctx context.Context, request function.Int64ParameterValidatorRequest, response *function.Int64ParameterValidatorResponse) {
47-
if request.Value.IsNull() || request.Value.IsUnknown() {
69+
if request.Value.IsNull() {
70+
return
71+
}
72+
73+
if request.Value.IsUnknown() {
74+
// Check if there is a lower bound refinement, and if that lower bound indicates the eventual value will be invalid
75+
if lowerRefn, ok := request.Value.LowerBoundRefinement(); ok {
76+
if lowerRefn.IsInclusive() && lowerRefn.LowerBound() > v.max {
77+
response.Error = function.NewArgumentFuncError(
78+
request.ArgumentPosition,
79+
fmt.Sprintf("Invalid Parameter Value: %s, got an unknown value that will be greater than: %d", v.Description(ctx), lowerRefn.LowerBound()),
80+
)
81+
} else if !lowerRefn.IsInclusive() && lowerRefn.LowerBound() >= v.max {
82+
response.Error = function.NewArgumentFuncError(
83+
request.ArgumentPosition,
84+
fmt.Sprintf("Invalid Parameter Value: %s, got an unknown value that will be at least: %d", v.Description(ctx), lowerRefn.LowerBound()),
85+
)
86+
}
87+
}
4888
return
4989
}
5090

int64validator/at_most_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,35 @@ func TestAtMostValidator(t *testing.T) {
4646
max: 2,
4747
expectError: true,
4848
},
49+
// Unknown value will be > 2
50+
"unknown lower bound exclusive - invalid less than bound": {
51+
val: types.Int64Unknown().RefineWithLowerBound(2, false),
52+
max: 1,
53+
expectError: true,
54+
},
55+
"unknown lower bound exclusive - invalid matches bound": {
56+
val: types.Int64Unknown().RefineWithLowerBound(2, false),
57+
max: 2,
58+
expectError: true,
59+
},
60+
"unknown lower bound exclusive - valid greater than bound": {
61+
val: types.Int64Unknown().RefineWithLowerBound(2, false),
62+
max: 3,
63+
},
64+
// Unknown value will be >= 2
65+
"unknown lower bound inclusive - invalid less than bound": {
66+
val: types.Int64Unknown().RefineWithLowerBound(2, true),
67+
max: 1,
68+
expectError: true,
69+
},
70+
"unknown lower bound inclusive - valid matches bound": {
71+
val: types.Int64Unknown().RefineWithLowerBound(2, true),
72+
max: 2,
73+
},
74+
"unknown lower bound inclusive - valid greater than bound": {
75+
val: types.Int64Unknown().RefineWithLowerBound(2, true),
76+
max: 3,
77+
},
4978
}
5079

5180
for name, test := range tests {

stringvalidator/length_at_most.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
1111
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatorfuncerr"
12+
"github.com/hashicorp/terraform-plugin-framework/diag"
1213
"github.com/hashicorp/terraform-plugin-framework/function"
1314
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1415
)
@@ -46,7 +47,18 @@ func (v lengthAtMostValidator) ValidateString(ctx context.Context, request valid
4647
return
4748
}
4849

49-
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
50+
if request.ConfigValue.IsNull() {
51+
return
52+
}
53+
54+
if request.ConfigValue.IsUnknown() {
55+
if prefixRefn, ok := request.ConfigValue.PrefixRefinement(); ok && len(prefixRefn.PrefixValue()) > v.maxLength {
56+
response.Diagnostics.Append(diag.NewAttributeErrorDiagnostic(
57+
request.Path,
58+
"Invalid Attribute Value Length",
59+
fmt.Sprintf("Attribute %s %s, got an unknown value with a prefix of length: %d", request.Path, v.Description(ctx), len(prefixRefn.PrefixValue())),
60+
))
61+
}
5062
return
5163
}
5264

@@ -75,7 +87,17 @@ func (v lengthAtMostValidator) ValidateParameterString(ctx context.Context, requ
7587
return
7688
}
7789

78-
if request.Value.IsNull() || request.Value.IsUnknown() {
90+
if request.Value.IsNull() {
91+
return
92+
}
93+
94+
if request.Value.IsUnknown() {
95+
if prefixRefn, ok := request.Value.PrefixRefinement(); ok && len(prefixRefn.PrefixValue()) > v.maxLength {
96+
response.Error = function.NewArgumentFuncError(
97+
request.ArgumentPosition,
98+
fmt.Sprintf("Invalid Parameter Value: %s, got an unknown value with a prefix of length: %d", v.Description(ctx), len(prefixRefn.PrefixValue())),
99+
)
100+
}
79101
return
80102
}
81103

0 commit comments

Comments
 (0)