github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/builtin/providers/aws/resource_aws_autoscaling_notification_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/service/autoscaling"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccASGNotification_basic(t *testing.T) {
    15  	var asgn autoscaling.DescribeNotificationConfigurationsOutput
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckASGNDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccASGNotificationConfig_basic,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckASGNotificationExists("aws_autoscaling_notification.example", []string{"foobar1-terraform-test"}, &asgn),
    26  					testAccCheckAWSASGNotificationAttributes("aws_autoscaling_notification.example", &asgn),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func TestAccASGNotification_update(t *testing.T) {
    34  	var asgn autoscaling.DescribeNotificationConfigurationsOutput
    35  
    36  	resource.Test(t, resource.TestCase{
    37  		PreCheck:     func() { testAccPreCheck(t) },
    38  		Providers:    testAccProviders,
    39  		CheckDestroy: testAccCheckASGNDestroy,
    40  		Steps: []resource.TestStep{
    41  			resource.TestStep{
    42  				Config: testAccASGNotificationConfig_basic,
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccCheckASGNotificationExists("aws_autoscaling_notification.example", []string{"foobar1-terraform-test"}, &asgn),
    45  					testAccCheckAWSASGNotificationAttributes("aws_autoscaling_notification.example", &asgn),
    46  				),
    47  			},
    48  
    49  			resource.TestStep{
    50  				Config: testAccASGNotificationConfig_update,
    51  				Check: resource.ComposeTestCheckFunc(
    52  					testAccCheckASGNotificationExists("aws_autoscaling_notification.example", []string{"foobar1-terraform-test", "barfoo-terraform-test"}, &asgn),
    53  					testAccCheckAWSASGNotificationAttributes("aws_autoscaling_notification.example", &asgn),
    54  				),
    55  			},
    56  		},
    57  	})
    58  }
    59  
    60  func testAccCheckASGNotificationExists(n string, groups []string, asgn *autoscaling.DescribeNotificationConfigurationsOutput) resource.TestCheckFunc {
    61  	return func(s *terraform.State) error {
    62  		rs, ok := s.RootModule().Resources[n]
    63  		if !ok {
    64  			return fmt.Errorf("Not found: %s", n)
    65  		}
    66  
    67  		if rs.Primary.ID == "" {
    68  			return fmt.Errorf("No ASG Notification ID is set")
    69  		}
    70  
    71  		var gl []*string
    72  		for _, g := range groups {
    73  			gl = append(gl, aws.String(g))
    74  		}
    75  
    76  		conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
    77  		opts := &autoscaling.DescribeNotificationConfigurationsInput{
    78  			AutoScalingGroupNames: gl,
    79  		}
    80  
    81  		resp, err := conn.DescribeNotificationConfigurations(opts)
    82  		if err != nil {
    83  			return fmt.Errorf("Error describing notifications")
    84  		}
    85  
    86  		*asgn = *resp
    87  
    88  		return nil
    89  	}
    90  }
    91  
    92  func testAccCheckASGNDestroy(s *terraform.State) error {
    93  	for _, rs := range s.RootModule().Resources {
    94  		if rs.Type != "aws_autoscaling_notification" {
    95  			continue
    96  		}
    97  
    98  		groups := []*string{aws.String("foobar1-terraform-test")}
    99  		conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
   100  		opts := &autoscaling.DescribeNotificationConfigurationsInput{
   101  			AutoScalingGroupNames: groups,
   102  		}
   103  
   104  		resp, err := conn.DescribeNotificationConfigurations(opts)
   105  		if err != nil {
   106  			return fmt.Errorf("Error describing notifications")
   107  		}
   108  
   109  		if len(resp.NotificationConfigurations) != 0 {
   110  			fmt.Errorf("Error finding notification descriptions")
   111  		}
   112  
   113  	}
   114  	return nil
   115  }
   116  
   117  func testAccCheckAWSASGNotificationAttributes(n string, asgn *autoscaling.DescribeNotificationConfigurationsOutput) resource.TestCheckFunc {
   118  	return func(s *terraform.State) error {
   119  		rs, ok := s.RootModule().Resources[n]
   120  		if !ok {
   121  			return fmt.Errorf("Not found: %s", n)
   122  		}
   123  
   124  		if rs.Primary.ID == "" {
   125  			return fmt.Errorf("No ASG Notification ID is set")
   126  		}
   127  
   128  		if len(asgn.NotificationConfigurations) == 0 {
   129  			return fmt.Errorf("Error: no ASG Notifications found")
   130  		}
   131  
   132  		// build a unique list of groups, notification types
   133  		gRaw := make(map[string]bool)
   134  		nRaw := make(map[string]bool)
   135  		for _, n := range asgn.NotificationConfigurations {
   136  			if *n.TopicARN == rs.Primary.Attributes["topic_arn"] {
   137  				gRaw[*n.AutoScalingGroupName] = true
   138  				nRaw[*n.NotificationType] = true
   139  			}
   140  		}
   141  
   142  		// Grab the keys here as the list of Groups
   143  		var gList []string
   144  		for k, _ := range gRaw {
   145  			gList = append(gList, k)
   146  		}
   147  
   148  		// Grab the keys here as the list of Types
   149  		var nList []string
   150  		for k, _ := range nRaw {
   151  			nList = append(nList, k)
   152  		}
   153  
   154  		typeCount, _ := strconv.Atoi(rs.Primary.Attributes["notifications.#"])
   155  
   156  		if len(nList) != typeCount {
   157  			return fmt.Errorf("Error: Bad ASG Notification count, expected (%d), got (%d)", typeCount, len(nList))
   158  		}
   159  
   160  		groupCount, _ := strconv.Atoi(rs.Primary.Attributes["group_names.#"])
   161  
   162  		if len(gList) != groupCount {
   163  			return fmt.Errorf("Error: Bad ASG Group count, expected (%d), got (%d)", typeCount, len(gList))
   164  		}
   165  
   166  		return nil
   167  	}
   168  }
   169  
   170  const testAccASGNotificationConfig_basic = `
   171  resource "aws_sns_topic" "topic_example" {
   172    name = "user-updates-topic"
   173  }
   174  
   175  resource "aws_launch_configuration" "foobar" {
   176    name = "foobarautoscaling-terraform-test"
   177    image_id = "ami-21f78e11"
   178    instance_type = "t1.micro"
   179  }
   180  
   181  resource "aws_autoscaling_group" "bar" {
   182    availability_zones = ["us-west-2a"]
   183    name = "foobar1-terraform-test"
   184    max_size = 1
   185    min_size = 1
   186    health_check_grace_period = 100
   187    health_check_type = "ELB"
   188    desired_capacity = 1
   189    force_delete = true
   190    termination_policies = ["OldestInstance"]
   191    launch_configuration = "${aws_launch_configuration.foobar.name}"
   192  }
   193  
   194  resource "aws_autoscaling_notification" "example" {
   195    group_names     = ["${aws_autoscaling_group.bar.name}"]
   196    notifications  = [
   197  	"autoscaling:EC2_INSTANCE_LAUNCH", 
   198  	"autoscaling:EC2_INSTANCE_TERMINATE", 
   199    ]
   200    topic_arn = "${aws_sns_topic.topic_example.arn}"
   201  }
   202  `
   203  
   204  const testAccASGNotificationConfig_update = `
   205  resource "aws_sns_topic" "user_updates" {
   206    name = "user-updates-topic"
   207  }
   208  
   209  resource "aws_launch_configuration" "foobar" {
   210    name = "foobarautoscaling-terraform-test"
   211    image_id = "ami-21f78e11"
   212    instance_type = "t1.micro"
   213  }
   214  
   215  resource "aws_autoscaling_group" "bar" {
   216    availability_zones = ["us-west-2a"]
   217    name = "foobar1-terraform-test"
   218    max_size = 1
   219    min_size = 1
   220    health_check_grace_period = 100
   221    health_check_type = "ELB"
   222    desired_capacity = 1
   223    force_delete = true
   224    termination_policies = ["OldestInstance"]
   225    launch_configuration = "${aws_launch_configuration.foobar.name}"
   226  }
   227  
   228  resource "aws_autoscaling_group" "foo" {
   229    availability_zones = ["us-west-2b"]
   230    name = "barfoo-terraform-test"
   231    max_size = 1
   232    min_size = 1
   233    health_check_grace_period = 200
   234    health_check_type = "ELB"
   235    desired_capacity = 1
   236    force_delete = true
   237    termination_policies = ["OldestInstance"]
   238    launch_configuration = "${aws_launch_configuration.foobar.name}"
   239  }
   240  
   241  resource "aws_autoscaling_notification" "example" {
   242  	group_names     = [
   243  	"${aws_autoscaling_group.bar.name}",
   244  	"${aws_autoscaling_group.foo.name}",
   245  	]
   246  	notifications  = [
   247  		"autoscaling:EC2_INSTANCE_LAUNCH", 
   248  		"autoscaling:EC2_INSTANCE_TERMINATE",
   249  		"autoscaling:EC2_INSTANCE_LAUNCH_ERROR"
   250  	]
   251  	topic_arn = "${aws_sns_topic.user_updates.arn}"
   252  }`