github.com/shvar/terraform@v0.6.9-0.20151215234924-3365cd2231df/builtin/providers/aws/resource_aws_autoscaling_lifecycle_hook_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/service/autoscaling" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccAWSAutoscalingLifecycleHook_basic(t *testing.T) { 14 var hook autoscaling.LifecycleHook 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckAWSAutoscalingLifecycleHookDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccAWSAutoscalingLifecycleHookConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckLifecycleHookExists("aws_autoscaling_lifecycle_hook.foobar", &hook), 25 resource.TestCheckResourceAttr("aws_autoscaling_lifecycle_hook.foobar", "autoscaling_group_name", "terraform-test-foobar5"), 26 resource.TestCheckResourceAttr("aws_autoscaling_lifecycle_hook.foobar", "default_result", "CONTINUE"), 27 resource.TestCheckResourceAttr("aws_autoscaling_lifecycle_hook.foobar", "heartbeat_timeout", "2000"), 28 resource.TestCheckResourceAttr("aws_autoscaling_lifecycle_hook.foobar", "lifecycle_transition", "autoscaling:EC2_INSTANCE_LAUNCHING"), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 func testAccCheckLifecycleHookExists(n string, hook *autoscaling.LifecycleHook) resource.TestCheckFunc { 36 return func(s *terraform.State) error { 37 rs, ok := s.RootModule().Resources[n] 38 if !ok { 39 return fmt.Errorf("Not found: %s", n) 40 } 41 42 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 43 params := &autoscaling.DescribeLifecycleHooksInput{ 44 AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]), 45 LifecycleHookNames: []*string{aws.String(rs.Primary.ID)}, 46 } 47 resp, err := conn.DescribeLifecycleHooks(params) 48 if err != nil { 49 return err 50 } 51 if len(resp.LifecycleHooks) == 0 { 52 return fmt.Errorf("LifecycleHook not found") 53 } 54 55 return nil 56 } 57 } 58 59 func testAccCheckAWSAutoscalingLifecycleHookDestroy(s *terraform.State) error { 60 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 61 62 for _, rs := range s.RootModule().Resources { 63 if rs.Type != "aws_autoscaling_group" { 64 continue 65 } 66 67 params := autoscaling.DescribeLifecycleHooksInput{ 68 AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]), 69 LifecycleHookNames: []*string{aws.String(rs.Primary.ID)}, 70 } 71 72 resp, err := conn.DescribeLifecycleHooks(¶ms) 73 74 if err == nil { 75 if len(resp.LifecycleHooks) != 0 && 76 *resp.LifecycleHooks[0].LifecycleHookName == rs.Primary.ID { 77 return fmt.Errorf("Lifecycle Hook Still Exists: %s", rs.Primary.ID) 78 } 79 } 80 } 81 82 return nil 83 } 84 85 var testAccAWSAutoscalingLifecycleHookConfig = fmt.Sprintf(` 86 resource "aws_launch_configuration" "foobar" { 87 name = "terraform-test-foobar5" 88 image_id = "ami-21f78e11" 89 instance_type = "t1.micro" 90 } 91 92 resource "aws_sqs_queue" "foobar" { 93 name = "foobar" 94 delay_seconds = 90 95 max_message_size = 2048 96 message_retention_seconds = 86400 97 receive_wait_time_seconds = 10 98 } 99 100 resource "aws_iam_role" "foobar" { 101 name = "foobar" 102 assume_role_policy = <<EOF 103 { 104 "Version" : "2012-10-17", 105 "Statement": [ { 106 "Effect": "Allow", 107 "Principal": {"AWS": "*"}, 108 "Action": [ "sts:AssumeRole" ] 109 } ] 110 } 111 EOF 112 } 113 114 resource "aws_iam_role_policy" "foobar" { 115 name = "foobar" 116 role = "${aws_iam_role.foobar.id}" 117 policy = <<EOF 118 { 119 "Version" : "2012-10-17", 120 "Statement": [ { 121 "Effect": "Allow", 122 "Action": [ 123 "sqs:SendMessage", 124 "sqs:GetQueueUrl", 125 "sns:Publish" 126 ], 127 "Resource": [ 128 "${aws_sqs_queue.foobar.arn}" 129 ] 130 } ] 131 } 132 EOF 133 } 134 135 136 resource "aws_autoscaling_group" "foobar" { 137 availability_zones = ["us-west-2a"] 138 name = "terraform-test-foobar5" 139 max_size = 5 140 min_size = 2 141 health_check_grace_period = 300 142 health_check_type = "ELB" 143 force_delete = true 144 termination_policies = ["OldestInstance"] 145 launch_configuration = "${aws_launch_configuration.foobar.name}" 146 tag { 147 key = "Foo" 148 value = "foo-bar" 149 propagate_at_launch = true 150 } 151 } 152 153 resource "aws_autoscaling_lifecycle_hook" "foobar" { 154 name = "foobar" 155 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 156 default_result = "CONTINUE" 157 heartbeat_timeout = 2000 158 lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING" 159 notification_metadata = <<EOF 160 { 161 "foo": "bar" 162 } 163 EOF 164 notification_target_arn = "${aws_sqs_queue.foobar.arn}" 165 role_arn = "${aws_iam_role.foobar.arn}" 166 } 167 `)