Skip to content

Commit b617962

Browse files
author
Pier-Luc Caron St-Pierre
authored
Bump terraform-plugin-go@v0.26.0 (#1231)
* Upgrade terraform-plugin-go to v0.26.0 * Migration from TypeWithValidate to ValidateableAttribute
1 parent 5f833dd commit b617962

File tree

12 files changed

+684
-195
lines changed

12 files changed

+684
-195
lines changed

.changelog/1231.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
Upgrade terraform-plugin-go to v0.26.0
3+
```

go.mod

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ require (
1515
github.com/hashicorp/go-version v1.7.0
1616
github.com/hashicorp/hcp-sdk-go v0.140.0
1717
github.com/hashicorp/terraform-plugin-docs v0.20.1
18-
github.com/hashicorp/terraform-plugin-framework v1.5.0
18+
github.com/hashicorp/terraform-plugin-framework v1.14.1
1919
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
20-
github.com/hashicorp/terraform-plugin-go v0.21.0
21-
github.com/hashicorp/terraform-plugin-mux v0.14.0
22-
github.com/hashicorp/terraform-plugin-sdk/v2 v2.31.0
20+
github.com/hashicorp/terraform-plugin-go v0.26.0
21+
github.com/hashicorp/terraform-plugin-mux v0.18.0
22+
github.com/hashicorp/terraform-plugin-sdk/v2 v2.36.1
2323
github.com/stretchr/testify v1.10.0
2424
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
2525
google.golang.org/grpc v1.70.0
@@ -35,15 +35,16 @@ require (
3535
github.com/yuin/goldmark v1.7.7 // indirect
3636
github.com/yuin/goldmark-meta v1.1.0 // indirect
3737
go.abhg.dev/goldmark/frontmatter v0.2.0 // indirect
38-
golang.org/x/sync v0.10.0 // indirect
38+
golang.org/x/sync v0.11.0 // indirect
39+
golang.org/x/tools v0.22.0 // indirect
3940
gopkg.in/yaml.v2 v2.4.0 // indirect
4041
)
4142

4243
require (
4344
github.com/Masterminds/goutils v1.1.1 // indirect
4445
github.com/Masterminds/semver/v3 v3.2.1 // indirect
4546
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
46-
github.com/ProtonMail/go-crypto v1.1.0-alpha.2 // indirect
47+
github.com/ProtonMail/go-crypto v1.1.3 // indirect
4748
github.com/agext/levenshtein v1.2.3 // indirect
4849
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
4950
github.com/armon/go-radix v1.0.0 // indirect
@@ -70,15 +71,15 @@ require (
7071
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
7172
github.com/hashicorp/go-hclog v1.6.3 // indirect
7273
github.com/hashicorp/go-multierror v1.1.1 // indirect
73-
github.com/hashicorp/go-plugin v1.6.0 // indirect
74-
github.com/hashicorp/hc-install v0.9.0 // indirect
75-
github.com/hashicorp/hcl/v2 v2.19.1 // indirect
74+
github.com/hashicorp/go-plugin v1.6.2 // indirect
75+
github.com/hashicorp/hc-install v0.9.1 // indirect
76+
github.com/hashicorp/hcl/v2 v2.23.0 // indirect
7677
github.com/hashicorp/logutils v1.0.0 // indirect
77-
github.com/hashicorp/terraform-exec v0.21.0 // indirect
78-
github.com/hashicorp/terraform-json v0.23.0 // indirect
78+
github.com/hashicorp/terraform-exec v0.22.0 // indirect
79+
github.com/hashicorp/terraform-json v0.24.0 // indirect
7980
github.com/hashicorp/terraform-plugin-log v0.9.0
80-
github.com/hashicorp/terraform-plugin-testing v1.6.0
81-
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
81+
github.com/hashicorp/terraform-plugin-testing v1.11.0
82+
github.com/hashicorp/terraform-registry-address v0.2.4 // indirect
8283
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
8384
github.com/hashicorp/yamux v0.1.1 // indirect
8485
github.com/huandu/xstrings v1.4.0 // indirect
@@ -105,19 +106,19 @@ require (
105106
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
106107
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
107108
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
108-
github.com/zclconf/go-cty v1.15.0 // indirect
109+
github.com/zclconf/go-cty v1.16.2 // indirect
109110
go.mongodb.org/mongo-driver v1.14.0 // indirect
110111
go.opentelemetry.io/otel v1.32.0 // indirect
111112
go.opentelemetry.io/otel/metric v1.32.0 // indirect
112113
go.opentelemetry.io/otel/trace v1.32.0 // indirect
113-
golang.org/x/crypto v0.32.0 // indirect
114-
golang.org/x/mod v0.21.0 // indirect
115-
golang.org/x/net v0.33.0 // indirect
114+
golang.org/x/crypto v0.33.0 // indirect
115+
golang.org/x/mod v0.22.0 // indirect
116+
golang.org/x/net v0.34.0 // indirect
116117
golang.org/x/oauth2 v0.24.0 // indirect
117-
golang.org/x/sys v0.29.0 // indirect
118-
golang.org/x/text v0.21.0 // indirect
118+
golang.org/x/sys v0.30.0 // indirect
119+
golang.org/x/text v0.22.0 // indirect
119120
google.golang.org/appengine v1.6.8 // indirect
120121
google.golang.org/genproto/googleapis/rpc v0.0.0-20241202173237-19429a94021a // indirect
121-
google.golang.org/protobuf v1.35.2 // indirect
122+
google.golang.org/protobuf v1.36.3 // indirect
122123
gopkg.in/yaml.v3 v3.0.1 // indirect
123124
)

go.sum

Lines changed: 50 additions & 50 deletions
Large diffs are not rendered by default.

internal/provider/customtypes/common.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,8 @@ import (
88

99
"github.com/hashicorp/terraform-plugin-framework/attr"
1010
"github.com/hashicorp/terraform-plugin-framework/diag"
11-
"github.com/hashicorp/terraform-plugin-framework/path"
1211
)
1312

14-
func newInvalidTerraformValueError(valuePath path.Path, err error) diag.Diagnostic {
15-
return diag.NewAttributeErrorDiagnostic(
16-
valuePath,
17-
"Invalid Terraform Value",
18-
"An unexpected error occurred while attempting to convert a Terraform value to a string. "+
19-
"This generally is an issue with the provider schema implementation. "+
20-
"Please contact the provider developers.\n\n"+
21-
"Path: "+valuePath.String()+"\n"+
22-
"Error: "+err.Error(),
23-
)
24-
}
25-
2613
func newSemanticEqualityCheckTypeError[V attr.Value](expected V, got V) diag.Diagnostic {
2714
return diag.NewErrorDiagnostic(
2815
"Semantic Equality Check Type Error",

internal/provider/customtypes/packer_fingerprint.go

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ import (
1111
"github.com/hashicorp/terraform-plugin-framework/attr"
1212
"github.com/hashicorp/terraform-plugin-framework/attr/xattr"
1313
"github.com/hashicorp/terraform-plugin-framework/diag"
14-
"github.com/hashicorp/terraform-plugin-framework/path"
14+
"github.com/hashicorp/terraform-plugin-framework/function"
1515
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1616
"github.com/hashicorp/terraform-plugin-go/tftypes"
1717
)
1818

19-
// PackerFingerprintType is a custom type for HCP Packer Fingerprints
19+
var (
20+
_ basetypes.StringTypable = &PackerFingerprintType{}
21+
)
22+
2023
type PackerFingerprintType struct {
2124
basetypes.StringType
2225
}
2326

24-
var _ basetypes.StringTypable = &PackerFingerprintType{}
25-
var _ xattr.TypeWithValidate = PackerFingerprintType{}
26-
2727
func (t PackerFingerprintType) String() string {
2828
return "PackerFingerprintType"
2929
}
@@ -66,13 +66,12 @@ func (t PackerFingerprintType) ValueFromTerraform(ctx context.Context, in tftype
6666
return fingerprintValue, nil
6767
}
6868

69-
func NewPackerFingerprintValue(value string) PackerFingerprintValue {
70-
return PackerFingerprintValue{
71-
StringValue: basetypes.NewStringValue(value),
72-
}
73-
}
69+
var (
70+
_ basetypes.StringValuable = &PackerFingerprintValue{}
71+
_ xattr.ValidateableAttribute = &PackerFingerprintValue{}
72+
_ function.ValidateableParameter = &PackerFingerprintValue{}
73+
)
7474

75-
// PackerFingerprintValue is a custom value used to validate that a string is an HCP Packer Fingerprint
7675
type PackerFingerprintValue struct {
7776
basetypes.StringValue
7877
}
@@ -92,38 +91,66 @@ func (v PackerFingerprintValue) Equal(o attr.Value) bool {
9291
return v.StringValue.Equal(other.StringValue)
9392
}
9493

95-
// Validate checks that the value is a valid PackerFingerprint, if it is known and not null.
96-
func (t PackerFingerprintType) Validate(ctx context.Context, value tftypes.Value, valuePath path.Path) diag.Diagnostics {
97-
if value.IsNull() || !value.IsKnown() {
98-
return nil
94+
func (v PackerFingerprintValue) ValidateAttribute(ctx context.Context, req xattr.ValidateAttributeRequest, resp *xattr.ValidateAttributeResponse) {
95+
if v.IsUnknown() || v.IsNull() {
96+
return
97+
}
98+
99+
valueString := v.ValueString()
100+
101+
if len(valueString) < 1 || len(valueString) > 40 {
102+
resp.Diagnostics.Append(
103+
diag.NewAttributeErrorDiagnostic(
104+
req.Path,
105+
"invalid format for an HCP Packer Fingerprint",
106+
"must be between 1 and 40 characters long, inclusive",
107+
),
108+
)
109+
return
99110
}
100111

101-
var diags diag.Diagnostics
102-
var valueString string
112+
if !regexp.MustCompile(`^[a-zA-Z0-9_\-\.\"]+$`).MatchString(valueString) {
113+
resp.Diagnostics.Append(
114+
diag.NewAttributeErrorDiagnostic(
115+
req.Path,
116+
"invalid format for an HCP Packer Fingerprint",
117+
// TODO: The regex also allows double quotes, and does not check the first or last characters.
118+
// This is because the v1 HCP Packer API allowed quotes. Once that API is deprecated,
119+
// and there are no more offending versions, we can add the strict validation.
120+
"must contain only alphanumeric characters, underscores, dashes, and periods",
121+
),
122+
)
123+
}
124+
}
103125

104-
if err := value.As(&valueString); err != nil {
105-
diags.Append(newInvalidTerraformValueError(valuePath, err))
106-
return diags
126+
func (v PackerFingerprintValue) ValidateParameter(ctx context.Context, req function.ValidateParameterRequest, resp *function.ValidateParameterResponse) {
127+
if v.IsUnknown() || v.IsNull() {
128+
return
107129
}
108130

131+
valueString := v.ValueString()
132+
109133
if len(valueString) < 1 || len(valueString) > 40 {
110-
diags.AddAttributeError(
111-
valuePath,
112-
"invalid format for an HCP Packer Fingerprint",
113-
"must be between 1 and 40 characters long, inclusive",
134+
resp.Error = function.NewArgumentFuncError(
135+
req.Position,
136+
"HCP Packer Fingerprint must be between 1 and 40 characters long, inclusive",
114137
)
138+
return
115139
}
116140

117141
if !regexp.MustCompile(`^[a-zA-Z0-9_\-\.\"]+$`).MatchString(valueString) {
118-
diags.AddAttributeError(
119-
valuePath,
120-
"invalid format for an HCP Packer Fingerprint",
142+
resp.Error = function.NewArgumentFuncError(
143+
req.Position,
121144
// TODO: The regex also allows double quotes, and does not check the first or last characters.
122145
// This is because the v1 HCP Packer API allowed quotes. Once that API is deprecated,
123146
// and there are no more offending versions, we can add the strict validation.
124-
"must contain only alphanumeric characters, underscores, dashes, and periods",
147+
"HCP Packer Fingerprint must contain only alphanumeric characters, underscores, dashes, and periods",
125148
)
126149
}
150+
}
127151

128-
return diags
152+
func NewPackerFingerprintValue(value string) PackerFingerprintValue {
153+
return PackerFingerprintValue{
154+
StringValue: basetypes.NewStringValue(value),
155+
}
129156
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package customtypes
2+
3+
import (
4+
"context"
5+
"strings"
6+
"testing"
7+
8+
"github.com/google/go-cmp/cmp"
9+
"github.com/hashicorp/terraform-plugin-framework/attr/xattr"
10+
"github.com/hashicorp/terraform-plugin-framework/diag"
11+
"github.com/hashicorp/terraform-plugin-framework/function"
12+
"github.com/hashicorp/terraform-plugin-framework/path"
13+
)
14+
15+
func TestPackerFingerprintValidateAttribute(t *testing.T) {
16+
testCases := map[string]struct {
17+
PackerFingerprint PackerFingerprintValue
18+
expectedDiags diag.Diagnostics
19+
}{
20+
"empty": {
21+
PackerFingerprint: PackerFingerprintValue{},
22+
},
23+
"valid": {
24+
PackerFingerprint: NewPackerFingerprintValue("01JDAW2CWCX1JHXE54XA7FFXHS"),
25+
},
26+
"too short": {
27+
PackerFingerprint: NewPackerFingerprintValue(""),
28+
expectedDiags: diag.Diagnostics{
29+
diag.NewAttributeErrorDiagnostic(
30+
path.Root("test"),
31+
"invalid format for an HCP Packer Fingerprint",
32+
"must be between 1 and 40 characters long, inclusive",
33+
),
34+
},
35+
},
36+
"too long": {
37+
PackerFingerprint: NewPackerFingerprintValue(strings.Repeat("A", 41)),
38+
expectedDiags: diag.Diagnostics{
39+
diag.NewAttributeErrorDiagnostic(
40+
path.Root("test"),
41+
"invalid format for an HCP Packer Fingerprint",
42+
"must be between 1 and 40 characters long, inclusive",
43+
),
44+
},
45+
},
46+
"invalid string": {
47+
PackerFingerprint: NewPackerFingerprintValue("$invalid$"),
48+
expectedDiags: diag.Diagnostics{
49+
diag.NewAttributeErrorDiagnostic(
50+
path.Root("test"),
51+
"invalid format for an HCP Packer Fingerprint",
52+
"must contain only alphanumeric characters, underscores, dashes, and periods",
53+
),
54+
},
55+
},
56+
}
57+
for name, testCase := range testCases {
58+
t.Run(name, func(t *testing.T) {
59+
resp := xattr.ValidateAttributeResponse{}
60+
61+
testCase.PackerFingerprint.ValidateAttribute(
62+
context.Background(),
63+
xattr.ValidateAttributeRequest{
64+
Path: path.Root("test"),
65+
},
66+
&resp,
67+
)
68+
69+
if diff := cmp.Diff(resp.Diagnostics, testCase.expectedDiags); diff != "" {
70+
t.Errorf("Unexpected diagnostics (-got, +expected): %s", diff)
71+
}
72+
})
73+
}
74+
}
75+
76+
func TestPackerFingerprintValidateParameter(t *testing.T) {
77+
testCases := map[string]struct {
78+
PackerFingerprint PackerFingerprintValue
79+
expectedFuncErr *function.FuncError
80+
}{
81+
"empty": {
82+
PackerFingerprint: PackerFingerprintValue{},
83+
},
84+
"valid": {
85+
PackerFingerprint: NewPackerFingerprintValue("01JDAW2CWCX1JHXE54XA7FFXHS"),
86+
},
87+
"too short": {
88+
PackerFingerprint: NewPackerFingerprintValue(""),
89+
expectedFuncErr: function.NewArgumentFuncError(
90+
0,
91+
"HCP Packer Fingerprint must be between 1 and 40 characters long, inclusive",
92+
),
93+
},
94+
"too long": {
95+
PackerFingerprint: NewPackerFingerprintValue(strings.Repeat("A", 41)),
96+
expectedFuncErr: function.NewArgumentFuncError(
97+
0,
98+
"HCP Packer Fingerprint must be between 1 and 40 characters long, inclusive",
99+
),
100+
},
101+
"invalid string": {
102+
PackerFingerprint: NewPackerFingerprintValue("$invalid$"),
103+
expectedFuncErr: function.NewArgumentFuncError(
104+
0,
105+
"HCP Packer Fingerprint must contain only alphanumeric characters, underscores, dashes, and periods",
106+
),
107+
},
108+
}
109+
for name, testCase := range testCases {
110+
t.Run(name, func(t *testing.T) {
111+
resp := function.ValidateParameterResponse{}
112+
113+
testCase.PackerFingerprint.ValidateParameter(
114+
context.Background(),
115+
function.ValidateParameterRequest{
116+
Position: int64(0),
117+
},
118+
&resp,
119+
)
120+
121+
if diff := cmp.Diff(resp.Error, testCase.expectedFuncErr); diff != "" {
122+
t.Errorf("Unexpected diagnostics (-got, +expected): %s", diff)
123+
}
124+
})
125+
}
126+
}

0 commit comments

Comments
 (0)