Skip to content

Commit

Permalink
Allow proper changing of google_compute_region_instance_group_manager…
Browse files Browse the repository at this point in the history
….distribution_policy_target_shape (GoogleCloudPlatform#12050)
  • Loading branch information
askubis authored and BBBmau committed Oct 23, 2024
1 parent 7c8a589 commit f5dbff9
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -1145,24 +1145,39 @@ func flattenRegionUpdatePolicy(updatePolicy *compute.InstanceGroupManagerUpdateP
return results
}

func expandDistributionPolicy(d *schema.ResourceData) *compute.DistributionPolicy {
dpz := d.Get("distribution_policy_zones").(*schema.Set)
func expandDistributionPolicyForUpdate(d *schema.ResourceData) *compute.DistributionPolicy {
dpts := d.Get("distribution_policy_target_shape").(string)
if dpz.Len() == 0 && dpts == "" {
if dpts == "" {
return nil
}
// distributionPolicy.Zones is NOT updateable.
return &compute.DistributionPolicy{TargetShape: dpts}
}

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)
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
}

return &compute.DistributionPolicy{Zones: distributionPolicyZoneConfigs, TargetShape: dpts}
distributionPolicy := &compute.DistributionPolicy{}

if dpz.Len() > 0 {
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)
}
distributionPolicy.Zones=distributionPolicyZoneConfigs
}
if dpts != "" {
distributionPolicy.TargetShape = dpts
}
return distributionPolicy
}

func flattenDistributionPolicy(distributionPolicy *compute.DistributionPolicy) []string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"},
},
},
})
}
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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 {
Expand Down

0 comments on commit f5dbff9

Please sign in to comment.