From 2be72aadc432d0809662bc967d9dfb870930cbe6 Mon Sep 17 00:00:00 2001 From: Edu Herraiz Date: Sat, 27 Jan 2018 16:50:10 +0100 Subject: [PATCH 1/8] [wip] Autoscaling configuration --- aws/opsworks_layers.go | 181 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) diff --git a/aws/opsworks_layers.go b/aws/opsworks_layers.go index d33eab61ca27..0fb1c3720b6f 100644 --- a/aws/opsworks_layers.go +++ b/aws/opsworks_layers.go @@ -202,6 +202,84 @@ func (lt *opsworksLayerType) SchemaResource() *schema.Resource { return hashcode.String(m["mount_point"].(string)) }, }, + + "autoscaling": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "downscaling_alarms": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + + "downscaling_cpu_threshold": { + Type: schema.TypeInt, + Optional: true, + }, + + "downscaling_ignore_metrics_time": { + Type: schema.TypeInt, + Optional: true, + }, + + "downscaling_instance_count": { + Type: schema.TypeInt, + Optional: true, + }, + + "downscaling_load_threshold": { + Type: schema.TypeInt, + Optional: true, + }, + + "downscaling_mem_threshold": { + Type: schema.TypeInt, + Optional: true, + }, + + "downscaling_threshold_wait_time": { + Type: schema.TypeInt, + Optional: true, + }, + + "upscaling_alarms": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + + "upscaling_cpu_threshold": { + Type: schema.TypeInt, + Optional: true, + }, + + "upscaling_ignore_metrics_time": { + Type: schema.TypeInt, + Optional: true, + }, + + "upscaling_instance_count": { + Type: schema.TypeInt, + Optional: true, + }, + + "upscaling_load_threshold": { + Type: schema.TypeInt, + Optional: true, + }, + + "upscaling_mem_threshold": { + Type: schema.TypeInt, + Optional: true, + }, + + "upscaling_threshold_wait_time": { + Type: schema.TypeInt, + Optional: true, + }, } if lt.CustomShortName { @@ -331,6 +409,26 @@ func (lt *opsworksLayerType) Read(d *schema.ResourceData, client *opsworks.OpsWo } } + /* get Autoscaling configuration */ + ascRequest := &opsworks.DescribeLoadBasedAutoScalingInput{ + LayerIds: []*string{ + aws.String(d.Id()), + }, + } + autoScalings, err := client.DescribeLoadBasedAutoScaling(ascRequest) + if err != nil { + return err + } + + if autoScalings.LoadBasedAutoScalingConfigurations == nil || len(autoScalings.LoadBasedAutoScalingConfigurations) == 0 { + d.Set("autoscaling", false) + } else { + autoScaling := autoScalings.LoadBasedAutoScalingConfigurations[0] + if autoScaling != nil { + lt.SetAutoscaling(d, autoScaling) + } + } + return nil } @@ -372,6 +470,7 @@ func (lt *opsworksLayerType) Create(d *schema.ResourceData, client *opsworks.Ops layerId := *resp.LayerId d.SetId(layerId) + /* Configure ELB */ loadBalancer := aws.String(d.Get("elastic_load_balancer").(string)) if loadBalancer != nil && *loadBalancer != "" { log.Printf("[DEBUG] Attaching load balancer: %s", *loadBalancer) @@ -384,6 +483,14 @@ func (lt *opsworksLayerType) Create(d *schema.ResourceData, client *opsworks.Ops } } + /* Configure Autoscaling */ + if d.Get("autoscaling").(bool) { + _, err := client.SetLoadBasedAutoScaling(lt.Autoscaling(d)) + if err != nil { + return err + } + } + return lt.Read(d, client) } @@ -643,3 +750,77 @@ func (lt *opsworksLayerType) SetVolumeConfigurations(d *schema.ResourceData, v [ d.Set("ebs_volume", newValue) } + + +func (lt *opsworksLayerType) Autoscaling(d *schema.ResourceData) *opsworks.SetLoadBasedAutoScalingInput { + return &opsworks.SetLoadBasedAutoScalingInput{ + Enable: aws.Bool(d.Get("autoscaling").(bool)), + LayerId: aws.String(d.Id()), + DownScaling: &opsworks.AutoScalingThresholds{ + Alarms: expandStringList(d.Get("downscaling_alarms").([]interface{})), + CpuThreshold: aws.Float64(float64(d.Get("downscaling_cpu_threshold").(float64))), + IgnoreMetricsTime: aws.Int64(int64(d.Get("downscaling_ignore_metrics_time").(int))), + InstanceCount: aws.Int64(int64(d.Get("downscaling_instance_count").(int))), + LoadThreshold: aws.Float64(float64(d.Get("downscaling_load_threshold").(float64))), + MemoryThreshold: aws.Float64(float64(d.Get("downscaling_mem_threshold").(float64))), + ThresholdsWaitTime: aws.Int64(int64(d.Get("downscaling_threshold_wait_time").(int))), + }, + UpScaling: &opsworks.AutoScalingThresholds{ + Alarms: expandStringList(d.Get("upscaling_alarms").([]interface{})), + CpuThreshold: aws.Float64(float64(d.Get("upscaling_cpu_threshold").(float64))), + IgnoreMetricsTime: aws.Int64(int64(d.Get("upscaling_ignore_metrics_time").(int))), + InstanceCount: aws.Int64(int64(d.Get("upscaling_instance_count").(int))), + LoadThreshold: aws.Float64(float64(d.Get("upscaling_load_threshold").(float64))), + MemoryThreshold: aws.Float64(float64(d.Get("upscaling_mem_threshold").(float64))), + ThresholdsWaitTime: aws.Int64(int64(d.Get("upscaling_threshold_wait_time").(int))), + }, + } +} + + +func (lt *opsworksLayerType) SetAutoscaling(d *schema.ResourceData, as *opsworks.LoadBasedAutoScalingConfiguration) { + d.Set("autoscaling", as.Enable) + d.Set("downscaling_alarms", flattenStringList(as.DownScaling.Alarms)) + + if as.DownScaling.CpuThreshold != nil { + d.Set("downscaling_cpu_threshold", as.DownScaling.CpuThreshold) + } + + if as.DownScaling.InstanceCount != nil { + d.Set("downscaling_instance_count", as.DownScaling.InstanceCount) + } + + if as.DownScaling.LoadThreshold != nil { + d.Set("downscaling_load_threshold", as.DownScaling.LoadThreshold) + } + + if as.DownScaling.MemoryThreshold != nil { + d.Set("downscaling_mem_threshold", as.DownScaling.MemoryThreshold) + } + + if as.DownScaling.ThresholdsWaitTime != nil { + d.Set("upscaling_threshold_wait_time", as.DownScaling.ThresholdsWaitTime) + } + + d.Set("upscaling_alarms", flattenStringList(as.UpScaling.Alarms)) + + if as.UpScaling.CpuThreshold != nil { + d.Set("upscaling_cpu_threshold", as.UpScaling.CpuThreshold) + } + + if as.UpScaling.InstanceCount != nil { + d.Set("upscaling_instance_count", as.UpScaling.InstanceCount) + } + + if as.UpScaling.LoadThreshold != nil { + d.Set("upscaling_load_threshold", as.UpScaling.LoadThreshold) + } + + if as.UpScaling.MemoryThreshold != nil { + d.Set("upscaling_mem_threshold", as.UpScaling.MemoryThreshold) + } + + if as.UpScaling.ThresholdsWaitTime != nil { + d.Set("upscaling_threshold_wait_time", as.UpScaling.ThresholdsWaitTime) + } +} From 18b42d0a7264ef955ca51d7114ea1af9e893af40 Mon Sep 17 00:00:00 2001 From: Edu Herraiz Date: Sat, 27 Jan 2018 17:33:07 +0100 Subject: [PATCH 2/8] [wip] Autoscaling configuration on update --- aws/opsworks_layers.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/aws/opsworks_layers.go b/aws/opsworks_layers.go index 0fb1c3720b6f..83d2c1a1f1b5 100644 --- a/aws/opsworks_layers.go +++ b/aws/opsworks_layers.go @@ -552,6 +552,12 @@ func (lt *opsworksLayerType) Update(d *schema.ResourceData, client *opsworks.Ops } } + /* Update Autoscaling */ + _, err := client.SetLoadBasedAutoScaling(lt.Autoscaling(d)) + if err != nil { + return err + } + _, err := client.UpdateLayer(req) if err != nil { return err From 907eb862f8c69ab0efa66b1a3d28392776ef5a10 Mon Sep 17 00:00:00 2001 From: Edu Herraiz Date: Sat, 27 Jan 2018 21:59:22 +0100 Subject: [PATCH 3/8] [wip] Document an Update, ready to test --- aws/opsworks_layers.go | 20 +++++++++---------- .../r/opsworks_custom_layer.html.markdown | 19 ++++++++++++++++++ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/aws/opsworks_layers.go b/aws/opsworks_layers.go index 83d2c1a1f1b5..b89de5569649 100644 --- a/aws/opsworks_layers.go +++ b/aws/opsworks_layers.go @@ -216,7 +216,7 @@ func (lt *opsworksLayerType) SchemaResource() *schema.Resource { }, "downscaling_cpu_threshold": { - Type: schema.TypeInt, + Type: schema.TypeFloat, Optional: true, }, @@ -231,17 +231,17 @@ func (lt *opsworksLayerType) SchemaResource() *schema.Resource { }, "downscaling_load_threshold": { - Type: schema.TypeInt, + Type: schema.TypeFloat, Optional: true, }, "downscaling_mem_threshold": { - Type: schema.TypeInt, + Type: schema.TypeFloat, Optional: true, }, "downscaling_threshold_wait_time": { - Type: schema.TypeInt, + Type: schema.TypeFloat, Optional: true, }, @@ -252,7 +252,7 @@ func (lt *opsworksLayerType) SchemaResource() *schema.Resource { }, "upscaling_cpu_threshold": { - Type: schema.TypeInt, + Type: schema.TypeFloat, Optional: true, }, @@ -267,12 +267,12 @@ func (lt *opsworksLayerType) SchemaResource() *schema.Resource { }, "upscaling_load_threshold": { - Type: schema.TypeInt, + Type: schema.TypeFloat, Optional: true, }, "upscaling_mem_threshold": { - Type: schema.TypeInt, + Type: schema.TypeFloat, Optional: true, }, @@ -553,9 +553,9 @@ func (lt *opsworksLayerType) Update(d *schema.ResourceData, client *opsworks.Ops } /* Update Autoscaling */ - _, err := client.SetLoadBasedAutoScaling(lt.Autoscaling(d)) - if err != nil { - return err + _, erra := client.SetLoadBasedAutoScaling(lt.Autoscaling(d)) + if erra != nil { + return erra } _, err := client.UpdateLayer(req) diff --git a/website/docs/r/opsworks_custom_layer.html.markdown b/website/docs/r/opsworks_custom_layer.html.markdown index b8a4563106c3..6635fbe1bb04 100644 --- a/website/docs/r/opsworks_custom_layer.html.markdown +++ b/website/docs/r/opsworks_custom_layer.html.markdown @@ -60,6 +60,25 @@ An `ebs_volume` block supports the following arguments: * `type` - (Optional) The type of volume to create. This may be `standard` (the default), `io1` or `gp2`. * `iops` - (Optional) For PIOPS volumes, the IOPS per disk. +We can configure the autoscaling Options with the following arguments: + +* `autoscaling` - (Optional) Enable autoscaling configuration for the layer. Default false. +* `downscaling_alarms`- (Optional) List of alarms to downscale. +* `downscaling_cpu_threshold` - (Optional) CPU threshold to downscale. +* `downscaling_ignore_metrics_time`- (Optional) Number of minutes ignore metrics after downscale. +* `downscaling_instance_count` - (Optional) Stop servers in batches of this number on downscale. +* `downscaling_load_threshold` - (Optional) Load threshold to downscale. +* `downscaling_mem_threshold` - (Optional) Mem threshold to downscale. +* `downscaling_threshold_wait_time` - (Optional) Number of minutes undershot the threshold to downscale. +* `upscaling_alarms`- (Optional) List of alarms to upscale. +* `upscaling_cpu_threshold` - (Optional) CPU threshold to upscale. +* `upscaling_ignore_metrics_time`- (Optional) Number of minutes ignore metrics after upscale. +* `upscaling_instance_count` - (Optional) Start servers in batches of this number on upscale. +* `upscaling_load_threshold` - (Optional) Load threshold to upscale. +* `upscaling_mem_threshold` - (Optional) Mem threshold to upscale. +* `upscaling_threshold_wait_time` - (Optional) Number of minutes exceed the threshold to upscale. + + ## Attributes Reference The following attributes are exported: From 75bf6cd9d9b4aaa87aa290904128ae527d31017d Mon Sep 17 00:00:00 2001 From: Edu Herraiz Date: Sun, 28 Jan 2018 03:30:29 +0100 Subject: [PATCH 4/8] [wip] Functional and documented --- aws/opsworks_layers.go | 83 +++++++++++++------ .../r/opsworks_custom_layer.html.markdown | 28 ++++--- 2 files changed, 73 insertions(+), 38 deletions(-) diff --git a/aws/opsworks_layers.go b/aws/opsworks_layers.go index b89de5569649..a80050293d10 100644 --- a/aws/opsworks_layers.go +++ b/aws/opsworks_layers.go @@ -218,31 +218,37 @@ func (lt *opsworksLayerType) SchemaResource() *schema.Resource { "downscaling_cpu_threshold": { Type: schema.TypeFloat, Optional: true, + Default: -1, }, "downscaling_ignore_metrics_time": { Type: schema.TypeInt, Optional: true, + Default: 10, }, "downscaling_instance_count": { Type: schema.TypeInt, Optional: true, + Default: 1, }, "downscaling_load_threshold": { Type: schema.TypeFloat, Optional: true, + Default: -1, }, "downscaling_mem_threshold": { Type: schema.TypeFloat, Optional: true, + Default: -1, }, "downscaling_threshold_wait_time": { - Type: schema.TypeFloat, + Type: schema.TypeInt, Optional: true, + Default: 10, }, "upscaling_alarms": { @@ -254,31 +260,37 @@ func (lt *opsworksLayerType) SchemaResource() *schema.Resource { "upscaling_cpu_threshold": { Type: schema.TypeFloat, Optional: true, + Default: -1, }, "upscaling_ignore_metrics_time": { Type: schema.TypeInt, Optional: true, + Default: 5, }, "upscaling_instance_count": { Type: schema.TypeInt, Optional: true, + Default: 1, }, "upscaling_load_threshold": { Type: schema.TypeFloat, Optional: true, + Default: -1, }, "upscaling_mem_threshold": { Type: schema.TypeFloat, Optional: true, + Default: -1, }, "upscaling_threshold_wait_time": { Type: schema.TypeInt, Optional: true, + Default: 5, }, } @@ -757,37 +769,50 @@ func (lt *opsworksLayerType) SetVolumeConfigurations(d *schema.ResourceData, v [ d.Set("ebs_volume", newValue) } - func (lt *opsworksLayerType) Autoscaling(d *schema.ResourceData) *opsworks.SetLoadBasedAutoScalingInput { return &opsworks.SetLoadBasedAutoScalingInput{ - Enable: aws.Bool(d.Get("autoscaling").(bool)), - LayerId: aws.String(d.Id()), - DownScaling: &opsworks.AutoScalingThresholds{ - Alarms: expandStringList(d.Get("downscaling_alarms").([]interface{})), - CpuThreshold: aws.Float64(float64(d.Get("downscaling_cpu_threshold").(float64))), - IgnoreMetricsTime: aws.Int64(int64(d.Get("downscaling_ignore_metrics_time").(int))), - InstanceCount: aws.Int64(int64(d.Get("downscaling_instance_count").(int))), - LoadThreshold: aws.Float64(float64(d.Get("downscaling_load_threshold").(float64))), - MemoryThreshold: aws.Float64(float64(d.Get("downscaling_mem_threshold").(float64))), - ThresholdsWaitTime: aws.Int64(int64(d.Get("downscaling_threshold_wait_time").(int))), - }, - UpScaling: &opsworks.AutoScalingThresholds{ - Alarms: expandStringList(d.Get("upscaling_alarms").([]interface{})), - CpuThreshold: aws.Float64(float64(d.Get("upscaling_cpu_threshold").(float64))), - IgnoreMetricsTime: aws.Int64(int64(d.Get("upscaling_ignore_metrics_time").(int))), - InstanceCount: aws.Int64(int64(d.Get("upscaling_instance_count").(int))), - LoadThreshold: aws.Float64(float64(d.Get("upscaling_load_threshold").(float64))), - MemoryThreshold: aws.Float64(float64(d.Get("upscaling_mem_threshold").(float64))), - ThresholdsWaitTime: aws.Int64(int64(d.Get("upscaling_threshold_wait_time").(int))), - }, - } + Enable: aws.Bool(d.Get("autoscaling").(bool)), + LayerId: aws.String(d.Id()), + DownScaling: &opsworks.AutoScalingThresholds{ + Alarms: expandStringList(d.Get("downscaling_alarms").([]interface{})), + CpuThreshold: aws.Float64(float64(d.Get("downscaling_cpu_threshold").(float64))), + IgnoreMetricsTime: aws.Int64(int64(d.Get("downscaling_ignore_metrics_time").(int))), + InstanceCount: aws.Int64(int64(d.Get("downscaling_instance_count").(int))), + LoadThreshold: aws.Float64(float64(d.Get("downscaling_load_threshold").(float64))), + MemoryThreshold: aws.Float64(float64(d.Get("downscaling_mem_threshold").(float64))), + ThresholdsWaitTime: aws.Int64(int64(d.Get("downscaling_threshold_wait_time").(int))), + }, + UpScaling: &opsworks.AutoScalingThresholds{ + Alarms: expandStringList(d.Get("upscaling_alarms").([]interface{})), + CpuThreshold: aws.Float64(float64(d.Get("upscaling_cpu_threshold").(float64))), + IgnoreMetricsTime: aws.Int64(int64(d.Get("upscaling_ignore_metrics_time").(int))), + InstanceCount: aws.Int64(int64(d.Get("upscaling_instance_count").(int))), + LoadThreshold: aws.Float64(float64(d.Get("upscaling_load_threshold").(float64))), + MemoryThreshold: aws.Float64(float64(d.Get("upscaling_mem_threshold").(float64))), + ThresholdsWaitTime: aws.Int64(int64(d.Get("upscaling_threshold_wait_time").(int))), + }, + } } - func (lt *opsworksLayerType) SetAutoscaling(d *schema.ResourceData, as *opsworks.LoadBasedAutoScalingConfiguration) { d.Set("autoscaling", as.Enable) d.Set("downscaling_alarms", flattenStringList(as.DownScaling.Alarms)) + /* TODO: Setup the cpu threshold if nothing is configured */ + /* In that case, we should use the same default values creates AWS */ + /* Anyway is commented in doc you should use at least one */ + /* if as.DownScaling.CpuThreshold == nil && + as.DownScaling.MemoryThreshold == nil && + as.DownScaling.LoadThreshold == nil && + len(flattenStringList(as.DownScaling.Alarms)) == 0 && + as.UpScaling.CpuThreshold == nil && + as.UpScaling.MemoryThreshold == nil && + as.UpScaling.LoadThreshold == nil && + len(flattenStringList(as.UpScaling.Alarms)) == 0 { + d.Set("upscaling_cpu_threshold", 80) + d.Set("downscaling_cpu_threshold", 30) + }*/ + if as.DownScaling.CpuThreshold != nil { d.Set("downscaling_cpu_threshold", as.DownScaling.CpuThreshold) } @@ -805,7 +830,11 @@ func (lt *opsworksLayerType) SetAutoscaling(d *schema.ResourceData, as *opsworks } if as.DownScaling.ThresholdsWaitTime != nil { - d.Set("upscaling_threshold_wait_time", as.DownScaling.ThresholdsWaitTime) + d.Set("downscaling_threshold_wait_time", as.DownScaling.ThresholdsWaitTime) + } + + if as.DownScaling.IgnoreMetricsTime != nil { + d.Set("downscaling_ignore_metrics_time", as.DownScaling.IgnoreMetricsTime) } d.Set("upscaling_alarms", flattenStringList(as.UpScaling.Alarms)) @@ -829,4 +858,8 @@ func (lt *opsworksLayerType) SetAutoscaling(d *schema.ResourceData, as *opsworks if as.UpScaling.ThresholdsWaitTime != nil { d.Set("upscaling_threshold_wait_time", as.UpScaling.ThresholdsWaitTime) } + + if as.UpScaling.IgnoreMetricsTime != nil { + d.Set("upscaling_ignore_metrics_time", as.UpScaling.IgnoreMetricsTime) + } } diff --git a/website/docs/r/opsworks_custom_layer.html.markdown b/website/docs/r/opsworks_custom_layer.html.markdown index 6635fbe1bb04..ff19eebf5534 100644 --- a/website/docs/r/opsworks_custom_layer.html.markdown +++ b/website/docs/r/opsworks_custom_layer.html.markdown @@ -60,23 +60,25 @@ An `ebs_volume` block supports the following arguments: * `type` - (Optional) The type of volume to create. This may be `standard` (the default), `io1` or `gp2`. * `iops` - (Optional) For PIOPS volumes, the IOPS per disk. -We can configure the autoscaling Options with the following arguments: +For the autoscaling you have to configure at least one of the threshold options (cpu, mem, load or alarms). +If you don't set at least one you'll receive a API error. +The `autoscaling` parameters are: * `autoscaling` - (Optional) Enable autoscaling configuration for the layer. Default false. * `downscaling_alarms`- (Optional) List of alarms to downscale. -* `downscaling_cpu_threshold` - (Optional) CPU threshold to downscale. -* `downscaling_ignore_metrics_time`- (Optional) Number of minutes ignore metrics after downscale. -* `downscaling_instance_count` - (Optional) Stop servers in batches of this number on downscale. -* `downscaling_load_threshold` - (Optional) Load threshold to downscale. -* `downscaling_mem_threshold` - (Optional) Mem threshold to downscale. -* `downscaling_threshold_wait_time` - (Optional) Number of minutes undershot the threshold to downscale. +* `downscaling_cpu_threshold` - (Optional) CPU threshold to downscale. Default -1 (disabled). +* `downscaling_ignore_metrics_time`- (Optional) Number of minutes ignore metrics after downscale. Default 10. +* `downscaling_instance_count` - (Optional) Stop servers in batches of this number on downscale. Default 1 +* `downscaling_load_threshold` - (Optional) Load threshold to downscale. Default -1 (disabled) +* `downscaling_mem_threshold` - (Optional) Mem threshold to downscale. Default -1 (disabled) +* `downscaling_threshold_wait_time` - (Optional) Number of minutes undershot the threshold to downscale. Default 10. * `upscaling_alarms`- (Optional) List of alarms to upscale. -* `upscaling_cpu_threshold` - (Optional) CPU threshold to upscale. -* `upscaling_ignore_metrics_time`- (Optional) Number of minutes ignore metrics after upscale. -* `upscaling_instance_count` - (Optional) Start servers in batches of this number on upscale. -* `upscaling_load_threshold` - (Optional) Load threshold to upscale. -* `upscaling_mem_threshold` - (Optional) Mem threshold to upscale. -* `upscaling_threshold_wait_time` - (Optional) Number of minutes exceed the threshold to upscale. +* `upscaling_cpu_threshold` - (Optional) CPU threshold to upscale. Default -1 (disabled) +* `upscaling_ignore_metrics_time`- (Optional) Number of minutes ignore metrics after upscale. Default 5. +* `upscaling_instance_count` - (Optional) Start servers in batches of this number on upscale. Default 1. +* `upscaling_load_threshold` - (Optional) Load threshold to upscale. Default -1 (disabled) +* `upscaling_mem_threshold` - (Optional) Mem threshold to upscale. Default -1 (disabled) +* `upscaling_threshold_wait_time` - (Optional) Number of minutes exceed the threshold to upscale. Default 5. ## Attributes Reference From 16b68d198cc1f1e9ecaab6f036ce182eb317da5b Mon Sep 17 00:00:00 2001 From: Edu Herraiz Date: Sun, 28 Jan 2018 05:12:09 +0100 Subject: [PATCH 5/8] Added tests for autoscaling, passed ok. make testacc TEST=./aws TESTARGS='-run=TestAccAWSOpsworksCustomLayer' ==> Checking that code complies with gofmt requirements... TF_ACC=1 go test ./aws -v -run=TestAccAWSOpsworksCustomLayer -timeout 120m === RUN TestAccAWSOpsworksCustomLayerImportBasic --- PASS: TestAccAWSOpsworksCustomLayerImportBasic (107.21s) === RUN TestAccAWSOpsworksCustomLayer --- PASS: TestAccAWSOpsworksCustomLayer (104.25s) PASS ok github.com/terraform-providers/terraform-provider-aws/aws 211.472s --- aws/opsworks_layers.go | 8 +- ...resource_aws_opsworks_custom_layer_test.go | 106 +++++++++++++++++- 2 files changed, 107 insertions(+), 7 deletions(-) diff --git a/aws/opsworks_layers.go b/aws/opsworks_layers.go index a80050293d10..4a4da671e9dd 100644 --- a/aws/opsworks_layers.go +++ b/aws/opsworks_layers.go @@ -218,7 +218,7 @@ func (lt *opsworksLayerType) SchemaResource() *schema.Resource { "downscaling_cpu_threshold": { Type: schema.TypeFloat, Optional: true, - Default: -1, + Default: 30, }, "downscaling_ignore_metrics_time": { @@ -236,13 +236,11 @@ func (lt *opsworksLayerType) SchemaResource() *schema.Resource { "downscaling_load_threshold": { Type: schema.TypeFloat, Optional: true, - Default: -1, }, "downscaling_mem_threshold": { Type: schema.TypeFloat, Optional: true, - Default: -1, }, "downscaling_threshold_wait_time": { @@ -260,7 +258,7 @@ func (lt *opsworksLayerType) SchemaResource() *schema.Resource { "upscaling_cpu_threshold": { Type: schema.TypeFloat, Optional: true, - Default: -1, + Default: 80, }, "upscaling_ignore_metrics_time": { @@ -278,13 +276,11 @@ func (lt *opsworksLayerType) SchemaResource() *schema.Resource { "upscaling_load_threshold": { Type: schema.TypeFloat, Optional: true, - Default: -1, }, "upscaling_mem_threshold": { Type: schema.TypeFloat, Optional: true, - Default: -1, }, "upscaling_threshold_wait_time": { diff --git a/aws/resource_aws_opsworks_custom_layer_test.go b/aws/resource_aws_opsworks_custom_layer_test.go index 95a36bd2cac3..967599539cfd 100644 --- a/aws/resource_aws_opsworks_custom_layer_test.go +++ b/aws/resource_aws_opsworks_custom_layer_test.go @@ -72,6 +72,45 @@ func TestAccAWSOpsworksCustomLayer(t *testing.T) { resource.TestCheckResourceAttr( "aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.size", "100", ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "autoscaling", "true", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "downscaling_cpu_threshold", "20", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "downscaling_ignore_metrics_time", "15", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "downscaling_instance_count", "2", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "downscaling_load_threshold", "5", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "downscaling_mem_threshold", "20", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "downscaling_threshold_wait_time", "30", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "upscaling_cpu_threshold", "80", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "downscaling_ignore_metrics_time", "15", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "upscaling_instance_count", "3", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "upscaling_load_threshold", "10", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "upscaling_mem_threshold", "80", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "upscaling_threshold_wait_time", "30", + ), ), }, { @@ -137,6 +176,45 @@ func TestAccAWSOpsworksCustomLayer(t *testing.T) { resource.TestCheckResourceAttr( "aws_opsworks_custom_layer.tf-acc", "custom_json", `{"layer_key":"layer_value2"}`, ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "autoscaling", "true", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "downscaling_cpu_threshold", "21", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "downscaling_ignore_metrics_time", "16", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "downscaling_instance_count", "3", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "downscaling_load_threshold", "6", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "downscaling_mem_threshold", "21", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "downscaling_threshold_wait_time", "31", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "upscaling_cpu_threshold", "81", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "downscaling_ignore_metrics_time", "16", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "upscaling_instance_count", "4", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "upscaling_load_threshold", "11", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "upscaling_mem_threshold", "81", + ), + resource.TestCheckResourceAttr( + "aws_opsworks_custom_layer.tf-acc", "upscaling_threshold_wait_time", "31", + ), ), }, }, @@ -308,7 +386,20 @@ resource "aws_opsworks_custom_layer" "tf-acc" { mount_point = "/home" size = 100 raid_level = 0 - } + }, + autoscaling = true + downscaling_cpu_threshold = 20 + downscaling_ignore_metrics_time = 15 + downscaling_instance_count = 2 + downscaling_load_threshold = 5 + downscaling_mem_threshold = 20 + downscaling_threshold_wait_time = 30 + upscaling_cpu_threshold = 80 + upscaling_ignore_metrics_time = 15 + upscaling_instance_count = 3 + upscaling_load_threshold = 10 + upscaling_mem_threshold = 80 + upscaling_threshold_wait_time = 30 } %s @@ -399,6 +490,19 @@ resource "aws_opsworks_custom_layer" "tf-acc" { iops = 3000 } custom_json = "{\"layer_key\": \"layer_value2\"}" + autoscaling = true + downscaling_cpu_threshold = 21 + downscaling_ignore_metrics_time = 16 + downscaling_instance_count = 3 + downscaling_load_threshold = 6 + downscaling_mem_threshold = 21 + downscaling_threshold_wait_time = 31 + upscaling_cpu_threshold = 81 + upscaling_ignore_metrics_time = 11 + upscaling_instance_count = 4 + upscaling_load_threshold = 11 + upscaling_mem_threshold = 81 + upscaling_threshold_wait_time = 31 } %s From 2c194e56be9497c08e748d9749eb3a8a0289032c Mon Sep 17 00:00:00 2001 From: Edu Herraiz Date: Sun, 28 Jan 2018 05:17:36 +0100 Subject: [PATCH 6/8] Fix dic --- website/docs/r/opsworks_custom_layer.html.markdown | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/website/docs/r/opsworks_custom_layer.html.markdown b/website/docs/r/opsworks_custom_layer.html.markdown index ff19eebf5534..a1caced8519c 100644 --- a/website/docs/r/opsworks_custom_layer.html.markdown +++ b/website/docs/r/opsworks_custom_layer.html.markdown @@ -66,18 +66,18 @@ The `autoscaling` parameters are: * `autoscaling` - (Optional) Enable autoscaling configuration for the layer. Default false. * `downscaling_alarms`- (Optional) List of alarms to downscale. -* `downscaling_cpu_threshold` - (Optional) CPU threshold to downscale. Default -1 (disabled). +* `downscaling_cpu_threshold` - (Optional) CPU threshold to downscale. Default 20. * `downscaling_ignore_metrics_time`- (Optional) Number of minutes ignore metrics after downscale. Default 10. * `downscaling_instance_count` - (Optional) Stop servers in batches of this number on downscale. Default 1 -* `downscaling_load_threshold` - (Optional) Load threshold to downscale. Default -1 (disabled) -* `downscaling_mem_threshold` - (Optional) Mem threshold to downscale. Default -1 (disabled) +* `downscaling_load_threshold` - (Optional) Load threshold to downscale. Default disabled. +* `downscaling_mem_threshold` - (Optional) Mem threshold to downscale. Default disabled. * `downscaling_threshold_wait_time` - (Optional) Number of minutes undershot the threshold to downscale. Default 10. * `upscaling_alarms`- (Optional) List of alarms to upscale. -* `upscaling_cpu_threshold` - (Optional) CPU threshold to upscale. Default -1 (disabled) +* `upscaling_cpu_threshold` - (Optional) CPU threshold to upscale. Default 80. * `upscaling_ignore_metrics_time`- (Optional) Number of minutes ignore metrics after upscale. Default 5. * `upscaling_instance_count` - (Optional) Start servers in batches of this number on upscale. Default 1. -* `upscaling_load_threshold` - (Optional) Load threshold to upscale. Default -1 (disabled) -* `upscaling_mem_threshold` - (Optional) Mem threshold to upscale. Default -1 (disabled) +* `upscaling_load_threshold` - (Optional) Load threshold to upscale. Default disabled. +* `upscaling_mem_threshold` - (Optional) Mem threshold to upscale. Default disabled. * `upscaling_threshold_wait_time` - (Optional) Number of minutes exceed the threshold to upscale. Default 5. From 1fac1e2265e2b599e3538061a54c4b855390cd6e Mon Sep 17 00:00:00 2001 From: Edu Herraiz Date: Sun, 28 Jan 2018 05:30:55 +0100 Subject: [PATCH 7/8] Clean code --- aws/opsworks_layers.go | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/aws/opsworks_layers.go b/aws/opsworks_layers.go index 4a4da671e9dd..50665a0151c1 100644 --- a/aws/opsworks_layers.go +++ b/aws/opsworks_layers.go @@ -794,21 +794,6 @@ func (lt *opsworksLayerType) SetAutoscaling(d *schema.ResourceData, as *opsworks d.Set("autoscaling", as.Enable) d.Set("downscaling_alarms", flattenStringList(as.DownScaling.Alarms)) - /* TODO: Setup the cpu threshold if nothing is configured */ - /* In that case, we should use the same default values creates AWS */ - /* Anyway is commented in doc you should use at least one */ - /* if as.DownScaling.CpuThreshold == nil && - as.DownScaling.MemoryThreshold == nil && - as.DownScaling.LoadThreshold == nil && - len(flattenStringList(as.DownScaling.Alarms)) == 0 && - as.UpScaling.CpuThreshold == nil && - as.UpScaling.MemoryThreshold == nil && - as.UpScaling.LoadThreshold == nil && - len(flattenStringList(as.UpScaling.Alarms)) == 0 { - d.Set("upscaling_cpu_threshold", 80) - d.Set("downscaling_cpu_threshold", 30) - }*/ - if as.DownScaling.CpuThreshold != nil { d.Set("downscaling_cpu_threshold", as.DownScaling.CpuThreshold) } From 68e1a26e3bd754780d253bed769ee1c11135a346 Mon Sep 17 00:00:00 2001 From: Edu Herraiz Date: Tue, 9 Oct 2018 10:34:24 +0200 Subject: [PATCH 8/8] Default values and small refactor --- aws/opsworks_layers.go | 50 +++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/aws/opsworks_layers.go b/aws/opsworks_layers.go index 1081552d9f2b..d5d997e66a1d 100644 --- a/aws/opsworks_layers.go +++ b/aws/opsworks_layers.go @@ -218,13 +218,13 @@ func (lt *opsworksLayerType) SchemaResource() *schema.Resource { "downscaling_cpu_threshold": { Type: schema.TypeFloat, Optional: true, - Default: 30, + Default: -1, // -1 disables the threshold }, "downscaling_ignore_metrics_time": { Type: schema.TypeInt, Optional: true, - Default: 10, + Default: -1, // -1 disables the threshold }, "downscaling_instance_count": { @@ -236,17 +236,19 @@ func (lt *opsworksLayerType) SchemaResource() *schema.Resource { "downscaling_load_threshold": { Type: schema.TypeFloat, Optional: true, + Default: -1, // -1 disables the threshold }, "downscaling_mem_threshold": { Type: schema.TypeFloat, Optional: true, + Default: -1, // -1 disables the threshold }, "downscaling_threshold_wait_time": { Type: schema.TypeInt, Optional: true, - Default: 10, + Default: -1, // -1 disables the threshold }, "upscaling_alarms": { @@ -258,13 +260,13 @@ func (lt *opsworksLayerType) SchemaResource() *schema.Resource { "upscaling_cpu_threshold": { Type: schema.TypeFloat, Optional: true, - Default: 80, + Default: -1, // -1 disables the threshold }, "upscaling_ignore_metrics_time": { Type: schema.TypeInt, Optional: true, - Default: 5, + Default: -1, // -1 disables the threshold }, "upscaling_instance_count": { @@ -276,17 +278,19 @@ func (lt *opsworksLayerType) SchemaResource() *schema.Resource { "upscaling_load_threshold": { Type: schema.TypeFloat, Optional: true, + Default: -1, // -1 disables the threshold }, "upscaling_mem_threshold": { Type: schema.TypeFloat, Optional: true, + Default: -1, // -1 disables the threshold }, "upscaling_threshold_wait_time": { Type: schema.TypeInt, Optional: true, - Default: 5, + Default: -1, // -1 disables the threshold }, } @@ -428,13 +432,9 @@ func (lt *opsworksLayerType) Read(d *schema.ResourceData, client *opsworks.OpsWo return err } - if autoScalings.LoadBasedAutoScalingConfigurations == nil || len(autoScalings.LoadBasedAutoScalingConfigurations) == 0 { - d.Set("autoscaling", false) - } else { - autoScaling := autoScalings.LoadBasedAutoScalingConfigurations[0] - if autoScaling != nil { - lt.SetAutoscaling(d, autoScaling) - } + d.Set("autoscaling", false) + if autoScalings.LoadBasedAutoScalingConfigurations != nil && len(autoScalings.LoadBasedAutoScalingConfigurations) != 0 && autoScalings.LoadBasedAutoScalingConfigurations[0] != nil { + lt.SetAutoscaling(d, autoScalings.LoadBasedAutoScalingConfigurations[0]) } return nil @@ -451,13 +451,13 @@ func (lt *opsworksLayerType) Create(d *schema.ResourceData, client *opsworks.Ops EnableAutoHealing: aws.Bool(d.Get("auto_healing").(bool)), InstallUpdatesOnBoot: aws.Bool(d.Get("install_updates_on_boot").(bool)), LifecycleEventConfiguration: lt.LifecycleEventConfiguration(d), - Name: aws.String(d.Get("name").(string)), - Packages: expandStringSet(d.Get("system_packages").(*schema.Set)), - Type: aws.String(lt.TypeName), - StackId: aws.String(d.Get("stack_id").(string)), - UseEbsOptimizedInstances: aws.Bool(d.Get("use_ebs_optimized_instances").(bool)), - Attributes: lt.AttributeMap(d), - VolumeConfigurations: lt.VolumeConfigurations(d), + Name: aws.String(d.Get("name").(string)), + Packages: expandStringSet(d.Get("system_packages").(*schema.Set)), + Type: aws.String(lt.TypeName), + StackId: aws.String(d.Get("stack_id").(string)), + UseEbsOptimizedInstances: aws.Bool(d.Get("use_ebs_optimized_instances").(bool)), + Attributes: lt.AttributeMap(d), + VolumeConfigurations: lt.VolumeConfigurations(d), } if lt.CustomShortName { @@ -514,11 +514,11 @@ func (lt *opsworksLayerType) Update(d *schema.ResourceData, client *opsworks.Ops EnableAutoHealing: aws.Bool(d.Get("auto_healing").(bool)), InstallUpdatesOnBoot: aws.Bool(d.Get("install_updates_on_boot").(bool)), LifecycleEventConfiguration: lt.LifecycleEventConfiguration(d), - Name: aws.String(d.Get("name").(string)), - Packages: expandStringSet(d.Get("system_packages").(*schema.Set)), - UseEbsOptimizedInstances: aws.Bool(d.Get("use_ebs_optimized_instances").(bool)), - Attributes: lt.AttributeMap(d), - VolumeConfigurations: lt.VolumeConfigurations(d), + Name: aws.String(d.Get("name").(string)), + Packages: expandStringSet(d.Get("system_packages").(*schema.Set)), + UseEbsOptimizedInstances: aws.Bool(d.Get("use_ebs_optimized_instances").(bool)), + Attributes: lt.AttributeMap(d), + VolumeConfigurations: lt.VolumeConfigurations(d), } if lt.CustomShortName {