Skip to content

Commit

Permalink
all: Initial provider defined functions implementation (#1288)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bflad authored Dec 14, 2023
1 parent e58f4d9 commit dbf10e6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .changes/unreleased/NOTES-20231107-141609.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
kind: NOTES
body: 'helper/schema: While this Go module will not receive support for
provider-defined functions, the provider server is updated to handle the new
operations, which will be required to prevent errors when updating
terraform-plugin-framework or terraform-plugin-mux in the future.'
time: 2023-11-07T14:16:09.783296-05:00
custom:
Issue: "1288"
32 changes: 32 additions & 0 deletions helper/schema/grpc_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (s *GRPCProviderServer) GetMetadata(ctx context.Context, req *tfprotov5.Get

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

resp := &tfprotov5.GetProviderSchemaResponse{
DataSourceSchemas: make(map[string]*tfprotov5.Schema, len(s.provider.DataSourcesMap)),
Functions: make(map[string]*tfprotov5.Function, 0),
ResourceSchemas: make(map[string]*tfprotov5.Schema, len(s.provider.ResourcesMap)),
ServerCapabilities: s.serverCapabilities(),
}
Expand Down Expand Up @@ -1271,6 +1273,36 @@ func (s *GRPCProviderServer) ReadDataSource(ctx context.Context, req *tfprotov5.
return resp, nil
}

func (s *GRPCProviderServer) CallFunction(ctx context.Context, req *tfprotov5.CallFunctionRequest) (*tfprotov5.CallFunctionResponse, error) {
ctx = logging.InitContext(ctx)

logging.HelperSchemaTrace(ctx, "Returning error for provider function call")

resp := &tfprotov5.CallFunctionResponse{
Diagnostics: []*tfprotov5.Diagnostic{
{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Function Not Found",
Detail: fmt.Sprintf("No function named %q was found in the provider.", req.Name),
},
},
}

return resp, nil
}

func (s *GRPCProviderServer) GetFunctions(ctx context.Context, req *tfprotov5.GetFunctionsRequest) (*tfprotov5.GetFunctionsResponse, error) {
ctx = logging.InitContext(ctx)

logging.HelperSchemaTrace(ctx, "Getting provider functions")

resp := &tfprotov5.GetFunctionsResponse{
Functions: make(map[string]*tfprotov5.Function, 0),
}

return resp, nil
}

func pathToAttributePath(path cty.Path) *tftypes.AttributePath {
var steps []tftypes.AttributePathStep

Expand Down
3 changes: 3 additions & 0 deletions helper/schema/grpc_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2111,6 +2111,7 @@ func TestGRPCProviderServerGetMetadata(t *testing.T) {
TypeName: "test_datasource2",
},
},
Functions: []tfprotov5.FunctionMetadata{},
Resources: []tfprotov5.ResourceMetadata{},
ServerCapabilities: &tfprotov5.ServerCapabilities{
GetProviderSchemaOptional: true,
Expand All @@ -2137,6 +2138,7 @@ func TestGRPCProviderServerGetMetadata(t *testing.T) {
TypeName: "test_datasource2",
},
},
Functions: []tfprotov5.FunctionMetadata{},
Resources: []tfprotov5.ResourceMetadata{
{
TypeName: "test_resource1",
Expand All @@ -2159,6 +2161,7 @@ func TestGRPCProviderServerGetMetadata(t *testing.T) {
},
Expected: &tfprotov5.GetMetadataResponse{
DataSources: []tfprotov5.DataSourceMetadata{},
Functions: []tfprotov5.FunctionMetadata{},
Resources: []tfprotov5.ResourceMetadata{
{
TypeName: "test_resource1",
Expand Down

0 comments on commit dbf10e6

Please sign in to comment.