Skip to content

Commit e4bcbbf

Browse files
committed
proto5 and proto6 impl
1 parent 6d3cff2 commit e4bcbbf

12 files changed

+894
-0
lines changed
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/action"
10+
"github.com/hashicorp/terraform-plugin-framework/diag"
11+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
12+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
13+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
14+
)
15+
16+
// ValidateActionConfigRequest returns the *fwserver.ValidateActionConfigRequest
17+
// equivalent of a *tfprotov5.ValidateActionConfigRequest.
18+
func ValidateActionConfigRequest(ctx context.Context, proto5 *tfprotov5.ValidateActionConfigRequest, reqAction action.Action, actionSchema fwschema.Schema) (*fwserver.ValidateActionConfigRequest, diag.Diagnostics) {
19+
if proto5 == nil {
20+
return nil, nil
21+
}
22+
23+
fw := &fwserver.ValidateActionConfigRequest{}
24+
25+
config, diags := Config(ctx, proto5.Config, actionSchema)
26+
27+
fw.Config = config
28+
fw.Action = reqAction
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/action"
12+
"github.com/hashicorp/terraform-plugin-framework/action/schema"
13+
"github.com/hashicorp/terraform-plugin-framework/diag"
14+
"github.com/hashicorp/terraform-plugin-framework/internal/fromproto5"
15+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
16+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
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 TestValidateActionConfigRequest(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.UnlinkedSchema{
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.ValidateActionConfigRequest
51+
actionSchema fwschema.Schema
52+
actionImpl action.Action
53+
expected *fwserver.ValidateActionConfigRequest
54+
expectedDiagnostics diag.Diagnostics
55+
}{
56+
"nil": {
57+
input: nil,
58+
expected: nil,
59+
},
60+
"empty": {
61+
input: &tfprotov5.ValidateActionConfigRequest{},
62+
expected: &fwserver.ValidateActionConfigRequest{},
63+
},
64+
"config-missing-schema": {
65+
input: &tfprotov5.ValidateActionConfigRequest{
66+
Config: &testProto5DynamicValue,
67+
},
68+
expected: &fwserver.ValidateActionConfigRequest{},
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.ValidateActionConfigRequest{
81+
Config: &testProto5DynamicValue,
82+
},
83+
actionSchema: testFwSchema,
84+
expected: &fwserver.ValidateActionConfigRequest{
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.ValidateActionConfigRequest(context.Background(), testCase.input, testCase.actionImpl, testCase.actionSchema)
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/action"
10+
"github.com/hashicorp/terraform-plugin-framework/diag"
11+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
12+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
13+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
14+
)
15+
16+
// ValidateActionConfigRequest returns the *fwserver.ValidateActionConfigRequest
17+
// equivalent of a *tfprotov6.ValidateActionConfigRequest.
18+
func ValidateActionConfigRequest(ctx context.Context, proto6 *tfprotov6.ValidateActionConfigRequest, reqAction action.Action, actionSchema fwschema.Schema) (*fwserver.ValidateActionConfigRequest, diag.Diagnostics) {
19+
if proto6 == nil {
20+
return nil, nil
21+
}
22+
23+
fw := &fwserver.ValidateActionConfigRequest{}
24+
25+
config, diags := Config(ctx, proto6.Config, actionSchema)
26+
27+
fw.Config = config
28+
fw.Action = reqAction
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/action"
12+
"github.com/hashicorp/terraform-plugin-framework/action/schema"
13+
"github.com/hashicorp/terraform-plugin-framework/diag"
14+
"github.com/hashicorp/terraform-plugin-framework/internal/fromproto6"
15+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
16+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
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 TestValidateActionConfigRequest(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.UnlinkedSchema{
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.ValidateActionConfigRequest
51+
actionSchema fwschema.Schema
52+
actionImpl action.Action
53+
expected *fwserver.ValidateActionConfigRequest
54+
expectedDiagnostics diag.Diagnostics
55+
}{
56+
"nil": {
57+
input: nil,
58+
expected: nil,
59+
},
60+
"empty": {
61+
input: &tfprotov6.ValidateActionConfigRequest{},
62+
expected: &fwserver.ValidateActionConfigRequest{},
63+
},
64+
"config-missing-schema": {
65+
input: &tfprotov6.ValidateActionConfigRequest{
66+
Config: &testProto6DynamicValue,
67+
},
68+
expected: &fwserver.ValidateActionConfigRequest{},
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.ValidateActionConfigRequest{
81+
Config: &testProto6DynamicValue,
82+
},
83+
actionSchema: testFwSchema,
84+
expected: &fwserver.ValidateActionConfigRequest{
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.ValidateActionConfigRequest(context.Background(), testCase.input, testCase.actionImpl, testCase.actionSchema)
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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package proto5server
5+
6+
import (
7+
"context"
8+
9+
"github.com/hashicorp/terraform-plugin-framework/internal/fromproto5"
10+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
11+
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
12+
"github.com/hashicorp/terraform-plugin-framework/internal/toproto5"
13+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
14+
)
15+
16+
// ValidateActionConfig satisfies the tfprotov5.ProviderServer interface.
17+
func (s *Server) ValidateActionConfig(ctx context.Context, proto5Req *tfprotov5.ValidateActionConfigRequest) (*tfprotov5.ValidateActionConfigResponse, error) {
18+
ctx = s.registerContext(ctx)
19+
ctx = logging.InitContext(ctx)
20+
21+
fwResp := &fwserver.ValidateActionConfigResponse{}
22+
23+
action, diags := s.FrameworkServer.Action(ctx, proto5Req.ActionType)
24+
25+
fwResp.Diagnostics.Append(diags...)
26+
27+
if fwResp.Diagnostics.HasError() {
28+
return toproto5.ValidateActionConfigResponse(ctx, fwResp), nil
29+
}
30+
31+
actionSchema, diags := s.FrameworkServer.ActionSchema(ctx, proto5Req.ActionType)
32+
33+
fwResp.Diagnostics.Append(diags...)
34+
35+
if fwResp.Diagnostics.HasError() {
36+
return toproto5.ValidateActionConfigResponse(ctx, fwResp), nil
37+
}
38+
39+
fwReq, diags := fromproto5.ValidateActionConfigRequest(ctx, proto5Req, action, actionSchema)
40+
41+
fwResp.Diagnostics.Append(diags...)
42+
43+
if fwResp.Diagnostics.HasError() {
44+
return toproto5.ValidateActionConfigResponse(ctx, fwResp), nil
45+
}
46+
47+
s.FrameworkServer.ValidateActionConfig(ctx, fwReq, fwResp)
48+
49+
return toproto5.ValidateActionConfigResponse(ctx, fwResp), nil
50+
}

0 commit comments

Comments
 (0)