Skip to content

Commit

Permalink
tfprotov5+tfprotov6: Make FunctionServer implementation optional
Browse files Browse the repository at this point in the history
Reference: #353
  • Loading branch information
bflad committed Dec 12, 2023
1 parent 2017fc8 commit fb1d856
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .changes/unreleased/FEATURES-20231026-164300.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
kind: FEATURES
body: 'tfprotov5: Upgraded protocol to 5.5, which implements support for provider
defined functions'
body: 'tfprotov5+tfprotov6: Upgraded protocols and added types to support
provider-defined functions'
time: 2023-10-26T16:43:00.024481-04:00
custom:
Issue: "351"
6 changes: 0 additions & 6 deletions .changes/unreleased/FEATURES-20231026-164327.yaml

This file was deleted.

5 changes: 2 additions & 3 deletions .changes/unreleased/NOTES-20231026-164359.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
kind: NOTES
body: 'all: If using terraform-plugin-framework, terraform-plugin-mux, or terraform-plugin-sdk,
only upgrade this Go module as part of upgrading those Go modules or you may receive
a `missing method CallFunction` or `missing method GetFunctions` error when compiling'
body: 'tfprotov5+tfprotov6: An upcoming release will require the FunctionServer
implementation as part of ProviderServer.'
time: 2023-10-26T16:43:59.845786-04:00
custom:
Issue: "351"
5 changes: 4 additions & 1 deletion tfprotov5/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ type ProviderServer interface {
// are a handy interface for defining what a function is to
// terraform-plugin-go, so they are their own interface that is composed
// into ProviderServer.
FunctionServer
//
// This will be required in an upcoming release.
// Reference: https://github.com/hashicorp/terraform-plugin-go/issues/353
// FunctionServer
}

// GetMetadataRequest represents a GetMetadata RPC request.
Expand Down
45 changes: 43 additions & 2 deletions tfprotov5/tf5server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,28 @@ func (s *server) CallFunction(ctx context.Context, protoReq *tfplugin5.CallFunct
logging.ProtocolTrace(ctx, "Received request")
defer logging.ProtocolTrace(ctx, "Served request")

// Remove this check and error in preference of s.downstream.CallFunction
// below once ProviderServer interface requires FunctionServer.
// Reference: https://github.com/hashicorp/terraform-plugin-go/issues/353
functionServer, ok := s.downstream.(tfprotov5.FunctionServer)

if !ok {
logging.ProtocolError(ctx, "ProviderServer does not implement FunctionServer")

protoResp := &tfplugin5.CallFunction_Response{
Diagnostics: []*tfplugin5.Diagnostic{
{
Severity: tfplugin5.Diagnostic_ERROR,
Summary: "Provider Functions Not Implemented",
Detail: "A provider-defined function call was received by the provider, however the provider does not implement functions. " +
"Either upgrade the provider to a version that implements provider-defined functions or this is a bug in Terraform that should be reported to the Terraform maintainers.",
},
},
}

return protoResp, nil
}

req, err := fromproto.CallFunctionRequest(protoReq)

if err != nil {
Expand All @@ -926,7 +948,9 @@ func (s *server) CallFunction(ctx context.Context, protoReq *tfplugin5.CallFunct

ctx = tf5serverlogging.DownstreamRequest(ctx)

resp, err := s.downstream.CallFunction(ctx, req)
// Reference: https://github.com/hashicorp/terraform-plugin-go/issues/353
// resp, err := s.downstream.CallFunction(ctx, req)
resp, err := functionServer.CallFunction(ctx, req)

if err != nil {
logging.ProtocolError(ctx, "Error from downstream", map[string]any{logging.KeyError: err})
Expand Down Expand Up @@ -954,6 +978,21 @@ func (s *server) GetFunctions(ctx context.Context, protoReq *tfplugin5.GetFuncti
logging.ProtocolTrace(ctx, "Received request")
defer logging.ProtocolTrace(ctx, "Served request")

// Remove this check and response in preference of s.downstream.GetFunctions
// below once ProviderServer interface requires FunctionServer.
// Reference: https://github.com/hashicorp/terraform-plugin-go/issues/353
functionServer, ok := s.downstream.(tfprotov5.FunctionServer)

if !ok {
logging.ProtocolWarn(ctx, "ProviderServer does not implement FunctionServer")

protoResp := &tfplugin5.GetFunctions_Response{
Functions: map[string]*tfplugin5.Function{},
}

return protoResp, nil
}

req, err := fromproto.GetFunctionsRequest(protoReq)

if err != nil {
Expand All @@ -964,7 +1003,9 @@ func (s *server) GetFunctions(ctx context.Context, protoReq *tfplugin5.GetFuncti

ctx = tf5serverlogging.DownstreamRequest(ctx)

resp, err := s.downstream.GetFunctions(ctx, req)
// Reference: https://github.com/hashicorp/terraform-plugin-go/issues/353
// resp, err := s.downstream.GetFunctions(ctx, req)
resp, err := functionServer.GetFunctions(ctx, req)

if err != nil {
logging.ProtocolError(ctx, "Error from downstream", map[string]any{logging.KeyError: err})
Expand Down
5 changes: 4 additions & 1 deletion tfprotov6/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ type ProviderServer interface {
// are a handy interface for defining what a function is to
// terraform-plugin-go, so they are their own interface that is composed
// into ProviderServer.
FunctionServer
//
// This will be required in an upcoming release.
// Reference: https://github.com/hashicorp/terraform-plugin-go/issues/353
// FunctionServer
}

// GetMetadataRequest represents a GetMetadata RPC request.
Expand Down
45 changes: 43 additions & 2 deletions tfprotov6/tf6server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,28 @@ func (s *server) CallFunction(ctx context.Context, protoReq *tfplugin6.CallFunct
logging.ProtocolTrace(ctx, "Received request")
defer logging.ProtocolTrace(ctx, "Served request")

// Remove this check and error in preference of s.downstream.CallFunction
// below once ProviderServer interface requires FunctionServer.
// Reference: https://github.com/hashicorp/terraform-plugin-go/issues/353
functionServer, ok := s.downstream.(tfprotov6.FunctionServer)

if !ok {
logging.ProtocolError(ctx, "ProviderServer does not implement FunctionServer")

protoResp := &tfplugin6.CallFunction_Response{
Diagnostics: []*tfplugin6.Diagnostic{
{
Severity: tfplugin6.Diagnostic_ERROR,
Summary: "Provider Functions Not Implemented",
Detail: "A provider-defined function call was received by the provider, however the provider does not implement functions. " +
"Either upgrade the provider to a version that implements provider-defined functions or this is a bug in Terraform that should be reported to the Terraform maintainers.",
},
},
}

return protoResp, nil
}

req, err := fromproto.CallFunctionRequest(protoReq)

if err != nil {
Expand All @@ -924,7 +946,9 @@ func (s *server) CallFunction(ctx context.Context, protoReq *tfplugin6.CallFunct

ctx = tf6serverlogging.DownstreamRequest(ctx)

resp, err := s.downstream.CallFunction(ctx, req)
// Reference: https://github.com/hashicorp/terraform-plugin-go/issues/353
// resp, err := s.downstream.CallFunction(ctx, req)
resp, err := functionServer.CallFunction(ctx, req)

if err != nil {
logging.ProtocolError(ctx, "Error from downstream", map[string]any{logging.KeyError: err})
Expand Down Expand Up @@ -952,6 +976,21 @@ func (s *server) GetFunctions(ctx context.Context, protoReq *tfplugin6.GetFuncti
logging.ProtocolTrace(ctx, "Received request")
defer logging.ProtocolTrace(ctx, "Served request")

// Remove this check and response in preference of s.downstream.GetFunctions
// below once ProviderServer interface requires FunctionServer.
// Reference: https://github.com/hashicorp/terraform-plugin-go/issues/353
functionServer, ok := s.downstream.(tfprotov6.FunctionServer)

if !ok {
logging.ProtocolWarn(ctx, "ProviderServer does not implement FunctionServer")

protoResp := &tfplugin6.GetFunctions_Response{
Functions: map[string]*tfplugin6.Function{},
}

return protoResp, nil
}

req, err := fromproto.GetFunctionsRequest(protoReq)

if err != nil {
Expand All @@ -962,7 +1001,9 @@ func (s *server) GetFunctions(ctx context.Context, protoReq *tfplugin6.GetFuncti

ctx = tf6serverlogging.DownstreamRequest(ctx)

resp, err := s.downstream.GetFunctions(ctx, req)
// Reference: https://github.com/hashicorp/terraform-plugin-go/issues/353
// resp, err := s.downstream.GetFunctions(ctx, req)
resp, err := functionServer.GetFunctions(ctx, req)

if err != nil {
logging.ProtocolError(ctx, "Error from downstream", map[string]any{logging.KeyError: err})
Expand Down

0 comments on commit fb1d856

Please sign in to comment.