Skip to content

Commit 4bb3f89

Browse files
authored
action: Initial package implementation and new RPC support (#1181)
* go mod * generate RPC methods * protov5 and fwserver impl * protov6 copy
1 parent 6ed8df7 commit 4bb3f89

34 files changed

+919
-5
lines changed

action/action.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package action
5+
6+
import "context"
7+
8+
type Action interface {
9+
// Schema should return the schema for this action.
10+
Schema(context.Context, SchemaRequest, *SchemaResponse)
11+
12+
// Metadata should return the full name of the action, such as examplecloud_do_thing.
13+
Metadata(context.Context, MetadataRequest, *MetadataResponse)
14+
15+
// TODO:Actions: Eventual landing place for all required methods to implement for an action
16+
}

action/doc.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
// TODO:Actions: Eventual package docs for actions
5+
package action

action/metadata.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package action
5+
6+
// MetadataRequest represents a request for the Action to return metadata,
7+
// such as its type name. An instance of this request struct is supplied as
8+
// an argument to the Action type Metadata method.
9+
type MetadataRequest struct {
10+
// ProviderTypeName is the string returned from
11+
// [provider.MetadataResponse.TypeName], if the Provider type implements
12+
// the Metadata method. This string should prefix the Action type name
13+
// with an underscore in the response.
14+
ProviderTypeName string
15+
}
16+
17+
// MetadataResponse represents a response to a MetadataRequest. An
18+
// instance of this response struct is supplied as an argument to the
19+
// Action type Metadata method.
20+
type MetadataResponse struct {
21+
// TypeName should be the full action type, including the provider
22+
// type prefix and an underscore. For example, examplecloud_thing.
23+
TypeName string
24+
}

action/schema.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package action
5+
6+
import (
7+
"github.com/hashicorp/terraform-plugin-framework/diag"
8+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
9+
)
10+
11+
// SchemaRequest represents a request for the Action to return its schema.
12+
// An instance of this request struct is supplied as an argument to the
13+
// Action type Schema method.
14+
type SchemaRequest struct{}
15+
16+
// SchemaResponse represents a response to a SchemaRequest. An instance of this
17+
// response struct is supplied as an argument to the Action type Schema
18+
// method.
19+
type SchemaResponse struct {
20+
// TODO:Actions: This will eventually be replaced by an interface defined in
21+
// an "actions/schema" package. Schema implementations that will fulfill this
22+
// interface will be unlinked, linked, or lifecycle. (also defined in the "actions/schema" package)
23+
Schema fwschema.Schema
24+
25+
// Diagnostics report errors or warnings related to retrieving the action schema.
26+
// An empty slice indicates success, with no warnings or errors generated.
27+
Diagnostics diag.Diagnostics
28+
}

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.29.0-alpha.1
9+
github.com/hashicorp/terraform-plugin-go v0.29.0-alpha.1.0.20250709165734-a8477a15f806
1010
github.com/hashicorp/terraform-plugin-log v0.9.0
1111
)
1212

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +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=
26-
github.com/hashicorp/terraform-plugin-go v0.29.0-alpha.1 h1:ZId6oWG8VTKhz207quE/Xh8a3HuoLtM/QkcSSypekIQ=
27-
github.com/hashicorp/terraform-plugin-go v0.29.0-alpha.1/go.mod h1:hL//wLEfYo0YVt0TC/VLzia/ADQQto3HEm4/jX2gkdY=
24+
github.com/hashicorp/terraform-plugin-go v0.29.0-alpha.1.0.20250709165734-a8477a15f806 h1:i3kA1sT/Fk8Ex+VVKdjf9sFOPwS7w3Q73pfbnxKwdjg=
25+
github.com/hashicorp/terraform-plugin-go v0.29.0-alpha.1.0.20250709165734-a8477a15f806/go.mod h1:hL//wLEfYo0YVt0TC/VLzia/ADQQto3HEm4/jX2gkdY=
2826
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
2927
github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow=
3028
github.com/hashicorp/terraform-registry-address v0.3.0 h1:HMpK3nqaGFPS9VmgRXrJL/dzHNdheGVKk5k7VlFxzCo=
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 fromproto5
5+
6+
import (
7+
"context"
8+
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
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 *tfprotov5.InvokeActionRequest.
18+
func InvokeActionRequest(ctx context.Context, proto5 *tfprotov5.InvokeActionRequest, reqAction action.Action, actionSchema fwschema.Schema) (*fwserver.InvokeActionRequest, diag.Diagnostics) {
19+
if proto5 == 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, proto5.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 fromproto5_test
5+
6+
// TODO:Actions: Add unit tests once this mapping logic is complete

internal/fromproto5/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 fromproto5
5+
6+
import (
7+
"context"
8+
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
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 *tfprotov5.PlanActionRequest.
18+
func PlanActionRequest(ctx context.Context, proto5 *tfprotov5.PlanActionRequest, reqAction action.Action, actionSchema fwschema.Schema) (*fwserver.PlanActionRequest, diag.Diagnostics) {
19+
if proto5 == 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, proto5.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 fromproto5_test
5+
6+
// TODO:Actions: Add unit tests once this mapping logic is complete

0 commit comments

Comments
 (0)