Skip to content

Commit

Permalink
Add mode and scaleDownControls for Compute Region AutoScaler (#3701) (#…
Browse files Browse the repository at this point in the history
…2226)

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jun 24, 2020
1 parent 4b1460c commit ebcb0e8
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 44 deletions.
6 changes: 6 additions & 0 deletions .changelog/3701.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```release-note:enhancement
compute: Added `mode` to `google_compute_region_autoscaler` `autoscaling_policy`
```
```release-note:enhancement
compute: Added `scale_down_control ` to `google_compute_region_autoscaler` `autoscaling_policy` (beta only)
```
219 changes: 219 additions & 0 deletions google-beta/resource_compute_region_autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,54 @@ Stackdriver Monitoring metric. Possible values: ["GAUGE", "DELTA_PER_SECOND", "D
},
},
},
"mode": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"OFF", "ONLY_UP", "ON", ""}, false),
Description: `Defines operating mode for this policy. Default value: "ON" Possible values: ["OFF", "ONLY_UP", "ON"]`,
Default: "ON",
},
"scale_down_control": {
Type: schema.TypeList,
Optional: true,
Description: `Defines scale down controls to reduce the risk of response latency
and outages due to abrupt scale-in events`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"max_scaled_down_replicas": {
Type: schema.TypeList,
Optional: true,
Description: `A nested object resource`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"fixed": {
Type: schema.TypeInt,
Optional: true,
Description: `Specifies a fixed number of VM instances. This must be a positive
integer.`,
},
"percent": {
Type: schema.TypeInt,
Optional: true,
Description: `Specifies a percentage of instances between 0 to 100%, inclusive.
For example, specify 80 for 80%.`,
},
},
},
AtLeastOneOf: []string{},
},
"time_window_sec": {
Type: schema.TypeInt,
Optional: true,
Description: `How long back autoscaling should look when computing recommendations
to include directives regarding slower scale down, as described above.`,
},
},
},
AtLeastOneOf: []string{},
},
},
},
},
Expand Down Expand Up @@ -540,6 +588,10 @@ func flattenComputeRegionAutoscalerAutoscalingPolicy(v interface{}, d *schema.Re
flattenComputeRegionAutoscalerAutoscalingPolicyMaxReplicas(original["maxNumReplicas"], d, config)
transformed["cooldown_period"] =
flattenComputeRegionAutoscalerAutoscalingPolicyCooldownPeriod(original["coolDownPeriodSec"], d, config)
transformed["mode"] =
flattenComputeRegionAutoscalerAutoscalingPolicyMode(original["mode"], d, config)
transformed["scale_down_control"] =
flattenComputeRegionAutoscalerAutoscalingPolicyScaleDownControl(original["scaleDownControl"], d, config)
transformed["cpu_utilization"] =
flattenComputeRegionAutoscalerAutoscalingPolicyCpuUtilization(original["cpuUtilization"], d, config)
transformed["metric"] =
Expand Down Expand Up @@ -599,6 +651,91 @@ func flattenComputeRegionAutoscalerAutoscalingPolicyCooldownPeriod(v interface{}
return v // let terraform core handle it otherwise
}

func flattenComputeRegionAutoscalerAutoscalingPolicyMode(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenComputeRegionAutoscalerAutoscalingPolicyScaleDownControl(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["max_scaled_down_replicas"] =
flattenComputeRegionAutoscalerAutoscalingPolicyScaleDownControlMaxScaledDownReplicas(original["maxScaledDownReplicas"], d, config)
transformed["time_window_sec"] =
flattenComputeRegionAutoscalerAutoscalingPolicyScaleDownControlTimeWindowSec(original["timeWindowSec"], d, config)
return []interface{}{transformed}
}
func flattenComputeRegionAutoscalerAutoscalingPolicyScaleDownControlMaxScaledDownReplicas(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["fixed"] =
flattenComputeRegionAutoscalerAutoscalingPolicyScaleDownControlMaxScaledDownReplicasFixed(original["fixed"], d, config)
transformed["percent"] =
flattenComputeRegionAutoscalerAutoscalingPolicyScaleDownControlMaxScaledDownReplicasPercent(original["percent"], d, config)
return []interface{}{transformed}
}
func flattenComputeRegionAutoscalerAutoscalingPolicyScaleDownControlMaxScaledDownReplicasFixed(v interface{}, d *schema.ResourceData, config *Config) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
return intVal
}
}

// number values are represented as float64
if floatVal, ok := v.(float64); ok {
intVal := int(floatVal)
return intVal
}

return v // let terraform core handle it otherwise
}

func flattenComputeRegionAutoscalerAutoscalingPolicyScaleDownControlMaxScaledDownReplicasPercent(v interface{}, d *schema.ResourceData, config *Config) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
return intVal
}
}

// number values are represented as float64
if floatVal, ok := v.(float64); ok {
intVal := int(floatVal)
return intVal
}

return v // let terraform core handle it otherwise
}

func flattenComputeRegionAutoscalerAutoscalingPolicyScaleDownControlTimeWindowSec(v interface{}, d *schema.ResourceData, config *Config) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
return intVal
}
}

// number values are represented as float64
if floatVal, ok := v.(float64); ok {
intVal := int(floatVal)
return intVal
}

return v // let terraform core handle it otherwise
}

func flattenComputeRegionAutoscalerAutoscalingPolicyCpuUtilization(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
Expand Down Expand Up @@ -724,6 +861,20 @@ func expandComputeRegionAutoscalerAutoscalingPolicy(v interface{}, d TerraformRe
transformed["coolDownPeriodSec"] = transformedCooldownPeriod
}

transformedMode, err := expandComputeRegionAutoscalerAutoscalingPolicyMode(original["mode"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedMode); val.IsValid() && !isEmptyValue(val) {
transformed["mode"] = transformedMode
}

transformedScaleDownControl, err := expandComputeRegionAutoscalerAutoscalingPolicyScaleDownControl(original["scale_down_control"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedScaleDownControl); val.IsValid() && !isEmptyValue(val) {
transformed["scaleDownControl"] = transformedScaleDownControl
}

transformedCpuUtilization, err := expandComputeRegionAutoscalerAutoscalingPolicyCpuUtilization(original["cpu_utilization"], d, config)
if err != nil {
return nil, err
Expand Down Expand Up @@ -760,6 +911,74 @@ func expandComputeRegionAutoscalerAutoscalingPolicyCooldownPeriod(v interface{},
return v, nil
}

func expandComputeRegionAutoscalerAutoscalingPolicyMode(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeRegionAutoscalerAutoscalingPolicyScaleDownControl(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedMaxScaledDownReplicas, err := expandComputeRegionAutoscalerAutoscalingPolicyScaleDownControlMaxScaledDownReplicas(original["max_scaled_down_replicas"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedMaxScaledDownReplicas); val.IsValid() && !isEmptyValue(val) {
transformed["maxScaledDownReplicas"] = transformedMaxScaledDownReplicas
}

transformedTimeWindowSec, err := expandComputeRegionAutoscalerAutoscalingPolicyScaleDownControlTimeWindowSec(original["time_window_sec"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedTimeWindowSec); val.IsValid() && !isEmptyValue(val) {
transformed["timeWindowSec"] = transformedTimeWindowSec
}

return transformed, nil
}

func expandComputeRegionAutoscalerAutoscalingPolicyScaleDownControlMaxScaledDownReplicas(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedFixed, err := expandComputeRegionAutoscalerAutoscalingPolicyScaleDownControlMaxScaledDownReplicasFixed(original["fixed"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedFixed); val.IsValid() && !isEmptyValue(val) {
transformed["fixed"] = transformedFixed
}

transformedPercent, err := expandComputeRegionAutoscalerAutoscalingPolicyScaleDownControlMaxScaledDownReplicasPercent(original["percent"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedPercent); val.IsValid() && !isEmptyValue(val) {
transformed["percent"] = transformedPercent
}

return transformed, nil
}

func expandComputeRegionAutoscalerAutoscalingPolicyScaleDownControlMaxScaledDownReplicasFixed(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeRegionAutoscalerAutoscalingPolicyScaleDownControlMaxScaledDownReplicasPercent(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeRegionAutoscalerAutoscalingPolicyScaleDownControlTimeWindowSec(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeRegionAutoscalerAutoscalingPolicyCpuUtilization(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down
99 changes: 55 additions & 44 deletions google-beta/resource_compute_region_autoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,32 @@ func TestAccComputeRegionAutoscaler_update(t *testing.T) {
})
}

func testAccComputeRegionAutoscaler_basic(it_name, tp_name, igm_name, autoscaler_name string) string {
func TestAccComputeRegionAutoscaler_scaleDownControl(t *testing.T) {
t.Parallel()

var it_name = fmt.Sprintf("region-autoscaler-test-%s", randString(t, 10))
var tp_name = fmt.Sprintf("region-autoscaler-test-%s", randString(t, 10))
var igm_name = fmt.Sprintf("region-autoscaler-test-%s", randString(t, 10))
var autoscaler_name = fmt.Sprintf("region-autoscaler-test-%s", randString(t, 10))

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeRegionAutoscalerDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeRegionAutoscaler_scaleDownControl(it_name, tp_name, igm_name, autoscaler_name),
},
{
ResourceName: "google_compute_region_autoscaler.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccComputeRegionAutoscaler_scaffolding(it_name, tp_name, igm_name string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-9"
Expand Down Expand Up @@ -84,6 +109,11 @@ resource "google_compute_region_instance_group_manager" "foobar" {
region = "us-central1"
}
`, it_name, tp_name, igm_name)
}

func testAccComputeRegionAutoscaler_basic(it_name, tp_name, igm_name, autoscaler_name string) string {
return testAccComputeRegionAutoscaler_scaffolding(it_name, tp_name, igm_name) + fmt.Sprintf(`
resource "google_compute_region_autoscaler" "foobar" {
description = "Resource created for Terraform acceptance testing"
name = "%s"
Expand All @@ -98,55 +128,30 @@ resource "google_compute_region_autoscaler" "foobar" {
}
}
}
`, it_name, tp_name, igm_name, autoscaler_name)
`, autoscaler_name)
}

func testAccComputeRegionAutoscaler_update(it_name, tp_name, igm_name, autoscaler_name string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-9"
project = "debian-cloud"
}
resource "google_compute_instance_template" "foobar" {
name = "%s"
machine_type = "n1-standard-1"
can_ip_forward = false
tags = ["foo", "bar"]
disk {
source_image = data.google_compute_image.my_image.self_link
auto_delete = true
boot = true
}
network_interface {
network = "default"
}
service_account {
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
}
}
resource "google_compute_target_pool" "foobar" {
description = "Resource created for Terraform acceptance testing"
name = "%s"
session_affinity = "CLIENT_IP_PROTO"
}
resource "google_compute_region_instance_group_manager" "foobar" {
description = "Terraform test instance group manager"
return testAccComputeRegionAutoscaler_scaffolding(it_name, tp_name, igm_name) + fmt.Sprintf(`
resource "google_compute_region_autoscaler" "foobar" {
description = "Resource created for Terraform acceptance testing"
name = "%s"
version {
instance_template = google_compute_instance_template.foobar.self_link
name = "primary"
region = "us-central1"
target = google_compute_region_instance_group_manager.foobar.self_link
autoscaling_policy {
max_replicas = 10
min_replicas = 1
cooldown_period = 60
cpu_utilization {
target = 0.5
}
}
target_pools = [google_compute_target_pool.foobar.self_link]
base_instance_name = "foobar"
region = "us-central1"
}
`, autoscaler_name)
}

func testAccComputeRegionAutoscaler_scaleDownControl(it_name, tp_name, igm_name, autoscaler_name string) string {
return testAccComputeRegionAutoscaler_scaffolding(it_name, tp_name, igm_name) + fmt.Sprintf(`
resource "google_compute_region_autoscaler" "foobar" {
description = "Resource created for Terraform acceptance testing"
name = "%s"
Expand All @@ -159,7 +164,13 @@ resource "google_compute_region_autoscaler" "foobar" {
cpu_utilization {
target = 0.5
}
scale_down_control {
max_scaled_down_replicas {
percent = 80
}
time_window_sec = 300
}
}
}
`, it_name, tp_name, igm_name, autoscaler_name)
`, autoscaler_name)
}
Loading

0 comments on commit ebcb0e8

Please sign in to comment.