Skip to content

Commit

Permalink
address reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
VenelinMartinov committed Dec 17, 2024
1 parent ebe0d43 commit f3e38a9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
32 changes: 18 additions & 14 deletions pkg/tfbridge/detailed_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,10 @@ func isTypeShapeMismatched(val resource.PropertyValue, propType shim.ValueType)

func containsReplace(m map[string]*pulumirpc.PropertyDiff) bool {
for _, v := range m {
if v.GetKind() == pulumirpc.PropertyDiff_UPDATE_REPLACE {
return true
}
if v.GetKind() == pulumirpc.PropertyDiff_ADD_REPLACE {
return true
}
if v.GetKind() == pulumirpc.PropertyDiff_DELETE_REPLACE {
switch v.GetKind() {
case pulumirpc.PropertyDiff_UPDATE_REPLACE,
pulumirpc.PropertyDiff_ADD_REPLACE,
pulumirpc.PropertyDiff_DELETE_REPLACE:
return true
}
}
Expand Down Expand Up @@ -509,6 +506,11 @@ func (differ detailedDiffer) makeDetailedDiffPropertyMap(

// MakeDetailedDiffV2 is the main entry point for calculating the detailed diff.
// This is an internal function that should not be used outside of the pulumi-terraform-bridge.
//
// The `replaceOverride` parameter is used to override the replace behavior of the detailed diff.
// If true, the diff will be overridden to return a replace.
// If false, the diff will be overridden to not return a replace.
// If nil, the detailed diff will be returned as is.
func MakeDetailedDiffV2(
ctx context.Context,
tfs shim.SchemaMap,
Expand All @@ -533,17 +535,19 @@ func MakeDetailedDiffV2(
res := differ.makeDetailedDiffPropertyMap(priorProps, props)

if replaceOverride != nil {
if containsReplace(res) && !*replaceOverride {
if *replaceOverride {
// We need to make sure there is a replace.
if !containsReplace(res) {
// We use the internal __meta property to trigger a replace when we have failed to
// determine the correct detailed diff for it.
res[metaKey] = &pulumirpc.PropertyDiff{Kind: pulumirpc.PropertyDiff_UPDATE_REPLACE}
}
} else {
// There is an override for no replaces, so ensure we don't have any.
for k, v := range res {
res[k] = demoteToNoReplace(v)
}
}

if !containsReplace(res) && *replaceOverride {
// We use the internal __meta property to trigger a replace when we have failed to
// determine the correct detailed diff for it.
res["__meta"] = &pulumirpc.PropertyDiff{Kind: pulumirpc.PropertyDiff_UPDATE_REPLACE}
}
}

return res
Expand Down
22 changes: 22 additions & 0 deletions pkg/tfbridge/detailed_diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2858,3 +2858,25 @@ func TestDemoteToNoReplace(t *testing.T) {
diff = &pulumirpc.PropertyDiff{Kind: pulumirpc.PropertyDiff_UPDATE}
require.Equal(t, demoteToNoReplace(diff), &pulumirpc.PropertyDiff{Kind: pulumirpc.PropertyDiff_UPDATE})
}

func TestContainsReplace(t *testing.T) {
t.Parallel()

require.True(t, containsReplace(map[string]*pulumirpc.PropertyDiff{
"foo": {Kind: pulumirpc.PropertyDiff_UPDATE_REPLACE},
}))

require.True(t, containsReplace(map[string]*pulumirpc.PropertyDiff{
"foo": {Kind: pulumirpc.PropertyDiff_ADD_REPLACE},
}))

require.True(t, containsReplace(map[string]*pulumirpc.PropertyDiff{
"foo": {Kind: pulumirpc.PropertyDiff_DELETE_REPLACE},
}))

require.False(t, containsReplace(map[string]*pulumirpc.PropertyDiff{
"foo": {Kind: pulumirpc.PropertyDiff_UPDATE},
}))

require.False(t, containsReplace(map[string]*pulumirpc.PropertyDiff{}))
}

0 comments on commit f3e38a9

Please sign in to comment.