Skip to content

Commit 1430952

Browse files
Add ScaleInControl fields to GCE Autoscalers (#4147) (#2703)
Signed-off-by: Modular Magician <[email protected]>
1 parent 58602dd commit 1430952

File tree

7 files changed

+660
-52
lines changed

7 files changed

+660
-52
lines changed

.changelog/4147.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
```release-note:enhancement
2+
compute: added `autoscaling_policy.0.scale_in_control` fields to `google_compute_autoscaler`
3+
```
4+
```release-note:enhancement
5+
compute: added `autoscaling_policy.0.scale_in_control` fields to `google_compute_region_autoscaler`
6+
```

google-beta/resource_compute_autoscaler.go

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,47 @@ For example, specify 80 for 80%.`,
269269
Type: schema.TypeInt,
270270
Optional: true,
271271
Description: `How long back autoscaling should look when computing recommendations
272+
to include directives regarding slower scale down, as described above.`,
273+
},
274+
},
275+
},
276+
AtLeastOneOf: []string{},
277+
},
278+
"scale_in_control": {
279+
Type: schema.TypeList,
280+
Optional: true,
281+
Description: `Defines scale in controls to reduce the risk of response latency
282+
and outages due to abrupt scale-in events`,
283+
MaxItems: 1,
284+
Elem: &schema.Resource{
285+
Schema: map[string]*schema.Schema{
286+
"max_scaled_in_replicas": {
287+
Type: schema.TypeList,
288+
Optional: true,
289+
Description: `A nested object resource`,
290+
MaxItems: 1,
291+
Elem: &schema.Resource{
292+
Schema: map[string]*schema.Schema{
293+
"fixed": {
294+
Type: schema.TypeInt,
295+
Optional: true,
296+
Description: `Specifies a fixed number of VM instances. This must be a positive
297+
integer.`,
298+
},
299+
"percent": {
300+
Type: schema.TypeInt,
301+
Optional: true,
302+
Description: `Specifies a percentage of instances between 0 to 100%, inclusive.
303+
For example, specify 80 for 80%.`,
304+
},
305+
},
306+
},
307+
AtLeastOneOf: []string{},
308+
},
309+
"time_window_sec": {
310+
Type: schema.TypeInt,
311+
Optional: true,
312+
Description: `How long back autoscaling should look when computing recommendations
272313
to include directives regarding slower scale down, as described above.`,
273314
},
274315
},
@@ -646,6 +687,8 @@ func flattenComputeAutoscalerAutoscalingPolicy(v interface{}, d *schema.Resource
646687
flattenComputeAutoscalerAutoscalingPolicyMode(original["mode"], d, config)
647688
transformed["scale_down_control"] =
648689
flattenComputeAutoscalerAutoscalingPolicyScaleDownControl(original["scaleDownControl"], d, config)
690+
transformed["scale_in_control"] =
691+
flattenComputeAutoscalerAutoscalingPolicyScaleInControl(original["scaleInControl"], d, config)
649692
transformed["cpu_utilization"] =
650693
flattenComputeAutoscalerAutoscalingPolicyCpuUtilization(original["cpuUtilization"], d, config)
651694
transformed["metric"] =
@@ -790,6 +833,87 @@ func flattenComputeAutoscalerAutoscalingPolicyScaleDownControlTimeWindowSec(v in
790833
return v // let terraform core handle it otherwise
791834
}
792835

836+
func flattenComputeAutoscalerAutoscalingPolicyScaleInControl(v interface{}, d *schema.ResourceData, config *Config) interface{} {
837+
if v == nil {
838+
return nil
839+
}
840+
original := v.(map[string]interface{})
841+
if len(original) == 0 {
842+
return nil
843+
}
844+
transformed := make(map[string]interface{})
845+
transformed["max_scaled_in_replicas"] =
846+
flattenComputeAutoscalerAutoscalingPolicyScaleInControlMaxScaledInReplicas(original["maxScaledInReplicas"], d, config)
847+
transformed["time_window_sec"] =
848+
flattenComputeAutoscalerAutoscalingPolicyScaleInControlTimeWindowSec(original["timeWindowSec"], d, config)
849+
return []interface{}{transformed}
850+
}
851+
func flattenComputeAutoscalerAutoscalingPolicyScaleInControlMaxScaledInReplicas(v interface{}, d *schema.ResourceData, config *Config) interface{} {
852+
if v == nil {
853+
return nil
854+
}
855+
original := v.(map[string]interface{})
856+
if len(original) == 0 {
857+
return nil
858+
}
859+
transformed := make(map[string]interface{})
860+
transformed["fixed"] =
861+
flattenComputeAutoscalerAutoscalingPolicyScaleInControlMaxScaledInReplicasFixed(original["fixed"], d, config)
862+
transformed["percent"] =
863+
flattenComputeAutoscalerAutoscalingPolicyScaleInControlMaxScaledInReplicasPercent(original["percent"], d, config)
864+
return []interface{}{transformed}
865+
}
866+
func flattenComputeAutoscalerAutoscalingPolicyScaleInControlMaxScaledInReplicasFixed(v interface{}, d *schema.ResourceData, config *Config) interface{} {
867+
// Handles the string fixed64 format
868+
if strVal, ok := v.(string); ok {
869+
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
870+
return intVal
871+
}
872+
}
873+
874+
// number values are represented as float64
875+
if floatVal, ok := v.(float64); ok {
876+
intVal := int(floatVal)
877+
return intVal
878+
}
879+
880+
return v // let terraform core handle it otherwise
881+
}
882+
883+
func flattenComputeAutoscalerAutoscalingPolicyScaleInControlMaxScaledInReplicasPercent(v interface{}, d *schema.ResourceData, config *Config) interface{} {
884+
// Handles the string fixed64 format
885+
if strVal, ok := v.(string); ok {
886+
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
887+
return intVal
888+
}
889+
}
890+
891+
// number values are represented as float64
892+
if floatVal, ok := v.(float64); ok {
893+
intVal := int(floatVal)
894+
return intVal
895+
}
896+
897+
return v // let terraform core handle it otherwise
898+
}
899+
900+
func flattenComputeAutoscalerAutoscalingPolicyScaleInControlTimeWindowSec(v interface{}, d *schema.ResourceData, config *Config) interface{} {
901+
// Handles the string fixed64 format
902+
if strVal, ok := v.(string); ok {
903+
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
904+
return intVal
905+
}
906+
}
907+
908+
// number values are represented as float64
909+
if floatVal, ok := v.(float64); ok {
910+
intVal := int(floatVal)
911+
return intVal
912+
}
913+
914+
return v // let terraform core handle it otherwise
915+
}
916+
793917
func flattenComputeAutoscalerAutoscalingPolicyCpuUtilization(v interface{}, d *schema.ResourceData, config *Config) interface{} {
794918
if v == nil {
795919
return nil
@@ -932,6 +1056,13 @@ func expandComputeAutoscalerAutoscalingPolicy(v interface{}, d TerraformResource
9321056
transformed["scaleDownControl"] = transformedScaleDownControl
9331057
}
9341058

1059+
transformedScaleInControl, err := expandComputeAutoscalerAutoscalingPolicyScaleInControl(original["scale_in_control"], d, config)
1060+
if err != nil {
1061+
return nil, err
1062+
} else if val := reflect.ValueOf(transformedScaleInControl); val.IsValid() && !isEmptyValue(val) {
1063+
transformed["scaleInControl"] = transformedScaleInControl
1064+
}
1065+
9351066
transformedCpuUtilization, err := expandComputeAutoscalerAutoscalingPolicyCpuUtilization(original["cpu_utilization"], d, config)
9361067
if err != nil {
9371068
return nil, err
@@ -1036,6 +1167,70 @@ func expandComputeAutoscalerAutoscalingPolicyScaleDownControlTimeWindowSec(v int
10361167
return v, nil
10371168
}
10381169

1170+
func expandComputeAutoscalerAutoscalingPolicyScaleInControl(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1171+
l := v.([]interface{})
1172+
if len(l) == 0 || l[0] == nil {
1173+
return nil, nil
1174+
}
1175+
raw := l[0]
1176+
original := raw.(map[string]interface{})
1177+
transformed := make(map[string]interface{})
1178+
1179+
transformedMaxScaledInReplicas, err := expandComputeAutoscalerAutoscalingPolicyScaleInControlMaxScaledInReplicas(original["max_scaled_in_replicas"], d, config)
1180+
if err != nil {
1181+
return nil, err
1182+
} else if val := reflect.ValueOf(transformedMaxScaledInReplicas); val.IsValid() && !isEmptyValue(val) {
1183+
transformed["maxScaledInReplicas"] = transformedMaxScaledInReplicas
1184+
}
1185+
1186+
transformedTimeWindowSec, err := expandComputeAutoscalerAutoscalingPolicyScaleInControlTimeWindowSec(original["time_window_sec"], d, config)
1187+
if err != nil {
1188+
return nil, err
1189+
} else if val := reflect.ValueOf(transformedTimeWindowSec); val.IsValid() && !isEmptyValue(val) {
1190+
transformed["timeWindowSec"] = transformedTimeWindowSec
1191+
}
1192+
1193+
return transformed, nil
1194+
}
1195+
1196+
func expandComputeAutoscalerAutoscalingPolicyScaleInControlMaxScaledInReplicas(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1197+
l := v.([]interface{})
1198+
if len(l) == 0 || l[0] == nil {
1199+
return nil, nil
1200+
}
1201+
raw := l[0]
1202+
original := raw.(map[string]interface{})
1203+
transformed := make(map[string]interface{})
1204+
1205+
transformedFixed, err := expandComputeAutoscalerAutoscalingPolicyScaleInControlMaxScaledInReplicasFixed(original["fixed"], d, config)
1206+
if err != nil {
1207+
return nil, err
1208+
} else if val := reflect.ValueOf(transformedFixed); val.IsValid() && !isEmptyValue(val) {
1209+
transformed["fixed"] = transformedFixed
1210+
}
1211+
1212+
transformedPercent, err := expandComputeAutoscalerAutoscalingPolicyScaleInControlMaxScaledInReplicasPercent(original["percent"], d, config)
1213+
if err != nil {
1214+
return nil, err
1215+
} else if val := reflect.ValueOf(transformedPercent); val.IsValid() && !isEmptyValue(val) {
1216+
transformed["percent"] = transformedPercent
1217+
}
1218+
1219+
return transformed, nil
1220+
}
1221+
1222+
func expandComputeAutoscalerAutoscalingPolicyScaleInControlMaxScaledInReplicasFixed(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1223+
return v, nil
1224+
}
1225+
1226+
func expandComputeAutoscalerAutoscalingPolicyScaleInControlMaxScaledInReplicasPercent(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1227+
return v, nil
1228+
}
1229+
1230+
func expandComputeAutoscalerAutoscalingPolicyScaleInControlTimeWindowSec(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1231+
return v, nil
1232+
}
1233+
10391234
func expandComputeAutoscalerAutoscalingPolicyCpuUtilization(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
10401235
l := v.([]interface{})
10411236
if len(l) == 0 || l[0] == nil {

0 commit comments

Comments
 (0)