From ca3dc379726d13279628b6b2df839e4766b9969d Mon Sep 17 00:00:00 2001 From: Adam Skubis Date: Fri, 18 Oct 2024 08:05:47 +0000 Subject: [PATCH 1/4] Allow proper changing of google_compute_region_instance_group_manager.distribution_policy_target_shape --- ...pute_region_instance_group_manager.go.tmpl | 24 +++--- ...region_instance_group_manager_test.go.tmpl | 73 +++++++++++++++++-- 2 files changed, 81 insertions(+), 16 deletions(-) diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager.go.tmpl b/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager.go.tmpl index 1889ca104bfb..17f7388a398e 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager.go.tmpl +++ b/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager.go.tmpl @@ -1152,17 +1152,19 @@ func expandDistributionPolicy(d *schema.ResourceData) *compute.DistributionPolic return nil } - distributionPolicyZoneConfigs := make([]*compute.DistributionPolicyZoneConfiguration, 0, dpz.Len()) - for _, raw := range dpz.List() { - data := raw.(string) - distributionPolicyZoneConfig := compute.DistributionPolicyZoneConfiguration{ - Zone: "zones/" + data, - } - - distributionPolicyZoneConfigs = append(distributionPolicyZoneConfigs, &distributionPolicyZoneConfig) - } - - return &compute.DistributionPolicy{Zones: distributionPolicyZoneConfigs, TargetShape: dpts} + if d.HasChange("distribution_policy_zones") { + distributionPolicyZoneConfigs := make([]*compute.DistributionPolicyZoneConfiguration, 0, dpz.Len()) + for _, raw := range dpz.List() { + data := raw.(string) + distributionPolicyZoneConfig := compute.DistributionPolicyZoneConfiguration{ + Zone: "zones/" + data, + } + + distributionPolicyZoneConfigs = append(distributionPolicyZoneConfigs, &distributionPolicyZoneConfig) + } + return &compute.DistributionPolicy{Zones: distributionPolicyZoneConfigs, TargetShape: dpts} + } + return &compute.DistributionPolicy{TargetShape: dpts} } func flattenDistributionPolicy(distributionPolicy *compute.DistributionPolicy) []string { diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager_test.go.tmpl b/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager_test.go.tmpl index 680a4a629675..96aee647feff 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager_test.go.tmpl +++ b/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager_test.go.tmpl @@ -312,7 +312,16 @@ func TestAccRegionInstanceGroupManager_distributionPolicy(t *testing.T) { CheckDestroy: testAccCheckRegionInstanceGroupManagerDestroyProducer(t), Steps: []resource.TestStep{ { - Config: testAccRegionInstanceGroupManager_distributionPolicy(template, igm, zones), + Config: testAccRegionInstanceGroupManager_distributionPolicyEmpty(template, igm), + }, + { + ResourceName: "google_compute_region_instance_group_manager.igm-basic", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"status"}, + }, + { + Config: testAccRegionInstanceGroupManager_distributionPolicy(template, igm), }, { ResourceName: "google_compute_region_instance_group_manager.igm-basic", @@ -329,6 +338,15 @@ func TestAccRegionInstanceGroupManager_distributionPolicy(t *testing.T) { ImportStateVerify: true, ImportStateVerifyIgnore: []string{"status"}, }, + { + Config: testAccRegionInstanceGroupManager_distributionPolicyEmpty(template, igm), + }, + { + ResourceName: "google_compute_region_instance_group_manager.igm-basic", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"status"}, + }, }, }) } @@ -1135,7 +1153,7 @@ resource "google_compute_region_instance_group_manager" "igm-basic" { `, primaryTemplate, canaryTemplate, igm) } -func testAccRegionInstanceGroupManager_distributionPolicy(template, igm string, zones []string) string { +func testAccRegionInstanceGroupManager_distributionPolicyEmpty(template, igm string) string { return fmt.Sprintf(` data "google_compute_image" "my_image" { family = "debian-11" @@ -1169,8 +1187,6 @@ resource "google_compute_region_instance_group_manager" "igm-basic" { base_instance_name = "tf-test-igm-basic" region = "us-central1" target_size = 2 - distribution_policy_zones = ["%s"] - distribution_policy_target_shape = "ANY" update_policy { instance_redistribution_type = "NONE" @@ -1180,7 +1196,54 @@ resource "google_compute_region_instance_group_manager" "igm-basic" { max_unavailable_fixed = 6 } } -`, template, igm, strings.Join(zones, "\",\"")) +`, template, igm) +} + +func testAccRegionInstanceGroupManager_distributionPolicy(template, igm string) string { + return fmt.Sprintf(` +data "google_compute_image" "my_image" { + family = "debian-11" + project = "debian-cloud" +} + +resource "google_compute_instance_template" "igm-basic" { + name = "%s" + machine_type = "e2-medium" + can_ip_forward = false + tags = ["foo", "bar"] + disk { + source_image = data.google_compute_image.my_image.self_link + auto_delete = true + boot = true + } + network_interface { + network = "default" + } +} + +resource "google_compute_region_instance_group_manager" "igm-basic" { + description = "Terraform test instance group manager" + name = "%s" + + version { + instance_template = google_compute_instance_template.igm-basic.self_link + name = "primary" + } + + base_instance_name = "tf-test-igm-basic" + region = "us-central1" + target_size = 2 + distribution_policy_target_shape = "BALANCED" + + update_policy { + instance_redistribution_type = "NONE" + type = "OPPORTUNISTIC" + minimal_action = "REPLACE" + max_surge_fixed = 0 + max_unavailable_fixed = 6 + } +} +`, template, igm) } func testAccRegionInstanceGroupManager_distributionPolicyUpdate(template, igm string, zones []string) string { From f2437629770b0422043f4d1bd7a3da40515abba3 Mon Sep 17 00:00:00 2001 From: askubis Date: Fri, 18 Oct 2024 10:29:07 +0200 Subject: [PATCH 2/4] Update resource_compute_region_instance_group_manager.go.tmpl fix indent --- .../resource_compute_region_instance_group_manager.go.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager.go.tmpl b/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager.go.tmpl index 17f7388a398e..05f720b76e40 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager.go.tmpl +++ b/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager.go.tmpl @@ -1164,7 +1164,7 @@ func expandDistributionPolicy(d *schema.ResourceData) *compute.DistributionPolic } return &compute.DistributionPolicy{Zones: distributionPolicyZoneConfigs, TargetShape: dpts} } - return &compute.DistributionPolicy{TargetShape: dpts} + return &compute.DistributionPolicy{TargetShape: dpts} } func flattenDistributionPolicy(distributionPolicy *compute.DistributionPolicy) []string { From 5899201108ee513f251a2030a6132a785a4584ba Mon Sep 17 00:00:00 2001 From: askubis Date: Fri, 18 Oct 2024 13:50:44 +0200 Subject: [PATCH 3/4] Update resource_compute_region_instance_group_manager.go.tmpl fix indent even more --- .../resource_compute_region_instance_group_manager.go.tmpl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager.go.tmpl b/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager.go.tmpl index 05f720b76e40..0fa6e3e4302b 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager.go.tmpl +++ b/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager.go.tmpl @@ -1162,9 +1162,9 @@ func expandDistributionPolicy(d *schema.ResourceData) *compute.DistributionPolic distributionPolicyZoneConfigs = append(distributionPolicyZoneConfigs, &distributionPolicyZoneConfig) } - return &compute.DistributionPolicy{Zones: distributionPolicyZoneConfigs, TargetShape: dpts} - } - return &compute.DistributionPolicy{TargetShape: dpts} + return &compute.DistributionPolicy{Zones: distributionPolicyZoneConfigs, TargetShape: dpts} + } + return &compute.DistributionPolicy{TargetShape: dpts} } func flattenDistributionPolicy(distributionPolicy *compute.DistributionPolicy) []string { From a5020b92aaef4f612e3cc5eaad49c4f3e5953f50 Mon Sep 17 00:00:00 2001 From: Adam Skubis Date: Mon, 21 Oct 2024 08:19:38 +0000 Subject: [PATCH 4/4] never send distributionPolicy.zones in update request (as it's immutable, and the field is marked in TFas ForceNew --- ...pute_region_instance_group_manager.go.tmpl | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager.go.tmpl b/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager.go.tmpl index 0fa6e3e4302b..3c036f3a1e86 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager.go.tmpl +++ b/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_group_manager.go.tmpl @@ -633,7 +633,7 @@ func resourceComputeRegionInstanceGroupManagerCreate(d *schema.ResourceData, met UpdatePolicy: expandRegionUpdatePolicy(d.Get("update_policy").([]interface{})), InstanceLifecyclePolicy: expandInstanceLifecyclePolicy(d.Get("instance_lifecycle_policy").([]interface{})), AllInstancesConfig: expandAllInstancesConfig(nil, d.Get("all_instances_config").([]interface{})), - DistributionPolicy: expandDistributionPolicy(d), + DistributionPolicy: expandDistributionPolicyForCreate(d), StatefulPolicy: expandStatefulPolicy(d), {{- if ne $.TargetVersionName "ga" }} Params: expandInstanceGroupManagerParams(d), @@ -907,7 +907,7 @@ func resourceComputeRegionInstanceGroupManagerUpdate(d *schema.ResourceData, met } if d.HasChange("distribution_policy_target_shape") { - updatedManager.DistributionPolicy = expandDistributionPolicy(d) + updatedManager.DistributionPolicy = expandDistributionPolicyForUpdate(d) change = true } @@ -1145,14 +1145,24 @@ func flattenRegionUpdatePolicy(updatePolicy *compute.InstanceGroupManagerUpdateP return results } -func expandDistributionPolicy(d *schema.ResourceData) *compute.DistributionPolicy { +func expandDistributionPolicyForUpdate(d *schema.ResourceData) *compute.DistributionPolicy { + dpts := d.Get("distribution_policy_target_shape").(string) + if dpts == "" { + return nil + } + // distributionPolicy.Zones is NOT updateable. + return &compute.DistributionPolicy{TargetShape: dpts} +} + +func expandDistributionPolicyForCreate(d *schema.ResourceData) *compute.DistributionPolicy { dpz := d.Get("distribution_policy_zones").(*schema.Set) dpts := d.Get("distribution_policy_target_shape").(string) if dpz.Len() == 0 && dpts == "" { return nil } + distributionPolicy := &compute.DistributionPolicy{} - if d.HasChange("distribution_policy_zones") { + if dpz.Len() > 0 { distributionPolicyZoneConfigs := make([]*compute.DistributionPolicyZoneConfiguration, 0, dpz.Len()) for _, raw := range dpz.List() { data := raw.(string) @@ -1162,9 +1172,12 @@ func expandDistributionPolicy(d *schema.ResourceData) *compute.DistributionPolic distributionPolicyZoneConfigs = append(distributionPolicyZoneConfigs, &distributionPolicyZoneConfig) } - return &compute.DistributionPolicy{Zones: distributionPolicyZoneConfigs, TargetShape: dpts} + distributionPolicy.Zones=distributionPolicyZoneConfigs } - return &compute.DistributionPolicy{TargetShape: dpts} + if dpts != "" { + distributionPolicy.TargetShape = dpts + } + return distributionPolicy } func flattenDistributionPolicy(distributionPolicy *compute.DistributionPolicy) []string {