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 e3afb57 commit ca80536
Show file tree
Hide file tree
Showing 3 changed files with 368 additions and 1 deletion.
Original file line number Diff line number Diff line change
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 @@ -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,6 +956,21 @@ 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
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 == false {{- 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,44 @@ func flattenRegionUpdatePolicy(updatePolicy *compute.InstanceGroupManagerUpdateP
return results
}

{{ if ne $.TargetVersionName `ga` -}}
func expandInstanceFlexibilityPolicy(d *schema.ResourceData) *compute.InstanceGroupManagerInstanceFlexibilityPolicy {
instanceFlexibilityPolicy := &compute.InstanceGroupManagerInstanceFlexibilityPolicy{}
if d.HasChange("instance_flexibility_policy") {
oldFlexibilityPolicy, newFlexibilityPolicy := d.GetChange("instance_flexibility_policy")
for _, ifp := range newFlexibilityPolicy.([]any) {
ifpData := ifp.(map[string]any)
instanceFlexibilityPolicy.InstanceSelections = expandInstanceSelections(ifpData["instance_selections"].(*schema.Set).List())
}
for _, ifp := range oldFlexibilityPolicy.([]any) {
ifpData := ifp.(map[string]any)
for _, instSl := range ifpData["instance_selections"].(*schema.Set).List() {
instSlData := instSl.(map[string] any)
name := instSlData["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 +1283,30 @@ func expandDistributionPolicyForCreate(d *schema.ResourceData) *compute.Distribu
return distributionPolicy
}

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

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

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

Expand Down
Loading

0 comments on commit ca80536

Please sign in to comment.