Skip to content

Commit

Permalink
handle set elements with computed properties
Browse files Browse the repository at this point in the history
  • Loading branch information
VenelinMartinov committed Oct 17, 2024
1 parent f53e80a commit 7466096
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
17 changes: 13 additions & 4 deletions pkg/tfbridge/detailed_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"slices"

"github.com/golang/glog"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
Expand Down Expand Up @@ -159,6 +160,7 @@ func (differ detailedDiffer) calculateSetHashIndexMap(path propertyPath, listVal

// Calculate the identity of each element
for i, newElem := range convertedListVal {

hash := tfs.SetHash(newElem)
identities[hash] = i
}
Expand Down Expand Up @@ -307,9 +309,12 @@ func (differ detailedDiffer) makeSetDiff(

oldIdentities := differ.calculateSetHashIndexMap(path, resource.NewArrayProperty(oldList))
newIdentities := differ.calculateSetHashIndexMap(path, resource.NewArrayProperty(newList))
inputIdentities := hashIndexMap{}

// TODO: We can not hash the inputs as they might not have the correct shape!
inputIdentities := differ.calculateSetHashIndexMap(path, resource.NewArrayProperty(newInputsList))
if !schemaContainsComputed(path, differ.tfs, differ.ps) {
// The inputs are only safe to hash if the schema has no computed properties
inputIdentities = differ.calculateSetHashIndexMap(path, resource.NewArrayProperty(newInputsList))
}

// The old indices and new inputs are the indices the engine can reference
// The new state indices need to be translated to new input indices when presenting the diff
Expand All @@ -322,8 +327,12 @@ func (differ detailedDiffer) makeSetDiff(
for hash, newIndex := range newIdentities {
if _, oldOk := oldIdentities[hash]; !oldOk {
inputIndex := inputIdentities[hash]
// TODO: make this a warning instead
contract.Assertf(inputIndex != -1, "could not find index of element in new inputs")
if inputIndex == -1 {
glog.Warningln(
"Element at path %s in new state not found in inputs, the displayed diff might be inaccurate",
path.String())
inputIndex = newIndex
}
_, oldChanged := setIndices[inputIndex]
setIndices[inputIndex] = setChangeIndex{
engineIndex: inputIndex, oldChanged: oldChanged, newStateIndex: newIndex, newChanged: true,
Expand Down
21 changes: 21 additions & 0 deletions pkg/tfbridge/property_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge/info"
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/walk"
)

// a variant of PropertyPath.Get which works on PropertyMaps
Expand Down Expand Up @@ -154,3 +155,23 @@ func willTriggerReplacementRecursive(

return replacement
}

func schemaContainsComputed(
path propertyPath, rootTFSchema shim.SchemaMap, rootPulumiSchema map[string]*info.Schema,
) bool {
computed := false
visitor := func(path walk.SchemaPath, tfs shim.Schema) {
if tfs.Computed() {
computed = true
}
}

tfs, _, err := lookupSchemas(path, rootTFSchema, rootPulumiSchema)
if err != nil {
return false
}

walk.VisitSchema(tfs, visitor)

return computed
}

0 comments on commit 7466096

Please sign in to comment.