Skip to content

Commit

Permalink
provider/aws: Addition of suspended_processes to aws_autoscaling_group
Browse files Browse the repository at this point in the history
Fixes #8954

Addition of the support of autoScaling group suspended_processes
  • Loading branch information
stack72 committed Nov 13, 2016
1 parent 8d993d9 commit 476d0fe
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 0 deletions.
81 changes: 81 additions & 0 deletions builtin/providers/aws/resource_aws_autoscaling_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ func resourceAwsAutoscalingGroup() *schema.Resource {
Set: schema.HashString,
},

"suspended_processes": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},

"metrics_granularity": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -381,6 +388,13 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{})
return err
}

if _, ok := d.GetOk("suspended_processes"); ok {
suspendedProcessesErr := enableASGSuspendedProcesses(d, conn)
if suspendedProcessesErr != nil {
return suspendedProcessesErr
}
}

if _, ok := d.GetOk("enabled_metrics"); ok {
metricsErr := enableASGMetricsCollection(d, conn)
if metricsErr != nil {
Expand Down Expand Up @@ -412,6 +426,11 @@ func resourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) e
d.Set("health_check_type", g.HealthCheckType)
d.Set("launch_configuration", g.LaunchConfigurationName)
d.Set("load_balancers", flattenStringList(g.LoadBalancerNames))
if g.SuspendedProcesses != nil {
if err := d.Set("suspended_processes", flattenAsgSuspendedProcesses(g.SuspendedProcesses)); err != nil {
log.Printf("[WARN] Error setting suspended_processes for %q: %s", d.Id(), err)
}
}
if err := d.Set("target_group_arns", flattenStringList(g.TargetGroupARNs)); err != nil {
log.Printf("[ERR] Error setting target groups: %s", err)
}
Expand Down Expand Up @@ -606,6 +625,12 @@ func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{})
}
}

if d.HasChange("suspended_processes") {
if err := updateASGSuspendedProcesses(d, conn); err != nil {
return errwrap.Wrapf("Error updating AutoScaling Group Suspended Processes: {{err}}", err)
}
}

return resourceAwsAutoscalingGroupRead(d, meta)
}

Expand Down Expand Up @@ -741,6 +766,20 @@ func resourceAwsAutoscalingGroupDrain(d *schema.ResourceData, meta interface{})
})
}

func enableASGSuspendedProcesses(d *schema.ResourceData, conn *autoscaling.AutoScaling) error {
props := &autoscaling.ScalingProcessQuery{
AutoScalingGroupName: aws.String(d.Id()),
ScalingProcesses: expandStringList(d.Get("suspended_processes").(*schema.Set).List()),
}

_, err := conn.SuspendProcesses(props)
if err != nil {
return err
}

return nil
}

func enableASGMetricsCollection(d *schema.ResourceData, conn *autoscaling.AutoScaling) error {
props := &autoscaling.EnableMetricsCollectionInput{
AutoScalingGroupName: aws.String(d.Id()),
Expand All @@ -757,6 +796,48 @@ func enableASGMetricsCollection(d *schema.ResourceData, conn *autoscaling.AutoSc
return nil
}

func updateASGSuspendedProcesses(d *schema.ResourceData, conn *autoscaling.AutoScaling) error {
o, n := d.GetChange("suspended_processes")
if o == nil {
o = new(schema.Set)
}
if n == nil {
n = new(schema.Set)
}

os := o.(*schema.Set)
ns := n.(*schema.Set)

resumeProcesses := os.Difference(ns)
if resumeProcesses.Len() != 0 {
props := &autoscaling.ScalingProcessQuery{
AutoScalingGroupName: aws.String(d.Id()),
ScalingProcesses: expandStringList(resumeProcesses.List()),
}

_, err := conn.ResumeProcesses(props)
if err != nil {
return fmt.Errorf("Error Resuming Processes for ASG %q: %s", d.Id(), err)
}
}

suspendedProcesses := ns.Difference(os)
if suspendedProcesses.Len() != 0 {
props := &autoscaling.ScalingProcessQuery{
AutoScalingGroupName: aws.String(d.Id()),
ScalingProcesses: expandStringList(suspendedProcesses.List()),
}

_, err := conn.SuspendProcesses(props)
if err != nil {
return fmt.Errorf("Error Suspending Processes for ASG %q: %s", d.Id(), err)
}
}

return nil

}

func updateASGMetricsCollection(d *schema.ResourceData, conn *autoscaling.AutoScaling) error {

o, n := d.GetChange("enabled_metrics")
Expand Down
107 changes: 107 additions & 0 deletions builtin/providers/aws/resource_aws_autoscaling_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,43 @@ func TestAccAWSAutoScalingGroup_enablingMetrics(t *testing.T) {
})
}

func TestAccAWSAutoScalingGroup_suspendingProcesses(t *testing.T) {
var group autoscaling.Group
randName := fmt.Sprintf("terraform-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAutoScalingGroupConfig(randName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "suspended_processes.#", ""),
),
},
{
Config: testAccAWSAutoScalingGroupConfigWithSuspendedProcesses(randName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "suspended_processes.#", "2"),
),
},
{
Config: testAccAWSAutoScalingGroupConfigWithSuspendedProcessesUpdated(randName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "suspended_processes.#", "2"),
),
},
},
})
}

func TestAccAWSAutoScalingGroup_withMetrics(t *testing.T) {
var group autoscaling.Group

Expand Down Expand Up @@ -1302,3 +1339,73 @@ resource "aws_autoscaling_group" "bar" {
}
`, name)
}

func testAccAWSAutoScalingGroupConfigWithSuspendedProcesses(name string) string {
return fmt.Sprintf(`
resource "aws_launch_configuration" "foobar" {
image_id = "ami-21f78e11"
instance_type = "t1.micro"
}
resource "aws_placement_group" "test" {
name = "asg_pg_%s"
strategy = "cluster"
}
resource "aws_autoscaling_group" "bar" {
availability_zones = ["us-west-2a"]
name = "%s"
max_size = 5
min_size = 2
health_check_type = "ELB"
desired_capacity = 4
force_delete = true
termination_policies = ["OldestInstance","ClosestToNextInstanceHour"]
launch_configuration = "${aws_launch_configuration.foobar.name}"
suspended_processes = ["AlarmNotification","ScheduledActions"]
tag {
key = "Foo"
value = "foo-bar"
propagate_at_launch = true
}
}
`, name, name)
}

func testAccAWSAutoScalingGroupConfigWithSuspendedProcessesUpdated(name string) string {
return fmt.Sprintf(`
resource "aws_launch_configuration" "foobar" {
image_id = "ami-21f78e11"
instance_type = "t1.micro"
}
resource "aws_placement_group" "test" {
name = "asg_pg_%s"
strategy = "cluster"
}
resource "aws_autoscaling_group" "bar" {
availability_zones = ["us-west-2a"]
name = "%s"
max_size = 5
min_size = 2
health_check_type = "ELB"
desired_capacity = 4
force_delete = true
termination_policies = ["OldestInstance","ClosestToNextInstanceHour"]
launch_configuration = "${aws_launch_configuration.foobar.name}"
suspended_processes = ["AZRebalance","ScheduledActions"]
tag {
key = "Foo"
value = "foo-bar"
propagate_at_launch = true
}
}
`, name, name)
}
10 changes: 10 additions & 0 deletions builtin/providers/aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,16 @@ func flattenCloudFormationOutputs(cfOutputs []*cloudformation.Output) map[string
return outputs
}

func flattenAsgSuspendedProcesses(list []*autoscaling.SuspendedProcess) []string {
strs := make([]string, 0, len(list))
for _, r := range list {
if r.ProcessName != nil {
strs = append(strs, *r.ProcessName)
}
}
return strs
}

func flattenAsgEnabledMetrics(list []*autoscaling.EnabledMetric) []string {
strs := make([]string, 0, len(list))
for _, r := range list {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ The following arguments are supported:
* `target_group_arns` (Optional) A list of `aws_alb_target_group` ARNs, for use with
Application Load Balancing
* `termination_policies` (Optional) A list of policies to decide how the instances in the auto scale group should be terminated. The allowed values are `OldestInstance`, `NewestInstance`, `OldestLaunchConfiguration`, `ClosestToNextInstanceHour`, `Default`.
* `suspended_processes` - (Optional) A list of processes to suspend for the AutoScaling Group. The allowed values are `Launch`, `Terminate`, `HealthCheck`, `ReplaceUnhealthy`, `AZRebalance`, `AlarmNotification`, `ScheduledActions`, `AddToLoadBalancer`.
Note that if you suspend either the `Launch` or `Terminate` process types, it can prevent your autoscaling group from functioning properly.
* `tag` (Optional) A list of tag blocks. Tags documented below.
* `placement_group` (Optional) The name of the placement group into which you'll launch your instances, if any.
* `metrics_granularity` - (Optional) The granularity to associate with the metrics to collect. The only valid value is `1Minute`. Default is `1Minute`.
Expand Down

0 comments on commit 476d0fe

Please sign in to comment.