Skip to content

Commit fbb721f

Browse files
committed
Breaking: Remove rawNames from MakeTerraformOutput & MakeTerraformOutputs
This has the same effect as 261df01, and needs the same test case adjustment. This is a "breaking change" to public functions, but we don't expect that these functions will be used outside of the bridge and we have made breaking changes to them in the past (#1642).
1 parent 1b087d3 commit fbb721f

File tree

3 files changed

+61
-37
lines changed

3 files changed

+61
-37
lines changed

pkg/tfbridge/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ func (p *Provider) Check(ctx context.Context, req *pulumirpc.CheckRequest) (*pul
810810

811811
// After all is said and done, we need to go back and return only what got populated as a diff from the origin.
812812
pinputs := MakeTerraformOutputs(
813-
ctx, p.tf, inputs, res.TF.Schema(), res.Schema.Fields, assets, false, p.supportsSecrets,
813+
ctx, p.tf, inputs, res.TF.Schema(), res.Schema.Fields, assets, p.supportsSecrets,
814814
)
815815

816816
pinputsWithSecrets := MarkSchemaSecrets(ctx, res.TF.Schema(), res.Schema.Fields,

pkg/tfbridge/schema.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ func MakeTerraformResult(
10211021
outs = obj
10221022
}
10231023

1024-
outMap := MakeTerraformOutputs(ctx, p, outs, tfs, ps, assets, false, supportsSecrets)
1024+
outMap := MakeTerraformOutputs(ctx, p, outs, tfs, ps, assets, supportsSecrets)
10251025

10261026
// If there is any Terraform metadata associated with this state, record it.
10271027
if state != nil && len(state.Meta()) != 0 {
@@ -1042,18 +1042,17 @@ func MakeTerraformOutputs(
10421042
tfs shim.SchemaMap,
10431043
ps map[string]*SchemaInfo,
10441044
assets AssetTable,
1045-
rawNames,
10461045
supportsSecrets bool,
10471046
) resource.PropertyMap {
10481047
result := make(resource.PropertyMap)
10491048

10501049
for key, value := range outs {
10511050
// First do a lookup of the name/info.
1052-
name, tfi, psi := getInfoFromTerraformName(key, tfs, ps, rawNames)
1051+
name, tfi, psi := getInfoFromTerraformName(key, tfs, ps, false)
10531052
contract.Assertf(name != "", `name != ""`)
10541053

10551054
// Next perform a translation of the value accordingly.
1056-
out := MakeTerraformOutput(ctx, p, value, tfi, psi, assets, rawNames, supportsSecrets)
1055+
out := MakeTerraformOutput(ctx, p, value, tfi, psi, assets, supportsSecrets)
10571056
//if !out.IsNull() {
10581057
result[name] = out
10591058
//}
@@ -1076,11 +1075,11 @@ func MakeTerraformOutput(
10761075
tfs shim.Schema,
10771076
ps *SchemaInfo,
10781077
assets AssetTable,
1079-
rawNames, supportsSecrets bool,
1078+
supportsSecrets bool,
10801079
) resource.PropertyValue {
10811080

10821081
buildOutput := func(p shim.Provider, v interface{},
1083-
tfs shim.Schema, ps *SchemaInfo, assets AssetTable, rawNames, supportsSecrets bool) resource.PropertyValue {
1082+
tfs shim.Schema, ps *SchemaInfo, assets AssetTable, supportsSecrets bool) resource.PropertyValue {
10841083
if assets != nil && ps != nil && ps.Asset != nil {
10851084
if asset, has := assets[ps]; has {
10861085
// if we have the value, it better actually be an asset or an archive.
@@ -1137,7 +1136,7 @@ func MakeTerraformOutput(
11371136
if err != nil || coerced == t {
11381137
return resource.NewStringProperty(t)
11391138
}
1140-
return MakeTerraformOutput(ctx, p, coerced, tfs, ps, assets, rawNames, supportsSecrets)
1139+
return MakeTerraformOutput(ctx, p, coerced, tfs, ps, assets, supportsSecrets)
11411140
case reflect.Slice:
11421141
elems := []interface{}{}
11431142
for i := 0; i < val.Len(); i++ {
@@ -1148,7 +1147,7 @@ func MakeTerraformOutput(
11481147

11491148
var arr []resource.PropertyValue
11501149
for _, elem := range elems {
1151-
arr = append(arr, MakeTerraformOutput(ctx, p, elem, tfes, pes, assets, rawNames, supportsSecrets))
1150+
arr = append(arr, MakeTerraformOutput(ctx, p, elem, tfes, pes, assets, supportsSecrets))
11521151
}
11531152
// For TypeList or TypeSet with MaxItems==1, we will have projected as a scalar nested value, so need to extract
11541153
// out the single element (or null).
@@ -1164,32 +1163,41 @@ func MakeTerraformOutput(
11641163
}
11651164
return resource.NewArrayProperty(arr)
11661165
case reflect.Map:
1167-
outs := map[string]interface{}{}
1166+
// Build a go map of output values.
1167+
outs := map[string]any{}
11681168
for _, key := range val.MapKeys() {
11691169
contract.Assertf(key.Kind() == reflect.String, "key.Kind() == reflect.String")
11701170
outs[key.String()] = val.MapIndex(key).Interface()
11711171
}
1172-
var tfflds shim.SchemaMap
1172+
11731173
if tfs != nil {
1174-
if res, isres := tfs.Elem().(shim.Resource); isres {
1175-
tfflds = res.Schema()
1174+
// This is an object, so we need key translations.
1175+
if res, ok := tfs.Elem().(shim.Resource); ok {
1176+
var psflds map[string]*SchemaInfo
1177+
if ps != nil {
1178+
psflds = ps.Fields
1179+
}
1180+
obj := MakeTerraformOutputs(ctx, p, outs,
1181+
res.Schema(), psflds, assets, supportsSecrets)
1182+
return resource.NewObjectProperty(obj)
11761183
}
11771184
}
1178-
var psflds map[string]*SchemaInfo
1179-
if ps != nil {
1180-
psflds = ps.Fields
1185+
1186+
// It's not an object, so it must be a map
1187+
obj := make(resource.PropertyMap, len(outs))
1188+
etfs, eps := elemSchemas(tfs, ps)
1189+
for k, v := range outs {
1190+
obj[resource.PropertyKey(k)] = MakeTerraformOutput(ctx, p, v,
1191+
etfs, eps, assets, supportsSecrets)
11811192
}
1182-
obj := MakeTerraformOutputs(
1183-
ctx, p, outs, tfflds, psflds, assets, rawNames || shimutil.IsOfTypeMap(tfs), supportsSecrets,
1184-
)
11851193
return resource.NewObjectProperty(obj)
11861194
default:
11871195
contract.Failf("Unexpected TF output property value: %#v with type %#T", v, v)
11881196
return resource.NewNullProperty()
11891197
}
11901198
}
11911199

1192-
output := buildOutput(p, v, tfs, ps, assets, rawNames, supportsSecrets)
1200+
output := buildOutput(p, v, tfs, ps, assets, supportsSecrets)
11931201

11941202
if tfs != nil && tfs.Sensitive() && supportsSecrets {
11951203
return resource.MakeSecret(output)

pkg/tfbridge/schema_test.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -396,10 +396,31 @@ func TestTerraformOutputsWithSecretsSupported(t *testing.T) {
396396
},
397397
{
398398
name: "object_property_value",
399+
400+
// NOTE: This input does not match the schema's type. The tfValue *should* be wrapped
401+
// in a []any{}. [MakeTerraformOutputs] handles this case, but it shouldn't need to
402+
// and tf should never return it.
399403
tfValue: map[string]interface{}{
400404
"property_a": "a",
401405
"property_b": true,
402406
},
407+
tfType: &schema.Schema{
408+
Type: shim.TypeList,
409+
Required: true,
410+
MaxItems: 1,
411+
Elem: (&schema.Resource{
412+
Schema: schema.SchemaMap{
413+
"property_a": (&schema.Schema{
414+
Type: shim.TypeString,
415+
Optional: true,
416+
}).Shim(),
417+
"property_b": (&schema.Schema{
418+
Type: shim.TypeBool,
419+
Optional: true,
420+
}).Shim(),
421+
},
422+
}).Shim(),
423+
},
403424
expect: autogold.Expect(resource.PropertyMap{resource.PropertyKey("objectPropertyValue"): resource.PropertyValue{
404425
V: resource.PropertyMap{
405426
resource.PropertyKey("propertyA"): resource.PropertyValue{
@@ -607,9 +628,8 @@ func TestTerraformOutputsWithSecretsSupported(t *testing.T) {
607628
},
608629
schemaMap,
609630
schemaInfo,
610-
nil, /* assets */
611-
false, /* useRawNames */
612-
true, /* supportsSecrets */
631+
nil, /* assets */
632+
true, /* supportsSecrets */
613633
)
614634
tt.expect.Equal(t, result)
615635
})
@@ -638,8 +658,7 @@ func TestTerraformOutputsWithSecretsUnsupported(t *testing.T) {
638658
},
639659
}),
640660
map[string]*SchemaInfo{},
641-
nil, /* assets */
642-
false, /*useRawNames*/
661+
nil, /* assets */
643662
false,
644663
)
645664
assert.Equal(t, resource.NewPropertyMapFromMap(map[string]interface{}{
@@ -1026,7 +1045,7 @@ func TestDefaults(t *testing.T) {
10261045
}
10271046
inputs, assets, err := makeTerraformInputsForConfig(olds, props, tfs, ps)
10281047
assert.NoError(t, err)
1029-
outputs := MakeTerraformOutputs(ctx, f.NewTestProvider(), inputs, tfs, ps, assets, false, true)
1048+
outputs := MakeTerraformOutputs(ctx, f.NewTestProvider(), inputs, tfs, ps, assets, true)
10301049

10311050
// sort the defaults list before the equality test below.
10321051
sortDefaultsList(outputs)
@@ -1072,7 +1091,7 @@ func TestDefaults(t *testing.T) {
10721091
assert.Equal(t, "true", inputs["x2stringxbool"])
10731092
assert.Equal(t, "1", inputs["x2stringxint"])
10741093

1075-
outputs = MakeTerraformOutputs(ctx, f.NewTestProvider(), inputs, tfs, ps, assets, false, true)
1094+
outputs = MakeTerraformOutputs(ctx, f.NewTestProvider(), inputs, tfs, ps, assets, true)
10761095

10771096
// sort the defaults list before the equality test below.
10781097
sortDefaultsList(outputs)
@@ -1141,7 +1160,7 @@ func TestDefaultsConflictsWith(t *testing.T) {
11411160

11421161
inputs, assets, err := makeTerraformInputsForConfig(olds, props, tfs, ps)
11431162
assert.NoError(t, err)
1144-
outputs := MakeTerraformOutputs(ctx, f.NewTestProvider(), inputs, tfs, ps, assets, false, true)
1163+
outputs := MakeTerraformOutputs(ctx, f.NewTestProvider(), inputs, tfs, ps, assets, true)
11451164
sortDefaultsList(outputs)
11461165

11471166
assert.Equal(t, resource.NewPropertyMapFromMap(map[string]interface{}{
@@ -1161,7 +1180,7 @@ func TestDefaultsConflictsWith(t *testing.T) {
11611180
inputs, assets, err = makeTerraformInputsForConfig(olds, props, tfs, ps)
11621181
assert.NoError(t, err)
11631182

1164-
outputs = MakeTerraformOutputs(ctx, f.NewTestProvider(), inputs, tfs, ps, assets, false, true)
1183+
outputs = MakeTerraformOutputs(ctx, f.NewTestProvider(), inputs, tfs, ps, assets, true)
11651184
sortDefaultsList(outputs)
11661185

11671186
assert.Equal(t, resource.NewPropertyMapFromMap(map[string]interface{}{
@@ -1195,7 +1214,7 @@ func TestComputedAsset(t *testing.T) {
11951214
}
11961215
inputs, assets, err := makeTerraformInputsNoDefaults(olds, props, tfs, ps)
11971216
assert.NoError(t, err)
1198-
outputs := MakeTerraformOutputs(ctx, shimv1.NewProvider(testTFProvider), inputs, tfs, ps, assets, false, true)
1217+
outputs := MakeTerraformOutputs(ctx, shimv1.NewProvider(testTFProvider), inputs, tfs, ps, assets, true)
11991218
assert.Equal(t, resource.PropertyMap{
12001219
"zzz": resource.PropertyValue{V: resource.Computed{Element: resource.PropertyValue{V: ""}}},
12011220
}, outputs)
@@ -1215,7 +1234,7 @@ func TestInvalidAsset(t *testing.T) {
12151234
}
12161235
inputs, assets, err := makeTerraformInputsNoDefaults(olds, props, tfs, ps)
12171236
assert.NoError(t, err)
1218-
outputs := MakeTerraformOutputs(ctx, shimv1.NewProvider(testTFProvider), inputs, tfs, ps, assets, false, true)
1237+
outputs := MakeTerraformOutputs(ctx, shimv1.NewProvider(testTFProvider), inputs, tfs, ps, assets, true)
12191238
assert.Equal(t, resource.PropertyMap{
12201239
"zzz": resource.NewStringProperty("invalid"),
12211240
}, outputs)
@@ -1280,8 +1299,7 @@ func TestOverridingTFSchema(t *testing.T) {
12801299
tfInputs,
12811300
tfSchema,
12821301
typeOverrides,
1283-
nil, /* assets */
1284-
false, /*useRawNames*/
1302+
nil, /* assets */
12851303
true,
12861304
)
12871305
assert.Equal(t, tfOutputs, result)
@@ -1332,7 +1350,7 @@ func TestArchiveAsAsset(t *testing.T) {
13321350
}
13331351
inputs, assets, err := makeTerraformInputsNoDefaults(olds, props, tfs, ps)
13341352
assert.NoError(t, err)
1335-
outputs := MakeTerraformOutputs(ctx, shimv1.NewProvider(testTFProvider), inputs, tfs, ps, assets, false, true)
1353+
outputs := MakeTerraformOutputs(ctx, shimv1.NewProvider(testTFProvider), inputs, tfs, ps, assets, true)
13361354
assert.True(t, arch.DeepEquals(outputs["zzz"]))
13371355
}
13381356

@@ -1701,8 +1719,7 @@ func TestStringOutputsWithSchema(t *testing.T) {
17011719
"not_a_float_value": {Type: schemav1.TypeFloat},
17021720
}),
17031721
map[string]*SchemaInfo{},
1704-
nil, /* assets */
1705-
false, /* useRawNames */
1722+
nil, /* assets */
17061723
true,
17071724
)
17081725

@@ -2760,7 +2777,6 @@ func TestOutputNumberTypes(t *testing.T) {
27602777
tfs,
27612778
map[string]*SchemaInfo{},
27622779
AssetTable{},
2763-
false,
27642780
true,
27652781
)
27662782
assert.Equal(t, resource.PropertyMap{

0 commit comments

Comments
 (0)