github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/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 TestAccAWSAutoscalingLifecycleHook_omitDefaultResult(t *testing.T) {
    36  	var hook autoscaling.LifecycleHook
    37  
    38  	resource.Test(t, resource.TestCase{
    39  		PreCheck:     func() { testAccPreCheck(t) },
    40  		Providers:    testAccProviders,
    41  		CheckDestroy: testAccCheckAWSAutoscalingLifecycleHookDestroy,
    42  		Steps: []resource.TestStep{
    43  			resource.TestStep{
    44  				Config: testAccAWSAutoscalingLifecycleHookConfig_omitDefaultResult,
    45  				Check: resource.ComposeTestCheckFunc(
    46  					testAccCheckLifecycleHookExists("aws_autoscaling_lifecycle_hook.foobar", &hook),
    47  					resource.TestCheckResourceAttr("aws_autoscaling_lifecycle_hook.foobar", "default_result", "ABANDON"),
    48  				),
    49  			},
    50  		},
    51  	})
    52  }
    53  
    54  func testAccCheckLifecycleHookExists(n string, hook *autoscaling.LifecycleHook) resource.TestCheckFunc {
    55  	return func(s *terraform.State) error {
    56  		rs, ok := s.RootModule().Resources[n]
    57  		if !ok {
    58  			return fmt.Errorf("Not found: %s", n)
    59  		}
    60  
    61  		conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
    62  		params := &autoscaling.DescribeLifecycleHooksInput{
    63  			AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]),
    64  			LifecycleHookNames:   []*string{aws.String(rs.Primary.ID)},
    65  		}
    66  		resp, err := conn.DescribeLifecycleHooks(params)
    67  		if err != nil {
    68  			return err
    69  		}
    70  		if len(resp.LifecycleHooks) == 0 {
    71  			return fmt.Errorf("LifecycleHook not found")
    72  		}
    73  
    74  		return nil
    75  	}
    76  }
    77  
    78  func testAccCheckAWSAutoscalingLifecycleHookDestroy(s *terraform.State) error {
    79  	conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
    80  
    81  	for _, rs := range s.RootModule().Resources {
    82  		if rs.Type != "aws_autoscaling_group" {
    83  			continue
    84  		}
    85  
    86  		params := autoscaling.DescribeLifecycleHooksInput{
    87  			AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]),
    88  			LifecycleHookNames:   []*string{aws.String(rs.Primary.ID)},
    89  		}
    90  
    91  		resp, err := conn.DescribeLifecycleHooks(&params)
    92  
    93  		if err == nil {
    94  			if len(resp.LifecycleHooks) != 0 &&
    95  				*resp.LifecycleHooks[0].LifecycleHookName == rs.Primary.ID {
    96  				return fmt.Errorf("Lifecycle Hook Still Exists: %s", rs.Primary.ID)
    97  			}
    98  		}
    99  	}
   100  
   101  	return nil
   102  }
   103  
   104  var testAccAWSAutoscalingLifecycleHookConfig = fmt.Sprintf(`
   105  resource "aws_launch_configuration" "foobar" {
   106      name = "terraform-test-foobar5"
   107      image_id = "ami-21f78e11"
   108      instance_type = "t1.micro"
   109  }
   110  
   111  resource "aws_sqs_queue" "foobar" {
   112    name = "foobar"
   113    delay_seconds = 90
   114    max_message_size = 2048
   115    message_retention_seconds = 86400
   116    receive_wait_time_seconds = 10
   117  }
   118  
   119  resource "aws_iam_role" "foobar" {
   120      name = "foobar"
   121      assume_role_policy = <<EOF
   122  {
   123    "Version" : "2012-10-17",
   124    "Statement": [ {
   125      "Effect": "Allow",
   126      "Principal": {"AWS": "*"},
   127      "Action": [ "sts:AssumeRole" ]
   128    } ]
   129  }
   130  EOF
   131  }
   132  
   133  resource "aws_iam_role_policy" "foobar" {
   134      name = "foobar"
   135      role = "${aws_iam_role.foobar.id}"
   136      policy = <<EOF
   137  {
   138      "Version" : "2012-10-17",
   139      "Statement": [ {
   140        "Effect": "Allow",
   141        "Action": [
   142  	"sqs:SendMessage",
   143  	"sqs:GetQueueUrl",
   144  	"sns:Publish"
   145        ],
   146        "Resource": [
   147  	"${aws_sqs_queue.foobar.arn}"
   148        ]
   149      } ]
   150  }
   151  EOF
   152  }
   153  
   154  
   155  resource "aws_autoscaling_group" "foobar" {
   156      availability_zones = ["us-west-2a"]
   157      name = "terraform-test-foobar5"
   158      max_size = 5
   159      min_size = 2
   160      health_check_grace_period = 300
   161      health_check_type = "ELB"
   162      force_delete = true
   163      termination_policies = ["OldestInstance"]
   164      launch_configuration = "${aws_launch_configuration.foobar.name}"
   165      tag {
   166          key = "Foo"
   167          value = "foo-bar"
   168          propagate_at_launch = true
   169      }
   170  }
   171  
   172  resource "aws_autoscaling_lifecycle_hook" "foobar" {
   173      name = "foobar"
   174      autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
   175      default_result = "CONTINUE"
   176      heartbeat_timeout = 2000
   177      lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING"
   178      notification_metadata = <<EOF
   179  {
   180    "foo": "bar"
   181  }
   182  EOF
   183      notification_target_arn = "${aws_sqs_queue.foobar.arn}"
   184      role_arn = "${aws_iam_role.foobar.arn}"
   185  }
   186  `)
   187  
   188  var testAccAWSAutoscalingLifecycleHookConfig_omitDefaultResult = fmt.Sprintf(`
   189  resource "aws_launch_configuration" "foobar" {
   190      name = "terraform-test-foobar5"
   191      image_id = "ami-21f78e11"
   192      instance_type = "t1.micro"
   193  }
   194  
   195  resource "aws_sqs_queue" "foobar" {
   196    name = "foobar"
   197    delay_seconds = 90
   198    max_message_size = 2048
   199    message_retention_seconds = 86400
   200    receive_wait_time_seconds = 10
   201  }
   202  
   203  resource "aws_iam_role" "foobar" {
   204      name = "foobar"
   205      assume_role_policy = <<EOF
   206  {
   207    "Version" : "2012-10-17",
   208    "Statement": [ {
   209      "Effect": "Allow",
   210      "Principal": {"AWS": "*"},
   211      "Action": [ "sts:AssumeRole" ]
   212    } ]
   213  }
   214  EOF
   215  }
   216  
   217  resource "aws_iam_role_policy" "foobar" {
   218      name = "foobar"
   219      role = "${aws_iam_role.foobar.id}"
   220      policy = <<EOF
   221  {
   222      "Version" : "2012-10-17",
   223      "Statement": [ {
   224        "Effect": "Allow",
   225        "Action": [
   226  	"sqs:SendMessage",
   227  	"sqs:GetQueueUrl",
   228  	"sns:Publish"
   229        ],
   230        "Resource": [
   231  	"${aws_sqs_queue.foobar.arn}"
   232        ]
   233      } ]
   234  }
   235  EOF
   236  }
   237  
   238  
   239  resource "aws_autoscaling_group" "foobar" {
   240      availability_zones = ["us-west-2a"]
   241      name = "terraform-test-foobar5"
   242      max_size = 5
   243      min_size = 2
   244      health_check_grace_period = 300
   245      health_check_type = "ELB"
   246      force_delete = true
   247      termination_policies = ["OldestInstance"]
   248      launch_configuration = "${aws_launch_configuration.foobar.name}"
   249      tag {
   250          key = "Foo"
   251          value = "foo-bar"
   252          propagate_at_launch = true
   253      }
   254  }
   255  
   256  resource "aws_autoscaling_lifecycle_hook" "foobar" {
   257      name = "foobar"
   258      autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
   259      heartbeat_timeout = 2000
   260      lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING"
   261      notification_metadata = <<EOF
   262  {
   263    "foo": "bar"
   264  }
   265  EOF
   266      notification_target_arn = "${aws_sqs_queue.foobar.arn}"
   267      role_arn = "${aws_iam_role.foobar.arn}"
   268  }
   269  `)