@@ -91,6 +91,45 @@ func ResourceComputeRegionInstanceGroupManager() *schema.Resource {
91
91
},
92
92
},
93
93
},
94
+ "instance_flexibility_policy" : {
95
+ Type : schema .TypeList ,
96
+ Optional : true ,
97
+ MaxItems : 1 ,
98
+ 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.` ,
99
+ Elem : & schema.Resource {
100
+ Schema : map [string ]* schema.Schema {
101
+ "instance_selections" : {
102
+ Type : schema .TypeSet ,
103
+ Optional : true ,
104
+ Description : `Named instance selections configuring properties that the group will use when creating new VMs.` ,
105
+ Elem : & schema.Resource {
106
+ Schema : map [string ]* schema.Schema {
107
+ "name" : {
108
+ Type : schema .TypeString ,
109
+ Required : true ,
110
+ Description : `Instance selection name.` ,
111
+ },
112
+
113
+ "rank" : {
114
+ Type : schema .TypeInt ,
115
+ Optional : true ,
116
+ 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.` ,
117
+ },
118
+
119
+ "machine_types" : {
120
+ Type : schema .TypeSet ,
121
+ Required : true ,
122
+ Elem : & schema.Schema {
123
+ Type : schema .TypeString ,
124
+ },
125
+ Description : `Full machine-type names, e.g. "n1-standard-16"` ,
126
+ },
127
+ },
128
+ },
129
+ },
130
+ },
131
+ },
132
+ },
94
133
95
134
"name" : {
96
135
Type : schema .TypeString ,
@@ -277,7 +316,6 @@ func ResourceComputeRegionInstanceGroupManager() *schema.Resource {
277
316
},
278
317
},
279
318
},
280
-
281
319
"standby_policy" : {
282
320
Type : schema .TypeList ,
283
321
Computed : true ,
@@ -379,7 +417,6 @@ func ResourceComputeRegionInstanceGroupManager() *schema.Resource {
379
417
ValidateFunc : validation .IntBetween (0 , 100 ),
380
418
Description : `Specifies a percentage of instances between 0 to 100%, inclusive. For example, specify 80 for 80%.` ,
381
419
},
382
-
383
420
"min_ready_sec" : {
384
421
Type : schema .TypeInt ,
385
422
Optional : true ,
@@ -620,6 +657,7 @@ func resourceComputeRegionInstanceGroupManagerCreate(d *schema.ResourceData, met
620
657
StandbyPolicy : expandStandbyPolicy (d ),
621
658
TargetSuspendedSize : int64 (d .Get ("target_suspended_size" ).(int )),
622
659
TargetStoppedSize : int64 (d .Get ("target_stopped_size" ).(int )),
660
+ InstanceFlexibilityPolicy : expandInstanceFlexibilityPolicy (d ),
623
661
UpdatePolicy : expandRegionUpdatePolicy (d .Get ("update_policy" ).([]interface {})),
624
662
InstanceLifecyclePolicy : expandInstanceLifecyclePolicy (d .Get ("instance_lifecycle_policy" ).([]interface {})),
625
663
AllInstancesConfig : expandAllInstancesConfig (nil , d .Get ("all_instances_config" ).([]interface {})),
@@ -819,6 +857,9 @@ func resourceComputeRegionInstanceGroupManagerRead(d *schema.ResourceData, meta
819
857
if err := d .Set ("target_stopped_size" , manager .TargetStoppedSize ); err != nil {
820
858
return fmt .Errorf ("Error setting target_stopped_size: %s" , err )
821
859
}
860
+ if err := d .Set ("instance_flexibility_policy" , flattenInstanceFlexibilityPolicy (manager .InstanceFlexibilityPolicy )); err != nil {
861
+ return err
862
+ }
822
863
if err := d .Set ("update_policy" , flattenRegionUpdatePolicy (manager .UpdatePolicy )); err != nil {
823
864
return fmt .Errorf ("Error setting update_policy in state: %s" , err .Error ())
824
865
}
@@ -891,12 +932,23 @@ func resourceComputeRegionInstanceGroupManagerUpdate(d *schema.ResourceData, met
891
932
updatedManager .Versions = expandVersions (d .Get ("version" ).([]interface {}))
892
933
change = true
893
934
}
935
+ var targetSizePatchUpdate bool
936
+ if d .HasChange ("instance_flexibility_policy" ) {
937
+ updatedManager .InstanceFlexibilityPolicy = expandInstanceFlexibilityPolicy (d )
938
+ change = true
939
+
940
+ // target size update should be done by patch instead of using resize
941
+ if d .HasChange ("target_size" ) {
942
+ updatedManager .TargetSize = int64 (d .Get ("target_size" ).(int ))
943
+ updatedManager .ForceSendFields = append (updatedManager .ForceSendFields , "TargetSize" )
944
+ targetSizePatchUpdate = true
945
+ }
946
+ }
894
947
895
948
if d .HasChange ("distribution_policy_target_shape" ) {
896
949
updatedManager .DistributionPolicy = expandDistributionPolicyForUpdate (d )
897
950
change = true
898
951
}
899
-
900
952
if d .HasChange ("standby_policy" ) {
901
953
updatedManager .StandbyPolicy = expandStandbyPolicy (d )
902
954
change = true
@@ -979,7 +1031,7 @@ func resourceComputeRegionInstanceGroupManagerUpdate(d *schema.ResourceData, met
979
1031
}
980
1032
981
1033
// target size should use resize
982
- if d .HasChange ("target_size" ) {
1034
+ if d .HasChange ("target_size" ) && ! targetSizePatchUpdate {
983
1035
d .Partial (true )
984
1036
targetSize := int64 (d .Get ("target_size" ).(int ))
985
1037
op , err := config .NewComputeClient (userAgent ).RegionInstanceGroupManagers .Resize (
@@ -1124,6 +1176,39 @@ func flattenRegionUpdatePolicy(updatePolicy *compute.InstanceGroupManagerUpdateP
1124
1176
}
1125
1177
return results
1126
1178
}
1179
+ func expandInstanceFlexibilityPolicy (d * schema.ResourceData ) * compute.InstanceGroupManagerInstanceFlexibilityPolicy {
1180
+ instanceFlexibilityPolicy := & compute.InstanceGroupManagerInstanceFlexibilityPolicy {}
1181
+ oldFlexibilityPolicy , newFlexibilityPolicy := d .GetChange ("instance_flexibility_policy" )
1182
+ for _ , flexibilityPolicy := range newFlexibilityPolicy .([]any ) {
1183
+ flexibilityPolicyData := flexibilityPolicy .(map [string ]any )
1184
+ instanceFlexibilityPolicy .InstanceSelections = expandInstanceSelections (flexibilityPolicyData ["instance_selections" ].(* schema.Set ).List ())
1185
+ }
1186
+ for _ , flexibilityPolicy := range oldFlexibilityPolicy .([]any ) {
1187
+ flexibilityPolicyData := flexibilityPolicy .(map [string ]any )
1188
+ for _ , instanceSelection := range flexibilityPolicyData ["instance_selections" ].(* schema.Set ).List () {
1189
+ instanceSelectionData := instanceSelection .(map [string ]any )
1190
+ name := instanceSelectionData ["name" ].(string )
1191
+ if _ , exist := instanceFlexibilityPolicy .InstanceSelections [name ]; ! exist {
1192
+ instanceFlexibilityPolicy .NullFields = append (instanceFlexibilityPolicy .NullFields , "InstanceSelections." + name )
1193
+ }
1194
+ }
1195
+ instanceFlexibilityPolicy .ForceSendFields = append (instanceFlexibilityPolicy .ForceSendFields , "InstanceSelections" )
1196
+ }
1197
+ return instanceFlexibilityPolicy
1198
+ }
1199
+
1200
+ func expandInstanceSelections (instanceSelections []any ) map [string ]compute.InstanceGroupManagerInstanceFlexibilityPolicyInstanceSelection {
1201
+ instanceSelectionsMap := make (map [string ]compute.InstanceGroupManagerInstanceFlexibilityPolicyInstanceSelection )
1202
+ for _ , instanceSelectionRaw := range instanceSelections {
1203
+ instanceSelectionData := instanceSelectionRaw .(map [string ]any )
1204
+ instanceSelection := compute.InstanceGroupManagerInstanceFlexibilityPolicyInstanceSelection {
1205
+ Rank : int64 (instanceSelectionData ["rank" ].(int )),
1206
+ MachineTypes : tpgresource .ConvertStringSet (instanceSelectionData ["machine_types" ].(* schema.Set )),
1207
+ }
1208
+ instanceSelectionsMap [instanceSelectionData ["name" ].(string )] = instanceSelection
1209
+ }
1210
+ return instanceSelectionsMap
1211
+ }
1127
1212
1128
1213
func expandDistributionPolicyForUpdate (d * schema.ResourceData ) * compute.DistributionPolicy {
1129
1214
dpts := d .Get ("distribution_policy_target_shape" ).(string )
@@ -1159,6 +1244,27 @@ func expandDistributionPolicyForCreate(d *schema.ResourceData) *compute.Distribu
1159
1244
}
1160
1245
return distributionPolicy
1161
1246
}
1247
+ func flattenInstanceFlexibilityPolicy (instanceFlexibilityPolicy * compute.InstanceGroupManagerInstanceFlexibilityPolicy ) []map [string ]any {
1248
+ flattenedInstanceFlexibilityPolicy := []map [string ]any {}
1249
+ if instanceFlexibilityPolicy != nil {
1250
+ instanceSelectionsMap := map [string ]any {}
1251
+ instanceSelectionsMap ["instance_selections" ] = flattenInstanceSelections (instanceFlexibilityPolicy .InstanceSelections )
1252
+ flattenedInstanceFlexibilityPolicy = append (flattenedInstanceFlexibilityPolicy , instanceSelectionsMap )
1253
+ }
1254
+ return flattenedInstanceFlexibilityPolicy
1255
+ }
1256
+
1257
+ func flattenInstanceSelections (instanceSelections map [string ]compute.InstanceGroupManagerInstanceFlexibilityPolicyInstanceSelection ) []map [string ]any {
1258
+ instanceSelectionsMap := make ([]map [string ]any , 0 , len (instanceSelections ))
1259
+ for instanceSelectionName , instanceSelection := range instanceSelections {
1260
+ instanceSelectionData := make (map [string ]any )
1261
+ instanceSelectionData ["name" ] = instanceSelectionName
1262
+ instanceSelectionData ["rank" ] = instanceSelection .Rank
1263
+ instanceSelectionData ["machine_types" ] = instanceSelection .MachineTypes
1264
+ instanceSelectionsMap = append (instanceSelectionsMap , instanceSelectionData )
1265
+ }
1266
+ return instanceSelectionsMap
1267
+ }
1162
1268
1163
1269
func flattenDistributionPolicy (distributionPolicy * compute.DistributionPolicy ) []string {
1164
1270
zones := make ([]string , 0 )
0 commit comments