Skip to content

Commit

Permalink
Add instanceFlexibilityPolicy to google_compute_region_instance_group…
Browse files Browse the repository at this point in the history
…_manager to create instances by instanceSelections
  • Loading branch information
chayankumar999 committed Oct 30, 2024
1 parent 81a9344 commit 8531b1a
Show file tree
Hide file tree
Showing 3 changed files with 378 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"

{{ if eq $.TargetVersionName `ga` }}
{{ if eq $.TargetVersionName "ga" }}
"google.golang.org/api/compute/v1"
{{- else }}
compute "google.golang.org/api/compute/v0.beta"
Expand Down Expand Up @@ -94,6 +94,48 @@ func ResourceComputeRegionInstanceGroupManager() *schema.Resource {
},
},

{{- if ne $.TargetVersionName "ga" }}
"instance_flexibility_policy": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: `The flexibility policy for this managed instance group. Instance flexibility allowing MIG to create VMs from multiple types of machines. Instance flexibility configuration on MIG overrides instance template configuration.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"instance_selections": {
Type: schema.TypeSet,
Optional: true,
Description: `Named instance selections configuring properties that the group will use when creating new VMs.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: `Instance selection name.`,
},

"rank": {
Type: schema.TypeInt,
Optional: true,
Description: `Preference of this instance selection. Lower number means higher preference. MIG will first try to create a VM based on the machine-type with lowest rank and fallback to next rank based on availability. Machine types and instance selections with the same rank have the same preference.`,
},

"machine_types": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: `Full machine-type names, e.g. "n1-standard-16"`,
},
},
},
},
},
},
},
{{- end }}

"name": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -280,7 +322,7 @@ func ResourceComputeRegionInstanceGroupManager() *schema.Resource {
},
},

{{ if ne $.TargetVersionName `ga` -}}
{{- if ne $.TargetVersionName "ga" }}
"standby_policy": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -384,7 +426,7 @@ func ResourceComputeRegionInstanceGroupManager() *schema.Resource {
Description: `Specifies a percentage of instances between 0 to 100%, inclusive. For example, specify 80 for 80%.`,
},

{{ if ne $.TargetVersionName `ga` -}}
{{- if ne $.TargetVersionName "ga" }}
"min_ready_sec": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -630,6 +672,9 @@ func resourceComputeRegionInstanceGroupManagerCreate(d *schema.ResourceData, met
TargetSuspendedSize: int64(d.Get("target_suspended_size").(int)),
TargetStoppedSize: int64(d.Get("target_stopped_size").(int)),
{{- end }}
{{- if ne $.TargetVersionName "ga" }}
InstanceFlexibilityPolicy: expandInstanceFlexibilityPolicy(d),
{{- end }}
UpdatePolicy: expandRegionUpdatePolicy(d.Get("update_policy").([]interface{})),
InstanceLifecyclePolicy: expandInstanceLifecyclePolicy(d.Get("instance_lifecycle_policy").([]interface{})),
AllInstancesConfig: expandAllInstancesConfig(nil, d.Get("all_instances_config").([]interface{})),
Expand Down Expand Up @@ -833,6 +878,11 @@ func resourceComputeRegionInstanceGroupManagerRead(d *schema.ResourceData, meta
return fmt.Errorf("Error setting target_stopped_size: %s", err)
}
{{- end }}
{{- if ne $.TargetVersionName "ga" }}
if err := d.Set("instance_flexibility_policy", flattenInstanceFlexibilityPolicy(manager.InstanceFlexibilityPolicy)); err != nil {
return err
}
{{- end }}
if err := d.Set("update_policy", flattenRegionUpdatePolicy(manager.UpdatePolicy)); err != nil {
return fmt.Errorf("Error setting update_policy in state: %s", err.Error())
}
Expand Down Expand Up @@ -906,12 +956,27 @@ func resourceComputeRegionInstanceGroupManagerUpdate(d *schema.ResourceData, met
change = true
}

{{- if ne $.TargetVersionName "ga" }}
var targetSizePatchUpdate bool
if d.HasChange("instance_flexibility_policy") {
updatedManager.InstanceFlexibilityPolicy = expandInstanceFlexibilityPolicy(d)
change = true

// target size update should be done by patch instead of using resize
if d.HasChange("target_size") {
updatedManager.TargetSize = int64(d.Get("target_size").(int))
updatedManager.ForceSendFields = append(updatedManager.ForceSendFields, "TargetSize")
targetSizePatchUpdate = true
}
}
{{- end }}

if d.HasChange("distribution_policy_target_shape") {
updatedManager.DistributionPolicy = expandDistributionPolicyForUpdate(d)
change = true
}

{{ if ne $.TargetVersionName `ga` -}}
{{- if ne $.TargetVersionName "ga" }}
if d.HasChange("standby_policy") {
updatedManager.StandbyPolicy = expandStandbyPolicy(d)
change = true
Expand Down Expand Up @@ -995,7 +1060,7 @@ func resourceComputeRegionInstanceGroupManagerUpdate(d *schema.ResourceData, met
}

// target size should use resize
if d.HasChange("target_size") {
if d.HasChange("target_size") {{- if ne $.TargetVersionName "ga" }} && !targetSizePatchUpdate {{- end}} {
d.Partial(true)
targetSize := int64(d.Get("target_size").(int))
op, err := config.NewComputeClient(userAgent).RegionInstanceGroupManagers.Resize(
Expand Down Expand Up @@ -1145,6 +1210,42 @@ func flattenRegionUpdatePolicy(updatePolicy *compute.InstanceGroupManagerUpdateP
return results
}

{{- if ne $.TargetVersionName "ga" }}
func expandInstanceFlexibilityPolicy(d *schema.ResourceData) *compute.InstanceGroupManagerInstanceFlexibilityPolicy {
instanceFlexibilityPolicy := &compute.InstanceGroupManagerInstanceFlexibilityPolicy{}
oldFlexibilityPolicy, newFlexibilityPolicy := d.GetChange("instance_flexibility_policy")
for _, flexibilityPolicy := range newFlexibilityPolicy.([]any) {
flexibilityPolicyData := flexibilityPolicy.(map[string]any)
instanceFlexibilityPolicy.InstanceSelections = expandInstanceSelections(flexibilityPolicyData["instance_selections"].(*schema.Set).List())
}
for _, flexibilityPolicy := range oldFlexibilityPolicy.([]any) {
flexibilityPolicyData := flexibilityPolicy.(map[string]any)
for _, instanceSelection := range flexibilityPolicyData["instance_selections"].(*schema.Set).List() {
instanceSelectionData := instanceSelection.(map[string] any)
name := instanceSelectionData["name"].(string)
if _, exist := instanceFlexibilityPolicy.InstanceSelections[name]; !exist {
instanceFlexibilityPolicy.NullFields = append(instanceFlexibilityPolicy.NullFields, "InstanceSelections." + name)
}
}
instanceFlexibilityPolicy.ForceSendFields = append(instanceFlexibilityPolicy.ForceSendFields, "InstanceSelections")
}
return instanceFlexibilityPolicy
}

func expandInstanceSelections(instanceSelections []any) map[string]compute.InstanceGroupManagerInstanceFlexibilityPolicyInstanceSelection {
instanceSelectionsMap := make(map[string]compute.InstanceGroupManagerInstanceFlexibilityPolicyInstanceSelection)
for _, instanceSelectionRaw := range instanceSelections {
instanceSelectionData := instanceSelectionRaw.(map[string]any)
instanceSelection := compute.InstanceGroupManagerInstanceFlexibilityPolicyInstanceSelection{
Rank: int64(instanceSelectionData["rank"].(int)),
MachineTypes: tpgresource.ConvertStringSet(instanceSelectionData["machine_types"].(*schema.Set)),
}
instanceSelectionsMap[instanceSelectionData["name"].(string)] = instanceSelection
}
return instanceSelectionsMap
}
{{- end }}

func expandDistributionPolicyForUpdate(d *schema.ResourceData) *compute.DistributionPolicy {
dpts := d.Get("distribution_policy_target_shape").(string)
if dpts == "" {
Expand Down Expand Up @@ -1180,6 +1281,30 @@ func expandDistributionPolicyForCreate(d *schema.ResourceData) *compute.Distribu
return distributionPolicy
}

{{- if ne $.TargetVersionName "ga" }}
func flattenInstanceFlexibilityPolicy(instanceFlexibilityPolicy *compute.InstanceGroupManagerInstanceFlexibilityPolicy) []map[string]any {
flattenedInstanceFlexibilityPolicy := []map[string]any{}
if instanceFlexibilityPolicy != nil {
instanceSelectionsMap := map[string]any{}
instanceSelectionsMap["instance_selections"] = flattenInstanceSelections(instanceFlexibilityPolicy.InstanceSelections)
flattenedInstanceFlexibilityPolicy = append(flattenedInstanceFlexibilityPolicy, instanceSelectionsMap)
}
return flattenedInstanceFlexibilityPolicy
}

func flattenInstanceSelections(instanceSelections map[string]compute.InstanceGroupManagerInstanceFlexibilityPolicyInstanceSelection) []map[string]any {
instanceSelectionsMap := make([]map[string]any, 0, len(instanceSelections))
for instanceSelectionName, instanceSelection := range instanceSelections {
instanceSelectionData := make(map[string]any)
instanceSelectionData["name"] = instanceSelectionName
instanceSelectionData["rank"] = instanceSelection.Rank
instanceSelectionData["machine_types"] = instanceSelection.MachineTypes
instanceSelectionsMap = append(instanceSelectionsMap, instanceSelectionData)
}
return instanceSelectionsMap
}
{{- end }}

func flattenDistributionPolicy(distributionPolicy *compute.DistributionPolicy) []string {
zones := make([]string, 0)

Expand Down
Loading

0 comments on commit 8531b1a

Please sign in to comment.