Skip to content

Commit dbf10e6

Browse files
authored
all: Initial provider defined functions implementation (#1288)
Reference: hashicorp/terraform-plugin-go#351 Reference: https://developer.hashicorp.com/terraform/plugin/framework/migrating The next versions of the plugin protocol (5.5/6.5) include support for provider defined functions. The terraform-plugin-sdk Go module will not be receiving this feature, however this Go module must be updated to handle the new RPCs with errors. Provider developers can still implement provider defined functions in their terraform-plugin-sdk based providers by following the framework migration documentation to introduce terraform-plugin-mux, which then will enable terraform-plugin-framework based provider function implementations.
1 parent e58f4d9 commit dbf10e6

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
kind: NOTES
2+
body: 'helper/schema: While this Go module will not receive support for
3+
provider-defined functions, the provider server is updated to handle the new
4+
operations, which will be required to prevent errors when updating
5+
terraform-plugin-framework or terraform-plugin-mux in the future.'
6+
time: 2023-11-07T14:16:09.783296-05:00
7+
custom:
8+
Issue: "1288"

helper/schema/grpc_provider.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func (s *GRPCProviderServer) GetMetadata(ctx context.Context, req *tfprotov5.Get
8080

8181
resp := &tfprotov5.GetMetadataResponse{
8282
DataSources: make([]tfprotov5.DataSourceMetadata, 0, len(s.provider.DataSourcesMap)),
83+
Functions: make([]tfprotov5.FunctionMetadata, 0),
8384
Resources: make([]tfprotov5.ResourceMetadata, 0, len(s.provider.ResourcesMap)),
8485
ServerCapabilities: s.serverCapabilities(),
8586
}
@@ -106,6 +107,7 @@ func (s *GRPCProviderServer) GetProviderSchema(ctx context.Context, req *tfproto
106107

107108
resp := &tfprotov5.GetProviderSchemaResponse{
108109
DataSourceSchemas: make(map[string]*tfprotov5.Schema, len(s.provider.DataSourcesMap)),
110+
Functions: make(map[string]*tfprotov5.Function, 0),
109111
ResourceSchemas: make(map[string]*tfprotov5.Schema, len(s.provider.ResourcesMap)),
110112
ServerCapabilities: s.serverCapabilities(),
111113
}
@@ -1271,6 +1273,36 @@ func (s *GRPCProviderServer) ReadDataSource(ctx context.Context, req *tfprotov5.
12711273
return resp, nil
12721274
}
12731275

1276+
func (s *GRPCProviderServer) CallFunction(ctx context.Context, req *tfprotov5.CallFunctionRequest) (*tfprotov5.CallFunctionResponse, error) {
1277+
ctx = logging.InitContext(ctx)
1278+
1279+
logging.HelperSchemaTrace(ctx, "Returning error for provider function call")
1280+
1281+
resp := &tfprotov5.CallFunctionResponse{
1282+
Diagnostics: []*tfprotov5.Diagnostic{
1283+
{
1284+
Severity: tfprotov5.DiagnosticSeverityError,
1285+
Summary: "Function Not Found",
1286+
Detail: fmt.Sprintf("No function named %q was found in the provider.", req.Name),
1287+
},
1288+
},
1289+
}
1290+
1291+
return resp, nil
1292+
}
1293+
1294+
func (s *GRPCProviderServer) GetFunctions(ctx context.Context, req *tfprotov5.GetFunctionsRequest) (*tfprotov5.GetFunctionsResponse, error) {
1295+
ctx = logging.InitContext(ctx)
1296+
1297+
logging.HelperSchemaTrace(ctx, "Getting provider functions")
1298+
1299+
resp := &tfprotov5.GetFunctionsResponse{
1300+
Functions: make(map[string]*tfprotov5.Function, 0),
1301+
}
1302+
1303+
return resp, nil
1304+
}
1305+
12741306
func pathToAttributePath(path cty.Path) *tftypes.AttributePath {
12751307
var steps []tftypes.AttributePathStep
12761308

helper/schema/grpc_provider_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,6 +2111,7 @@ func TestGRPCProviderServerGetMetadata(t *testing.T) {
21112111
TypeName: "test_datasource2",
21122112
},
21132113
},
2114+
Functions: []tfprotov5.FunctionMetadata{},
21142115
Resources: []tfprotov5.ResourceMetadata{},
21152116
ServerCapabilities: &tfprotov5.ServerCapabilities{
21162117
GetProviderSchemaOptional: true,
@@ -2137,6 +2138,7 @@ func TestGRPCProviderServerGetMetadata(t *testing.T) {
21372138
TypeName: "test_datasource2",
21382139
},
21392140
},
2141+
Functions: []tfprotov5.FunctionMetadata{},
21402142
Resources: []tfprotov5.ResourceMetadata{
21412143
{
21422144
TypeName: "test_resource1",
@@ -2159,6 +2161,7 @@ func TestGRPCProviderServerGetMetadata(t *testing.T) {
21592161
},
21602162
Expected: &tfprotov5.GetMetadataResponse{
21612163
DataSources: []tfprotov5.DataSourceMetadata{},
2164+
Functions: []tfprotov5.FunctionMetadata{},
21622165
Resources: []tfprotov5.ResourceMetadata{
21632166
{
21642167
TypeName: "test_resource1",

0 commit comments

Comments
 (0)