github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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  			rs = rs
    40  			return fmt.Errorf("Not found: %s", n)
    41  		}
    42  
    43  		conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
    44  		params := &autoscaling.DescribeLifecycleHooksInput{
    45  			AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]),
    46  			LifecycleHookNames:   []*string{aws.String(rs.Primary.ID)},
    47  		}
    48  		resp, err := conn.DescribeLifecycleHooks(params)
    49  		if err != nil {
    50  			return err
    51  		}
    52  		if len(resp.LifecycleHooks) == 0 {
    53  			return fmt.Errorf("LifecycleHook not found")
    54  		}
    55  
    56  		return nil
    57  	}
    58  }
    59  
    60  func testAccCheckAWSAutoscalingLifecycleHookDestroy(s *terraform.State) error {
    61  	conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
    62  
    63  	for _, rs := range s.RootModule().Resources {
    64  		if rs.Type != "aws_autoscaling_group" {
    65  			continue
    66  		}
    67  
    68  		params := autoscaling.DescribeLifecycleHooksInput{
    69  			AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]),
    70  			LifecycleHookNames:   []*string{aws.String(rs.Primary.ID)},
    71  		}
    72  
    73  		resp, err := conn.DescribeLifecycleHooks(&params)
    74  
    75  		if err == nil {
    76  			if len(resp.LifecycleHooks) != 0 &&
    77  				*resp.LifecycleHooks[0].LifecycleHookName == rs.Primary.ID {
    78  				return fmt.Errorf("Lifecycle Hook Still Exists: %s", rs.Primary.ID)
    79  			}
    80  		}
    81  	}
    82  
    83  	return nil
    84  }
    85  
    86  var testAccAWSAutoscalingLifecycleHookConfig = fmt.Sprintf(`
    87  resource "aws_launch_configuration" "foobar" {
    88      name = "terraform-test-foobar5"
    89      image_id = "ami-21f78e11"
    90      instance_type = "t1.micro"
    91  }
    92  
    93  resource "aws_sqs_queue" "foobar" {
    94    name = "foobar"
    95    delay_seconds = 90
    96    max_message_size = 2048
    97    message_retention_seconds = 86400
    98    receive_wait_time_seconds = 10
    99  }
   100  
   101  resource "aws_iam_role" "foobar" {
   102      name = "foobar"
   103      assume_role_policy = <<EOF
   104  {
   105    "Version" : "2012-10-17",
   106    "Statement": [ {
   107      "Effect": "Allow",
   108      "Principal": {"AWS": "*"},
   109      "Action": [ "sts:AssumeRole" ]
   110    } ]
   111  }
   112  EOF
   113  }
   114  
   115  resource "aws_iam_role_policy" "foobar" {
   116      name = "foobar"
   117      role = "${aws_iam_role.foobar.id}"
   118      policy = <<EOF
   119  {
   120      "Version" : "2012-10-17",
   121      "Statement": [ {
   122        "Effect": "Allow",
   123        "Action": [
   124  	"sqs:SendMessage",
   125  	"sqs:GetQueueUrl",
   126  	"sns:Publish"
   127        ],
   128        "Resource": [
   129  	"${aws_sqs_queue.foobar.arn}"
   130        ]
   131      } ]
   132  }
   133  EOF
   134  }
   135  
   136  
   137  resource "aws_autoscaling_group" "foobar" {
   138      availability_zones = ["us-west-2a"]
   139      name = "terraform-test-foobar5"
   140      max_size = 5
   141      min_size = 2
   142      health_check_grace_period = 300
   143      health_check_type = "ELB"
   144      force_delete = true
   145      termination_policies = ["OldestInstance"]
   146      launch_configuration = "${aws_launch_configuration.foobar.name}"
   147      tag {
   148          key = "Foo"
   149          value = "foo-bar"
   150          propagate_at_launch = true
   151      }
   152  }
   153  
   154  resource "aws_autoscaling_lifecycle_hook" "foobar" {
   155      name = "foobar"
   156      autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
   157      default_result = "CONTINUE"
   158      heartbeat_timeout = 2000
   159      lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING"
   160      notification_metadata = <<EOF
   161  {
   162    "foo": "bar"
   163  }
   164  EOF
   165      notification_target_arn = "${aws_sqs_queue.foobar.arn}"
   166      role_arn = "${aws_iam_role.foobar.arn}"
   167  }
   168  `)