Skip to content

Commit 3c21a45

Browse files
committed
protov6 copy
1 parent bf10880 commit 3c21a45

File tree

10 files changed

+263
-4
lines changed

10 files changed

+263
-4
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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-go/tfprotov6"
10+
11+
"github.com/hashicorp/terraform-plugin-framework/action"
12+
"github.com/hashicorp/terraform-plugin-framework/diag"
13+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
14+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
15+
)
16+
17+
// InvokeActionRequest returns the *fwserver.InvokeActionRequest equivalent of a *tfprotov6.InvokeActionRequest.
18+
func InvokeActionRequest(ctx context.Context, proto6 *tfprotov6.InvokeActionRequest, reqAction action.Action, actionSchema fwschema.Schema) (*fwserver.InvokeActionRequest, diag.Diagnostics) {
19+
if proto6 == nil {
20+
return nil, nil
21+
}
22+
23+
var diags diag.Diagnostics
24+
25+
// Panic prevention here to simplify the calling implementations.
26+
// This should not happen, but just in case.
27+
if actionSchema == nil {
28+
diags.AddError(
29+
"Missing Action Schema",
30+
"An unexpected error was encountered when handling the request. "+
31+
"This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+
32+
"Please report this to the provider developer:\n\n"+
33+
"Missing schema.",
34+
)
35+
36+
return nil, diags
37+
}
38+
39+
fw := &fwserver.InvokeActionRequest{
40+
ActionSchema: actionSchema,
41+
}
42+
43+
config, configDiags := Config(ctx, proto6.Config, actionSchema)
44+
45+
diags.Append(configDiags...)
46+
47+
fw.Config = config
48+
49+
// TODO:Actions: Here we need to retrieve linked resource data
50+
51+
return fw, diags
52+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto6_test
5+
6+
// TODO:Actions: Add unit tests once this mapping logic is complete

internal/fromproto6/planaction.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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-go/tfprotov6"
10+
11+
"github.com/hashicorp/terraform-plugin-framework/action"
12+
"github.com/hashicorp/terraform-plugin-framework/diag"
13+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
14+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
15+
)
16+
17+
// PlanActionRequest returns the *fwserver.PlanActionRequest equivalent of a *tfprotov6.PlanActionRequest.
18+
func PlanActionRequest(ctx context.Context, proto6 *tfprotov6.PlanActionRequest, reqAction action.Action, actionSchema fwschema.Schema) (*fwserver.PlanActionRequest, diag.Diagnostics) {
19+
if proto6 == nil {
20+
return nil, nil
21+
}
22+
23+
var diags diag.Diagnostics
24+
25+
// Panic prevention here to simplify the calling implementations.
26+
// This should not happen, but just in case.
27+
if actionSchema == nil {
28+
diags.AddError(
29+
"Missing Action Schema",
30+
"An unexpected error was encountered when handling the request. "+
31+
"This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+
32+
"Please report this to the provider developer:\n\n"+
33+
"Missing schema.",
34+
)
35+
36+
return nil, diags
37+
}
38+
39+
fw := &fwserver.PlanActionRequest{
40+
ActionSchema: actionSchema,
41+
}
42+
43+
config, configDiags := Config(ctx, proto6.Config, actionSchema)
44+
45+
diags.Append(configDiags...)
46+
47+
fw.Config = config
48+
49+
// TODO:Actions: Here we need to retrieve client capabilities and linked resource data
50+
51+
return fw, diags
52+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto6_test
5+
6+
// TODO:Actions: Add unit tests once this mapping logic is complete

internal/proto6server/server_invokeaction.go

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,75 @@ package proto6server
66
import (
77
"context"
88

9+
"github.com/hashicorp/terraform-plugin-framework/diag"
10+
"github.com/hashicorp/terraform-plugin-framework/internal/fromproto6"
11+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
12+
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
13+
"github.com/hashicorp/terraform-plugin-framework/internal/toproto6"
914
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
1015
)
1116

17+
// invokeActionErrorDiagnostics returns a value suitable for
18+
// [InvokeActionServerStream.Events]. It yields a single result that contains
19+
// the given error diagnostics.
20+
func invokeActionErrorDiagnostics(ctx context.Context, diags diag.Diagnostics) (*tfprotov6.InvokeActionServerStream, error) {
21+
return &tfprotov6.InvokeActionServerStream{
22+
Events: func(push func(tfprotov6.InvokeActionEvent) bool) {
23+
push(tfprotov6.InvokeActionEvent{
24+
Type: tfprotov6.CompletedInvokeActionEventType{
25+
Diagnostics: toproto6.Diagnostics(ctx, diags),
26+
},
27+
})
28+
},
29+
}, nil
30+
}
31+
1232
// InvokeAction satisfies the tfprotov6.ProviderServer interface.
1333
func (s *Server) InvokeAction(ctx context.Context, proto6Req *tfprotov6.InvokeActionRequest) (*tfprotov6.InvokeActionServerStream, error) {
14-
// TODO:Actions: Implement
15-
panic("unimplemented")
34+
ctx = s.registerContext(ctx)
35+
ctx = logging.InitContext(ctx)
36+
37+
fwResp := &fwserver.InvokeActionResponse{}
38+
39+
action, diags := s.FrameworkServer.Action(ctx, proto6Req.ActionType)
40+
41+
fwResp.Diagnostics.Append(diags...)
42+
43+
if fwResp.Diagnostics.HasError() {
44+
return invokeActionErrorDiagnostics(ctx, fwResp.Diagnostics)
45+
}
46+
47+
actionSchema, diags := s.FrameworkServer.ActionSchema(ctx, proto6Req.ActionType)
48+
49+
fwResp.Diagnostics.Append(diags...)
50+
51+
if fwResp.Diagnostics.HasError() {
52+
return invokeActionErrorDiagnostics(ctx, fwResp.Diagnostics)
53+
}
54+
55+
fwReq, diags := fromproto6.InvokeActionRequest(ctx, proto6Req, action, actionSchema)
56+
57+
fwResp.Diagnostics.Append(diags...)
58+
59+
if fwResp.Diagnostics.HasError() {
60+
return invokeActionErrorDiagnostics(ctx, fwResp.Diagnostics)
61+
}
62+
63+
s.FrameworkServer.InvokeAction(ctx, fwReq, fwResp)
64+
65+
// TODO:Actions: This is a stub implementation, so we aren't currently exposing any streaming mechanism to the developer.
66+
// That will eventually need to change to send progress events back to Terraform.
67+
//
68+
// This logic will likely need to be moved over to the "toproto" package as well.
69+
protoStream := &tfprotov6.InvokeActionServerStream{
70+
Events: func(push func(tfprotov6.InvokeActionEvent) bool) {
71+
push(tfprotov6.InvokeActionEvent{
72+
Type: tfprotov6.CompletedInvokeActionEventType{
73+
Diagnostics: toproto6.Diagnostics(ctx, fwResp.Diagnostics),
74+
},
75+
})
76+
},
77+
}
78+
79+
return protoStream, nil
1680
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package proto6server_test
5+
6+
// TODO:Actions: Add unit tests once InvokeAction is implemented

internal/proto6server/server_planaction.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,45 @@ package proto6server
66
import (
77
"context"
88

9+
"github.com/hashicorp/terraform-plugin-framework/internal/fromproto6"
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/toproto6"
913
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
1014
)
1115

1216
// PlanAction satisfies the tfprotov6.ProviderServer interface.
1317
func (s *Server) PlanAction(ctx context.Context, proto6Req *tfprotov6.PlanActionRequest) (*tfprotov6.PlanActionResponse, error) {
14-
// TODO:Actions: Implement
15-
panic("unimplemented")
18+
ctx = s.registerContext(ctx)
19+
ctx = logging.InitContext(ctx)
20+
21+
fwResp := &fwserver.PlanActionResponse{}
22+
23+
action, diags := s.FrameworkServer.Action(ctx, proto6Req.ActionType)
24+
25+
fwResp.Diagnostics.Append(diags...)
26+
27+
if fwResp.Diagnostics.HasError() {
28+
return toproto6.PlanActionResponse(ctx, fwResp), nil
29+
}
30+
31+
actionSchema, diags := s.FrameworkServer.ActionSchema(ctx, proto6Req.ActionType)
32+
33+
fwResp.Diagnostics.Append(diags...)
34+
35+
if fwResp.Diagnostics.HasError() {
36+
return toproto6.PlanActionResponse(ctx, fwResp), nil
37+
}
38+
39+
fwReq, diags := fromproto6.PlanActionRequest(ctx, proto6Req, action, actionSchema)
40+
41+
fwResp.Diagnostics.Append(diags...)
42+
43+
if fwResp.Diagnostics.HasError() {
44+
return toproto6.PlanActionResponse(ctx, fwResp), nil
45+
}
46+
47+
s.FrameworkServer.PlanAction(ctx, fwReq, fwResp)
48+
49+
return toproto6.PlanActionResponse(ctx, fwResp), nil
1650
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package proto6server_test
5+
6+
// TODO:Actions: Add unit tests once PlanAction is implemented

internal/toproto6/planaction.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package toproto6
5+
6+
import (
7+
"context"
8+
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
10+
11+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
12+
)
13+
14+
// PlanActionResponse returns the *tfprotov6.PlanActionResponse equivalent of a *fwserver.PlanActionResponse.
15+
func PlanActionResponse(ctx context.Context, fw *fwserver.PlanActionResponse) *tfprotov6.PlanActionResponse {
16+
if fw == nil {
17+
return nil
18+
}
19+
20+
proto6 := &tfprotov6.PlanActionResponse{
21+
Diagnostics: Diagnostics(ctx, fw.Diagnostics),
22+
}
23+
24+
// TODO:Actions: Here we need to set deferred and linked resource data
25+
26+
return proto6
27+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package toproto6_test
5+
6+
// TODO:Actions: Add unit tests once this mapping logic is complete

0 commit comments

Comments
 (0)