diff --git a/go.mod b/go.mod index ed6dc4b..0226986 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.20 require ( github.com/google/go-cmp v0.6.0 - github.com/hashicorp/terraform-plugin-go v0.19.2-0.20231211173412-2017fc8cadb4 + github.com/hashicorp/terraform-plugin-go v0.19.2-0.20231212192300-fb1d856be38c github.com/hashicorp/terraform-plugin-log v0.9.0 google.golang.org/grpc v1.59.0 ) diff --git a/go.sum b/go.sum index 03a586e..8977863 100644 --- a/go.sum +++ b/go.sum @@ -16,8 +16,8 @@ github.com/hashicorp/go-plugin v1.6.0 h1:wgd4KxHJTVGGqWBq4QPB1i5BZNEx9BR8+OFmHDm github.com/hashicorp/go-plugin v1.6.0/go.mod h1:lBS5MtSSBZk0SHc66KACcjjlU6WzEVP/8pwz68aMkCI= github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8= github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= -github.com/hashicorp/terraform-plugin-go v0.19.2-0.20231211173412-2017fc8cadb4 h1:Q3xOtc6IB248qD76gKYD6FEPD0eFYKKr9n18Zh5o5I0= -github.com/hashicorp/terraform-plugin-go v0.19.2-0.20231211173412-2017fc8cadb4/go.mod h1:dYHQT4x4qtYsGsD0ZmWo0JJz9rIxSv/ETdK8p+1qlhI= +github.com/hashicorp/terraform-plugin-go v0.19.2-0.20231212192300-fb1d856be38c h1:+/OS6VVVQZ3mdF1H2KbMYxKp329S61MDbNLQs4L1eCg= +github.com/hashicorp/terraform-plugin-go v0.19.2-0.20231212192300-fb1d856be38c/go.mod h1:dYHQT4x4qtYsGsD0ZmWo0JJz9rIxSv/ETdK8p+1qlhI= github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0= github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow= github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI= diff --git a/tf5muxserver/mux_server_CallFunction.go b/tf5muxserver/mux_server_CallFunction.go index c72bdb2..7e4ec8c 100644 --- a/tf5muxserver/mux_server_CallFunction.go +++ b/tf5muxserver/mux_server_CallFunction.go @@ -30,7 +30,28 @@ func (s *muxServer) CallFunction(ctx context.Context, req *tfprotov5.CallFunctio } ctx = logging.Tfprotov5ProviderServerContext(ctx, server) + + // Remove and call server.CallFunction below directly. + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := server.(tfprotov5.FunctionServer) + + if !ok { + resp := &tfprotov5.CallFunctionResponse{ + Diagnostics: []*tfprotov5.Diagnostic{ + { + Severity: tfprotov5.DiagnosticSeverityError, + 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 resp, nil + } + logging.MuxTrace(ctx, "calling downstream server") - return server.CallFunction(ctx, req) + // return server.CallFunction(ctx, req) + return functionServer.CallFunction(ctx, req) } diff --git a/tf5muxserver/mux_server_CallFunction_test.go b/tf5muxserver/mux_server_CallFunction_test.go index d0aea7e..66c1256 100644 --- a/tf5muxserver/mux_server_CallFunction_test.go +++ b/tf5muxserver/mux_server_CallFunction_test.go @@ -39,7 +39,15 @@ func TestMuxServerCallFunction(t *testing.T) { t.Fatalf("unexpected error setting up factory: %s", err) } - _, err = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{ + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := muxServer.ProviderServer().(tfprotov5.FunctionServer) + + if !ok { + t.Fatal("muxServer should implement tfprotov5.FunctionServer") + } + + // _, err = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{ + _, err = functionServer.CallFunction(ctx, &tfprotov5.CallFunctionRequest{ Name: "test_function1", }) @@ -55,7 +63,8 @@ func TestMuxServerCallFunction(t *testing.T) { t.Errorf("unexpected test_function1 CallFunction called on server2") } - _, err = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{ + // _, err = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{ + _, err = functionServer.CallFunction(ctx, &tfprotov5.CallFunctionRequest{ Name: "test_function2", }) diff --git a/tf5muxserver/mux_server_GetFunctions.go b/tf5muxserver/mux_server_GetFunctions.go index 9090adf..c8927fc 100644 --- a/tf5muxserver/mux_server_GetFunctions.go +++ b/tf5muxserver/mux_server_GetFunctions.go @@ -29,9 +29,19 @@ func (s *muxServer) GetFunctions(ctx context.Context, req *tfprotov5.GetFunction for _, server := range s.servers { ctx := logging.Tfprotov5ProviderServerContext(ctx, server) + + // Remove and call server.GetFunctions below directly. + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := server.(tfprotov5.FunctionServer) + + if !ok { + continue + } + logging.MuxTrace(ctx, "calling downstream server") - serverResp, err := server.GetFunctions(ctx, &tfprotov5.GetFunctionsRequest{}) + // serverResp, err := server.GetFunctions(ctx, &tfprotov5.GetFunctionsRequest{}) + serverResp, err := functionServer.GetFunctions(ctx, &tfprotov5.GetFunctionsRequest{}) if err != nil { return resp, fmt.Errorf("error calling GetFunctions for %T: %w", server, err) diff --git a/tf5muxserver/mux_server_GetFunctions_test.go b/tf5muxserver/mux_server_GetFunctions_test.go index 0c65873..4482913 100644 --- a/tf5muxserver/mux_server_GetFunctions_test.go +++ b/tf5muxserver/mux_server_GetFunctions_test.go @@ -311,7 +311,15 @@ func TestMuxServerGetFunctions(t *testing.T) { t.Fatalf("unexpected error: %s", err) } - resp, err := muxServer.ProviderServer().GetFunctions(context.Background(), &tfprotov5.GetFunctionsRequest{}) + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := muxServer.ProviderServer().(tfprotov5.FunctionServer) + + if !ok { + t.Fatal("muxServer should implement tfprotov5.FunctionServer") + } + + // resp, err := muxServer.ProviderServer().GetFunctions(context.Background(), &tfprotov5.GetFunctionsRequest{}) + resp, err := functionServer.GetFunctions(context.Background(), &tfprotov5.GetFunctionsRequest{}) if err != nil { t.Fatalf("unexpected error: %s", err) diff --git a/tf5muxserver/mux_server_test.go b/tf5muxserver/mux_server_test.go index e64951d..c5e434c 100644 --- a/tf5muxserver/mux_server_test.go +++ b/tf5muxserver/mux_server_test.go @@ -435,7 +435,15 @@ func TestMuxServerGetFunctionServer_GetProviderSchema(t *testing.T) { terraformOp := func() { defer wg.Done() - _, _ = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{ + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := muxServer.ProviderServer().(tfprotov5.FunctionServer) + + if !ok { + t.Fatal("muxServer should implement tfprotov5.FunctionServer") + } + + // _, _ = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{ + _, _ = functionServer.CallFunction(ctx, &tfprotov5.CallFunctionRequest{ Name: "test_function1", }) } @@ -498,7 +506,15 @@ func TestMuxServerGetFunctionServer_GetProviderSchema_Duplicate(t *testing.T) { terraformOp := func() { defer wg.Done() - resp, _ := muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{ + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := muxServer.ProviderServer().(tfprotov5.FunctionServer) + + if !ok { + t.Fatal("muxServer should implement tfprotov5.FunctionServer") + } + + // resp, _ := muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{ + resp, _ := functionServer.CallFunction(ctx, &tfprotov5.CallFunctionRequest{ Name: "test_function", }) @@ -562,7 +578,15 @@ func TestMuxServerGetFunctionServer_GetMetadata(t *testing.T) { terraformOp := func() { defer wg.Done() - _, _ = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{ + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := muxServer.ProviderServer().(tfprotov5.FunctionServer) + + if !ok { + t.Fatal("muxServer should implement tfprotov5.FunctionServer") + } + + // _, _ = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{ + _, _ = functionServer.CallFunction(ctx, &tfprotov5.CallFunctionRequest{ Name: "test_function1", }) } @@ -629,7 +653,15 @@ func TestMuxServerGetFunctionServer_GetMetadata_Duplicate(t *testing.T) { terraformOp := func() { defer wg.Done() - resp, _ := muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{ + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := muxServer.ProviderServer().(tfprotov5.FunctionServer) + + if !ok { + t.Fatal("muxServer should implement tfprotov5.FunctionServer") + } + + // resp, _ := muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{ + resp, _ := functionServer.CallFunction(ctx, &tfprotov5.CallFunctionRequest{ Name: "test_function", }) @@ -691,7 +723,15 @@ func TestMuxServerGetFunctionServer_GetMetadata_Partial(t *testing.T) { terraformOp := func() { defer wg.Done() - _, _ = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{ + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := muxServer.ProviderServer().(tfprotov5.FunctionServer) + + if !ok { + t.Fatal("muxServer should implement tfprotov5.FunctionServer") + } + + // _, _ = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{ + _, _ = functionServer.CallFunction(ctx, &tfprotov5.CallFunctionRequest{ Name: "test_function1", }) } @@ -757,7 +797,15 @@ func TestMuxServerGetFunctionServer_Missing(t *testing.T) { terraformOp := func() { defer wg.Done() - resp, _ := muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{ + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := muxServer.ProviderServer().(tfprotov5.FunctionServer) + + if !ok { + t.Fatal("muxServer should implement tfprotov5.FunctionServer") + } + + // resp, _ := muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{ + resp, _ := functionServer.CallFunction(ctx, &tfprotov5.CallFunctionRequest{ Name: "test_function_nonexistent", }) diff --git a/tf5to6server/tf5to6server.go b/tf5to6server/tf5to6server.go index 7158a85..8fae458 100644 --- a/tf5to6server/tf5to6server.go +++ b/tf5to6server/tf5to6server.go @@ -44,8 +44,29 @@ func (s v5tov6Server) ApplyResourceChange(ctx context.Context, req *tfprotov6.Ap } func (s v5tov6Server) CallFunction(ctx context.Context, req *tfprotov6.CallFunctionRequest) (*tfprotov6.CallFunctionResponse, error) { + // Remove and call s.v5Server.CallFunction below directly. + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := s.v5Server.(tfprotov5.FunctionServer) + + if !ok { + v6Resp := &tfprotov6.CallFunctionResponse{ + Diagnostics: []*tfprotov6.Diagnostic{ + { + Severity: tfprotov6.DiagnosticSeverityError, + 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 v6Resp, nil + } + v5Req := tfprotov6tov5.CallFunctionRequest(req) - v5Resp, err := s.v5Server.CallFunction(ctx, v5Req) + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + // v5Resp, err := s.v5Server.CallFunction(ctx, v5Req) + v5Resp, err := functionServer.CallFunction(ctx, v5Req) if err != nil { return nil, err @@ -66,8 +87,22 @@ func (s v5tov6Server) ConfigureProvider(ctx context.Context, req *tfprotov6.Conf } func (s v5tov6Server) GetFunctions(ctx context.Context, req *tfprotov6.GetFunctionsRequest) (*tfprotov6.GetFunctionsResponse, error) { + // Remove and call s.v5Server.GetFunctions below directly. + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := s.v5Server.(tfprotov5.FunctionServer) + + if !ok { + v6Resp := &tfprotov6.GetFunctionsResponse{ + Functions: map[string]*tfprotov6.Function{}, + } + + return v6Resp, nil + } + v5Req := tfprotov6tov5.GetFunctionsRequest(req) - v5Resp, err := s.v5Server.GetFunctions(ctx, v5Req) + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + // v5Resp, err := s.v5Server.GetFunctions(ctx, v5Req) + v5Resp, err := functionServer.GetFunctions(ctx, v5Req) if err != nil { return nil, err diff --git a/tf5to6server/tf5to6server_test.go b/tf5to6server/tf5to6server_test.go index 229ad26..1856409 100644 --- a/tf5to6server/tf5to6server_test.go +++ b/tf5to6server/tf5to6server_test.go @@ -178,7 +178,15 @@ func TestV6ToV5ServerCallFunction(t *testing.T) { t.Fatalf("unexpected error upgrading server: %s", err) } - _, err = v6server.CallFunction(ctx, &tfprotov6.CallFunctionRequest{ + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := v6server.(tfprotov6.FunctionServer) + + if !ok { + t.Fatal("v6server should implement tfprotov6.FunctionServer") + } + + //_, err = v6server.CallFunction(ctx, &tfprotov6.CallFunctionRequest{ + _, err = functionServer.CallFunction(ctx, &tfprotov6.CallFunctionRequest{ Name: "test_function", }) @@ -238,7 +246,15 @@ func TestV6ToV5ServerGetFunctions(t *testing.T) { t.Fatalf("unexpected error upgrading server: %s", err) } - _, err = v6server.GetFunctions(ctx, &tfprotov6.GetFunctionsRequest{}) + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := v6server.(tfprotov6.FunctionServer) + + if !ok { + t.Fatal("v6server should implement tfprotov6.FunctionServer") + } + + //_, err = v6server.GetFunction(ctx, &tfprotov6.GetFunctionRequest{}) + _, err = functionServer.GetFunctions(ctx, &tfprotov6.GetFunctionsRequest{}) if err != nil { t.Fatalf("unexpected error: %s", err) diff --git a/tf6muxserver/mux_server_CallFunction.go b/tf6muxserver/mux_server_CallFunction.go index 6804ca3..b600d49 100644 --- a/tf6muxserver/mux_server_CallFunction.go +++ b/tf6muxserver/mux_server_CallFunction.go @@ -30,7 +30,28 @@ func (s *muxServer) CallFunction(ctx context.Context, req *tfprotov6.CallFunctio } ctx = logging.Tfprotov6ProviderServerContext(ctx, server) + + // Remove and call server.CallFunction below directly. + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := server.(tfprotov6.FunctionServer) + + if !ok { + resp := &tfprotov6.CallFunctionResponse{ + Diagnostics: []*tfprotov6.Diagnostic{ + { + Severity: tfprotov6.DiagnosticSeverityError, + 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 resp, nil + } + logging.MuxTrace(ctx, "calling downstream server") - return server.CallFunction(ctx, req) + // return server.CallFunction(ctx, req) + return functionServer.CallFunction(ctx, req) } diff --git a/tf6muxserver/mux_server_CallFunction_test.go b/tf6muxserver/mux_server_CallFunction_test.go index 2b81171..17f6b3f 100644 --- a/tf6muxserver/mux_server_CallFunction_test.go +++ b/tf6muxserver/mux_server_CallFunction_test.go @@ -39,7 +39,15 @@ func TestMuxServerCallFunction(t *testing.T) { t.Fatalf("unexpected error setting up factory: %s", err) } - _, err = muxServer.ProviderServer().CallFunction(ctx, &tfprotov6.CallFunctionRequest{ + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := muxServer.ProviderServer().(tfprotov6.FunctionServer) + + if !ok { + t.Fatal("muxServer should implement tfprotov6.FunctionServer") + } + + // _, err = muxServer.ProviderServer().CallFunction(ctx, &tfprotov6.CallFunctionRequest{ + _, err = functionServer.CallFunction(ctx, &tfprotov6.CallFunctionRequest{ Name: "test_function1", }) @@ -55,7 +63,8 @@ func TestMuxServerCallFunction(t *testing.T) { t.Errorf("unexpected test_function1 CallFunction called on server2") } - _, err = muxServer.ProviderServer().CallFunction(ctx, &tfprotov6.CallFunctionRequest{ + // _, err = muxServer.ProviderServer().CallFunction(ctx, &tfprotov6.CallFunctionRequest{ + _, err = functionServer.CallFunction(ctx, &tfprotov6.CallFunctionRequest{ Name: "test_function2", }) diff --git a/tf6muxserver/mux_server_GetFunctions.go b/tf6muxserver/mux_server_GetFunctions.go index 91ebfd4..cbb484e 100644 --- a/tf6muxserver/mux_server_GetFunctions.go +++ b/tf6muxserver/mux_server_GetFunctions.go @@ -29,9 +29,19 @@ func (s *muxServer) GetFunctions(ctx context.Context, req *tfprotov6.GetFunction for _, server := range s.servers { ctx := logging.Tfprotov6ProviderServerContext(ctx, server) + + // Remove and call server.GetFunctions below directly. + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := server.(tfprotov6.FunctionServer) + + if !ok { + continue + } + logging.MuxTrace(ctx, "calling downstream server") - serverResp, err := server.GetFunctions(ctx, &tfprotov6.GetFunctionsRequest{}) + // serverResp, err := server.GetFunctions(ctx, &tfprotov6.GetFunctionsRequest{}) + serverResp, err := functionServer.GetFunctions(ctx, &tfprotov6.GetFunctionsRequest{}) if err != nil { return resp, fmt.Errorf("error calling GetFunctions for %T: %w", server, err) diff --git a/tf6muxserver/mux_server_GetFunctions_test.go b/tf6muxserver/mux_server_GetFunctions_test.go index 0168acc..185047f 100644 --- a/tf6muxserver/mux_server_GetFunctions_test.go +++ b/tf6muxserver/mux_server_GetFunctions_test.go @@ -311,7 +311,15 @@ func TestMuxServerGetFunctions(t *testing.T) { t.Fatalf("unexpected error: %s", err) } - resp, err := muxServer.ProviderServer().GetFunctions(context.Background(), &tfprotov6.GetFunctionsRequest{}) + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := muxServer.ProviderServer().(tfprotov6.FunctionServer) + + if !ok { + t.Fatal("muxServer should implement tfprotov6.FunctionServer") + } + + // resp, err := muxServer.ProviderServer().GetFunctions(context.Background(), &tfprotov6.GetFunctionsRequest{}) + resp, err := functionServer.GetFunctions(context.Background(), &tfprotov6.GetFunctionsRequest{}) if err != nil { t.Fatalf("unexpected error: %s", err) diff --git a/tf6muxserver/mux_server_test.go b/tf6muxserver/mux_server_test.go index 84932a4..d7d68f9 100644 --- a/tf6muxserver/mux_server_test.go +++ b/tf6muxserver/mux_server_test.go @@ -435,7 +435,15 @@ func TestMuxServerGetFunctionServer_GetProviderSchema(t *testing.T) { terraformOp := func() { defer wg.Done() - _, _ = muxServer.ProviderServer().CallFunction(ctx, &tfprotov6.CallFunctionRequest{ + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := muxServer.ProviderServer().(tfprotov6.FunctionServer) + + if !ok { + t.Fatal("muxServer should implement tfprotov6.FunctionServer") + } + + //_, _ = muxServer.ProviderServer().CallFunction(ctx, &tfprotov6.CallFunctionRequest{ + _, _ = functionServer.CallFunction(ctx, &tfprotov6.CallFunctionRequest{ Name: "test_function1", }) } @@ -498,7 +506,15 @@ func TestMuxServerGetFunctionServer_GetProviderSchema_Duplicate(t *testing.T) { terraformOp := func() { defer wg.Done() - resp, _ := muxServer.ProviderServer().CallFunction(ctx, &tfprotov6.CallFunctionRequest{ + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := muxServer.ProviderServer().(tfprotov6.FunctionServer) + + if !ok { + t.Fatal("muxServer should implement tfprotov6.FunctionServer") + } + + // resp, _ := muxServer.ProviderServer().CallFunction(ctx, &tfprotov6.CallFunctionRequest{ + resp, _ := functionServer.CallFunction(ctx, &tfprotov6.CallFunctionRequest{ Name: "test_function", }) @@ -562,7 +578,15 @@ func TestMuxServerGetFunctionServer_GetMetadata(t *testing.T) { terraformOp := func() { defer wg.Done() - _, _ = muxServer.ProviderServer().CallFunction(ctx, &tfprotov6.CallFunctionRequest{ + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := muxServer.ProviderServer().(tfprotov6.FunctionServer) + + if !ok { + t.Fatal("muxServer should implement tfprotov6.FunctionServer") + } + + // _, _ = muxServer.ProviderServer().CallFunction(ctx, &tfprotov6.CallFunctionRequest{ + _, _ = functionServer.CallFunction(ctx, &tfprotov6.CallFunctionRequest{ Name: "test_function1", }) } @@ -629,7 +653,15 @@ func TestMuxServerGetFunctionServer_GetMetadata_Duplicate(t *testing.T) { terraformOp := func() { defer wg.Done() - resp, _ := muxServer.ProviderServer().CallFunction(ctx, &tfprotov6.CallFunctionRequest{ + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := muxServer.ProviderServer().(tfprotov6.FunctionServer) + + if !ok { + t.Fatal("muxServer should implement tfprotov6.FunctionServer") + } + + // resp, _ := muxServer.ProviderServer().CallFunction(ctx, &tfprotov6.CallFunctionRequest{ + resp, _ := functionServer.CallFunction(ctx, &tfprotov6.CallFunctionRequest{ Name: "test_function", }) @@ -691,7 +723,15 @@ func TestMuxServerGetFunctionServer_GetMetadata_Partial(t *testing.T) { terraformOp := func() { defer wg.Done() - _, _ = muxServer.ProviderServer().CallFunction(ctx, &tfprotov6.CallFunctionRequest{ + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := muxServer.ProviderServer().(tfprotov6.FunctionServer) + + if !ok { + t.Fatal("muxServer should implement tfprotov6.FunctionServer") + } + + // _, _ = muxServer.ProviderServer().CallFunction(ctx, &tfprotov6.CallFunctionRequest{ + _, _ = functionServer.CallFunction(ctx, &tfprotov6.CallFunctionRequest{ Name: "test_function1", }) } @@ -757,7 +797,15 @@ func TestMuxServerGetFunctionServer_Missing(t *testing.T) { terraformOp := func() { defer wg.Done() - resp, _ := muxServer.ProviderServer().CallFunction(ctx, &tfprotov6.CallFunctionRequest{ + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := muxServer.ProviderServer().(tfprotov6.FunctionServer) + + if !ok { + t.Fatal("muxServer should implement tfprotov6.FunctionServer") + } + + //resp, _ := muxServer.ProviderServer().CallFunction(ctx, &tfprotov6.CallFunctionRequest{ + resp, _ := functionServer.CallFunction(ctx, &tfprotov6.CallFunctionRequest{ Name: "test_function_nonexistent", }) diff --git a/tf6to5server/tf6to5server.go b/tf6to5server/tf6to5server.go index 14fc93f..d7cd394 100644 --- a/tf6to5server/tf6to5server.go +++ b/tf6to5server/tf6to5server.go @@ -56,8 +56,29 @@ func (s v6tov5Server) ApplyResourceChange(ctx context.Context, req *tfprotov5.Ap } func (s v6tov5Server) CallFunction(ctx context.Context, req *tfprotov5.CallFunctionRequest) (*tfprotov5.CallFunctionResponse, error) { + // Remove and call s.v6Server.CallFunction below directly. + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := s.v6Server.(tfprotov6.FunctionServer) + + if !ok { + v5Resp := &tfprotov5.CallFunctionResponse{ + Diagnostics: []*tfprotov5.Diagnostic{ + { + Severity: tfprotov5.DiagnosticSeverityError, + 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 v5Resp, nil + } + v6Req := tfprotov5tov6.CallFunctionRequest(req) - v6Resp, err := s.v6Server.CallFunction(ctx, v6Req) + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + // v6Resp, err := s.v6Server.CallFunction(ctx, v6Req) + v6Resp, err := functionServer.CallFunction(ctx, v6Req) if err != nil { return nil, err @@ -78,8 +99,22 @@ func (s v6tov5Server) ConfigureProvider(ctx context.Context, req *tfprotov5.Conf } func (s v6tov5Server) GetFunctions(ctx context.Context, req *tfprotov5.GetFunctionsRequest) (*tfprotov5.GetFunctionsResponse, error) { + // Remove and call s.v6Server.GetFunctions below directly. + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := s.v6Server.(tfprotov6.FunctionServer) + + if !ok { + v5Resp := &tfprotov5.GetFunctionsResponse{ + Functions: map[string]*tfprotov5.Function{}, + } + + return v5Resp, nil + } + v6Req := tfprotov5tov6.GetFunctionsRequest(req) - v6Resp, err := s.v6Server.GetFunctions(ctx, v6Req) + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + // v6Resp, err := s.v6Server.GetFunctions(ctx, v6Req) + v6Resp, err := functionServer.GetFunctions(ctx, v6Req) if err != nil { return nil, err diff --git a/tf6to5server/tf6to5server_test.go b/tf6to5server/tf6to5server_test.go index 634498d..de8ba35 100644 --- a/tf6to5server/tf6to5server_test.go +++ b/tf6to5server/tf6to5server_test.go @@ -276,7 +276,15 @@ func TestV6ToV5ServerCallFunction(t *testing.T) { t.Fatalf("unexpected error downgrading server: %s", err) } - _, err = v5server.CallFunction(ctx, &tfprotov5.CallFunctionRequest{ + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := v5server.(tfprotov5.FunctionServer) + + if !ok { + t.Fatal("v5server should implement tfprotov5.FunctionServer") + } + + // _, err = v5server.CallFunction(ctx, &tfprotov5.CallFunctionRequest{ + _, err = functionServer.CallFunction(ctx, &tfprotov5.CallFunctionRequest{ Name: "test_function", }) @@ -336,7 +344,15 @@ func TestV5ToV6ServerGetFunctions(t *testing.T) { t.Fatalf("unexpected error downgrading server: %s", err) } - _, err = v5server.GetFunctions(ctx, &tfprotov5.GetFunctionsRequest{}) + // Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210 + functionServer, ok := v5server.(tfprotov5.FunctionServer) + + if !ok { + t.Fatal("v5server should implement tfprotov5.FunctionServer") + } + + //_, err = v5server.GetFunctions(ctx, &tfprotov5.GetFunctionsRequest{}) + _, err = functionServer.GetFunctions(ctx, &tfprotov5.GetFunctionsRequest{}) if err != nil { t.Fatalf("unexpected error: %s", err)