-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix custom diffs for fields with diff supression (#3032)
Fix custom diffs for fields with diff impression: - existing `ComputedIfAnyAttributeChanged` merged with `ComputedIfAnyAttributeChangedWithSuppressDiff` - changed the way how the suppress diff is invoked from within `ComputedIfAnyAttributeChanged` (ResourceData and string values for old and new) - fix the failing tests (behavior was fixed) - add test to validate the behavior in warehouse
- Loading branch information
1 parent
751239b
commit 2499602
Showing
34 changed files
with
357 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package sdkv2enhancements | ||
|
||
import ( | ||
"reflect" | ||
"unsafe" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
// CreateResourceDataFromResourceDiff allows to create schema.ResourceData out of schema.ResourceDiff. | ||
// Unfortunately, schema.resourceDiffer is an unexported interface, and functions like schema.SchemaDiffSuppressFunc do not use the interface but a concrete implementation. | ||
// One use case in which we needed to have schema.ResourceData from schema.ResourceDiff was to run schema.SchemaDiffSuppressFunc from inside schema.CustomizeDiffFunc. | ||
// This implementation uses: | ||
// - schema.InternalMap that exposes hidden schema.schemaMap (a wrapper over map[string]*schema.Schema) | ||
// - (m schemaMap) Data method allowing to create schema.ResourceData from terraform.InstanceState and terraform.InstanceDiff | ||
// - terraform.InstanceState and terraform.InstanceDiff are unexported in schema.ResourceDiff, so we get them using reflection | ||
func CreateResourceDataFromResourceDiff(resourceSchema schema.InternalMap, diff *schema.ResourceDiff) (*schema.ResourceData, bool) { | ||
unexportedState := reflect.ValueOf(diff).Elem().FieldByName("state") | ||
stateFromResourceDiff := reflect.NewAt(unexportedState.Type(), unsafe.Pointer(unexportedState.UnsafeAddr())).Elem().Interface() | ||
unexportedDiff := reflect.ValueOf(diff).Elem().FieldByName("diff") | ||
diffFroResourceDif := reflect.NewAt(unexportedDiff.Type(), unsafe.Pointer(unexportedDiff.UnsafeAddr())).Elem().Interface() | ||
castState, ok := stateFromResourceDiff.(*terraform.InstanceState) | ||
if !ok { | ||
return nil, false | ||
} | ||
castDiff, ok := diffFroResourceDif.(*terraform.InstanceDiff) | ||
if !ok { | ||
return nil, false | ||
} | ||
resourceData, err := resourceSchema.Data(castState, castDiff) | ||
if err != nil { | ||
return nil, false | ||
} | ||
return resourceData, true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.