-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/aws: Add autoscaling_policy
- Loading branch information
1 parent
a7ba47c
commit 81435f3
Showing
5 changed files
with
363 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
builtin/providers/aws/resource_aws_autoscaling_policy.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/awslabs/aws-sdk-go/aws" | ||
"github.com/awslabs/aws-sdk-go/service/autoscaling" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsAutoscalingPolicy() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsAutoscalingPolicyCreate, | ||
Read: resourceAwsAutoscalingPolicyRead, | ||
Update: resourceAwsAutoscalingPolicyUpdate, | ||
Delete: resourceAwsAutoscalingPolicyDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"adjustment_type": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"autoscaling_group_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"cooldown": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
}, | ||
"min_adjustment_step": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
}, | ||
"policy_arn": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"scaling_adjustment": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsAutoscalingPolicyCreate(d *schema.ResourceData, meta interface{}) error { | ||
autoscalingconn := meta.(*AWSClient).autoscalingconn | ||
|
||
params := getAwsAutoscalingPutScalingPolicyInput(d) | ||
|
||
log.Printf("[DEBUG] AutoScaling PutScalingPolicy: %#v", params) | ||
resp, err := autoscalingconn.PutScalingPolicy(¶ms) | ||
if err != nil { | ||
return fmt.Errorf("Error putting scaling policy: %s", err) | ||
} | ||
|
||
d.Set("policy_arn", resp.PolicyARN) | ||
d.SetId(d.Get("name").(string)) | ||
log.Printf("[INFO] AutoScaling Scaling PolicyARN: %s", d.Get("policy_arn").(string)) | ||
|
||
return resourceAwsAutoscalingPolicyRead(d, meta) | ||
} | ||
|
||
func resourceAwsAutoscalingPolicyRead(d *schema.ResourceData, meta interface{}) error { | ||
p, err := getAwsAutoscalingPolicy(d, meta) | ||
if err != nil { | ||
return err | ||
} | ||
if p == nil { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
log.Printf("[DEBUG] Read Scaling Policy: ASG: %s, SP: %s, Obj: %#v", d.Get("autoscaling_group_name"), d.Get("name"), p) | ||
|
||
d.Set("adjustment_type", p.AdjustmentType) | ||
d.Set("autoscaling_group_name", p.AutoScalingGroupName) | ||
d.Set("cooldown", p.Cooldown) | ||
d.Set("min_adjustment_step", p.MinAdjustmentStep) | ||
d.Set("policy_arn", p.PolicyARN) | ||
d.Set("name", p.PolicyName) | ||
d.Set("scaling_adjustment", p.ScalingAdjustment) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsAutoscalingPolicyUpdate(d *schema.ResourceData, meta interface{}) error { | ||
autoscalingconn := meta.(*AWSClient).autoscalingconn | ||
|
||
params := getAwsAutoscalingPutScalingPolicyInput(d) | ||
|
||
log.Printf("[DEBUG] Autoscaling Update Scaling Policy: %#v", params) | ||
_, err := autoscalingconn.PutScalingPolicy(¶ms) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return resourceAwsAutoscalingPolicyRead(d, meta) | ||
} | ||
|
||
func resourceAwsAutoscalingPolicyDelete(d *schema.ResourceData, meta interface{}) error { | ||
autoscalingconn := meta.(*AWSClient).autoscalingconn | ||
p, err := getAwsAutoscalingPolicy(d, meta) | ||
if err != nil { | ||
return err | ||
} | ||
if p == nil { | ||
return nil | ||
} | ||
|
||
params := autoscaling.DeletePolicyInput{ | ||
AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)), | ||
PolicyName: aws.String(d.Get("name").(string)), | ||
} | ||
if _, err := autoscalingconn.DeletePolicy(¶ms); err != nil { | ||
return fmt.Errorf("Autoscaling Scaling Policy: %s ", err) | ||
} | ||
|
||
d.SetId("") | ||
return nil | ||
} | ||
|
||
// PutScalingPolicy seems to require all params to be resent, so create and update can share this common function | ||
func getAwsAutoscalingPutScalingPolicyInput(d *schema.ResourceData) autoscaling.PutScalingPolicyInput { | ||
var params = autoscaling.PutScalingPolicyInput{ | ||
AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)), | ||
PolicyName: aws.String(d.Get("name").(string)), | ||
} | ||
|
||
if v, ok := d.GetOk("adjustment_type"); ok { | ||
params.AdjustmentType = aws.String(v.(string)) | ||
} | ||
|
||
if v, ok := d.GetOk("cooldown"); ok { | ||
params.Cooldown = aws.Long(int64(v.(int))) | ||
} | ||
|
||
if v, ok := d.GetOk("scaling_adjustment"); ok { | ||
params.ScalingAdjustment = aws.Long(int64(v.(int))) | ||
} | ||
|
||
if v, ok := d.GetOk("min_adjustment_step"); ok { | ||
params.MinAdjustmentStep = aws.Long(int64(v.(int))) | ||
} | ||
|
||
return params | ||
} | ||
|
||
func getAwsAutoscalingPolicy(d *schema.ResourceData, meta interface{}) (*autoscaling.ScalingPolicy, error) { | ||
autoscalingconn := meta.(*AWSClient).autoscalingconn | ||
|
||
params := autoscaling.DescribePoliciesInput{ | ||
AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)), | ||
PolicyNames: []*string{aws.String(d.Get("name").(string))}, | ||
} | ||
|
||
log.Printf("[DEBUG] AutoScaling Scaling Policy Describe Params: %#v", params) | ||
resp, err := autoscalingconn.DescribePolicies(¶ms) | ||
if err != nil { | ||
return nil, fmt.Errorf("Error retrieving scaling policies: %s", err) | ||
} | ||
|
||
// find scaling policy | ||
name := d.Get("name") | ||
for idx, sp := range resp.ScalingPolicies { | ||
if *sp.PolicyName == name { | ||
return resp.ScalingPolicies[idx], nil | ||
} | ||
} | ||
|
||
// policy not found | ||
return nil, nil | ||
} |
118 changes: 118 additions & 0 deletions
118
builtin/providers/aws/resource_aws_autoscaling_policy_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/awslabs/aws-sdk-go/aws" | ||
"github.com/awslabs/aws-sdk-go/service/autoscaling" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAWSAutoscalingPolicy_basic(t *testing.T) { | ||
var policy autoscaling.ScalingPolicy | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSAutoscalingPolicyDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccAWSAutoscalingPolicyConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar", &policy), | ||
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar", "adjustment_type", "ChangeInCapacity"), | ||
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar", "cooldown", "300"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckScalingPolicyExists(n string, policy *autoscaling.ScalingPolicy) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
rs = rs | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
conn := testAccProvider.Meta().(*AWSClient).autoscalingconn | ||
params := &autoscaling.DescribePoliciesInput{ | ||
AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]), | ||
PolicyNames: []*string{aws.String(rs.Primary.ID)}, | ||
} | ||
resp, err := conn.DescribePolicies(params) | ||
if err != nil { | ||
return err | ||
} | ||
if len(resp.ScalingPolicies) == 0 { | ||
return fmt.Errorf("ScalingPolicy not found") | ||
} | ||
|
||
*policy = *resp.ScalingPolicies[0] | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckAWSAutoscalingPolicyDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).autoscalingconn | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_autoscaling_group" { | ||
continue | ||
} | ||
|
||
params := autoscaling.DescribePoliciesInput{ | ||
AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]), | ||
PolicyNames: []*string{aws.String(rs.Primary.ID)}, | ||
} | ||
|
||
resp, err := conn.DescribePolicies(¶ms) | ||
|
||
if err == nil { | ||
if len(resp.ScalingPolicies) != 0 && | ||
*resp.ScalingPolicies[0].PolicyName == rs.Primary.ID { | ||
return fmt.Errorf("Scaling Policy Still Exists: %s", rs.Primary.ID) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
var testAccAWSAutoscalingPolicyConfig = fmt.Sprintf(` | ||
resource "aws_launch_configuration" "foobar" { | ||
name = "terraform-test-foobar5" | ||
image_id = "ami-21f78e11" | ||
instance_type = "t1.micro" | ||
} | ||
resource "aws_autoscaling_group" "foobar" { | ||
availability_zones = ["us-west-2a"] | ||
name = "terraform-test-foobar5" | ||
max_size = 5 | ||
min_size = 2 | ||
health_check_grace_period = 300 | ||
health_check_type = "ELB" | ||
desired_capacity = 4 | ||
force_delete = true | ||
termination_policies = ["OldestInstance"] | ||
launch_configuration = "${aws_launch_configuration.foobar.name}" | ||
tag { | ||
key = "Foo" | ||
value = "foo-bar" | ||
propagate_at_launch = true | ||
} | ||
} | ||
resource "aws_autoscaling_policy" "foobar" { | ||
name = "foobar" | ||
scaling_adjustment = 4 | ||
adjustment_type = "ChangeInCapacity" | ||
cooldown = 300 | ||
autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" | ||
} | ||
`) |
59 changes: 59 additions & 0 deletions
59
website/source/docs/providers/aws/r/autoscale_scaling_policy.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_autoscaling_policy" | ||
sidebar_current: "docs-aws-resource-autoscale-policy" | ||
description: |- | ||
Provides an AutoScaling Scaling Group resource. | ||
--- | ||
|
||
# aws\_autoscaling\_scaling\_policy | ||
|
||
Provides an AutoScaling Scaling Policy resource. | ||
|
||
## Example Usage | ||
``` | ||
resource "aws_autoscaling_group" "bar" { | ||
availability_zones = ["us-east-1a"] | ||
name = "foobar3-terraform-test" | ||
max_size = 5 | ||
min_size = 2 | ||
health_check_grace_period = 300 | ||
health_check_type = "ELB" | ||
desired_capacity = 4 | ||
force_delete = true | ||
launch_configuration = "${aws_launch_configuration.foobar.name}" | ||
tag { | ||
key = "foo" | ||
value = "bar" | ||
propagate_at_launch = true | ||
} | ||
tag { | ||
key = "lorem" | ||
value = "ipsum" | ||
propagate_at_launch = false | ||
} | ||
} | ||
resource "aws_autoscaling_policy" "bat" { | ||
name = "foobar3-terraform-test" | ||
scaling_adjustment = 4 | ||
adjustment_type = "ChangeInCapacity" | ||
cooldown = 300 | ||
autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `adjustment_type` - (Required) Specifies whether the `scaling_adjustment` is an absolute number or a percentage of the current capacity. Valid values are `ChangeInCapacity`, `ExactCapacity`, and `PercentChangeInCapacity`. | ||
* `autoscaling_group_name` - (Required) The name or ARN of the group. | ||
* `cooldown` - (Optional) The amount of time, in seconds, after a scaling activity completes and before the next scaling activity can start. | ||
* `min_adjustment_step` - (Optional) Used with `adjustment_type` with the value `PercentChangeInCapacity`, the scaling policy changes the `desired_capacity` of the Auto Scaling group by at least the number of instances specified in the value. | ||
* `name` - (Required) The name of the policy. | ||
* `scaling_adjustment` - (Required) The number of instances by which to scale. `adjustment_type` determines the interpretation of this number (e.g., as an absolute number or as a percentage of the existing Auto Scaling group size). A positive increment adds to the current capacity and a negative value removes from the current capacity. | ||
|
||
## Attribute Reference | ||
* `policy_arn` - The ARN assigned by AWS to the scaling policy. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters