diff --git a/config/common/common.go b/config/common/common.go index 08e43f25ed..eca60ead77 100644 --- a/config/common/common.go +++ b/config/common/common.go @@ -16,6 +16,7 @@ import ( xpresource "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/crossplane/upjet/pkg/config" "github.com/crossplane/upjet/pkg/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" jsoniter "github.com/json-iterator/go" "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" @@ -142,3 +143,31 @@ func RemovePolicyVersion(p string) (string, error) { r, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(m) return string(r), errors.Wrap(err, "failed to marshal the policy map as JSON") } + +// RemoveDiffIfEmpty removes supplied keys from Terraform diffs, if old and new +// values for the key are empty (""). It is probably safe to delete all such +// keys in the diff. Until we decide to do so, we'll specify the keys, to be on +// the safe side. +func RemoveDiffIfEmpty(keys []string) config.CustomDiff { //nolint:gocyclo // The implementation is pretty straightforward as of this writing. + return func(diff *terraform.InstanceDiff, state *terraform.InstanceState, config *terraform.ResourceConfig) (*terraform.InstanceDiff, error) { + // Skip diff customization on create + if state == nil || state.Empty() { + return diff, nil + } + if config == nil { + return nil, errors.New("resource config cannot be nil") + } + // Skip no diff or destroy diffs + if diff == nil || diff.Empty() || diff.Destroy || diff.Attributes == nil { + return diff, nil + } + + for _, key := range keys { + if diff.Attributes[key] != nil && diff.Attributes[key].Old == "" && diff.Attributes[key].New == "" { + delete(diff.Attributes, key) + } + } + + return diff, nil + } +} diff --git a/config/ec2/config.go b/config/ec2/config.go index e9c9eae023..a1059c7856 100644 --- a/config/ec2/config.go +++ b/config/ec2/config.go @@ -53,6 +53,9 @@ func Configure(p *config.Provider) { //nolint:gocyclo "cpu_threads_per_core", }, } + r.TerraformCustomDiff = common.RemoveDiffIfEmpty([]string{ + "volume_tags.%", + }) config.MoveToStatus(r.TerraformResource, "security_groups") }) p.AddResourceConfigurator("aws_eip", func(r *config.Resource) { diff --git a/go.mod b/go.mod index 8c05c8ac38..e581bfe5e2 100644 --- a/go.mod +++ b/go.mod @@ -439,4 +439,4 @@ require ( // copied from the Terraform provider replace github.com/hashicorp/terraform-plugin-log => github.com/gdavison/terraform-plugin-log v0.0.0-20230928191232-6c653d8ef8fb -replace github.com/hashicorp/terraform-provider-aws => github.com/upbound/terraform-provider-aws v0.0.0-20241203141151-997a60ec5360 +replace github.com/hashicorp/terraform-provider-aws => github.com/upbound/terraform-provider-aws v0.0.0-20250102143213-c0eb96154778 diff --git a/go.sum b/go.sum index e2f89981ec..6cd2734875 100644 --- a/go.sum +++ b/go.sum @@ -865,8 +865,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tmccombs/hcl2json v0.3.3 h1:+DLNYqpWE0CsOQiEZu+OZm5ZBImake3wtITYxQ8uLFQ= github.com/tmccombs/hcl2json v0.3.3/go.mod h1:Y2chtz2x9bAeRTvSibVRVgbLJhLJXKlUeIvjeVdnm4w= -github.com/upbound/terraform-provider-aws v0.0.0-20241203141151-997a60ec5360 h1:HLrTYw0kB74C7JMNM5dUe0h7V2nzvdSMzciWmsJCUvQ= -github.com/upbound/terraform-provider-aws v0.0.0-20241203141151-997a60ec5360/go.mod h1:7l6VnUvAF/LY5jcEABkfHuK8gAkOODwTT1TcGpy+tl0= +github.com/upbound/terraform-provider-aws v0.0.0-20250102143213-c0eb96154778 h1:RxW17Zn9BdCvSSeB7LGOZCoJJYO1d4VDvrdPmSRP5G8= +github.com/upbound/terraform-provider-aws v0.0.0-20250102143213-c0eb96154778/go.mod h1:7l6VnUvAF/LY5jcEABkfHuK8gAkOODwTT1TcGpy+tl0= github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI= github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=