Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoscaling Opsworks Configuration #3165

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 213 additions & 12 deletions aws/opsworks_layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,96 @@ 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.TypeFloat,
Optional: true,
Default: -1, // -1 disables the threshold
},

"downscaling_ignore_metrics_time": {
Type: schema.TypeInt,
Optional: true,
Default: -1, // -1 disables the threshold
},

"downscaling_instance_count": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
},

"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: -1, // -1 disables the threshold
},

"upscaling_alarms": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"upscaling_cpu_threshold": {
Type: schema.TypeFloat,
Optional: true,
Default: -1, // -1 disables the threshold
},

"upscaling_ignore_metrics_time": {
Type: schema.TypeInt,
Optional: true,
Default: -1, // -1 disables the threshold
},

"upscaling_instance_count": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
},

"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: -1, // -1 disables the threshold
},
}

if lt.CustomShortName {
Expand Down Expand Up @@ -331,6 +421,22 @@ 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
}

d.Set("autoscaling", false)
if autoScalings.LoadBasedAutoScalingConfigurations != nil && len(autoScalings.LoadBasedAutoScalingConfigurations) != 0 && autoScalings.LoadBasedAutoScalingConfigurations[0] != nil {
lt.SetAutoscaling(d, autoScalings.LoadBasedAutoScalingConfigurations[0])
}

return nil
}

Expand All @@ -345,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 {
Expand All @@ -372,6 +478,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)
Expand All @@ -384,6 +491,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)
}

Expand All @@ -399,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 {
Expand Down Expand Up @@ -445,6 +560,12 @@ func (lt *opsworksLayerType) Update(d *schema.ResourceData, client *opsworks.Ops
}
}

/* Update Autoscaling */
_, erra := client.SetLoadBasedAutoScaling(lt.Autoscaling(d))
if erra != nil {
return erra
}

_, err := client.UpdateLayer(req)
if err != nil {
return err
Expand Down Expand Up @@ -643,3 +764,83 @@ 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))),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, these float64() of float64 types are triggering make lint failures in CI since they are unnecessary:

$ make lint
==> Checking source code against linters...
aws/opsworks_layers.go:1::warning: file is not gofmted with -s (gofmt)
aws/opsworks_layers.go:774:43:warning: unnecessary conversion (unconvert)
aws/opsworks_layers.go:777:43:warning: unnecessary conversion (unconvert)
aws/opsworks_layers.go:778:43:warning: unnecessary conversion (unconvert)
aws/opsworks_layers.go:783:43:warning: unnecessary conversion (unconvert)
aws/opsworks_layers.go:786:43:warning: unnecessary conversion (unconvert)
aws/opsworks_layers.go:787:43:warning: unnecessary conversion (unconvert)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! Thank you. I will check it tomorrow.

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("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))

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)
}

if as.UpScaling.IgnoreMetricsTime != nil {
d.Set("upscaling_ignore_metrics_time", as.UpScaling.IgnoreMetricsTime)
}
}
Loading