From 98c078f64000d6e9f28fa529591ccedd5ee41a7f Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Tue, 13 Aug 2024 21:17:13 +0000 Subject: [PATCH] add send_secondary_ip_range_if_empty (#11410) Co-authored-by: Riley Karson [upstream:4472fffc4067e62b24edb37e22f4fe61dfcc555c] Signed-off-by: Modular Magician --- go.mod | 2 +- go.sum | 4 +-- .../services/compute/compute_subnetwork.go | 32 +++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index d761565cd..b8c34d94c 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/hashicorp/hcl/v2 v2.20.1 github.com/hashicorp/terraform-json v0.22.1 github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0 - github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20240813201407-f2425b4433f6 + github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20240813211423-bff22d1f68ad github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.9.0 diff --git a/go.sum b/go.sum index f8c31ddb9..27d0f6135 100644 --- a/go.sum +++ b/go.sum @@ -190,8 +190,8 @@ github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0 h1:qHprzXy/As0rxedphECBEQAh github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0/go.mod h1:H+8tjs9TjV2w57QFVSMBQacf8k/E1XwLXGCARgViC6A= github.com/hashicorp/terraform-plugin-testing v1.5.1 h1:T4aQh9JAhmWo4+t1A7x+rnxAJHCDIYW9kXyo4sVO92c= github.com/hashicorp/terraform-plugin-testing v1.5.1/go.mod h1:dg8clO6K59rZ8w9EshBmDp1CxTIPu3yA4iaDpX1h5u0= -github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20240813201407-f2425b4433f6 h1:SLS0h0Io1bzZmjS0JGG6GRe+7Szi1t8ydbBUZXnG9EE= -github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20240813201407-f2425b4433f6/go.mod h1:IkI2dOHongwQ2RIUyitBH4rDJvYBuClAoFCheApCTpY= +github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20240813211423-bff22d1f68ad h1:4v6d8n+4QoS5BoK40yQjbTtL8M9FIaaN1O8Hvpo/Ucw= +github.com/hashicorp/terraform-provider-google-beta v1.20.1-0.20240813211423-bff22d1f68ad/go.mod h1:IkI2dOHongwQ2RIUyitBH4rDJvYBuClAoFCheApCTpY= github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI= github.com/hashicorp/terraform-registry-address v0.2.3/go.mod h1:lFHA76T8jfQteVfT7caREqguFrW3c4MFSPhZB7HHgUM= github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ= diff --git a/tfplan2cai/converters/google/resources/services/compute/compute_subnetwork.go b/tfplan2cai/converters/google/resources/services/compute/compute_subnetwork.go index 23ab830bd..f3520e50e 100644 --- a/tfplan2cai/converters/google/resources/services/compute/compute_subnetwork.go +++ b/tfplan2cai/converters/google/resources/services/compute/compute_subnetwork.go @@ -17,6 +17,7 @@ package compute import ( "context" "fmt" + "log" "net" "reflect" @@ -48,6 +49,37 @@ func IsShrinkageIpCidr(_ context.Context, old, new, _ interface{}) bool { return true } +func sendSecondaryIpRangeIfEmptyDiff(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error { + // on create, return immediately as we don't need to determine if the value is empty or not + if diff.Id() == "" { + return nil + } + + sendZero := diff.Get("send_secondary_ip_range_if_empty").(bool) + if !sendZero { + return nil + } + + configSecondaryIpRange := diff.GetRawConfig().GetAttr("secondary_ip_range") + if !configSecondaryIpRange.IsKnown() { + return nil + } + configValueIsEmpty := configSecondaryIpRange.IsNull() || configSecondaryIpRange.LengthInt() == 0 + + stateSecondaryIpRange := diff.GetRawState().GetAttr("secondary_ip_range") + if !stateSecondaryIpRange.IsKnown() { + return nil + } + stateValueIsEmpty := stateSecondaryIpRange.IsNull() || stateSecondaryIpRange.LengthInt() == 0 + + if configValueIsEmpty && !stateValueIsEmpty { + log.Printf("[DEBUG] setting secondary_ip_range to newly empty") + diff.SetNew("secondary_ip_range", make([]interface{}, 0)) + } + + return nil +} + const ComputeSubnetworkAssetType string = "compute.googleapis.com/Subnetwork" func ResourceConverterComputeSubnetwork() cai.ResourceConverter {