Repository: pulumi/pulumi-terraform-bridge
Pull request #326 (2021-02-10 05:35:58)

Ensure PreConfigureCallback gets called
It appears this was regressed by https://github.com/pulumi/pulumi-terraform-bridge/pull/242, which partially reverted https://github.com/pulumi/pulumi-terraform-bridge/pull/225 but left in some code that doesn't appear to be correct without the other reverted code (it's not immediately clear why it's correct even with that original code - but at least it seems intentional as part of the original change).

Also added tests to ensure that PreConfigureCallback is called.

Fixes pulumi/pulumi-aws#1316.

github-actions[bot]: Diff for [pulumi-random](https://github.com/pulumi-bot/pulumi-random/compare/fc1299a..ffc6738) with merge commit 9facda3fb5563985e64b0ad4478d115972db60b0
github-actions[bot]: Diff for [pulumi-azuread](https://github.com/pulumi-bot/pulumi-azuread/compare/29e6413..0ecaa66) with merge commit 9facda3fb5563985e64b0ad4478d115972db60b0
github-actions[bot]: Diff for [pulumi-gcp](https://github.com/pulumi-bot/pulumi-gcp/compare/e38e52d8..38c23573) with merge commit 9facda3fb5563985e64b0ad4478d115972db60b0
github-actions[bot]: Diff for [pulumi-azure](https://github.com/pulumi-bot/pulumi-azure/compare/fd6a4d490..41daed41b) with merge commit 9facda3fb5563985e64b0ad4478d115972db60b0
github-actions[bot]: Diff for [pulumi-aws](https://github.com/pulumi-bot/pulumi-aws/compare/8b64e6f91..31905df71) with merge commit 9facda3fb5563985e64b0ad4478d115972db60b0

Patch:
diff --git a/pkg/tfbridge/provider.go b/pkg/tfbridge/provider.go
index e29fbecf..d6e97633 100644
--- a/pkg/tfbridge/provider.go
+++ b/pkg/tfbridge/provider.go
@@ -524,24 +524,21 @@ func (p *Provider) Configure(ctx context.Context,
 		return nil, errors.Wrap(err, "could not marshal config state")
 	}
 
-	if req.Variables == nil {
-		// We only follow this path if the CLI hasbn't already called CheckConfig
-		if p.info.PreConfigureCallback != nil {
-			if err = p.info.PreConfigureCallback(vars, config); err != nil {
-				return nil, err
-			}
-		}
-
-		missingKeys, validationErrors := validateProviderConfig(ctx, p, config)
-		if len(missingKeys) > 0 {
-			err = rpcerror.WithDetails(
-				rpcerror.New(codes.InvalidArgument, "required configuration keys were missing"),
-				&pulumirpc.ConfigureErrorMissingKeys{MissingKeys: missingKeys})
+	if p.info.PreConfigureCallback != nil {
+		if err = p.info.PreConfigureCallback(vars, config); err != nil {
 			return nil, err
 		}
-		if validationErrors != nil {
-			return nil, validationErrors
-		}
+	}
+
+	missingKeys, validationErrors := validateProviderConfig(ctx, p, config)
+	if len(missingKeys) > 0 {
+		err = rpcerror.WithDetails(
+			rpcerror.New(codes.InvalidArgument, "required configuration keys were missing"),
+			&pulumirpc.ConfigureErrorMissingKeys{MissingKeys: missingKeys})
+		return nil, err
+	}
+	if validationErrors != nil {
+		return nil, validationErrors
 	}
 
 	// Now actually attempt to do the configuring and return its resulting error (if any).


Test patch:
diff --git a/pkg/tfbridge/provider_test.go b/pkg/tfbridge/provider_test.go
index c63b37e8..457efd95 100644
--- a/pkg/tfbridge/provider_test.go
+++ b/pkg/tfbridge/provider_test.go
@@ -2,6 +2,7 @@ package tfbridge
 
 import (
 	"context"
+	"errors"
 	"sort"
 	"testing"
 
@@ -518,3 +519,35 @@ func TestProviderCheckV2(t *testing.T) {
 	assert.Equal(t, "Required attribute is not set", failures[2].Reason)
 	assert.Equal(t, "", failures[2].Property)
 }
+
+func testProviderPreConfigureCallback(t *testing.T, provider *Provider) {
+	expectedErr := errors.New("failedToPreConfigure")
+	provider.info = ProviderInfo{
+		PreConfigureCallback: func(vars resource.PropertyMap, config shim.ResourceConfig) error {
+			return expectedErr
+		},
+	}
+	_, err := provider.Configure(context.Background(), &pulumirpc.ConfigureRequest{
+		Variables: map[string]string{
+			"foo:config:bar": "invalid",
+		},
+	})
+	assert.Equal(t, expectedErr, err)
+}
+
+func TestProviderPreConfigureCallbackV1(t *testing.T) {
+	provider := &Provider{
+		tf:     shimv1.NewProvider(testTFProvider),
+		config: shimv1.NewSchemaMap(testTFProvider.Schema),
+	}
+	testProviderPreConfigureCallback(t, provider)
+
+}
+
+func TestProviderPreConfigureCallbackV2(t *testing.T) {
+	provider := &Provider{
+		tf:     shimv2.NewProvider(testTFProviderV2),
+		config: shimv2.NewSchemaMap(testTFProviderV2.Schema),
+	}
+	testProviderPreConfigureCallback(t, provider)
+}