Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tf5muxserver+tf6muxserver: Defer combined server implementation errors until GetProviderSchema RPC #121

Merged
merged 2 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/121.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
tf5muxserver+tf6muxserver: Deferred combined server implementation errors until `GetProviderSchema` RPC to prevent confusing Terraform CLI plugin startup errors
```
40 changes: 26 additions & 14 deletions tf5muxserver/mux_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ type muxServer struct {
// Underlying servers for requests that should be handled by all servers
servers []tfprotov5.ProviderServer

// Server errors are cached during server creation and deferred until
// the GetProviderSchema call. This is to prevent confusing Terraform CLI
// errors about the plugin not starting properly, which do not display the
// stderr output from the plugin.
//
// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/77
// Reference: https://github.com/hashicorp/terraform/issues/31363
serverDataSourceSchemaDuplicates []string
serverProviderSchemaDifferences []string
serverProviderMetaSchemaDifferences []string
serverResourceSchemaDuplicates []string

// Schemas are cached during server creation
dataSourceSchemas map[string]*tfprotov5.Schema
providerMetaSchema *tfprotov5.Schema
Expand Down Expand Up @@ -79,36 +91,36 @@ func NewMuxServer(ctx context.Context, servers ...func() tfprotov5.ProviderServe

if resp.Provider != nil {
if result.providerSchema != nil && !schemaEquals(resp.Provider, result.providerSchema) {
return result, fmt.Errorf("got a different provider schema across servers. Provider schemas must be identical across providers. Diff: %s", schemaDiff(resp.Provider, result.providerSchema))
result.serverProviderSchemaDifferences = append(result.serverProviderSchemaDifferences, schemaDiff(resp.Provider, result.providerSchema))
} else {
result.providerSchema = resp.Provider
}

result.providerSchema = resp.Provider
}

if resp.ProviderMeta != nil {
if result.providerMetaSchema != nil && !schemaEquals(resp.ProviderMeta, result.providerMetaSchema) {
return result, fmt.Errorf("got a different provider meta schema across servers. Provider metadata schemas must be identical across providers. Diff: %s", schemaDiff(resp.ProviderMeta, result.providerMetaSchema))
result.serverProviderMetaSchemaDifferences = append(result.serverProviderMetaSchemaDifferences, schemaDiff(resp.ProviderMeta, result.providerMetaSchema))
} else {
result.providerMetaSchema = resp.ProviderMeta
}

result.providerMetaSchema = resp.ProviderMeta
}

for resourceType, schema := range resp.ResourceSchemas {
if _, ok := result.resources[resourceType]; ok {
return result, fmt.Errorf("resource %q is implemented by multiple servers; only one implementation allowed", resourceType)
result.serverResourceSchemaDuplicates = append(result.serverResourceSchemaDuplicates, resourceType)
} else {
result.resources[resourceType] = server
result.resourceSchemas[resourceType] = schema
}

result.resources[resourceType] = server
result.resourceSchemas[resourceType] = schema
}

for dataSourceType, schema := range resp.DataSourceSchemas {
if _, ok := result.dataSources[dataSourceType]; ok {
return result, fmt.Errorf("data source %q is implemented by multiple servers; only one implementation allowed", dataSourceType)
result.serverDataSourceSchemaDuplicates = append(result.serverDataSourceSchemaDuplicates, dataSourceType)
} else {
result.dataSources[dataSourceType] = server
result.dataSourceSchemas[dataSourceType] = schema
}

result.dataSources[dataSourceType] = server
result.dataSourceSchemas[dataSourceType] = schema
}

result.servers = append(result.servers, server)
Expand Down
46 changes: 44 additions & 2 deletions tf5muxserver/mux_server_GetProviderSchema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,52 @@ func (s muxServer) GetProviderSchema(ctx context.Context, req *tfprotov5.GetProv
ctx = logging.RpcContext(ctx, rpc)
logging.MuxTrace(ctx, "serving cached schema information")

return &tfprotov5.GetProviderSchemaResponse{
resp := &tfprotov5.GetProviderSchemaResponse{
Provider: s.providerSchema,
ResourceSchemas: s.resourceSchemas,
DataSourceSchemas: s.dataSourceSchemas,
ProviderMeta: s.providerMetaSchema,
}, nil
}

for _, diff := range s.serverProviderSchemaDifferences {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{
Summary: "Invalid Provider Server Combination",
Detail: "The combined provider has differing provider schema implementations across providers. " +
"Provider schemas must be identical across providers. " +
"This is always an issue in the provider implementation and should be reported to the provider developers.\n\n" +
"Provider schema difference: " + diff,
})
}

for _, diff := range s.serverProviderMetaSchemaDifferences {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{
Summary: "Invalid Provider Server Combination",
Detail: "The combined provider has differing provider meta schema implementations across providers. " +
"Provider meta schemas must be identical across providers. " +
"This is always an issue in the provider implementation and should be reported to the provider developers.\n\n" +
"Provider meta schema difference: " + diff,
})
}

for _, dataSourceType := range s.serverDataSourceSchemaDuplicates {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{
Summary: "Invalid Provider Server Combination",
Detail: "The combined provider has multiple implementations of the same data source type across providers. " +
"Data source types must be implemented by only one provider. " +
"This is always an issue in the provider implementation and should be reported to the provider developers.\n\n" +
"Duplicate data source type: " + dataSourceType,
})
}

for _, resourceType := range s.serverResourceSchemaDuplicates {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{
Summary: "Invalid Provider Server Combination",
Detail: "The combined provider has multiple implementations of the same resource type across providers. " +
"Resource types must be implemented by only one provider. " +
"This is always an issue in the provider implementation and should be reported to the provider developers.\n\n" +
"Duplicate resource type: " + resourceType,
})
}

return resp, nil
}
209 changes: 209 additions & 0 deletions tf5muxserver/mux_server_GetProviderSchema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestMuxServerGetProviderSchema(t *testing.T) {
testCases := map[string]struct {
servers []func() tfprotov5.ProviderServer
expectedDataSourceSchemas map[string]*tfprotov5.Schema
expectedDiagnostics []*tfprotov5.Diagnostic
expectedProviderSchema *tfprotov5.Schema
expectedProviderMetaSchema *tfprotov5.Schema
expectedResourceSchemas map[string]*tfprotov5.Schema
Expand Down Expand Up @@ -416,6 +417,210 @@ func TestMuxServerGetProviderSchema(t *testing.T) {
},
},
},
"duplicate-data-source-type": {
servers: []func() tfprotov5.ProviderServer{
(&tf5testserver.TestServer{
DataSourceSchemas: map[string]*tfprotov5.Schema{
"test_foo": {},
},
}).ProviderServer,
(&tf5testserver.TestServer{
DataSourceSchemas: map[string]*tfprotov5.Schema{
"test_foo": {},
},
}).ProviderServer,
},
expectedDataSourceSchemas: map[string]*tfprotov5.Schema{
"test_foo": {},
},
expectedDiagnostics: []*tfprotov5.Diagnostic{
{
Summary: "Invalid Provider Server Combination",
Detail: "The combined provider has multiple implementations of the same data source type across providers. " +
"Data source types must be implemented by only one provider. " +
"This is always an issue in the provider implementation and should be reported to the provider developers.\n\n" +
"Duplicate data source type: test_foo",
},
},
expectedResourceSchemas: map[string]*tfprotov5.Schema{},
},
"duplicate-resource-type": {
servers: []func() tfprotov5.ProviderServer{
(&tf5testserver.TestServer{
ResourceSchemas: map[string]*tfprotov5.Schema{
"test_foo": {},
},
}).ProviderServer,
(&tf5testserver.TestServer{
ResourceSchemas: map[string]*tfprotov5.Schema{
"test_foo": {},
},
}).ProviderServer,
},
expectedDataSourceSchemas: map[string]*tfprotov5.Schema{},
expectedDiagnostics: []*tfprotov5.Diagnostic{
{
Summary: "Invalid Provider Server Combination",
Detail: "The combined provider has multiple implementations of the same resource type across providers. " +
"Resource types must be implemented by only one provider. " +
"This is always an issue in the provider implementation and should be reported to the provider developers.\n\n" +
"Duplicate resource type: test_foo",
},
},
expectedResourceSchemas: map[string]*tfprotov5.Schema{
"test_foo": {},
},
},
"provider-mismatch": {
servers: []func() tfprotov5.ProviderServer{
(&tf5testserver.TestServer{
ProviderSchema: &tfprotov5.Schema{
Block: &tfprotov5.SchemaBlock{
Attributes: []*tfprotov5.SchemaAttribute{
{
Name: "testattribute1",
Type: tftypes.String,
Required: true,
},
},
},
},
}).ProviderServer,
(&tf5testserver.TestServer{
ProviderSchema: &tfprotov5.Schema{
Block: &tfprotov5.SchemaBlock{
Attributes: []*tfprotov5.SchemaAttribute{
{
Name: "testattribute2",
Type: tftypes.String,
Required: true,
},
},
},
},
}).ProviderServer,
},
expectedDataSourceSchemas: map[string]*tfprotov5.Schema{},
expectedDiagnostics: []*tfprotov5.Diagnostic{
{
Summary: "Invalid Provider Server Combination",
Detail: "The combined provider has differing provider schema implementations across providers. " +
"Provider schemas must be identical across providers. " +
"This is always an issue in the provider implementation and should be reported to the provider developers.\n\n" +
"Provider schema difference: " + cmp.Diff(
&tfprotov5.Schema{
Block: &tfprotov5.SchemaBlock{
Attributes: []*tfprotov5.SchemaAttribute{
{
Name: "testattribute2",
Type: tftypes.String,
Required: true,
},
},
},
},
&tfprotov5.Schema{
Block: &tfprotov5.SchemaBlock{
Attributes: []*tfprotov5.SchemaAttribute{
{
Name: "testattribute1",
Type: tftypes.String,
Required: true,
},
},
},
},
),
},
},
expectedProviderSchema: &tfprotov5.Schema{
Block: &tfprotov5.SchemaBlock{
Attributes: []*tfprotov5.SchemaAttribute{
{
Name: "testattribute1",
Type: tftypes.String,
Required: true,
},
},
},
},
expectedResourceSchemas: map[string]*tfprotov5.Schema{},
},
"provider-meta-mismatch": {
servers: []func() tfprotov5.ProviderServer{
(&tf5testserver.TestServer{
ProviderMetaSchema: &tfprotov5.Schema{
Block: &tfprotov5.SchemaBlock{
Attributes: []*tfprotov5.SchemaAttribute{
{
Name: "testattribute1",
Type: tftypes.String,
Required: true,
},
},
},
},
}).ProviderServer,
(&tf5testserver.TestServer{
ProviderMetaSchema: &tfprotov5.Schema{
Block: &tfprotov5.SchemaBlock{
Attributes: []*tfprotov5.SchemaAttribute{
{
Name: "testattribute2",
Type: tftypes.String,
Required: true,
},
},
},
},
}).ProviderServer,
},
expectedDataSourceSchemas: map[string]*tfprotov5.Schema{},
expectedDiagnostics: []*tfprotov5.Diagnostic{
{
Summary: "Invalid Provider Server Combination",
Detail: "The combined provider has differing provider meta schema implementations across providers. " +
"Provider meta schemas must be identical across providers. " +
"This is always an issue in the provider implementation and should be reported to the provider developers.\n\n" +
"Provider meta schema difference: " + cmp.Diff(
&tfprotov5.Schema{
Block: &tfprotov5.SchemaBlock{
Attributes: []*tfprotov5.SchemaAttribute{
{
Name: "testattribute2",
Type: tftypes.String,
Required: true,
},
},
},
},
&tfprotov5.Schema{
Block: &tfprotov5.SchemaBlock{
Attributes: []*tfprotov5.SchemaAttribute{
{
Name: "testattribute1",
Type: tftypes.String,
Required: true,
},
},
},
},
),
},
},
expectedProviderMetaSchema: &tfprotov5.Schema{
Block: &tfprotov5.SchemaBlock{
Attributes: []*tfprotov5.SchemaAttribute{
{
Name: "testattribute1",
Type: tftypes.String,
Required: true,
},
},
},
},
expectedResourceSchemas: map[string]*tfprotov5.Schema{},
},
}

for name, testCase := range testCases {
Expand All @@ -440,6 +645,10 @@ func TestMuxServerGetProviderSchema(t *testing.T) {
t.Errorf("data source schemas didn't match expectations: %s", diff)
}

if diff := cmp.Diff(resp.Diagnostics, testCase.expectedDiagnostics); diff != "" {
t.Errorf("diagnostics didn't match expectations: %s", diff)
}

if diff := cmp.Diff(resp.Provider, testCase.expectedProviderSchema); diff != "" {
t.Errorf("provider schema didn't match expectations: %s", diff)
}
Expand Down
Loading