github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSASGNotification_basic(t *testing.T) {
    16  	var asgn autoscaling.DescribeNotificationConfigurationsOutput
    17  
    18  	rName := acctest.RandString(5)
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckASGNDestroy,
    24  		Steps: []resource.TestStep{
    25  			resource.TestStep{
    26  				Config: testAccASGNotificationConfig_basic(rName),
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckASGNotificationExists("aws_autoscaling_notification.example", []string{"foobar1-terraform-test-" + rName}, &asgn),
    29  					testAccCheckAWSASGNotificationAttributes("aws_autoscaling_notification.example", &asgn),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func TestAccAWSASGNotification_update(t *testing.T) {
    37  	var asgn autoscaling.DescribeNotificationConfigurationsOutput
    38  
    39  	rName := acctest.RandString(5)
    40  
    41  	resource.Test(t, resource.TestCase{
    42  		PreCheck:     func() { testAccPreCheck(t) },
    43  		Providers:    testAccProviders,
    44  		CheckDestroy: testAccCheckASGNDestroy,
    45  		Steps: []resource.TestStep{
    46  			resource.TestStep{
    47  				Config: testAccASGNotificationConfig_basic(rName),
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckASGNotificationExists("aws_autoscaling_notification.example", []string{"foobar1-terraform-test-" + rName}, &asgn),
    50  					testAccCheckAWSASGNotificationAttributes("aws_autoscaling_notification.example", &asgn),
    51  				),
    52  			},
    53  
    54  			resource.TestStep{
    55  				Config: testAccASGNotificationConfig_update(rName),
    56  				Check: resource.ComposeTestCheckFunc(
    57  					testAccCheckASGNotificationExists("aws_autoscaling_notification.example", []string{"foobar1-terraform-test-" + rName, "barfoo-terraform-test-" + rName}, &asgn),
    58  					testAccCheckAWSASGNotificationAttributes("aws_autoscaling_notification.example", &asgn),
    59  				),
    60  			},
    61  		},
    62  	})
    63  }
    64  
    65  func TestAccAWSASGNotification_Pagination(t *testing.T) {
    66  	var asgn autoscaling.DescribeNotificationConfigurationsOutput
    67  
    68  	resource.Test(t, resource.TestCase{
    69  		PreCheck:     func() { testAccPreCheck(t) },
    70  		Providers:    testAccProviders,
    71  		CheckDestroy: testAccCheckASGNDestroy,
    72  		Steps: []resource.TestStep{
    73  			resource.TestStep{
    74  				Config: testAccASGNotificationConfig_pagination,
    75  				Check: resource.ComposeTestCheckFunc(
    76  					testAccCheckASGNotificationExists("aws_autoscaling_notification.example",
    77  						[]string{
    78  							"foobar3-terraform-test-0",
    79  							"foobar3-terraform-test-1",
    80  							"foobar3-terraform-test-2",
    81  							"foobar3-terraform-test-3",
    82  							"foobar3-terraform-test-4",
    83  							"foobar3-terraform-test-5",
    84  							"foobar3-terraform-test-6",
    85  							"foobar3-terraform-test-7",
    86  							"foobar3-terraform-test-8",
    87  							"foobar3-terraform-test-9",
    88  							"foobar3-terraform-test-10",
    89  							"foobar3-terraform-test-11",
    90  							"foobar3-terraform-test-12",
    91  							"foobar3-terraform-test-13",
    92  							"foobar3-terraform-test-14",
    93  							"foobar3-terraform-test-15",
    94  							"foobar3-terraform-test-16",
    95  							"foobar3-terraform-test-17",
    96  							"foobar3-terraform-test-18",
    97  							"foobar3-terraform-test-19",
    98  						}, &asgn),
    99  					testAccCheckAWSASGNotificationAttributes("aws_autoscaling_notification.example", &asgn),
   100  				),
   101  			},
   102  		},
   103  	})
   104  }
   105  
   106  func testAccCheckASGNotificationExists(n string, groups []string, asgn *autoscaling.DescribeNotificationConfigurationsOutput) resource.TestCheckFunc {
   107  	return func(s *terraform.State) error {
   108  		rs, ok := s.RootModule().Resources[n]
   109  		if !ok {
   110  			return fmt.Errorf("Not found: %s", n)
   111  		}
   112  
   113  		if rs.Primary.ID == "" {
   114  			return fmt.Errorf("No ASG Notification ID is set")
   115  		}
   116  
   117  		conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
   118  		opts := &autoscaling.DescribeNotificationConfigurationsInput{
   119  			AutoScalingGroupNames: aws.StringSlice(groups),
   120  			MaxRecords:            aws.Int64(100),
   121  		}
   122  
   123  		resp, err := conn.DescribeNotificationConfigurations(opts)
   124  		if err != nil {
   125  			return fmt.Errorf("Error describing notifications: %s", err)
   126  		}
   127  
   128  		*asgn = *resp
   129  
   130  		return nil
   131  	}
   132  }
   133  
   134  func testAccCheckASGNDestroy(s *terraform.State) error {
   135  	for _, rs := range s.RootModule().Resources {
   136  		if rs.Type != "aws_autoscaling_notification" {
   137  			continue
   138  		}
   139  
   140  		groups := []*string{aws.String("foobar1-terraform-test")}
   141  		conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
   142  		opts := &autoscaling.DescribeNotificationConfigurationsInput{
   143  			AutoScalingGroupNames: groups,
   144  		}
   145  
   146  		resp, err := conn.DescribeNotificationConfigurations(opts)
   147  		if err != nil {
   148  			return fmt.Errorf("Error describing notifications")
   149  		}
   150  
   151  		if len(resp.NotificationConfigurations) != 0 {
   152  			return fmt.Errorf("Error finding notification descriptions")
   153  		}
   154  
   155  	}
   156  	return nil
   157  }
   158  
   159  func testAccCheckAWSASGNotificationAttributes(n string, asgn *autoscaling.DescribeNotificationConfigurationsOutput) resource.TestCheckFunc {
   160  	return func(s *terraform.State) error {
   161  		rs, ok := s.RootModule().Resources[n]
   162  		if !ok {
   163  			return fmt.Errorf("Not found: %s", n)
   164  		}
   165  
   166  		if rs.Primary.ID == "" {
   167  			return fmt.Errorf("No ASG Notification ID is set")
   168  		}
   169  
   170  		if len(asgn.NotificationConfigurations) == 0 {
   171  			return fmt.Errorf("Error: no ASG Notifications found")
   172  		}
   173  
   174  		// build a unique list of groups, notification types
   175  		gRaw := make(map[string]bool)
   176  		nRaw := make(map[string]bool)
   177  
   178  		for _, n := range asgn.NotificationConfigurations {
   179  			if *n.TopicARN == rs.Primary.Attributes["topic_arn"] {
   180  				gRaw[*n.AutoScalingGroupName] = true
   181  				nRaw[*n.NotificationType] = true
   182  			}
   183  		}
   184  
   185  		// Grab the keys here as the list of Groups
   186  		var gList []string
   187  		for k, _ := range gRaw {
   188  			gList = append(gList, k)
   189  		}
   190  
   191  		// Grab the keys here as the list of Types
   192  		var nList []string
   193  		for k, _ := range nRaw {
   194  			nList = append(nList, k)
   195  		}
   196  
   197  		typeCount, _ := strconv.Atoi(rs.Primary.Attributes["notifications.#"])
   198  
   199  		if len(nList) != typeCount {
   200  			return fmt.Errorf("Error: Bad ASG Notification count, expected (%d), got (%d)", typeCount, len(nList))
   201  		}
   202  
   203  		groupCount, _ := strconv.Atoi(rs.Primary.Attributes["group_names.#"])
   204  
   205  		if len(gList) != groupCount {
   206  			return fmt.Errorf("Error: Bad ASG Group count, expected (%d), got (%d)", typeCount, len(gList))
   207  		}
   208  
   209  		return nil
   210  	}
   211  }
   212  
   213  func testAccASGNotificationConfig_basic(rName string) string {
   214  	return fmt.Sprintf(`
   215  resource "aws_sns_topic" "topic_example" {
   216    name = "user-updates-topic-%s"
   217  }
   218  
   219  resource "aws_launch_configuration" "foobar" {
   220    name = "foobarautoscaling-terraform-test-%s"
   221    image_id = "ami-21f78e11"
   222    instance_type = "t1.micro"
   223  }
   224  
   225  resource "aws_autoscaling_group" "bar" {
   226    availability_zones = ["us-west-2a"]
   227    name = "foobar1-terraform-test-%s"
   228    max_size = 1
   229    min_size = 1
   230    health_check_grace_period = 100
   231    health_check_type = "ELB"
   232    desired_capacity = 1
   233    force_delete = true
   234    termination_policies = ["OldestInstance"]
   235    launch_configuration = "${aws_launch_configuration.foobar.name}"
   236  }
   237  
   238  resource "aws_autoscaling_notification" "example" {
   239    group_names     = ["${aws_autoscaling_group.bar.name}"]
   240    notifications  = [
   241  	"autoscaling:EC2_INSTANCE_LAUNCH", 
   242  	"autoscaling:EC2_INSTANCE_TERMINATE", 
   243    ]
   244    topic_arn = "${aws_sns_topic.topic_example.arn}"
   245  }
   246  `, rName, rName, rName)
   247  }
   248  
   249  func testAccASGNotificationConfig_update(rName string) string {
   250  	return fmt.Sprintf(`
   251  resource "aws_sns_topic" "topic_example" {
   252    name = "user-updates-topic-%s"
   253  }
   254  
   255  resource "aws_launch_configuration" "foobar" {
   256    name = "foobarautoscaling-terraform-test-%s"
   257    image_id = "ami-21f78e11"
   258    instance_type = "t1.micro"
   259  }
   260  
   261  resource "aws_autoscaling_group" "bar" {
   262    availability_zones = ["us-west-2a"]
   263    name = "foobar1-terraform-test-%s"
   264    max_size = 1
   265    min_size = 1
   266    health_check_grace_period = 100
   267    health_check_type = "ELB"
   268    desired_capacity = 1
   269    force_delete = true
   270    termination_policies = ["OldestInstance"]
   271    launch_configuration = "${aws_launch_configuration.foobar.name}"
   272  }
   273  
   274  resource "aws_autoscaling_group" "foo" {
   275    availability_zones = ["us-west-2b"]
   276    name = "barfoo-terraform-test-%s"
   277    max_size = 1
   278    min_size = 1
   279    health_check_grace_period = 200
   280    health_check_type = "ELB"
   281    desired_capacity = 1
   282    force_delete = true
   283    termination_policies = ["OldestInstance"]
   284    launch_configuration = "${aws_launch_configuration.foobar.name}"
   285  }
   286  
   287  resource "aws_autoscaling_notification" "example" {
   288  	group_names     = [
   289  	"${aws_autoscaling_group.bar.name}",
   290  	"${aws_autoscaling_group.foo.name}",
   291  	]
   292  	notifications  = [
   293  		"autoscaling:EC2_INSTANCE_LAUNCH", 
   294  		"autoscaling:EC2_INSTANCE_TERMINATE",
   295  		"autoscaling:EC2_INSTANCE_LAUNCH_ERROR"
   296  	]
   297  	topic_arn = "${aws_sns_topic.topic_example.arn}"
   298  }`, rName, rName, rName, rName)
   299  }
   300  
   301  const testAccASGNotificationConfig_pagination = `
   302  resource "aws_sns_topic" "user_updates" {
   303    name = "user-updates-topic"
   304  }
   305  
   306  resource "aws_launch_configuration" "foobar" {
   307    image_id = "ami-21f78e11"
   308    instance_type = "t1.micro"
   309  }
   310  
   311  resource "aws_autoscaling_group" "bar" {
   312    availability_zones = ["us-west-2a"]
   313    count = 20
   314    name = "foobar3-terraform-test-${count.index}"
   315    max_size = 1
   316    min_size = 0
   317    health_check_grace_period = 300
   318    health_check_type = "ELB"
   319    desired_capacity = 0
   320    force_delete = true
   321    termination_policies = ["OldestInstance"]
   322    launch_configuration = "${aws_launch_configuration.foobar.name}"
   323  }
   324  
   325  resource "aws_autoscaling_notification" "example" {
   326    group_names = [
   327      "${aws_autoscaling_group.bar.*.name}",
   328    ]
   329    notifications  = [
   330      "autoscaling:EC2_INSTANCE_LAUNCH",
   331      "autoscaling:EC2_INSTANCE_TERMINATE",
   332      "autoscaling:TEST_NOTIFICATION"
   333    ]
   334  	topic_arn = "${aws_sns_topic.user_updates.arn}"
   335  }`