Skip to content

Commit

Permalink
add redistribution_type to update_policy for RIGM
Browse files Browse the repository at this point in the history
  • Loading branch information
emilymye committed Aug 20, 2019
1 parent fc0d197 commit be8e476
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,14 @@ func resourceComputeRegionInstanceGroupManager() *schema.Resource {
Optional: true,
ValidateFunc: validation.IntBetween(0, 3600),
},
<% unless version == 'ga' -%>
"instance_redistribution_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"PROACTIVE", "NONE", ""}, false),
DiffSuppressFunc: emptyOrDefaultStringSuppress("PROACTIVE"),
},
<% end -%>
},
},
},
Expand Down Expand Up @@ -359,7 +367,7 @@ func resourceComputeRegionInstanceGroupManagerCreate(d *schema.ResourceData, met
<% unless version == 'ga' -%>
AutoHealingPolicies: expandAutoHealingPolicies(d.Get("auto_healing_policies").([]interface{})),
Versions: expandVersions(d.Get("version").([]interface{})),
UpdatePolicy: expandUpdatePolicy(d.Get("update_policy").([]interface{})),
UpdatePolicy: expandRegionUpdatePolicy(d.Get("update_policy").([]interface{})),
<% end -%>
DistributionPolicy: expandDistributionPolicy(d.Get("distribution_policy_zones").(*schema.Set)),
// Force send TargetSize to allow size of 0.
Expand Down Expand Up @@ -486,7 +494,7 @@ func resourceComputeRegionInstanceGroupManagerRead(d *schema.ResourceData, meta
if err := d.Set("version", flattenVersions(manager.Versions)); err != nil {
return err
}
if err := d.Set("update_policy", flattenUpdatePolicy(manager.UpdatePolicy)); err != nil {
if err := d.Set("update_policy", flattenRegionUpdatePolicy(manager.UpdatePolicy)); err != nil {
return fmt.Errorf("Error setting update_policy in state: %s", err.Error())
}
<% end -%>
Expand Down Expand Up @@ -656,7 +664,7 @@ func resourceComputeRegionInstanceGroupManagerUpdate(d *schema.ResourceData, met
}

if d.HasChange("update_policy") {
updatedManager.UpdatePolicy = expandUpdatePolicy(d.Get("update_policy").([]interface{}))
updatedManager.UpdatePolicy = expandRegionUpdatePolicy(d.Get("update_policy").([]interface{}))
change = true
}

Expand Down Expand Up @@ -755,6 +763,83 @@ func resourceComputeRegionInstanceGroupManagerDelete(d *schema.ResourceData, met
return nil
}

<% unless version == 'ga' -%>
func expandRegionUpdatePolicy(configured []interface{}) *computeBeta.InstanceGroupManagerUpdatePolicy {
updatePolicy := &computeBeta.InstanceGroupManagerUpdatePolicy{}

for _, raw := range configured {
data := raw.(map[string]interface{})

updatePolicy.MinimalAction = data["minimal_action"].(string)
updatePolicy.Type = data["type"].(string)
updatePolicy.InstanceRedistributionType = data["instance_redistribution_type"].(string)

// percent and fixed values are conflicting
// when the percent values are set, the fixed values will be ignored
if v := data["max_surge_percent"]; v.(int) > 0 {
updatePolicy.MaxSurge = &computeBeta.FixedOrPercent{
Percent: int64(v.(int)),
NullFields: []string{"Fixed"},
}
} else {
updatePolicy.MaxSurge = &computeBeta.FixedOrPercent{
Fixed: int64(data["max_surge_fixed"].(int)),
// allow setting this value to 0
ForceSendFields: []string{"Fixed"},
NullFields: []string{"Percent"},
}
}

if v := data["max_unavailable_percent"]; v.(int) > 0 {
updatePolicy.MaxUnavailable = &computeBeta.FixedOrPercent{
Percent: int64(v.(int)),
NullFields: []string{"Fixed"},
}
} else {
updatePolicy.MaxUnavailable = &computeBeta.FixedOrPercent{
Fixed: int64(data["max_unavailable_fixed"].(int)),
// allow setting this value to 0
ForceSendFields: []string{"Fixed"},
NullFields: []string{"Percent"},
}
}

if v, ok := data["min_ready_sec"]; ok {
updatePolicy.MinReadySec = int64(v.(int))
}
}
return updatePolicy
}

func flattenRegionUpdatePolicy(updatePolicy *computeBeta.InstanceGroupManagerUpdatePolicy) []map[string]interface{} {
results := []map[string]interface{}{}
if updatePolicy != nil {
up := map[string]interface{}{}
if updatePolicy.MaxSurge != nil {
up["max_surge_fixed"] = updatePolicy.MaxSurge.Fixed
up["max_surge_percent"] = updatePolicy.MaxSurge.Percent
} else {
up["max_surge_fixed"] = 0
up["max_surge_percent"] = 0
}
if updatePolicy.MaxUnavailable != nil {
up["max_unavailable_fixed"] = updatePolicy.MaxUnavailable.Fixed
up["max_unavailable_percent"] = updatePolicy.MaxUnavailable.Percent
} else {
up["max_unavailable_fixed"] = 0
up["max_unavailable_percent"] = 0
}
up["min_ready_sec"] = updatePolicy.MinReadySec
up["minimal_action"] = updatePolicy.MinimalAction
up["type"] = updatePolicy.Type
up["instance_redistribution_type"] = updatePolicy.InstanceRedistributionType

results = append(results, up)
}
return results
}
<% end -%>

func expandDistributionPolicy(configured *schema.Set) *computeBeta.DistributionPolicy {
if configured.Len() == 0 {
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ func TestAccRegionInstanceGroupManager_rollingUpdatePolicy(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccRegionInstanceGroupManager_rollingUpdatePolicySetToDefault(igm),
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
{
Config: testAccRegionInstanceGroupManager_rollingUpdatePolicy2(igm),
},
Expand Down Expand Up @@ -1030,6 +1035,67 @@ resource "google_compute_region_instance_group_manager" "igm-rolling-update-poli
}
<% end -%>

<% unless version == 'ga' -%>
func testAccRegionInstanceGroupManager_rollingUpdatePolicySetToDefault(igm string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-9"
project = "debian-cloud"
}

resource "google_compute_instance_template" "igm-rolling-update-policy" {
machine_type = "n1-standard-1"
can_ip_forward = false
tags = ["terraform-testing"]

disk {
source_image = "${data.google_compute_image.my_image.self_link}"
auto_delete = true
boot = true
}

network_interface {
network = "default"
}

service_account {
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
}

lifecycle {
create_before_destroy = true
}
}

resource "google_compute_region_instance_group_manager" "igm-rolling-update-policy" {
description = "Terraform test instance group manager"
name = "%s"
version {
instance_template = "${google_compute_instance_template.igm-rolling-update-policy.self_link}"
name = "primary"
}
base_instance_name = "igm-rolling-update-policy"
region = "us-central1"
target_size = 4
distribution_policy_zones = ["us-central1-a", "us-central1-f"]

update_policy {
type = "PROACTIVE"
instance_redistribution_type = "PROACTIVE"
minimal_action = "REPLACE"
max_surge_fixed = 2
max_unavailable_fixed = 2
min_ready_sec = 20
}

named_port {
name = "customhttp"
port = 8080
}
}`, igm)
}
<% end -%>

<% unless version == 'ga' -%>
func testAccRegionInstanceGroupManager_rollingUpdatePolicy2(igm string) string {
return fmt.Sprintf(`
Expand Down Expand Up @@ -1070,11 +1136,12 @@ resource "google_compute_region_instance_group_manager" "igm-rolling-update-poli
distribution_policy_zones = ["us-central1-a", "us-central1-f"]
target_size = 3
update_policy {
type = "PROACTIVE"
minimal_action = "REPLACE"
max_surge_fixed = 2
max_unavailable_fixed = 0
min_ready_sec = 10
type = "PROACTIVE"
instance_redistribution_type = "NONE"
minimal_action = "REPLACE"
max_surge_fixed = 2
max_unavailable_fixed = 0
min_ready_sec = 10
}
named_port {
name = "customhttp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ The `update_policy` block supports:
```hcl
update_policy{
type = "PROACTIVE"
instance_redistribution_type = "PROACTIVE"
minimal_action = "REPLACE"
max_surge_percent = 20
max_unavailable_fixed = 2
Expand All @@ -157,6 +158,8 @@ update_policy{

* `type` - (Required) - The type of update. Valid values are `"OPPORTUNISTIC"`, `"PROACTIVE"`

* `instance_redistribution_type` - (Optional) - The instance redistribution policy for regional managed instance groups. Valid values are: `"PROACTIVE"`, `"NONE"`. If `PROACTIVE` (default), the group attempts to maintain an even distribution of VM instances across zones in the region. If `NONE`, proactive redistribution is disabled.

* `max_surge_fixed` - (Optional), The maximum number of instances that can be created above the specified targetSize during the update process. Conflicts with `max_surge_percent`. It has to be either 0 or at least equal to the number of zones. If fixed values are used, at least one of `max_unavailable_fixed` or `max_surge_fixed` must be greater than 0.

* `max_surge_percent` - (Optional), The maximum number of instances(calculated as percentage) that can be created above the specified targetSize during the update process. Conflicts with `max_surge_fixed`. Percent value is only allowed for regional managed instance groups with size at least 10.
Expand Down

0 comments on commit be8e476

Please sign in to comment.