Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for capacity aware modes to `google_compute_region_instance_group_manager #4348

Merged
merged 11 commits into from
Jan 21, 2021
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ func resourceComputeRegionInstanceGroupManager() *schema.Resource {
},
},

"distribution_policy_target_shape": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: `The shape to which the group converges either proactively or on resize events (depending on the value set in updatePolicy.instanceRedistributionType).`,
},

"update_policy": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -343,7 +351,7 @@ func resourceComputeRegionInstanceGroupManagerCreate(d *schema.ResourceData, met
AutoHealingPolicies: expandAutoHealingPolicies(d.Get("auto_healing_policies").([]interface{})),
Versions: expandVersions(d.Get("version").([]interface{})),
UpdatePolicy: expandRegionUpdatePolicy(d.Get("update_policy").([]interface{})),
DistributionPolicy: expandDistributionPolicy(d.Get("distribution_policy_zones").(*schema.Set)),
DistributionPolicy: expandDistributionPolicy(d),
StatefulPolicy: expandStatefulPolicy(d.Get("stateful_disk").(*schema.Set).List()),
// Force send TargetSize to allow size of 0.
ForceSendFields: []string{"TargetSize"},
Expand Down Expand Up @@ -464,6 +472,9 @@ func resourceComputeRegionInstanceGroupManagerRead(d *schema.ResourceData, meta
if err := d.Set("distribution_policy_zones", flattenDistributionPolicy(manager.DistributionPolicy)); err != nil {
return err
}
if err := d.Set("distribution_policy_target_shape", manager.DistributionPolicy.TargetShape); err != nil {
return err
}
if err := d.Set("self_link", ConvertSelfLinkToV1(manager.SelfLink)); err != nil {
return fmt.Errorf("Error setting self_link: %s", err)
}
Expand Down Expand Up @@ -714,21 +725,24 @@ func flattenRegionUpdatePolicy(updatePolicy *computeBeta.InstanceGroupManagerUpd
return results
}

func expandDistributionPolicy(configured *schema.Set) *computeBeta.DistributionPolicy {
if configured.Len() == 0 {
func expandDistributionPolicy(d *schema.ResourceData) *computeBeta.DistributionPolicy {
dpz := d.Get("distribution_policy_zones").(*schema.Set)
dpts := d.Get("distribution_policy_target_shape").(string)
if dpz.Len() == 0 && dpts == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to set the target shape if the distribution policy zones are empty? We are opening the possibility of an empty list of zones here, which seems like it might cause problems

Copy link
Contributor Author

@upodroid upodroid Jan 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return nil
}

distributionPolicyZoneConfigs := make([]*computeBeta.DistributionPolicyZoneConfiguration, 0, configured.Len())
for _, raw := range configured.List() {
distributionPolicyZoneConfigs := make([]*computeBeta.DistributionPolicyZoneConfiguration, 0, dpz.Len())
for _, raw := range dpz.List() {
data := raw.(string)
distributionPolicyZoneConfig := computeBeta.DistributionPolicyZoneConfiguration{
Zone: "zones/" + data,
}

distributionPolicyZoneConfigs = append(distributionPolicyZoneConfigs, &distributionPolicyZoneConfig)
}
return &computeBeta.DistributionPolicy{Zones: distributionPolicyZoneConfigs}

return &computeBeta.DistributionPolicy{Zones: distributionPolicyZoneConfigs, TargetShape: dpts}
}

func flattenDistributionPolicy(distributionPolicy *computeBeta.DistributionPolicy) []string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1058,10 +1058,11 @@ resource "google_compute_region_instance_group_manager" "igm-basic" {
name = "primary"
}

base_instance_name = "igm-basic"
region = "us-central1"
target_size = 2
distribution_policy_zones = ["%s"]
base_instance_name = "igm-basic"
region = "us-central1"
target_size = 2
distribution_policy_zones = ["%s"]
distribution_policy_target_shape = "ANY"
}
`, template, igm, strings.Join(zones, "\",\""))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ description: |-

The Google Compute Engine Regional Instance Group Manager API creates and manages pools
of homogeneous Compute Engine virtual machine instances from a common instance
template. For more information, see [the official documentation](https://cloud.google.com/compute/docs/instance-groups/distributing-instances-with-regional-instance-groups)
and [API](https://cloud.google.com/compute/docs/reference/latest/regionInstanceGroupManagers)
template.

~> **Note:** Use [google_compute_instance_group_manager](/docs/providers/google/r/compute_instance_group_manager.html) to create a single-zone instance group manager.
To get more information about regionInstanceGroupManagers, see:

* [API documentation](https://cloud.google.com/compute/docs/reference/latest/regionInstanceGroupManagers)
* How-to Guides
* [Regional Instance Groups Guide](https://cloud.google.com/compute/docs/instance-groups/distributing-instances-with-regional-instance-groups)

~> **Note:** Use [google_compute_instance_group_manager](/docs/providers/google/r/compute_instance_group_manager.html) to create a zonal instance group manager.

## Example Usage with top level instance template (`google` provider)

Expand Down Expand Up @@ -138,6 +143,8 @@ group. You can specify only one value. Structure is documented below. For more i
* `distribution_policy_zones` - (Optional) The distribution policy for this managed instance
group. You can specify one or more values. For more information, see the [official documentation](https://cloud.google.com/compute/docs/instance-groups/distributing-instances-with-regional-instance-groups#selectingzones).

* `distribution_policy_target_shape` - (Optional) The shape to which the group converges either proactively or on resize events (depending on the value set in update_policy.0.instance_redistribution_type). For more information see the [official documentation](https://cloud.google.com/compute/docs/instance-groups/regional-mig-distribution-shape).

* `stateful_disk` - (Optional) Disks created on the instances that will be preserved on instance delete, update, etc. Structure is documented below. For more information see the [official documentation](https://cloud.google.com/compute/docs/instance-groups/configuring-stateful-disks-in-migs). Proactive cross zone instance redistribution must be disabled before you can update stateful disks on existing instance group managers. This can be controlled via the `update_policy`.

- - -
Expand Down