Add support for aws autoscaling lifecycle hooks.
This commit is contained in:
parent
7d600cadd2
commit
57c80a0d46
|
@ -164,6 +164,7 @@ func Provider() terraform.ResourceProvider {
|
|||
"aws_autoscaling_notification": resourceAwsAutoscalingNotification(),
|
||||
"aws_autoscaling_policy": resourceAwsAutoscalingPolicy(),
|
||||
"aws_cloudwatch_log_group": resourceAwsCloudWatchLogGroup(),
|
||||
"aws_autoscaling_lifecycle_hook": resourceAwsAutoscalingLifecycleHook(),
|
||||
"aws_cloudwatch_metric_alarm": resourceAwsCloudWatchMetricAlarm(),
|
||||
"aws_customer_gateway": resourceAwsCustomerGateway(),
|
||||
"aws_db_instance": resourceAwsDbInstance(),
|
||||
|
|
|
@ -0,0 +1,175 @@
|
|||
package aws
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/autoscaling"
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
)
|
||||
|
||||
func resourceAwsAutoscalingLifecycleHook() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: resourceAwsAutoscalingLifecycleHookPut,
|
||||
Read: resourceAwsAutoscalingLifecycleHookRead,
|
||||
Update: resourceAwsAutoscalingLifecycleHookPut,
|
||||
Delete: resourceAwsAutoscalingLifecycleHookDelete,
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
"autoscaling_group_name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
"default_result": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
"heartbeat_timeout": &schema.Schema{
|
||||
Type: schema.TypeInt,
|
||||
Optional: true,
|
||||
},
|
||||
"lifecycle_transition": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
"notification_metadata": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
"notification_target_arn": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
"role_arn": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func resourceAwsAutoscalingLifecycleHookPut(d *schema.ResourceData, meta interface{}) error {
|
||||
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
||||
|
||||
params := getAwsAutoscalingPutLifecycleHookInput(d)
|
||||
|
||||
log.Printf("[DEBUG] AutoScaling PutLifecyleHook: %#v", params)
|
||||
_, err := autoscalingconn.PutLifecycleHook(¶ms)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error putting lifecycle hook: %s", err)
|
||||
}
|
||||
|
||||
d.SetId(d.Get("name").(string))
|
||||
|
||||
return resourceAwsAutoscalingLifecycleHookRead(d, meta)
|
||||
}
|
||||
|
||||
func resourceAwsAutoscalingLifecycleHookRead(d *schema.ResourceData, meta interface{}) error {
|
||||
p, err := getAwsAutoscalingLifecycleHook(d, meta)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if p == nil {
|
||||
d.SetId("")
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Read Lifecycle Hook: ASG: %s, SH: %s, Obj: %#v", d.Get("autoscaling_group_name"), d.Get("name"), p)
|
||||
|
||||
d.Set("default_result", p.DefaultResult)
|
||||
d.Set("heartbeat_timeout", p.HeartbeatTimeout)
|
||||
d.Set("lifecycle_transition", p.LifecycleTransition)
|
||||
d.Set("notification_metadata", p.NotificationMetadata)
|
||||
d.Set("notification_target_arn", p.NotificationTargetARN)
|
||||
d.Set("name", p.LifecycleHookName)
|
||||
d.Set("role_arn", p.RoleARN)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceAwsAutoscalingLifecycleHookDelete(d *schema.ResourceData, meta interface{}) error {
|
||||
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
||||
p, err := getAwsAutoscalingLifecycleHook(d, meta)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
params := autoscaling.DeleteLifecycleHookInput{
|
||||
AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)),
|
||||
LifecycleHookName: aws.String(d.Get("name").(string)),
|
||||
}
|
||||
if _, err := autoscalingconn.DeleteLifecycleHook(¶ms); err != nil {
|
||||
return fmt.Errorf("Autoscaling Lifecycle Hook: %s ", err)
|
||||
}
|
||||
|
||||
d.SetId("")
|
||||
return nil
|
||||
}
|
||||
|
||||
func getAwsAutoscalingPutLifecycleHookInput(d *schema.ResourceData) autoscaling.PutLifecycleHookInput {
|
||||
var params = autoscaling.PutLifecycleHookInput{
|
||||
AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)),
|
||||
LifecycleHookName: aws.String(d.Get("name").(string)),
|
||||
}
|
||||
|
||||
if v, ok := d.GetOk("default_result"); ok {
|
||||
params.DefaultResult = aws.String(v.(string))
|
||||
}
|
||||
|
||||
if v, ok := d.GetOk("heartbeat_timeout"); ok {
|
||||
params.HeartbeatTimeout = aws.Int64(int64(v.(int)))
|
||||
}
|
||||
|
||||
if v, ok := d.GetOk("lifecycle_transition"); ok {
|
||||
params.LifecycleTransition = aws.String(v.(string))
|
||||
}
|
||||
|
||||
if v, ok := d.GetOk("notification_metadata"); ok {
|
||||
params.NotificationMetadata = aws.String(v.(string))
|
||||
}
|
||||
|
||||
if v, ok := d.GetOk("notification_target_arn"); ok {
|
||||
params.NotificationTargetARN = aws.String(v.(string))
|
||||
}
|
||||
|
||||
if v, ok := d.GetOk("role_arn"); ok {
|
||||
params.RoleARN = aws.String(v.(string))
|
||||
}
|
||||
|
||||
return params
|
||||
}
|
||||
|
||||
func getAwsAutoscalingLifecycleHook(d *schema.ResourceData, meta interface{}) (*autoscaling.LifecycleHook, error) {
|
||||
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
||||
|
||||
params := autoscaling.DescribeLifecycleHooksInput{
|
||||
AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)),
|
||||
LifecycleHookNames: []*string{aws.String(d.Get("name").(string))},
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] AutoScaling Lifecycle Hook Describe Params: %#v", params)
|
||||
resp, err := autoscalingconn.DescribeLifecycleHooks(¶ms)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error retrieving lifecycle hooks: %s", err)
|
||||
}
|
||||
|
||||
// find lifecycle hooks
|
||||
name := d.Get("name")
|
||||
for idx, sp := range resp.LifecycleHooks {
|
||||
if *sp.LifecycleHookName == name {
|
||||
return resp.LifecycleHooks[idx], nil
|
||||
}
|
||||
}
|
||||
|
||||
// lifecycle hook not found
|
||||
return nil, nil
|
||||
}
|
|
@ -0,0 +1,168 @@
|
|||
package aws
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/autoscaling"
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
func TestAccAWSAutoscalingLifecycleHook_basic(t *testing.T) {
|
||||
var hook autoscaling.LifecycleHook
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckAWSAutoscalingLifecycleHookDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccAWSAutoscalingLifecycleHookConfig,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckLifecycleHookExists("aws_autoscaling_lifecycle_hook.foobar", &hook),
|
||||
resource.TestCheckResourceAttr("aws_autoscaling_lifecycle_hook.foobar", "autoscaling_group_name", "terraform-test-foobar5"),
|
||||
resource.TestCheckResourceAttr("aws_autoscaling_lifecycle_hook.foobar", "default_result", "CONTINUE"),
|
||||
resource.TestCheckResourceAttr("aws_autoscaling_lifecycle_hook.foobar", "heartbeat_timeout", "2000"),
|
||||
resource.TestCheckResourceAttr("aws_autoscaling_lifecycle_hook.foobar", "lifecycle_transition", "autoscaling:EC2_INSTANCE_LAUNCHING"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccCheckLifecycleHookExists(n string, hook *autoscaling.LifecycleHook) 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.DescribeLifecycleHooksInput{
|
||||
AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]),
|
||||
LifecycleHookNames: []*string{aws.String(rs.Primary.ID)},
|
||||
}
|
||||
resp, err := conn.DescribeLifecycleHooks(params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(resp.LifecycleHooks) == 0 {
|
||||
return fmt.Errorf("LifecycleHook not found")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func testAccCheckAWSAutoscalingLifecycleHookDestroy(s *terraform.State) error {
|
||||
conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
|
||||
|
||||
for _, rs := range s.RootModule().Resources {
|
||||
if rs.Type != "aws_autoscaling_group" {
|
||||
continue
|
||||
}
|
||||
|
||||
params := autoscaling.DescribeLifecycleHooksInput{
|
||||
AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]),
|
||||
LifecycleHookNames: []*string{aws.String(rs.Primary.ID)},
|
||||
}
|
||||
|
||||
resp, err := conn.DescribeLifecycleHooks(¶ms)
|
||||
|
||||
if err == nil {
|
||||
if len(resp.LifecycleHooks) != 0 &&
|
||||
*resp.LifecycleHooks[0].LifecycleHookName == rs.Primary.ID {
|
||||
return fmt.Errorf("Lifecycle Hook Still Exists: %s", rs.Primary.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var testAccAWSAutoscalingLifecycleHookConfig = fmt.Sprintf(`
|
||||
resource "aws_launch_configuration" "foobar" {
|
||||
name = "terraform-test-foobar5"
|
||||
image_id = "ami-21f78e11"
|
||||
instance_type = "t1.micro"
|
||||
}
|
||||
|
||||
resource "aws_sqs_queue" "foobar" {
|
||||
name = "foobar"
|
||||
delay_seconds = 90
|
||||
max_message_size = 2048
|
||||
message_retention_seconds = 86400
|
||||
receive_wait_time_seconds = 10
|
||||
}
|
||||
|
||||
resource "aws_iam_role" "foobar" {
|
||||
name = "foobar"
|
||||
assume_role_policy = <<EOF
|
||||
{
|
||||
"Version" : "2012-10-17",
|
||||
"Statement": [ {
|
||||
"Effect": "Allow",
|
||||
"Principal": {"AWS": "*"},
|
||||
"Action": [ "sts:AssumeRole" ]
|
||||
} ]
|
||||
}
|
||||
EOF
|
||||
}
|
||||
|
||||
resource "aws_iam_role_policy" "foobar" {
|
||||
name = "foobar"
|
||||
role = "${aws_iam_role.foobar.id}"
|
||||
policy = <<EOF
|
||||
{
|
||||
"Version" : "2012-10-17",
|
||||
"Statement": [ {
|
||||
"Effect": "Allow",
|
||||
"Action": [
|
||||
"sqs:SendMessage",
|
||||
"sqs:GetQueueUrl",
|
||||
"sns:Publish"
|
||||
],
|
||||
"Resource": [
|
||||
"${aws_sqs_queue.foobar.arn}"
|
||||
]
|
||||
} ]
|
||||
}
|
||||
EOF
|
||||
}
|
||||
|
||||
|
||||
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"
|
||||
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_lifecycle_hook" "foobar" {
|
||||
name = "foobar"
|
||||
autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
|
||||
default_result = "CONTINUE"
|
||||
heartbeat_timeout = 2000
|
||||
lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING"
|
||||
notification_metadata = <<EOF
|
||||
{
|
||||
"foo": "bar"
|
||||
}
|
||||
EOF
|
||||
notification_target_arn = "${aws_sqs_queue.foobar.arn}"
|
||||
role_arn = "${aws_iam_role.foobar.arn}"
|
||||
}
|
||||
`)
|
|
@ -64,7 +64,7 @@
|
|||
</li>
|
||||
|
||||
<li<%= sidebar_current("docs-aws-resource-autoscaling-lifecycle-hook") %>>
|
||||
<a href="/docs/providers/aws/r/autoscaling_lifecycle_hooks.html">aws_autoscaling_lifecycle_hooks</a>
|
||||
<a href="/docs/providers/aws/r/autoscaling_lifecycle_hooks.html">aws_autoscaling_lifecycle_hook</a>
|
||||
</li>
|
||||
|
||||
<li<%= sidebar_current("docs-aws-resource-autoscaling-notification") %>>
|
||||
|
|
Loading…
Reference in New Issue