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