Skip to content

Commit

Permalink
Add PF provider options and accurate previews flag (#2663)
Browse files Browse the repository at this point in the history
This change adds provider options for the PF bridge, similar to the
SDKv2 bridge provider options, as well as a flag for accurate bridge
previews.

I've also enabled the flag for all diff tests.
  • Loading branch information
VenelinMartinov authored Nov 25, 2024
1 parent 005a1f5 commit 3ec60c3
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 26 deletions.
23 changes: 12 additions & 11 deletions dynamic/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ func TestStacktraceDisplayed(t *testing.T) {
}

func TestPrimitiveTypes(t *testing.T) {
t.Parallel()
skipWindows(t)
// TODO[pulumi/pulumi-terraform-bridge#2517]: fix once accurate bridge previews are enabled by default
t.Setenv("PULUMI_TF_BRIDGE_ACCURATE_BRIDGE_PREVIEW", "true")

ctx := context.Background()

Expand Down Expand Up @@ -114,26 +115,26 @@ func TestPrimitiveTypes(t *testing.T) {
t.Run("check", assertGRPCCall(grpc.Check, &pulumirpc.CheckRequest{
Urn: urn,
News: inputs(),
}))
}, noParallel))

t.Run("create(preview)", assertGRPCCall(grpc.Create, &pulumirpc.CreateRequest{
Preview: true,
Urn: urn,
Properties: inputs(),
}))
}, noParallel))

t.Run("create", assertGRPCCall(grpc.Create, &pulumirpc.CreateRequest{
Urn: urn,
Properties: inputs(),
}))
}, noParallel))

t.Run("diff(none)", assertGRPCCall(grpc.Diff, &pulumirpc.DiffRequest{
Id: "example-id-0",
Urn: urn,
Olds: outputs(),
News: inputs(),
OldInputs: inputs(),
}))
}, noParallel))

t.Run("diff(some)", assertGRPCCall(grpc.Diff, &pulumirpc.DiffRequest{
Id: "example-id-1",
Expand All @@ -155,7 +156,7 @@ func TestPrimitiveTypes(t *testing.T) {
"attrStringDefaultOverridden": resource.NewProperty("overridden"),
}),
OldInputs: inputs(),
}))
}, noParallel))

t.Run("diff(all)", assertGRPCCall(grpc.Diff, &pulumirpc.DiffRequest{
Id: "example-id-2",
Expand All @@ -177,13 +178,13 @@ func TestPrimitiveTypes(t *testing.T) {
"attrNumberRequired": resource.NewProperty(12.3456789),
}),
OldInputs: inputs(),
}))
}, noParallel))

t.Run("delete", assertGRPCCall(grpc.Delete, &pulumirpc.DeleteRequest{
Id: "example-id-delete",
Urn: urn,
Properties: outputs(),
}))
}, noParallel))

t.Run("update", assertGRPCCall(grpc.Update, &pulumirpc.UpdateRequest{
Id: "example-update-id",
Expand All @@ -192,18 +193,18 @@ func TestPrimitiveTypes(t *testing.T) {
News: marshal(with(outputProps(), resource.PropertyMap{
"attrBoolRequired": resource.NewProperty(false),
})),
}))
}, noParallel))

t.Run("read", assertGRPCCall(grpc.Read, &pulumirpc.ReadRequest{
Id: "example-read-id",
Urn: urn,
Properties: outputs(),
}))
}, noParallel))

t.Run("import", assertGRPCCall(grpc.Read, &pulumirpc.ReadRequest{
Id: "example-read-id",
Urn: urn,
}))
}, noParallel))
}

func TestConfigure(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/pf/tests/genrandom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func TestGenRandom(t *testing.T) {

t.Run(trace, func(t *testing.T) {
p := testprovider.RandomProvider()
p.EnableAccurateBridgePreview = true
server, err := newProviderServer(t, p)
require.NoError(t, err)
testutils.ReplayFile(t, server, trace)
Expand Down
4 changes: 3 additions & 1 deletion pkg/pf/tests/genupdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func TestGenUpdates(t *testing.T) {
t.Parallel()
trace := "testdata/updateprogram.json"

server, err := newProviderServer(t, testprovider.SyntheticTestBridgeProvider())
info := testprovider.SyntheticTestBridgeProvider()
info.EnableAccurateBridgePreview = true
server, err := newProviderServer(t, info)
require.NoError(t, err)
testutils.ReplayFile(t, server, trace)
}
2 changes: 1 addition & 1 deletion pkg/pf/tests/internal/cross-tests/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func Diff(t T, res pb.Resource, tfConfig1, tfConfig2 map[string]cty.Value, optio
require.NoError(t, err)
t.Logf("Pulumi.yaml:\n%s", string(bytes))

pt, err := pulcheck.PulCheck(t, bridgedProvider(prov), string(bytes))
pt, err := pulcheck.PulCheck(t, bridgedProvider(prov, bridgedProviderOpts{enableAccurateBridgePreview: true}), string(bytes))
require.NoError(t, err)
pt.Up(t)

Expand Down
15 changes: 10 additions & 5 deletions pkg/pf/tests/internal/cross-tests/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,19 @@ func skipUnlessLinux(t T) {
}
}

func bridgedProvider(prov *providerbuilder.Provider) info.Provider {
type bridgedProviderOpts struct {
enableAccurateBridgePreview bool
}

func bridgedProvider(prov *providerbuilder.Provider, opts bridgedProviderOpts) info.Provider {
shimProvider := tfbridge.ShimProvider(prov)

provider := tfbridge0.ProviderInfo{
P: shimProvider,
Name: prov.TypeName,
Version: prov.Version,
MetadataInfo: &tfbridge0.MetadataInfo{},
P: shimProvider,
Name: prov.TypeName,
Version: prov.Version,
MetadataInfo: &tfbridge0.MetadataInfo{},
EnableAccurateBridgePreview: opts.enableAccurateBridgePreview,
}

provider.MustComputeTokens(tokens.SingleModule(prov.TypeName, "index", tokens.MakeStandard(prov.TypeName)))
Expand Down
8 changes: 6 additions & 2 deletions pkg/pf/tests/provider_diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func TestEmptyTestresDiff(t *testing.T) {
// Test removing an optional input.
func TestOptionRemovalTestresDiff(t *testing.T) {
t.Parallel()
server, err := newProviderServer(t, testprovider.SyntheticTestBridgeProvider())
info := testprovider.SyntheticTestBridgeProvider()
info.EnableAccurateBridgePreview = true
server, err := newProviderServer(t, info)
require.NoError(t, err)
testCase := `
{
Expand Down Expand Up @@ -270,7 +272,9 @@ func TestSetNestedObjectAdded(t *testing.T) {

func TestSetNestedObjectAddedOtherDiff(t *testing.T) {
t.Parallel()
server, err := newProviderServer(t, testprovider.SyntheticTestBridgeProvider())
info := testprovider.SyntheticTestBridgeProvider()
info.EnableAccurateBridgePreview = true
server, err := newProviderServer(t, info)
require.NoError(t, err)
testCase := `
{
Expand Down
34 changes: 34 additions & 0 deletions pkg/pf/tfbridge/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package tfbridge
import (
"context"
"fmt"
"os"

"github.com/blang/semver"
pfprovider "github.com/hashicorp/terraform-plugin-framework/provider"
Expand All @@ -27,6 +28,7 @@ import (
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
Expand All @@ -42,6 +44,31 @@ import (
"github.com/pulumi/pulumi-terraform-bridge/v3/unstable/logging"
)

type providerOptions struct {
enableAccurateBridgePreview bool
}

type providerOption func(providerOptions) (providerOptions, error)

func withAccurateBridgePreview() providerOption {
return func(opts providerOptions) (providerOptions, error) {
opts.enableAccurateBridgePreview = true
return opts, nil
}
}

func getProviderOptions(opts []providerOption) (providerOptions, error) {
res := providerOptions{}
for _, o := range opts {
var err error
res, err = o(res)
if err != nil {
return res, err
}
}
return res, nil
}

// Provider implements the Pulumi resource provider operations for any
// Terraform plugin built with Terraform Plugin Framework.
//
Expand All @@ -66,6 +93,7 @@ type provider struct {
lastKnownProviderConfig resource.PropertyMap

schemaOnlyProvider shim.Provider
providerOpts []providerOption
}

var _ pl.ProviderWithContext = &provider{}
Expand Down Expand Up @@ -148,6 +176,11 @@ func newProviderWithContext(ctx context.Context, info tfbridge.ProviderInfo,
}
}

opts := []providerOption{}
if info.EnableAccurateBridgePreview || cmdutil.IsTruthy(os.Getenv("PULUMI_TF_BRIDGE_ACCURATE_BRIDGE_PREVIEW")) {
opts = append(opts, withAccurateBridgePreview())
}

p := &provider{
tfServer: server6,
info: info,
Expand All @@ -160,6 +193,7 @@ func newProviderWithContext(ctx context.Context, info tfbridge.ProviderInfo,
version: semverVersion,
schemaOnlyProvider: info.P,
parameterize: meta.XParamaterize,
providerOpts: opts,
}

return configencoding.New(p), nil
Expand Down
19 changes: 13 additions & 6 deletions pkg/pf/tfbridge/provider_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,24 @@ func (p *provider) DiffWithContext(
changes = plugin.DiffSome
}

pluginDetailedDiff, err := calculateDetailedDiff(ctx, &rh, priorState, plannedStateValue, checkedInputs)
if err != nil {
return plugin.DiffResult{}, err
}

diffResult := plugin.DiffResult{
Changes: changes,
ReplaceKeys: replaceKeys,
ChangedKeys: changedKeys,
DeleteBeforeReplace: deleteBeforeReplace,
DetailedDiff: pluginDetailedDiff,
}

providerOpts, err := getProviderOptions(p.providerOpts)
if err != nil {
return plugin.DiffResult{}, err
}

if providerOpts.enableAccurateBridgePreview {
pluginDetailedDiff, err := calculateDetailedDiff(ctx, &rh, priorState, plannedStateValue, checkedInputs)
if err != nil {
return plugin.DiffResult{}, err
}
diffResult.DetailedDiff = pluginDetailedDiff
}

// TODO[pulumi/pulumi-terraform-bridge#824] StableKeys
Expand Down

0 comments on commit 3ec60c3

Please sign in to comment.