Skip to content

Commit 4606268

Browse files
committed
Added List Resource Config Validation
1 parent 52edf5b commit 4606268

31 files changed

+1795
-13
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ toolchain go1.23.7
66

77
require (
88
github.com/google/go-cmp v0.7.0
9-
github.com/hashicorp/terraform-plugin-go v0.28.1-0.20250616135123-a19df43120ea
9+
github.com/hashicorp/terraform-plugin-go v0.28.1-0.20250703143221-06cc08e56c87
1010
github.com/hashicorp/terraform-plugin-log v0.9.0
1111
)
1212

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ github.com/hashicorp/go-plugin v1.6.3 h1:xgHB+ZUSYeuJi96WtxEjzi23uh7YQpznjGh0U0U
2121
github.com/hashicorp/go-plugin v1.6.3/go.mod h1:MRobyh+Wc/nYy1V4KAXUiYfzxoYhs7V1mlH1Z7iY2h0=
2222
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
2323
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
24-
github.com/hashicorp/terraform-plugin-go v0.28.1-0.20250616135123-a19df43120ea h1:U9EAAeQtszGlR7mDS7rY77B/a4/XiMDB8HfAtqLAuAQ=
25-
github.com/hashicorp/terraform-plugin-go v0.28.1-0.20250616135123-a19df43120ea/go.mod h1:hL//wLEfYo0YVt0TC/VLzia/ADQQto3HEm4/jX2gkdY=
24+
github.com/hashicorp/terraform-plugin-go v0.28.1-0.20250703143221-06cc08e56c87 h1:7GqdqQtDa2XSOL80U8cfgrnmisAfzZtH2FPe0SxvCJM=
25+
github.com/hashicorp/terraform-plugin-go v0.28.1-0.20250703143221-06cc08e56c87/go.mod h1:hL//wLEfYo0YVt0TC/VLzia/ADQQto3HEm4/jX2gkdY=
2626
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
2727
github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow=
2828
github.com/hashicorp/terraform-registry-address v0.3.0 h1:HMpK3nqaGFPS9VmgRXrJL/dzHNdheGVKk5k7VlFxzCo=
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto5
5+
6+
import (
7+
"context"
8+
9+
"github.com/hashicorp/terraform-plugin-framework/diag"
10+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
11+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
12+
"github.com/hashicorp/terraform-plugin-framework/list"
13+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
14+
)
15+
16+
// ValidateListResourceConfigRequest returns the *fwserver.ValidateListResourceConfigRequest
17+
// equivalent of a *tfprotov5.ValidateListResourceConfigRequest.
18+
func ValidateListResourceConfigRequest(ctx context.Context, proto5 *tfprotov5.ValidateListResourceConfigRequest, listResource list.ListResource, listResourceSchema fwschema.Schema) (*fwserver.ValidateListResourceConfigRequest, diag.Diagnostics) {
19+
if proto5 == nil {
20+
return nil, nil
21+
}
22+
23+
fw := &fwserver.ValidateListResourceConfigRequest{}
24+
25+
config, diags := Config(ctx, proto5.Config, listResourceSchema)
26+
27+
fw.Config = config
28+
fw.ListResource = listResource
29+
30+
return fw, diags
31+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto5_test
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/google/go-cmp/cmp"
11+
"github.com/hashicorp/terraform-plugin-framework/diag"
12+
"github.com/hashicorp/terraform-plugin-framework/internal/fromproto5"
13+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
14+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
15+
"github.com/hashicorp/terraform-plugin-framework/list"
16+
"github.com/hashicorp/terraform-plugin-framework/list/schema"
17+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
18+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
19+
"github.com/hashicorp/terraform-plugin-go/tftypes"
20+
)
21+
22+
func TestValidateListResourceConfigRequest(t *testing.T) {
23+
t.Parallel()
24+
25+
testProto5Type := tftypes.Object{
26+
AttributeTypes: map[string]tftypes.Type{
27+
"test_attribute": tftypes.String,
28+
},
29+
}
30+
31+
testProto5Value := tftypes.NewValue(testProto5Type, map[string]tftypes.Value{
32+
"test_attribute": tftypes.NewValue(tftypes.String, "test-value"),
33+
})
34+
35+
testProto5DynamicValue, err := tfprotov5.NewDynamicValue(testProto5Type, testProto5Value)
36+
37+
if err != nil {
38+
t.Fatalf("unexpected error calling tfprotov5.NewDynamicValue(): %s", err)
39+
}
40+
41+
testFwSchema := schema.Schema{
42+
Attributes: map[string]schema.Attribute{
43+
"test_attribute": schema.StringAttribute{
44+
Required: true,
45+
},
46+
},
47+
}
48+
49+
testCases := map[string]struct {
50+
input *tfprotov5.ValidateListResourceConfigRequest
51+
listResourceSchema fwschema.Schema
52+
listResource list.ListResource
53+
expected *fwserver.ValidateListResourceConfigRequest
54+
expectedDiagnostics diag.Diagnostics
55+
}{
56+
"nil": {
57+
input: nil,
58+
expected: nil,
59+
},
60+
"empty": {
61+
input: &tfprotov5.ValidateListResourceConfigRequest{},
62+
expected: &fwserver.ValidateListResourceConfigRequest{},
63+
},
64+
"config-missing-schema": {
65+
input: &tfprotov5.ValidateListResourceConfigRequest{
66+
Config: &testProto5DynamicValue,
67+
},
68+
expected: &fwserver.ValidateListResourceConfigRequest{},
69+
expectedDiagnostics: diag.Diagnostics{
70+
diag.NewErrorDiagnostic(
71+
"Unable to Convert Configuration",
72+
"An unexpected error was encountered when converting the configuration from the protocol type. "+
73+
"This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+
74+
"Please report this to the provider developer:\n\n"+
75+
"Missing schema.",
76+
),
77+
},
78+
},
79+
"config": {
80+
input: &tfprotov5.ValidateListResourceConfigRequest{
81+
Config: &testProto5DynamicValue,
82+
},
83+
listResourceSchema: testFwSchema,
84+
expected: &fwserver.ValidateListResourceConfigRequest{
85+
Config: &tfsdk.Config{
86+
Raw: testProto5Value,
87+
Schema: testFwSchema,
88+
},
89+
},
90+
},
91+
}
92+
93+
for name, testCase := range testCases {
94+
t.Run(name, func(t *testing.T) {
95+
t.Parallel()
96+
97+
got, diags := fromproto5.ValidateListResourceConfigRequest(context.Background(), testCase.input, testCase.listResource, testCase.listResourceSchema)
98+
99+
if diff := cmp.Diff(got, testCase.expected); diff != "" {
100+
t.Errorf("unexpected difference: %s", diff)
101+
}
102+
103+
if diff := cmp.Diff(diags, testCase.expectedDiagnostics); diff != "" {
104+
t.Errorf("unexpected diagnostics difference: %s", diff)
105+
}
106+
})
107+
}
108+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto6
5+
6+
import (
7+
"context"
8+
9+
"github.com/hashicorp/terraform-plugin-framework/diag"
10+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
11+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
12+
"github.com/hashicorp/terraform-plugin-framework/list"
13+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
14+
)
15+
16+
// ValidateListResourceConfigRequest returns the *fwserver.ValidateListResourceConfigRequest
17+
// equivalent of a *tfprotov6.ValidateListResourceConfigRequest.
18+
func ValidateListResourceConfigRequest(ctx context.Context, proto6 *tfprotov6.ValidateListResourceConfigRequest, listResource list.ListResource, listResourceSchema fwschema.Schema) (*fwserver.ValidateListResourceConfigRequest, diag.Diagnostics) {
19+
if proto6 == nil {
20+
return nil, nil
21+
}
22+
23+
fw := &fwserver.ValidateListResourceConfigRequest{}
24+
25+
config, diags := Config(ctx, proto6.Config, listResourceSchema)
26+
27+
fw.Config = config
28+
fw.ListResource = listResource
29+
30+
return fw, diags
31+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto6_test
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/google/go-cmp/cmp"
11+
"github.com/hashicorp/terraform-plugin-framework/diag"
12+
"github.com/hashicorp/terraform-plugin-framework/internal/fromproto6"
13+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
14+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
15+
"github.com/hashicorp/terraform-plugin-framework/list"
16+
"github.com/hashicorp/terraform-plugin-framework/list/schema"
17+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
18+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
19+
"github.com/hashicorp/terraform-plugin-go/tftypes"
20+
)
21+
22+
func TestValidateListResourceConfigRequest(t *testing.T) {
23+
t.Parallel()
24+
25+
testProto6Type := tftypes.Object{
26+
AttributeTypes: map[string]tftypes.Type{
27+
"test_attribute": tftypes.String,
28+
},
29+
}
30+
31+
testProto6Value := tftypes.NewValue(testProto6Type, map[string]tftypes.Value{
32+
"test_attribute": tftypes.NewValue(tftypes.String, "test-value"),
33+
})
34+
35+
testProto6DynamicValue, err := tfprotov6.NewDynamicValue(testProto6Type, testProto6Value)
36+
37+
if err != nil {
38+
t.Fatalf("unexpected error calling tfprotov6.NewDynamicValue(): %s", err)
39+
}
40+
41+
testFwSchema := schema.Schema{
42+
Attributes: map[string]schema.Attribute{
43+
"test_attribute": schema.StringAttribute{
44+
Required: true,
45+
},
46+
},
47+
}
48+
49+
testCases := map[string]struct {
50+
input *tfprotov6.ValidateListResourceConfigRequest
51+
listResourceSchema fwschema.Schema
52+
listResource list.ListResource
53+
expected *fwserver.ValidateListResourceConfigRequest
54+
expectedDiagnostics diag.Diagnostics
55+
}{
56+
"nil": {
57+
input: nil,
58+
expected: nil,
59+
},
60+
"empty": {
61+
input: &tfprotov6.ValidateListResourceConfigRequest{},
62+
expected: &fwserver.ValidateListResourceConfigRequest{},
63+
},
64+
"config-missing-schema": {
65+
input: &tfprotov6.ValidateListResourceConfigRequest{
66+
Config: &testProto6DynamicValue,
67+
},
68+
expected: &fwserver.ValidateListResourceConfigRequest{},
69+
expectedDiagnostics: diag.Diagnostics{
70+
diag.NewErrorDiagnostic(
71+
"Unable to Convert Configuration",
72+
"An unexpected error was encountered when converting the configuration from the protocol type. "+
73+
"This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+
74+
"Please report this to the provider developer:\n\n"+
75+
"Missing schema.",
76+
),
77+
},
78+
},
79+
"config": {
80+
input: &tfprotov6.ValidateListResourceConfigRequest{
81+
Config: &testProto6DynamicValue,
82+
},
83+
listResourceSchema: testFwSchema,
84+
expected: &fwserver.ValidateListResourceConfigRequest{
85+
Config: &tfsdk.Config{
86+
Raw: testProto6Value,
87+
Schema: testFwSchema,
88+
},
89+
},
90+
},
91+
}
92+
93+
for name, testCase := range testCases {
94+
t.Run(name, func(t *testing.T) {
95+
t.Parallel()
96+
97+
got, diags := fromproto6.ValidateListResourceConfigRequest(context.Background(), testCase.input, testCase.listResource, testCase.listResourceSchema)
98+
99+
if diff := cmp.Diff(got, testCase.expected); diff != "" {
100+
t.Errorf("unexpected difference: %s", diff)
101+
}
102+
103+
if diff := cmp.Diff(diags, testCase.expectedDiagnostics); diff != "" {
104+
t.Errorf("unexpected diagnostics difference: %s", diff)
105+
}
106+
})
107+
}
108+
}

internal/fwserver/server.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ type Server struct {
4040
// to [ephemeral.ConfigureRequest.ProviderData].
4141
EphemeralResourceConfigureData any
4242

43+
// ListResourceConfigureData is the
44+
// [provider.ConfigureResponse.ListResourceData] field value which is passed
45+
// to [list.ConfigureRequest.ProviderData].
46+
ListResourceConfigureData any
47+
4348
// dataSourceSchemas is the cached DataSource Schemas for RPCs that need to
4449
// convert configuration data from the protocol. If not found, it will be
4550
// fetched from the DataSourceType.GetSchema() method.

0 commit comments

Comments
 (0)