Skip to content

Commit

Permalink
tf5muxserver+tf6muxserver: Always send ServerCapabilities.PlanDestroy
Browse files Browse the repository at this point in the history
  • Loading branch information
bflad committed Feb 2, 2023
1 parent 4c070ce commit 5231dbb
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 38 deletions.
14 changes: 0 additions & 14 deletions tf5muxserver/mux_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ type muxServer struct {
// Underlying servers for requests that should be handled by all servers
servers []tfprotov5.ProviderServer

// Mux server capabilities use a logical OR of each of the capabilities
// across all servers and is cached during server creation. Individual
// RPC handlers check against resourceCapabilities, which aligns to the
// capabilities of the server for the particular resource type.
serverCapabilities *tfprotov5.ServerCapabilities

// 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
Expand Down Expand Up @@ -73,7 +67,6 @@ func NewMuxServer(ctx context.Context, servers ...func() tfprotov5.ProviderServe
resources: make(map[string]tfprotov5.ProviderServer),
resourceCapabilities: make(map[string]*tfprotov5.ServerCapabilities),
resourceSchemas: make(map[string]*tfprotov5.Schema),
serverCapabilities: &tfprotov5.ServerCapabilities{},
}

for _, serverFunc := range servers {
Expand Down Expand Up @@ -114,13 +107,6 @@ func NewMuxServer(ctx context.Context, servers ...func() tfprotov5.ProviderServe
}
}

// Use logical OR across server capabilities.
if resp.ServerCapabilities != nil {
if resp.ServerCapabilities.PlanDestroy {
result.serverCapabilities.PlanDestroy = true
}
}

for resourceType, schema := range resp.ResourceSchemas {
if _, ok := result.resources[resourceType]; ok {
result.serverResourceSchemaDuplicates = append(result.serverResourceSchemaDuplicates, resourceType)
Expand Down
16 changes: 11 additions & 5 deletions tf5muxserver/mux_server_GetProviderSchema.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ func (s muxServer) GetProviderSchema(ctx context.Context, req *tfprotov5.GetProv
logging.MuxTrace(ctx, "serving cached schema information")

resp := &tfprotov5.GetProviderSchemaResponse{
Provider: s.providerSchema,
ResourceSchemas: s.resourceSchemas,
DataSourceSchemas: s.dataSourceSchemas,
ProviderMeta: s.providerMetaSchema,
ServerCapabilities: s.serverCapabilities,
Provider: s.providerSchema,
ResourceSchemas: s.resourceSchemas,
DataSourceSchemas: s.dataSourceSchemas,
ProviderMeta: s.providerMetaSchema,

// Always announce all ServerCapabilities. Individual capabilities are
// handled in their respective RPCs to protect downstream servers if
// they are not compatible with a capability.
ServerCapabilities: &tfprotov5.ServerCapabilities{
PlanDestroy: true,
},
}

for _, diff := range s.serverProviderSchemaDifferences {
Expand Down
15 changes: 15 additions & 0 deletions tf5muxserver/mux_server_GetProviderSchema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ func TestMuxServerGetProviderSchema(t *testing.T) {
},
},
},
expectedServerCapabilities: &tfprotov5.ServerCapabilities{
PlanDestroy: true,
},
},
"duplicate-data-source-type": {
servers: []func() tfprotov5.ProviderServer{
Expand Down Expand Up @@ -444,6 +447,9 @@ func TestMuxServerGetProviderSchema(t *testing.T) {
},
},
expectedResourceSchemas: map[string]*tfprotov5.Schema{},
expectedServerCapabilities: &tfprotov5.ServerCapabilities{
PlanDestroy: true,
},
},
"duplicate-resource-type": {
servers: []func() tfprotov5.ProviderServer{
Expand Down Expand Up @@ -471,6 +477,9 @@ func TestMuxServerGetProviderSchema(t *testing.T) {
expectedResourceSchemas: map[string]*tfprotov5.Schema{
"test_foo": {},
},
expectedServerCapabilities: &tfprotov5.ServerCapabilities{
PlanDestroy: true,
},
},
"provider-mismatch": {
servers: []func() tfprotov5.ProviderServer{
Expand Down Expand Up @@ -546,6 +555,9 @@ func TestMuxServerGetProviderSchema(t *testing.T) {
},
},
expectedResourceSchemas: map[string]*tfprotov5.Schema{},
expectedServerCapabilities: &tfprotov5.ServerCapabilities{
PlanDestroy: true,
},
},
"provider-meta-mismatch": {
servers: []func() tfprotov5.ProviderServer{
Expand Down Expand Up @@ -621,6 +633,9 @@ func TestMuxServerGetProviderSchema(t *testing.T) {
},
},
expectedResourceSchemas: map[string]*tfprotov5.Schema{},
expectedServerCapabilities: &tfprotov5.ServerCapabilities{
PlanDestroy: true,
},
},
"server-capabilities": {
servers: []func() tfprotov5.ProviderServer{
Expand Down
14 changes: 0 additions & 14 deletions tf6muxserver/mux_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ type muxServer struct {
// Underlying servers for requests that should be handled by all servers
servers []tfprotov6.ProviderServer

// Mux server capabilities use a logical OR of each of the capabilities
// across all servers and is cached during server creation. Individual
// RPC handlers check against resourceCapabilities, which aligns to the
// capabilities of the server for the particular resource type.
serverCapabilities *tfprotov6.ServerCapabilities

// 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
Expand Down Expand Up @@ -73,7 +67,6 @@ func NewMuxServer(ctx context.Context, servers ...func() tfprotov6.ProviderServe
resources: make(map[string]tfprotov6.ProviderServer),
resourceCapabilities: make(map[string]*tfprotov6.ServerCapabilities),
resourceSchemas: make(map[string]*tfprotov6.Schema),
serverCapabilities: &tfprotov6.ServerCapabilities{},
}

for _, serverFunc := range servers {
Expand Down Expand Up @@ -114,13 +107,6 @@ func NewMuxServer(ctx context.Context, servers ...func() tfprotov6.ProviderServe
}
}

// Use logical OR across server capabilities.
if resp.ServerCapabilities != nil {
if resp.ServerCapabilities.PlanDestroy {
result.serverCapabilities.PlanDestroy = true
}
}

for resourceType, schema := range resp.ResourceSchemas {
if _, ok := result.resources[resourceType]; ok {
result.serverResourceSchemaDuplicates = append(result.serverResourceSchemaDuplicates, resourceType)
Expand Down
16 changes: 11 additions & 5 deletions tf6muxserver/mux_server_GetProviderSchema.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ func (s muxServer) GetProviderSchema(ctx context.Context, req *tfprotov6.GetProv
logging.MuxTrace(ctx, "serving cached schema information")

resp := &tfprotov6.GetProviderSchemaResponse{
Provider: s.providerSchema,
ResourceSchemas: s.resourceSchemas,
DataSourceSchemas: s.dataSourceSchemas,
ProviderMeta: s.providerMetaSchema,
ServerCapabilities: s.serverCapabilities,
Provider: s.providerSchema,
ResourceSchemas: s.resourceSchemas,
DataSourceSchemas: s.dataSourceSchemas,
ProviderMeta: s.providerMetaSchema,

// Always announce all ServerCapabilities. Individual capabilities are
// handled in their respective RPCs to protect downstream servers if
// they are not compatible with a capability.
ServerCapabilities: &tfprotov6.ServerCapabilities{
PlanDestroy: true,
},
}

for _, diff := range s.serverProviderSchemaDifferences {
Expand Down
15 changes: 15 additions & 0 deletions tf6muxserver/mux_server_GetProviderSchema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ func TestMuxServerGetProviderSchema(t *testing.T) {
},
},
},
expectedServerCapabilities: &tfprotov6.ServerCapabilities{
PlanDestroy: true,
},
},
"duplicate-data-source-type": {
servers: []func() tfprotov6.ProviderServer{
Expand Down Expand Up @@ -444,6 +447,9 @@ func TestMuxServerGetProviderSchema(t *testing.T) {
},
},
expectedResourceSchemas: map[string]*tfprotov6.Schema{},
expectedServerCapabilities: &tfprotov6.ServerCapabilities{
PlanDestroy: true,
},
},
"duplicate-resource-type": {
servers: []func() tfprotov6.ProviderServer{
Expand Down Expand Up @@ -471,6 +477,9 @@ func TestMuxServerGetProviderSchema(t *testing.T) {
expectedResourceSchemas: map[string]*tfprotov6.Schema{
"test_foo": {},
},
expectedServerCapabilities: &tfprotov6.ServerCapabilities{
PlanDestroy: true,
},
},
"provider-mismatch": {
servers: []func() tfprotov6.ProviderServer{
Expand Down Expand Up @@ -546,6 +555,9 @@ func TestMuxServerGetProviderSchema(t *testing.T) {
},
},
expectedResourceSchemas: map[string]*tfprotov6.Schema{},
expectedServerCapabilities: &tfprotov6.ServerCapabilities{
PlanDestroy: true,
},
},
"provider-meta-mismatch": {
servers: []func() tfprotov6.ProviderServer{
Expand Down Expand Up @@ -621,6 +633,9 @@ func TestMuxServerGetProviderSchema(t *testing.T) {
},
},
expectedResourceSchemas: map[string]*tfprotov6.Schema{},
expectedServerCapabilities: &tfprotov6.ServerCapabilities{
PlanDestroy: true,
},
},
"server-capabilities": {
servers: []func() tfprotov6.ProviderServer{
Expand Down

0 comments on commit 5231dbb

Please sign in to comment.