Skip to content

Commit

Permalink
[dynamic] send an empty config on destroy calls (#2815)
Browse files Browse the repository at this point in the history
Similar to
pulumi/pulumi-terraform-provider#36, older
versions of terraform-plugin-sdk/v2 require an empty (non-nil)
`req.Config` field to avoid crashing.

```go
	configVal, err := msgpack.Unmarshal(req.Config.MsgPack, schemaBlock.ImpliedType())
	if err != nil {
		resp.Diagnostics = convert.AppendProtoDiag(ctx, resp.Diagnostics, err)
		return resp, nil
	}
```


([src](https://github.com/pulumi/terraform-plugin-sdk/blob/1798ae4ae71f7ffff29312a07c92d41205aff03b/helper/schema/grpc_provider.go#L926-L930))

Newer versions of SDKv2 add a
[guard](https://github.com/pulumi/terraform-plugin-sdk/blob/da29621efae69dd03dfdc540aa5f63beab1b764c/helper/schema/grpc_provider.go#L810-L822)
before this line is reached.

Fixes pulumi/pulumi-terraform-provider#55
  • Loading branch information
iwahbe authored Jan 8, 2025
1 parent 960b71c commit a0d0562
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 6 deletions.
53 changes: 49 additions & 4 deletions dynamic/log_replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@ import (
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/pulumi/providertest/pulumitest/optnewstack"
"github.com/pulumi/providertest/pulumitest/opttest"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/stretchr/testify/require"
"gotest.tools/v3/assert"

"github.com/pulumi/pulumi-terraform-bridge/v3/dynamic/internal/shim/grpcutil"
v6shim "github.com/pulumi/pulumi-terraform-bridge/v3/dynamic/internal/shim/protov6"
"github.com/pulumi/pulumi-terraform-bridge/v3/dynamic/parameterize"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/tests/pulcheck"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/tfbridge"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge/info"
)

func TestLogReplayProvider(t *testing.T) {
t.Parallel()
grpcLogs, err := os.ReadFile("./testdata/TestLogReplayProvider/grpc_log_random.json")
if err != nil {
t.Fatalf("failed to read grpc log: %v", err)
}
require.NoError(t, err)

provPlugin := grpcutil.NewLogReplayProvider("random", "0.0.1", grpcLogs)
prov := v6shim.New(provPlugin)
require.NoError(t, err)

resp, err := prov.GetProviderSchema(context.Background(), &tfprotov6.GetProviderSchemaRequest{})
require.NoError(t, err)
Expand All @@ -49,6 +50,50 @@ func TestLogReplayProvider(t *testing.T) {
"the config is is msgpack encoded, so we compare the bytes")
}

// TestConfigInDestroy asserts that a nil config is passed to SDKv2 providers on destroy.
//
// This acts as a regression test for https://github.com/pulumi/pulumi-terraform-provider/issues/55.
func TestConfigInDestroy(t *testing.T) {
t.Parallel()

logs, err := os.ReadFile("./testdata/TestConfigInDestroy/logs.json")
require.NoError(t, err)
p := makeLogReplayProvider(t, "phpipam", "0.0.1", logs)

ctx := context.Background()
prov, err := tfbridge.NewProvider(ctx, p, tfbridge.ProviderMetadata{PackageSchema: []byte(`{}`)})
require.NoError(t, err)

resp, err := prov.Delete(ctx, plugin.DeleteRequest{
ID: "12",
URN: "urn:pulumi:prod::space-ipam::phpipam:index/section:Section::section",
Name: "section",
Type: "phpipam:index/section:Section",
Inputs: resource.PropertyMap{
"name": resource.NewProperty("Section Name"),
},
Outputs: resource.PropertyMap{
"__meta": resource.NewProperty("{\"private_state\":\"bnVsbA==\"}"),
"description": resource.NewProperty(""),
"displayOrder": resource.NewProperty(0.0),
"dnsResolverId": resource.NewProperty(0.0),
"editDate": resource.NewProperty(""),
"masterSectionId": resource.NewProperty(0.0),
"name": resource.NewProperty("Section Name"),
"permissions": resource.NewProperty(""),
"phpipamSectionId": resource.NewProperty("12"),
"sectionId": resource.NewProperty(12.0),
"showSupernetOnly": resource.NewProperty(false),
"showVlanInSubnetListing": resource.NewProperty(false),
"showVrfInSubnetListing": resource.NewProperty(false),
"strictMode": resource.NewProperty(true),
"subnetOrdering": resource.NewProperty(""),
},
})
require.NoError(t, err)
assert.Equal(t, plugin.DeleteResponse{}, resp)
}

type runProvider struct {
tfprotov6.ProviderServer
name, version string
Expand Down
Loading

0 comments on commit a0d0562

Please sign in to comment.