Skip to content

Commit

Permalink
Adding diagnostics returned from server.GetProviderSchema() to the di…
Browse files Browse the repository at this point in the history
…agnostics in the GetProviderSchemaResponse (#176)
  • Loading branch information
bendbennett committed Jul 13, 2023
1 parent 17e128a commit 335c1ef
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
17 changes: 17 additions & 0 deletions internal/tf5testserver/tf5testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,20 @@ func (s *TestServer) PrepareProviderConfig(_ context.Context, req *tfprotov5.Pre
s.PrepareProviderConfigCalled = true
return s.PrepareProviderConfigResponse, nil
}

type TestServerDiags struct {
*TestServer
Diagnostics []*tfprotov5.Diagnostic
}

func (s *TestServerDiags) GetProviderSchema(ctx context.Context, req *tfprotov5.GetProviderSchemaRequest) (*tfprotov5.GetProviderSchemaResponse, error) {
resp, err := s.TestServer.GetProviderSchema(ctx, req)

resp.Diagnostics = append(resp.Diagnostics, s.Diagnostics...)

return resp, err
}

func (s *TestServerDiags) ProviderServer() tfprotov5.ProviderServer {
return s
}
17 changes: 17 additions & 0 deletions internal/tf6testserver/tf6testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,20 @@ func (s *TestServer) ValidateProviderConfig(_ context.Context, req *tfprotov6.Va
s.ValidateProviderConfigCalled = true
return s.ValidateProviderConfigResponse, nil
}

type TestServerDiags struct {
*TestServer
Diagnostics []*tfprotov6.Diagnostic
}

func (s *TestServerDiags) GetProviderSchema(ctx context.Context, req *tfprotov6.GetProviderSchemaRequest) (*tfprotov6.GetProviderSchemaResponse, error) {
resp, err := s.TestServer.GetProviderSchema(ctx, req)

resp.Diagnostics = append(resp.Diagnostics, s.Diagnostics...)

return resp, err
}

func (s *TestServerDiags) ProviderServer() tfprotov6.ProviderServer {
return s
}
3 changes: 3 additions & 0 deletions tf5muxserver/mux_server_GetProviderSchema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

"github.com/hashicorp/terraform-plugin-go/tfprotov5"

"github.com/hashicorp/terraform-plugin-mux/internal/logging"
)

Expand Down Expand Up @@ -42,6 +43,8 @@ func (s *muxServer) GetProviderSchema(ctx context.Context, req *tfprotov5.GetPro
return resp, fmt.Errorf("error calling GetProviderSchema for %T: %w", server, err)
}

resp.Diagnostics = append(resp.Diagnostics, serverResp.Diagnostics...)

if serverResp.Provider != nil {
if resp.Provider != nil && !schemaEquals(serverResp.Provider, resp.Provider) {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{
Expand Down
24 changes: 24 additions & 0 deletions tf5muxserver/mux_server_GetProviderSchema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-plugin-mux/internal/tf5testserver"
"github.com/hashicorp/terraform-plugin-mux/tf5muxserver"
)
Expand Down Expand Up @@ -486,6 +487,29 @@ func TestMuxServerGetProviderSchema(t *testing.T) {
PlanDestroy: true,
},
},
"get-provider-schema-diagnostics": {
servers: []func() tfprotov5.ProviderServer{
(&tf5testserver.TestServerDiags{
TestServer: &tf5testserver.TestServer{},
Diagnostics: []*tfprotov5.Diagnostic{
{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Summary",
Detail: "Detail",
},
},
}).ProviderServer,
},
expectedDiagnostics: []*tfprotov5.Diagnostic{
{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Summary",
Detail: "Detail",
},
},
expectedDataSourceSchemas: map[string]*tfprotov5.Schema{},
expectedResourceSchemas: map[string]*tfprotov5.Schema{},
},
"provider-mismatch": {
servers: []func() tfprotov5.ProviderServer{
(&tf5testserver.TestServer{
Expand Down
3 changes: 3 additions & 0 deletions tf6muxserver/mux_server_GetProviderSchema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"

"github.com/hashicorp/terraform-plugin-mux/internal/logging"
)

Expand Down Expand Up @@ -42,6 +43,8 @@ func (s *muxServer) GetProviderSchema(ctx context.Context, req *tfprotov6.GetPro
return resp, fmt.Errorf("error calling GetProviderSchema for %T: %w", server, err)
}

resp.Diagnostics = append(resp.Diagnostics, serverResp.Diagnostics...)

if serverResp.Provider != nil {
if resp.Provider != nil && !schemaEquals(serverResp.Provider, resp.Provider) {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov6.Diagnostic{
Expand Down
24 changes: 24 additions & 0 deletions tf6muxserver/mux_server_GetProviderSchema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-plugin-mux/internal/tf6testserver"
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
)
Expand Down Expand Up @@ -486,6 +487,29 @@ func TestMuxServerGetProviderSchema(t *testing.T) {
PlanDestroy: true,
},
},
"get-provider-schema-diagnostics": {
servers: []func() tfprotov6.ProviderServer{
(&tf6testserver.TestServerDiags{
TestServer: &tf6testserver.TestServer{},
Diagnostics: []*tfprotov6.Diagnostic{
{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "Summary",
Detail: "Detail",
},
},
}).ProviderServer,
},
expectedDiagnostics: []*tfprotov6.Diagnostic{
{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "Summary",
Detail: "Detail",
},
},
expectedDataSourceSchemas: map[string]*tfprotov6.Schema{},
expectedResourceSchemas: map[string]*tfprotov6.Schema{},
},
"provider-mismatch": {
servers: []func() tfprotov6.ProviderServer{
(&tf6testserver.TestServer{
Expand Down

0 comments on commit 335c1ef

Please sign in to comment.