diff --git a/pkg/tfbridge/provider.go b/pkg/tfbridge/provider.go index 787f0f217..533949205 100755 --- a/pkg/tfbridge/provider.go +++ b/pkg/tfbridge/provider.go @@ -360,7 +360,6 @@ func ignoredTokens(info *info.Provider) map[string]bool { // initResourceMaps creates maps from Pulumi types and tokens to Terraform resource type. func (p *Provider) initResourceMaps() { - ignoredTokens := ignoredTokens(&p.info) // Fetch a list of all resource types handled by this provider and make a map. @@ -1130,9 +1129,8 @@ func (p *Provider) Create(ctx context.Context, req *pulumirpc.CreateRequest) (*p } // To get Terraform to create a new resource, the ID must be blank and existing state must be empty (since the // resource does not exist yet), and the diff object should have no old state and all of the new state. - config, assets, err := makeTerraformConfigWithOpts( + config, assets, err := MakeTerraformConfig( ctx, p, props, res.TF.Schema(), res.Schema.Fields, - makeTerraformConfigOpts{}, ) if err != nil { return nil, errors.Wrapf(err, "preparing %s's new property inputs", urn) diff --git a/pkg/tfbridge/schema.go b/pkg/tfbridge/schema.go index bafb34996..c8eefa3d3 100644 --- a/pkg/tfbridge/schema.go +++ b/pkg/tfbridge/schema.go @@ -298,9 +298,8 @@ type conversionContext struct { } type makeTerraformInputsOptions struct { - DisableDefaults bool - DisableTFDefaults bool - EnableMaxItemsOneDefaults bool + DisableDefaults bool + DisableTFDefaults bool } func makeTerraformInputsWithOptions( @@ -319,13 +318,12 @@ func makeTerraformInputsWithOptions( } cctx := &conversionContext{ - Ctx: ctx, - ComputeDefaultOptions: cdOptions, - ProviderConfig: config, - ApplyDefaults: !opts.DisableDefaults, - ApplyTFDefaults: !opts.DisableTFDefaults, - ApplyMaxItemsOneDefaults: opts.EnableMaxItemsOneDefaults, - Assets: AssetTable{}, + Ctx: ctx, + ComputeDefaultOptions: cdOptions, + ProviderConfig: config, + ApplyDefaults: !opts.DisableDefaults, + ApplyTFDefaults: !opts.DisableTFDefaults, + Assets: AssetTable{}, } inputs, err := cctx.makeTerraformInputs(olds, news, tfs, ps) @@ -1232,22 +1230,6 @@ func MakeTerraformOutput( return output } -type makeTerraformConfigOpts struct { - EnableMaxItemsOneDefaults bool -} - -func makeTerraformConfigWithOpts(ctx context.Context, p *Provider, m resource.PropertyMap, - tfs shim.SchemaMap, ps map[string]*SchemaInfo, opts makeTerraformConfigOpts) (shim.ResourceConfig, AssetTable, error) { - inputs, assets, err := makeTerraformInputsWithOptions(ctx, nil, p.configValues, nil, m, tfs, ps, - makeTerraformInputsOptions{ - DisableDefaults: true, DisableTFDefaults: true, EnableMaxItemsOneDefaults: opts.EnableMaxItemsOneDefaults}, - ) - if err != nil { - return nil, nil, err - } - return MakeTerraformConfigFromInputs(ctx, p.tf, inputs), assets, nil -} - // MakeTerraformConfig creates a Terraform config map, used in state and diff calculations, from a Pulumi property map. func MakeTerraformConfig(ctx context.Context, p *Provider, m resource.PropertyMap, tfs shim.SchemaMap, ps map[string]*SchemaInfo) (shim.ResourceConfig, AssetTable, error) { @@ -1307,12 +1289,11 @@ func MakeTerraformConfigFromInputs( return p.NewResourceConfig(ctx, raw) } -type makeTerraformStateOpts struct { - EnableMaxItemsOneDefaults bool -} - -func makeTerraformStateWithOpts( - ctx context.Context, res Resource, id string, m resource.PropertyMap, opts makeTerraformStateOpts, +// MakeTerraformState converts a Pulumi property bag into its Terraform equivalent. This requires +// flattening everything and serializing individual properties as strings. This is a little awkward, but it's how +// Terraform represents resource properties (schemas are simply sugar on top). +func MakeTerraformState( + ctx context.Context, res Resource, id string, m resource.PropertyMap, ) (shim.InstanceState, error) { // Parse out any metadata from the state. var meta map[string]interface{} @@ -1332,7 +1313,8 @@ func makeTerraformStateWithOpts( // ints, to represent numbers. inputs, _, err := makeTerraformInputsWithOptions(ctx, nil, nil, nil, m, res.TF.Schema(), res.Schema.Fields, makeTerraformInputsOptions{ - DisableDefaults: true, DisableTFDefaults: true, EnableMaxItemsOneDefaults: opts.EnableMaxItemsOneDefaults}) + DisableDefaults: true, DisableTFDefaults: true, + }) if err != nil { return nil, err } @@ -1340,15 +1322,6 @@ func makeTerraformStateWithOpts( return res.TF.InstanceState(id, inputs, meta) } -// MakeTerraformState converts a Pulumi property bag into its Terraform equivalent. This requires -// flattening everything and serializing individual properties as strings. This is a little awkward, but it's how -// Terraform represents resource properties (schemas are simply sugar on top). -func MakeTerraformState( - ctx context.Context, res Resource, id string, m resource.PropertyMap, -) (shim.InstanceState, error) { - return makeTerraformStateWithOpts(ctx, res, id, m, makeTerraformStateOpts{}) -} - // UnmarshalTerraformState unmarshals a Terraform instance state from an RPC property map. func UnmarshalTerraformState( ctx context.Context, r Resource, id string, m *pbstruct.Struct, l string, @@ -1366,7 +1339,7 @@ func UnmarshalTerraformState( return nil, err } - return makeTerraformStateWithOpts(ctx, r, id, props, makeTerraformStateOpts{}) + return MakeTerraformState(ctx, r, id, props) } // IsMaxItemsOne returns true if the schema/info pair represents a TypeList or TypeSet which should project @@ -1387,7 +1360,8 @@ func IsMaxItemsOne(tfs shim.Schema, info *SchemaInfo) bool { // getInfoFromTerraformName does a map lookup to find the Pulumi name and schema info, if any. func getInfoFromTerraformName(key string, tfs shim.SchemaMap, ps map[string]*SchemaInfo, rawName bool) (resource.PropertyKey, - shim.Schema, *SchemaInfo) { + shim.Schema, *SchemaInfo, +) { info := ps[key] var name string diff --git a/pkg/tfbridge/schema_test.go b/pkg/tfbridge/schema_test.go index 8247d49b6..39974f239 100644 --- a/pkg/tfbridge/schema_test.go +++ b/pkg/tfbridge/schema_test.go @@ -59,13 +59,6 @@ func makeTerraformInputsForConfig(olds, news resource.PropertyMap, makeTerraformInputsOptions{}) } -func makeTerraformInputsForCreate(olds, news resource.PropertyMap, - tfs shim.SchemaMap, ps map[string]*SchemaInfo, -) (map[string]interface{}, AssetTable, error) { - return makeTerraformInputsWithOptions(context.Background(), nil, nil, olds, news, tfs, ps, - makeTerraformInputsOptions{DisableDefaults: true, DisableTFDefaults: true, EnableMaxItemsOneDefaults: true}) -} - func makeTerraformInput(v resource.PropertyValue, tfs shim.Schema, ps *SchemaInfo) (interface{}, error) { ctx := &conversionContext{} return ctx.makeTerraformInput("v", resource.PropertyValue{}, v, tfs, ps) @@ -233,7 +226,6 @@ func TestMakeTerraformInputsWithMaxItemsOne(t *testing.T) { news resource.PropertyMap expectedNoDefaults map[string]interface{} expectedForConfig map[string]interface{} - expectedForCreate map[string]interface{} }{ "empty-olds": { olds: resource.PropertyMap{}, @@ -248,9 +240,6 @@ func TestMakeTerraformInputsWithMaxItemsOne(t *testing.T) { expectedForConfig: map[string]interface{}{ "__defaults": []interface{}{}, }, - expectedForCreate: map[string]interface{}{ - "element": []interface{}{}, - }, }, "non-empty-olds": { olds: resource.PropertyMap{ @@ -272,9 +261,6 @@ func TestMakeTerraformInputsWithMaxItemsOne(t *testing.T) { expectedForConfig: map[string]interface{}{ "__defaults": []interface{}{}, }, - expectedForCreate: map[string]interface{}{ - "element": []interface{}{}, - }, }, "non-missing-news": { olds: resource.PropertyMap{ @@ -299,9 +285,6 @@ func TestMakeTerraformInputsWithMaxItemsOne(t *testing.T) { "__defaults": []interface{}{}, "element": []interface{}{"el"}, }, - expectedForCreate: map[string]interface{}{ - "element": []interface{}{"el"}, - }, }, } @@ -316,11 +299,6 @@ func TestMakeTerraformInputsWithMaxItemsOne(t *testing.T) { tt.olds, tt.news, tfs, nil /* ps */) require.NoError(t, err) assert.Equal(t, tt.expectedForConfig, resultForConfig) - - resultForCreate, _, err := makeTerraformInputsForCreate( - tt.olds, tt.news, tfs, nil /* ps */) - require.NoError(t, err) - assert.Equal(t, tt.expectedForCreate, resultForCreate) }) } } @@ -711,8 +689,8 @@ func TestMetaProperties(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, props) - state, err = makeTerraformStateWithOpts( - ctx, Resource{TF: res, Schema: &ResourceInfo{}}, state.ID(), props, makeTerraformStateOpts{}) + state, err = MakeTerraformState( + ctx, Resource{TF: res, Schema: &ResourceInfo{}}, state.ID(), props) assert.NoError(t, err) assert.NotNil(t, state) @@ -726,8 +704,8 @@ func TestMetaProperties(t *testing.T) { // Delete the resource's meta-property and ensure that we re-populate its schema version. delete(props, metaKey) - state, err = makeTerraformStateWithOpts( - ctx, Resource{TF: res, Schema: &ResourceInfo{}}, state.ID(), props, makeTerraformStateOpts{}) + state, err = MakeTerraformState( + ctx, Resource{TF: res, Schema: &ResourceInfo{}}, state.ID(), props) assert.NoError(t, err) assert.NotNil(t, state) @@ -765,8 +743,8 @@ func TestMetaProperties(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, props) - state, err = makeTerraformStateWithOpts( - ctx, Resource{TF: res, Schema: &ResourceInfo{}}, state.ID(), props, makeTerraformStateOpts{}) + state, err = MakeTerraformState( + ctx, Resource{TF: res, Schema: &ResourceInfo{}}, state.ID(), props) assert.NoError(t, err) assert.NotNil(t, state) @@ -793,8 +771,8 @@ func TestInjectingCustomTimeouts(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, props) - state, err = makeTerraformStateWithOpts( - ctx, Resource{TF: res, Schema: &ResourceInfo{}}, state.ID(), props, makeTerraformStateOpts{}) + state, err = MakeTerraformState( + ctx, Resource{TF: res, Schema: &ResourceInfo{}}, state.ID(), props) assert.NoError(t, err) assert.NotNil(t, state) @@ -808,8 +786,8 @@ func TestInjectingCustomTimeouts(t *testing.T) { // Delete the resource's meta-property and ensure that we re-populate its schema version. delete(props, metaKey) - state, err = makeTerraformStateWithOpts( - ctx, Resource{TF: res, Schema: &ResourceInfo{}}, state.ID(), props, makeTerraformStateOpts{}) + state, err = MakeTerraformState( + ctx, Resource{TF: res, Schema: &ResourceInfo{}}, state.ID(), props) assert.NoError(t, err) assert.NotNil(t, state) @@ -850,8 +828,8 @@ func TestInjectingCustomTimeouts(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, props) - state, err = makeTerraformStateWithOpts( - ctx, Resource{TF: res, Schema: &ResourceInfo{}}, state.ID(), props, makeTerraformStateOpts{}) + state, err = MakeTerraformState( + ctx, Resource{TF: res, Schema: &ResourceInfo{}}, state.ID(), props) assert.NoError(t, err) assert.NotNil(t, state) @@ -906,8 +884,8 @@ func TestResultAttributesRoundTrip(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, props) - state, err = makeTerraformStateWithOpts( - ctx, Resource{TF: res, Schema: &ResourceInfo{}}, state.ID(), props, makeTerraformStateOpts{}) + state, err = MakeTerraformState( + ctx, Resource{TF: res, Schema: &ResourceInfo{}}, state.ID(), props) assert.NoError(t, err) assert.NotNil(t, state)