Skip to content

Commit

Permalink
Refactoring to use TestServer.GetProviderSchemaResponse field (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Jul 14, 2023
1 parent 7985f2f commit a6d7225
Show file tree
Hide file tree
Showing 34 changed files with 2,484 additions and 1,774 deletions.
6 changes: 0 additions & 6 deletions .changes/unreleased/BUG FIXES-20230713-155423.yaml

This file was deleted.

6 changes: 0 additions & 6 deletions .changes/unreleased/BUG FIXES-20230713-155511.yaml

This file was deleted.

6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20230714-094529.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'tf5muxserver: Ensure `GetProviderSchema` RPC diagnostics are properly returned
to Terraform'
time: 2023-07-14T09:45:29.631825+01:00
custom:
Issue: "176"
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20230714-094547.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'tf6muxserver: Ensure `GetProviderSchema` RPC diagnostics are properly returned
to Terraform'
time: 2023-07-14T09:45:47.81872+01:00
custom:
Issue: "176"
43 changes: 3 additions & 40 deletions internal/tf5testserver/tf5testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@ import (
var _ tfprotov5.ProviderServer = &TestServer{}

type TestServer struct {
DataSourceSchemas map[string]*tfprotov5.Schema
ProviderMetaSchema *tfprotov5.Schema
ProviderSchema *tfprotov5.Schema
ResourceSchemas map[string]*tfprotov5.Schema
ServerCapabilities *tfprotov5.ServerCapabilities

ApplyResourceChangeCalled map[string]bool

ConfigureProviderCalled bool

GetProviderSchemaCalled bool
GetProviderSchemaCalled bool
GetProviderSchemaResponse *tfprotov5.GetProviderSchemaResponse

ImportResourceStateCalled map[string]bool

Expand Down Expand Up @@ -65,22 +60,7 @@ func (s *TestServer) ConfigureProvider(_ context.Context, _ *tfprotov5.Configure

func (s *TestServer) GetProviderSchema(_ context.Context, _ *tfprotov5.GetProviderSchemaRequest) (*tfprotov5.GetProviderSchemaResponse, error) {
s.GetProviderSchemaCalled = true

if s.DataSourceSchemas == nil {
s.DataSourceSchemas = make(map[string]*tfprotov5.Schema)
}

if s.ResourceSchemas == nil {
s.ResourceSchemas = make(map[string]*tfprotov5.Schema)
}

return &tfprotov5.GetProviderSchemaResponse{
Provider: s.ProviderSchema,
ProviderMeta: s.ProviderMetaSchema,
ResourceSchemas: s.ResourceSchemas,
DataSourceSchemas: s.DataSourceSchemas,
ServerCapabilities: s.ServerCapabilities,
}, nil
return s.GetProviderSchemaResponse, nil
}

func (s *TestServer) ImportResourceState(_ context.Context, req *tfprotov5.ImportResourceStateRequest) (*tfprotov5.ImportResourceStateResponse, error) {
Expand Down Expand Up @@ -162,20 +142,3 @@ 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
}
43 changes: 3 additions & 40 deletions internal/tf6testserver/tf6testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@ import (
var _ tfprotov6.ProviderServer = &TestServer{}

type TestServer struct {
DataSourceSchemas map[string]*tfprotov6.Schema
ProviderMetaSchema *tfprotov6.Schema
ProviderSchema *tfprotov6.Schema
ResourceSchemas map[string]*tfprotov6.Schema
ServerCapabilities *tfprotov6.ServerCapabilities

ApplyResourceChangeCalled map[string]bool

ConfigureProviderCalled bool

GetProviderSchemaCalled bool
GetProviderSchemaCalled bool
GetProviderSchemaResponse *tfprotov6.GetProviderSchemaResponse

ImportResourceStateCalled map[string]bool

Expand Down Expand Up @@ -65,22 +60,7 @@ func (s *TestServer) ConfigureProvider(_ context.Context, _ *tfprotov6.Configure

func (s *TestServer) GetProviderSchema(_ context.Context, _ *tfprotov6.GetProviderSchemaRequest) (*tfprotov6.GetProviderSchemaResponse, error) {
s.GetProviderSchemaCalled = true

if s.DataSourceSchemas == nil {
s.DataSourceSchemas = make(map[string]*tfprotov6.Schema)
}

if s.ResourceSchemas == nil {
s.ResourceSchemas = make(map[string]*tfprotov6.Schema)
}

return &tfprotov6.GetProviderSchemaResponse{
Provider: s.ProviderSchema,
ProviderMeta: s.ProviderMetaSchema,
ResourceSchemas: s.ResourceSchemas,
DataSourceSchemas: s.DataSourceSchemas,
ServerCapabilities: s.ServerCapabilities,
}, nil
return s.GetProviderSchemaResponse, nil
}

func (s *TestServer) ImportResourceState(_ context.Context, req *tfprotov6.ImportResourceStateRequest) (*tfprotov6.ImportResourceStateResponse, error) {
Expand Down Expand Up @@ -162,20 +142,3 @@ 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
}
13 changes: 9 additions & 4 deletions tf5muxserver/mux_server_ApplyResourceChange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

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

"github.com/hashicorp/terraform-plugin-mux/internal/tf5testserver"
"github.com/hashicorp/terraform-plugin-mux/tf5muxserver"
)
Expand All @@ -17,13 +18,17 @@ func TestMuxServerApplyResourceChange(t *testing.T) {

ctx := context.Background()
testServer1 := &tf5testserver.TestServer{
ResourceSchemas: map[string]*tfprotov5.Schema{
"test_resource_server1": {},
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{
ResourceSchemas: map[string]*tfprotov5.Schema{
"test_resource_server1": {},
},
},
}
testServer2 := &tf5testserver.TestServer{
ResourceSchemas: map[string]*tfprotov5.Schema{
"test_resource_server2": {},
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{
ResourceSchemas: map[string]*tfprotov5.Schema{
"test_resource_server2": {},
},
},
}

Expand Down
19 changes: 18 additions & 1 deletion tf5muxserver/mux_server_ConfigureProvider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

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

"github.com/hashicorp/terraform-plugin-mux/internal/tf5testserver"
"github.com/hashicorp/terraform-plugin-mux/tf5muxserver"
)
Expand All @@ -16,7 +17,23 @@ func TestMuxServerConfigureProvider(t *testing.T) {
t.Parallel()

ctx := context.Background()
testServers := [5]*tf5testserver.TestServer{{}, {}, {}, {}, {}}
testServers := [5]*tf5testserver.TestServer{
{
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{},
},
{
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{},
},
{
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{},
},
{
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{},
},
{
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{},
},
}

servers := []func() tfprotov5.ProviderServer{
testServers[0].ProviderServer,
Expand Down
Loading

0 comments on commit a6d7225

Please sign in to comment.