github.com/aznashwan/terraform@v0.4.3-0.20151118032030-21f93ca4558d/builtin/providers/aws/resource_aws_elb_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"reflect"
     7  	"regexp"
     8  	"sort"
     9  	"testing"
    10  
    11  	"github.com/aws/aws-sdk-go/aws"
    12  	"github.com/aws/aws-sdk-go/aws/awserr"
    13  	"github.com/aws/aws-sdk-go/service/elb"
    14  	"github.com/hashicorp/terraform/helper/resource"
    15  	"github.com/hashicorp/terraform/terraform"
    16  )
    17  
    18  func TestAccAWSELB_basic(t *testing.T) {
    19  	var conf elb.LoadBalancerDescription
    20  	ssl_certificate_id := os.Getenv("AWS_SSL_CERTIFICATE_ID")
    21  
    22  	resource.Test(t, resource.TestCase{
    23  		PreCheck:     func() { testAccPreCheck(t) },
    24  		Providers:    testAccProviders,
    25  		CheckDestroy: testAccCheckAWSELBDestroy,
    26  		Steps: []resource.TestStep{
    27  			resource.TestStep{
    28  				Config: testAccAWSELBConfig,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
    31  					testAccCheckAWSELBAttributes(&conf),
    32  					resource.TestCheckResourceAttr(
    33  						"aws_elb.bar", "name", "foobar-terraform-test"),
    34  					resource.TestCheckResourceAttr(
    35  						"aws_elb.bar", "availability_zones.2487133097", "us-west-2a"),
    36  					resource.TestCheckResourceAttr(
    37  						"aws_elb.bar", "availability_zones.221770259", "us-west-2b"),
    38  					resource.TestCheckResourceAttr(
    39  						"aws_elb.bar", "availability_zones.2050015877", "us-west-2c"),
    40  					resource.TestCheckResourceAttr(
    41  						"aws_elb.bar", "listener.206423021.instance_port", "8000"),
    42  					resource.TestCheckResourceAttr(
    43  						"aws_elb.bar", "listener.206423021.instance_protocol", "http"),
    44  					resource.TestCheckResourceAttr(
    45  						"aws_elb.bar", "listener.206423021.ssl_certificate_id", ssl_certificate_id),
    46  					resource.TestCheckResourceAttr(
    47  						"aws_elb.bar", "listener.206423021.lb_port", "80"),
    48  					resource.TestCheckResourceAttr(
    49  						"aws_elb.bar", "listener.206423021.lb_protocol", "http"),
    50  					resource.TestCheckResourceAttr(
    51  						"aws_elb.bar", "cross_zone_load_balancing", "true"),
    52  				),
    53  			},
    54  		},
    55  	})
    56  }
    57  
    58  func TestAccAWSELB_fullCharacterRange(t *testing.T) {
    59  	var conf elb.LoadBalancerDescription
    60  
    61  	resource.Test(t, resource.TestCase{
    62  		PreCheck:     func() { testAccPreCheck(t) },
    63  		Providers:    testAccProviders,
    64  		CheckDestroy: testAccCheckAWSELBDestroy,
    65  		Steps: []resource.TestStep{
    66  			resource.TestStep{
    67  				Config: testAccAWSELBFullRangeOfCharacters,
    68  				Check: resource.ComposeTestCheckFunc(
    69  					testAccCheckAWSELBExists("aws_elb.foo", &conf),
    70  					resource.TestCheckResourceAttr(
    71  						"aws_elb.foo", "name", "FoobarTerraform-test123"),
    72  				),
    73  			},
    74  		},
    75  	})
    76  }
    77  
    78  func TestAccAWSELB_AccessLogs(t *testing.T) {
    79  	var conf elb.LoadBalancerDescription
    80  
    81  	resource.Test(t, resource.TestCase{
    82  		PreCheck:     func() { testAccPreCheck(t) },
    83  		Providers:    testAccProviders,
    84  		CheckDestroy: testAccCheckAWSELBDestroy,
    85  		Steps: []resource.TestStep{
    86  			resource.TestStep{
    87  				Config: testAccAWSELBAccessLogs,
    88  				Check: resource.ComposeTestCheckFunc(
    89  					testAccCheckAWSELBExists("aws_elb.foo", &conf),
    90  					resource.TestCheckResourceAttr(
    91  						"aws_elb.foo", "name", "FoobarTerraform-test123"),
    92  				),
    93  			},
    94  
    95  			resource.TestStep{
    96  				Config: testAccAWSELBAccessLogsOn,
    97  				Check: resource.ComposeTestCheckFunc(
    98  					testAccCheckAWSELBExists("aws_elb.foo", &conf),
    99  					resource.TestCheckResourceAttr(
   100  						"aws_elb.foo", "name", "FoobarTerraform-test123"),
   101  					resource.TestCheckResourceAttr(
   102  						"aws_elb.foo", "access_logs.#", "1"),
   103  					resource.TestCheckResourceAttr(
   104  						"aws_elb.foo", "access_logs.1713209538.bucket", "terraform-access-logs-bucket"),
   105  					resource.TestCheckResourceAttr(
   106  						"aws_elb.foo", "access_logs.1713209538.interval", "5"),
   107  				),
   108  			},
   109  
   110  			resource.TestStep{
   111  				Config: testAccAWSELBAccessLogs,
   112  				Check: resource.ComposeTestCheckFunc(
   113  					testAccCheckAWSELBExists("aws_elb.foo", &conf),
   114  					resource.TestCheckResourceAttr(
   115  						"aws_elb.foo", "name", "FoobarTerraform-test123"),
   116  					resource.TestCheckResourceAttr(
   117  						"aws_elb.foo", "access_logs.#", "0"),
   118  				),
   119  			},
   120  		},
   121  	})
   122  }
   123  
   124  func TestAccAWSELB_generatedName(t *testing.T) {
   125  	var conf elb.LoadBalancerDescription
   126  	generatedNameRegexp := regexp.MustCompile("^tf-lb-")
   127  
   128  	resource.Test(t, resource.TestCase{
   129  		PreCheck:     func() { testAccPreCheck(t) },
   130  		Providers:    testAccProviders,
   131  		CheckDestroy: testAccCheckAWSELBDestroy,
   132  		Steps: []resource.TestStep{
   133  			resource.TestStep{
   134  				Config: testAccAWSELBGeneratedName,
   135  				Check: resource.ComposeTestCheckFunc(
   136  					testAccCheckAWSELBExists("aws_elb.foo", &conf),
   137  					resource.TestMatchResourceAttr(
   138  						"aws_elb.foo", "name", generatedNameRegexp),
   139  				),
   140  			},
   141  		},
   142  	})
   143  }
   144  
   145  func TestAccAWSELB_tags(t *testing.T) {
   146  	var conf elb.LoadBalancerDescription
   147  	var td elb.TagDescription
   148  
   149  	resource.Test(t, resource.TestCase{
   150  		PreCheck:     func() { testAccPreCheck(t) },
   151  		Providers:    testAccProviders,
   152  		CheckDestroy: testAccCheckAWSELBDestroy,
   153  		Steps: []resource.TestStep{
   154  			resource.TestStep{
   155  				Config: testAccAWSELBConfig,
   156  				Check: resource.ComposeTestCheckFunc(
   157  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   158  					testAccCheckAWSELBAttributes(&conf),
   159  					resource.TestCheckResourceAttr(
   160  						"aws_elb.bar", "name", "foobar-terraform-test"),
   161  					testAccLoadTags(&conf, &td),
   162  					testAccCheckELBTags(&td.Tags, "bar", "baz"),
   163  				),
   164  			},
   165  
   166  			resource.TestStep{
   167  				Config: testAccAWSELBConfig_TagUpdate,
   168  				Check: resource.ComposeTestCheckFunc(
   169  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   170  					testAccCheckAWSELBAttributes(&conf),
   171  					resource.TestCheckResourceAttr(
   172  						"aws_elb.bar", "name", "foobar-terraform-test"),
   173  					testAccLoadTags(&conf, &td),
   174  					testAccCheckELBTags(&td.Tags, "foo", "bar"),
   175  					testAccCheckELBTags(&td.Tags, "new", "type"),
   176  				),
   177  			},
   178  		},
   179  	})
   180  }
   181  
   182  func testAccLoadTags(conf *elb.LoadBalancerDescription, td *elb.TagDescription) resource.TestCheckFunc {
   183  	return func(s *terraform.State) error {
   184  		conn := testAccProvider.Meta().(*AWSClient).elbconn
   185  
   186  		describe, err := conn.DescribeTags(&elb.DescribeTagsInput{
   187  			LoadBalancerNames: []*string{conf.LoadBalancerName},
   188  		})
   189  
   190  		if err != nil {
   191  			return err
   192  		}
   193  		if len(describe.TagDescriptions) > 0 {
   194  			*td = *describe.TagDescriptions[0]
   195  		}
   196  		return nil
   197  	}
   198  }
   199  
   200  func TestAccAWSELB_InstanceAttaching(t *testing.T) {
   201  	var conf elb.LoadBalancerDescription
   202  
   203  	testCheckInstanceAttached := func(count int) resource.TestCheckFunc {
   204  		return func(*terraform.State) error {
   205  			if len(conf.Instances) != count {
   206  				return fmt.Errorf("instance count does not match")
   207  			}
   208  			return nil
   209  		}
   210  	}
   211  
   212  	resource.Test(t, resource.TestCase{
   213  		PreCheck:     func() { testAccPreCheck(t) },
   214  		Providers:    testAccProviders,
   215  		CheckDestroy: testAccCheckAWSELBDestroy,
   216  		Steps: []resource.TestStep{
   217  			resource.TestStep{
   218  				Config: testAccAWSELBConfig,
   219  				Check: resource.ComposeTestCheckFunc(
   220  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   221  					testAccCheckAWSELBAttributes(&conf),
   222  				),
   223  			},
   224  
   225  			resource.TestStep{
   226  				Config: testAccAWSELBConfigNewInstance,
   227  				Check: resource.ComposeTestCheckFunc(
   228  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   229  					testCheckInstanceAttached(1),
   230  				),
   231  			},
   232  		},
   233  	})
   234  }
   235  
   236  func TestAccAWSELBUpdate_Listener(t *testing.T) {
   237  	var conf elb.LoadBalancerDescription
   238  
   239  	resource.Test(t, resource.TestCase{
   240  		PreCheck:     func() { testAccPreCheck(t) },
   241  		Providers:    testAccProviders,
   242  		CheckDestroy: testAccCheckAWSELBDestroy,
   243  		Steps: []resource.TestStep{
   244  			resource.TestStep{
   245  				Config: testAccAWSELBConfig,
   246  				Check: resource.ComposeTestCheckFunc(
   247  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   248  					testAccCheckAWSELBAttributes(&conf),
   249  					resource.TestCheckResourceAttr(
   250  						"aws_elb.bar", "listener.206423021.instance_port", "8000"),
   251  				),
   252  			},
   253  
   254  			resource.TestStep{
   255  				Config: testAccAWSELBConfigListener_update,
   256  				Check: resource.ComposeTestCheckFunc(
   257  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   258  					resource.TestCheckResourceAttr(
   259  						"aws_elb.bar", "listener.3931999347.instance_port", "8080"),
   260  				),
   261  			},
   262  		},
   263  	})
   264  }
   265  
   266  func TestAccAWSELB_HealthCheck(t *testing.T) {
   267  	var conf elb.LoadBalancerDescription
   268  
   269  	resource.Test(t, resource.TestCase{
   270  		PreCheck:     func() { testAccPreCheck(t) },
   271  		Providers:    testAccProviders,
   272  		CheckDestroy: testAccCheckAWSELBDestroy,
   273  		Steps: []resource.TestStep{
   274  			resource.TestStep{
   275  				Config: testAccAWSELBConfigHealthCheck,
   276  				Check: resource.ComposeTestCheckFunc(
   277  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   278  					testAccCheckAWSELBAttributesHealthCheck(&conf),
   279  					resource.TestCheckResourceAttr(
   280  						"aws_elb.bar", "health_check.3484319807.healthy_threshold", "5"),
   281  					resource.TestCheckResourceAttr(
   282  						"aws_elb.bar", "health_check.3484319807.unhealthy_threshold", "5"),
   283  					resource.TestCheckResourceAttr(
   284  						"aws_elb.bar", "health_check.3484319807.target", "HTTP:8000/"),
   285  					resource.TestCheckResourceAttr(
   286  						"aws_elb.bar", "health_check.3484319807.timeout", "30"),
   287  					resource.TestCheckResourceAttr(
   288  						"aws_elb.bar", "health_check.3484319807.interval", "60"),
   289  				),
   290  			},
   291  		},
   292  	})
   293  }
   294  
   295  func TestAccAWSELBUpdate_HealthCheck(t *testing.T) {
   296  	resource.Test(t, resource.TestCase{
   297  		PreCheck:     func() { testAccPreCheck(t) },
   298  		Providers:    testAccProviders,
   299  		CheckDestroy: testAccCheckAWSELBDestroy,
   300  		Steps: []resource.TestStep{
   301  			resource.TestStep{
   302  				Config: testAccAWSELBConfigHealthCheck,
   303  				Check: resource.ComposeTestCheckFunc(
   304  					resource.TestCheckResourceAttr(
   305  						"aws_elb.bar", "health_check.3484319807.healthy_threshold", "5"),
   306  				),
   307  			},
   308  			resource.TestStep{
   309  				Config: testAccAWSELBConfigHealthCheck_update,
   310  				Check: resource.ComposeTestCheckFunc(
   311  					resource.TestCheckResourceAttr(
   312  						"aws_elb.bar", "health_check.2648756019.healthy_threshold", "10"),
   313  				),
   314  			},
   315  		},
   316  	})
   317  }
   318  
   319  func TestAccAWSELB_Timeout(t *testing.T) {
   320  	var conf elb.LoadBalancerDescription
   321  
   322  	resource.Test(t, resource.TestCase{
   323  		PreCheck:     func() { testAccPreCheck(t) },
   324  		Providers:    testAccProviders,
   325  		CheckDestroy: testAccCheckAWSELBDestroy,
   326  		Steps: []resource.TestStep{
   327  			resource.TestStep{
   328  				Config: testAccAWSELBConfigIdleTimeout,
   329  				Check: resource.ComposeTestCheckFunc(
   330  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   331  					resource.TestCheckResourceAttr(
   332  						"aws_elb.bar", "idle_timeout", "200",
   333  					),
   334  				),
   335  			},
   336  		},
   337  	})
   338  }
   339  
   340  func TestAccAWSELBUpdate_Timeout(t *testing.T) {
   341  	resource.Test(t, resource.TestCase{
   342  		PreCheck:     func() { testAccPreCheck(t) },
   343  		Providers:    testAccProviders,
   344  		CheckDestroy: testAccCheckAWSELBDestroy,
   345  		Steps: []resource.TestStep{
   346  			resource.TestStep{
   347  				Config: testAccAWSELBConfigIdleTimeout,
   348  				Check: resource.ComposeTestCheckFunc(
   349  					resource.TestCheckResourceAttr(
   350  						"aws_elb.bar", "idle_timeout", "200",
   351  					),
   352  				),
   353  			},
   354  			resource.TestStep{
   355  				Config: testAccAWSELBConfigIdleTimeout_update,
   356  				Check: resource.ComposeTestCheckFunc(
   357  					resource.TestCheckResourceAttr(
   358  						"aws_elb.bar", "idle_timeout", "400",
   359  					),
   360  				),
   361  			},
   362  		},
   363  	})
   364  }
   365  
   366  func TestAccAWSELB_ConnectionDraining(t *testing.T) {
   367  	resource.Test(t, resource.TestCase{
   368  		PreCheck:     func() { testAccPreCheck(t) },
   369  		Providers:    testAccProviders,
   370  		CheckDestroy: testAccCheckAWSELBDestroy,
   371  		Steps: []resource.TestStep{
   372  			resource.TestStep{
   373  				Config: testAccAWSELBConfigConnectionDraining,
   374  				Check: resource.ComposeTestCheckFunc(
   375  					resource.TestCheckResourceAttr(
   376  						"aws_elb.bar", "connection_draining", "true",
   377  					),
   378  					resource.TestCheckResourceAttr(
   379  						"aws_elb.bar", "connection_draining_timeout", "400",
   380  					),
   381  				),
   382  			},
   383  		},
   384  	})
   385  }
   386  
   387  func TestAccAWSELBUpdate_ConnectionDraining(t *testing.T) {
   388  	resource.Test(t, resource.TestCase{
   389  		PreCheck:     func() { testAccPreCheck(t) },
   390  		Providers:    testAccProviders,
   391  		CheckDestroy: testAccCheckAWSELBDestroy,
   392  		Steps: []resource.TestStep{
   393  			resource.TestStep{
   394  				Config: testAccAWSELBConfigConnectionDraining,
   395  				Check: resource.ComposeTestCheckFunc(
   396  					resource.TestCheckResourceAttr(
   397  						"aws_elb.bar", "connection_draining", "true",
   398  					),
   399  					resource.TestCheckResourceAttr(
   400  						"aws_elb.bar", "connection_draining_timeout", "400",
   401  					),
   402  				),
   403  			},
   404  			resource.TestStep{
   405  				Config: testAccAWSELBConfigConnectionDraining_update_timeout,
   406  				Check: resource.ComposeTestCheckFunc(
   407  					resource.TestCheckResourceAttr(
   408  						"aws_elb.bar", "connection_draining", "true",
   409  					),
   410  					resource.TestCheckResourceAttr(
   411  						"aws_elb.bar", "connection_draining_timeout", "600",
   412  					),
   413  				),
   414  			},
   415  			resource.TestStep{
   416  				Config: testAccAWSELBConfigConnectionDraining_update_disable,
   417  				Check: resource.ComposeTestCheckFunc(
   418  					resource.TestCheckResourceAttr(
   419  						"aws_elb.bar", "connection_draining", "false",
   420  					),
   421  				),
   422  			},
   423  		},
   424  	})
   425  }
   426  
   427  func TestAccAWSELB_SecurityGroups(t *testing.T) {
   428  	resource.Test(t, resource.TestCase{
   429  		PreCheck:     func() { testAccPreCheck(t) },
   430  		Providers:    testAccProviders,
   431  		CheckDestroy: testAccCheckAWSELBDestroy,
   432  		Steps: []resource.TestStep{
   433  			resource.TestStep{
   434  				Config: testAccAWSELBConfig,
   435  				Check: resource.ComposeTestCheckFunc(
   436  					resource.TestCheckResourceAttr(
   437  						"aws_elb.bar", "security_groups.#", "0",
   438  					),
   439  				),
   440  			},
   441  			resource.TestStep{
   442  				Config: testAccAWSELBConfigSecurityGroups,
   443  				Check: resource.ComposeTestCheckFunc(
   444  					resource.TestCheckResourceAttr(
   445  						"aws_elb.bar", "security_groups.#", "1",
   446  					),
   447  				),
   448  			},
   449  		},
   450  	})
   451  }
   452  
   453  // Unit test for listeners hash
   454  func TestResourceAwsElbListenerHash(t *testing.T) {
   455  	cases := map[string]struct {
   456  		Left  map[string]interface{}
   457  		Right map[string]interface{}
   458  		Match bool
   459  	}{
   460  		"protocols are case insensitive": {
   461  			map[string]interface{}{
   462  				"instance_port":     80,
   463  				"instance_protocol": "TCP",
   464  				"lb_port":           80,
   465  				"lb_protocol":       "TCP",
   466  			},
   467  			map[string]interface{}{
   468  				"instance_port":     80,
   469  				"instance_protocol": "Tcp",
   470  				"lb_port":           80,
   471  				"lb_protocol":       "tcP",
   472  			},
   473  			true,
   474  		},
   475  	}
   476  
   477  	for tn, tc := range cases {
   478  		leftHash := resourceAwsElbListenerHash(tc.Left)
   479  		rightHash := resourceAwsElbListenerHash(tc.Right)
   480  		if leftHash == rightHash != tc.Match {
   481  			t.Fatalf("%s: expected match: %t, but did not get it", tn, tc.Match)
   482  		}
   483  	}
   484  }
   485  
   486  func TestResourceAWSELB_validateElbNameCannotBeginWithHyphen(t *testing.T) {
   487  	var elbName = "-Testing123"
   488  	_, errors := validateElbName(elbName, "SampleKey")
   489  
   490  	if len(errors) != 1 {
   491  		t.Fatalf("Expected the ELB Name to trigger a validation error")
   492  	}
   493  }
   494  
   495  func TestResourceAWSELB_validateElbNameCannotBeLongerThen32Characters(t *testing.T) {
   496  	var elbName = "Testing123dddddddddddddddddddvvvv"
   497  	_, errors := validateElbName(elbName, "SampleKey")
   498  
   499  	if len(errors) != 1 {
   500  		t.Fatalf("Expected the ELB Name to trigger a validation error")
   501  	}
   502  }
   503  
   504  func TestResourceAWSELB_validateElbNameCannotHaveSpecialCharacters(t *testing.T) {
   505  	var elbName = "Testing123%%"
   506  	_, errors := validateElbName(elbName, "SampleKey")
   507  
   508  	if len(errors) != 1 {
   509  		t.Fatalf("Expected the ELB Name to trigger a validation error")
   510  	}
   511  }
   512  
   513  func TestResourceAWSELB_validateElbNameCannotEndWithHyphen(t *testing.T) {
   514  	var elbName = "Testing123-"
   515  	_, errors := validateElbName(elbName, "SampleKey")
   516  
   517  	if len(errors) != 1 {
   518  		t.Fatalf("Expected the ELB Name to trigger a validation error")
   519  	}
   520  }
   521  
   522  func testAccCheckAWSELBDestroy(s *terraform.State) error {
   523  	conn := testAccProvider.Meta().(*AWSClient).elbconn
   524  
   525  	for _, rs := range s.RootModule().Resources {
   526  		if rs.Type != "aws_elb" {
   527  			continue
   528  		}
   529  
   530  		describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancersInput{
   531  			LoadBalancerNames: []*string{aws.String(rs.Primary.ID)},
   532  		})
   533  
   534  		if err == nil {
   535  			if len(describe.LoadBalancerDescriptions) != 0 &&
   536  				*describe.LoadBalancerDescriptions[0].LoadBalancerName == rs.Primary.ID {
   537  				return fmt.Errorf("ELB still exists")
   538  			}
   539  		}
   540  
   541  		// Verify the error
   542  		providerErr, ok := err.(awserr.Error)
   543  		if !ok {
   544  			return err
   545  		}
   546  
   547  		if providerErr.Code() != "InvalidLoadBalancerName.NotFound" {
   548  			return fmt.Errorf("Unexpected error: %s", err)
   549  		}
   550  	}
   551  
   552  	return nil
   553  }
   554  
   555  func testAccCheckAWSELBAttributes(conf *elb.LoadBalancerDescription) resource.TestCheckFunc {
   556  	return func(s *terraform.State) error {
   557  		zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"}
   558  		azs := make([]string, 0, len(conf.AvailabilityZones))
   559  		for _, x := range conf.AvailabilityZones {
   560  			azs = append(azs, *x)
   561  		}
   562  		sort.StringSlice(azs).Sort()
   563  		if !reflect.DeepEqual(azs, zones) {
   564  			return fmt.Errorf("bad availability_zones")
   565  		}
   566  
   567  		if *conf.LoadBalancerName != "foobar-terraform-test" {
   568  			return fmt.Errorf("bad name")
   569  		}
   570  
   571  		l := elb.Listener{
   572  			InstancePort:     aws.Int64(int64(8000)),
   573  			InstanceProtocol: aws.String("HTTP"),
   574  			LoadBalancerPort: aws.Int64(int64(80)),
   575  			Protocol:         aws.String("HTTP"),
   576  		}
   577  
   578  		if !reflect.DeepEqual(conf.ListenerDescriptions[0].Listener, &l) {
   579  			return fmt.Errorf(
   580  				"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   581  				conf.ListenerDescriptions[0].Listener,
   582  				l)
   583  		}
   584  
   585  		if *conf.DNSName == "" {
   586  			return fmt.Errorf("empty dns_name")
   587  		}
   588  
   589  		return nil
   590  	}
   591  }
   592  
   593  func testAccCheckAWSELBAttributesHealthCheck(conf *elb.LoadBalancerDescription) resource.TestCheckFunc {
   594  	return func(s *terraform.State) error {
   595  		zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"}
   596  		azs := make([]string, 0, len(conf.AvailabilityZones))
   597  		for _, x := range conf.AvailabilityZones {
   598  			azs = append(azs, *x)
   599  		}
   600  		sort.StringSlice(azs).Sort()
   601  		if !reflect.DeepEqual(azs, zones) {
   602  			return fmt.Errorf("bad availability_zones")
   603  		}
   604  
   605  		if *conf.LoadBalancerName != "foobar-terraform-test" {
   606  			return fmt.Errorf("bad name")
   607  		}
   608  
   609  		check := &elb.HealthCheck{
   610  			Timeout:            aws.Int64(int64(30)),
   611  			UnhealthyThreshold: aws.Int64(int64(5)),
   612  			HealthyThreshold:   aws.Int64(int64(5)),
   613  			Interval:           aws.Int64(int64(60)),
   614  			Target:             aws.String("HTTP:8000/"),
   615  		}
   616  
   617  		if !reflect.DeepEqual(conf.HealthCheck, check) {
   618  			return fmt.Errorf(
   619  				"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   620  				conf.HealthCheck,
   621  				check)
   622  		}
   623  
   624  		if *conf.DNSName == "" {
   625  			return fmt.Errorf("empty dns_name")
   626  		}
   627  
   628  		return nil
   629  	}
   630  }
   631  
   632  func testAccCheckAWSELBExists(n string, res *elb.LoadBalancerDescription) resource.TestCheckFunc {
   633  	return func(s *terraform.State) error {
   634  		rs, ok := s.RootModule().Resources[n]
   635  		if !ok {
   636  			return fmt.Errorf("Not found: %s", n)
   637  		}
   638  
   639  		if rs.Primary.ID == "" {
   640  			return fmt.Errorf("No ELB ID is set")
   641  		}
   642  
   643  		conn := testAccProvider.Meta().(*AWSClient).elbconn
   644  
   645  		describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancersInput{
   646  			LoadBalancerNames: []*string{aws.String(rs.Primary.ID)},
   647  		})
   648  
   649  		if err != nil {
   650  			return err
   651  		}
   652  
   653  		if len(describe.LoadBalancerDescriptions) != 1 ||
   654  			*describe.LoadBalancerDescriptions[0].LoadBalancerName != rs.Primary.ID {
   655  			return fmt.Errorf("ELB not found")
   656  		}
   657  
   658  		*res = *describe.LoadBalancerDescriptions[0]
   659  
   660  		// Confirm source_security_group_id for ELBs in a VPC
   661  		// 	See https://github.com/hashicorp/terraform/pull/3780
   662  		if res.VPCId != nil {
   663  			sgid := rs.Primary.Attributes["source_security_group_id"]
   664  			if sgid == "" {
   665  				return fmt.Errorf("Expected to find source_security_group_id for ELB, but was empty")
   666  			}
   667  		}
   668  
   669  		return nil
   670  	}
   671  }
   672  
   673  const testAccAWSELBConfig = `
   674  resource "aws_elb" "bar" {
   675    name = "foobar-terraform-test"
   676    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   677  
   678    listener {
   679      instance_port = 8000
   680      instance_protocol = "http"
   681      lb_port = 80
   682      // Protocol should be case insensitive
   683      lb_protocol = "HttP"
   684    }
   685  
   686  	tags {
   687  		bar = "baz"
   688  	}
   689  
   690    cross_zone_load_balancing = true
   691  }
   692  `
   693  
   694  const testAccAWSELBFullRangeOfCharacters = `
   695  resource "aws_elb" "foo" {
   696    name = "FoobarTerraform-test123"
   697    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   698  
   699    listener {
   700      instance_port = 8000
   701      instance_protocol = "http"
   702      lb_port = 80
   703      lb_protocol = "http"
   704    }
   705  }
   706  `
   707  
   708  const testAccAWSELBAccessLogs = `
   709  resource "aws_elb" "foo" {
   710    name = "FoobarTerraform-test123"
   711    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   712  
   713    listener {
   714      instance_port = 8000
   715      instance_protocol = "http"
   716      lb_port = 80
   717      lb_protocol = "http"
   718    }
   719  }
   720  `
   721  const testAccAWSELBAccessLogsOn = `
   722  # an S3 bucket configured for Access logs
   723  # The 797873946194 is the AWS ID for us-west-2, so this test
   724  # must be ran in us-west-2
   725  resource "aws_s3_bucket" "acceslogs_bucket" {
   726    bucket = "terraform-access-logs-bucket"
   727    acl = "private"
   728    force_destroy = true
   729    policy = <<EOF
   730  {
   731    "Id": "Policy1446577137248",
   732    "Statement": [
   733      {
   734        "Action": "s3:PutObject",
   735        "Effect": "Allow",
   736        "Principal": {
   737          "AWS": "arn:aws:iam::797873946194:root"
   738        },
   739        "Resource": "arn:aws:s3:::terraform-access-logs-bucket/*",
   740        "Sid": "Stmt1446575236270"
   741      }
   742    ],
   743    "Version": "2012-10-17"
   744  }
   745  EOF
   746  }
   747  
   748  resource "aws_elb" "foo" {
   749    name = "FoobarTerraform-test123"
   750    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   751  
   752    listener {
   753      instance_port = 8000
   754      instance_protocol = "http"
   755      lb_port = 80
   756      lb_protocol = "http"
   757    }
   758  
   759  	access_logs {
   760  		interval = 5
   761  		bucket = "${aws_s3_bucket.acceslogs_bucket.bucket}"
   762  	}
   763  }
   764  `
   765  
   766  const testAccAWSELBGeneratedName = `
   767  resource "aws_elb" "foo" {
   768    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   769  
   770    listener {
   771      instance_port = 8000
   772      instance_protocol = "http"
   773      lb_port = 80
   774      lb_protocol = "http"
   775    }
   776  }
   777  `
   778  
   779  const testAccAWSELBConfig_TagUpdate = `
   780  resource "aws_elb" "bar" {
   781    name = "foobar-terraform-test"
   782    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   783  
   784    listener {
   785      instance_port = 8000
   786      instance_protocol = "http"
   787      lb_port = 80
   788      lb_protocol = "http"
   789    }
   790  
   791  	tags {
   792  		foo = "bar"
   793  		new = "type"
   794  	}
   795  
   796    cross_zone_load_balancing = true
   797  }
   798  `
   799  
   800  const testAccAWSELBConfigNewInstance = `
   801  resource "aws_elb" "bar" {
   802    name = "foobar-terraform-test"
   803    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   804  
   805    listener {
   806      instance_port = 8000
   807      instance_protocol = "http"
   808      lb_port = 80
   809      lb_protocol = "http"
   810    }
   811  
   812    instances = ["${aws_instance.foo.id}"]
   813  }
   814  
   815  resource "aws_instance" "foo" {
   816  	# us-west-2
   817  	ami = "ami-043a5034"
   818  	instance_type = "t1.micro"
   819  }
   820  `
   821  
   822  const testAccAWSELBConfigListenerSSLCertificateId = `
   823  resource "aws_elb" "bar" {
   824    name = "foobar-terraform-test"
   825    availability_zones = ["us-west-2a"]
   826  
   827    listener {
   828      instance_port = 8000
   829      instance_protocol = "http"
   830      ssl_certificate_id = "%s"
   831      lb_port = 443
   832      lb_protocol = "https"
   833    }
   834  }
   835  `
   836  
   837  const testAccAWSELBConfigHealthCheck = `
   838  resource "aws_elb" "bar" {
   839    name = "foobar-terraform-test"
   840    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   841  
   842    listener {
   843      instance_port = 8000
   844      instance_protocol = "http"
   845      lb_port = 80
   846      lb_protocol = "http"
   847    }
   848  
   849    health_check {
   850      healthy_threshold = 5
   851      unhealthy_threshold = 5
   852      target = "HTTP:8000/"
   853      interval = 60
   854      timeout = 30
   855    }
   856  }
   857  `
   858  
   859  const testAccAWSELBConfigHealthCheck_update = `
   860  resource "aws_elb" "bar" {
   861    name = "foobar-terraform-test"
   862    availability_zones = ["us-west-2a"]
   863  
   864    listener {
   865      instance_port = 8000
   866      instance_protocol = "http"
   867      lb_port = 80
   868      lb_protocol = "http"
   869    }
   870  
   871    health_check {
   872      healthy_threshold = 10
   873      unhealthy_threshold = 5
   874      target = "HTTP:8000/"
   875      interval = 60
   876      timeout = 30
   877    }
   878  }
   879  `
   880  
   881  const testAccAWSELBConfigListener_update = `
   882  resource "aws_elb" "bar" {
   883    name = "foobar-terraform-test"
   884    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   885  
   886    listener {
   887      instance_port = 8080
   888      instance_protocol = "http"
   889      lb_port = 80
   890      lb_protocol = "http"
   891    }
   892  }
   893  `
   894  
   895  const testAccAWSELBConfigIdleTimeout = `
   896  resource "aws_elb" "bar" {
   897  	name = "foobar-terraform-test"
   898  	availability_zones = ["us-west-2a"]
   899  
   900  	listener {
   901  		instance_port = 8000
   902  		instance_protocol = "http"
   903  		lb_port = 80
   904  		lb_protocol = "http"
   905  	}
   906  
   907  	idle_timeout = 200
   908  }
   909  `
   910  
   911  const testAccAWSELBConfigIdleTimeout_update = `
   912  resource "aws_elb" "bar" {
   913  	name = "foobar-terraform-test"
   914  	availability_zones = ["us-west-2a"]
   915  
   916  	listener {
   917  		instance_port = 8000
   918  		instance_protocol = "http"
   919  		lb_port = 80
   920  		lb_protocol = "http"
   921  	}
   922  
   923  	idle_timeout = 400
   924  }
   925  `
   926  
   927  const testAccAWSELBConfigConnectionDraining = `
   928  resource "aws_elb" "bar" {
   929  	name = "foobar-terraform-test"
   930  	availability_zones = ["us-west-2a"]
   931  
   932  	listener {
   933  		instance_port = 8000
   934  		instance_protocol = "http"
   935  		lb_port = 80
   936  		lb_protocol = "http"
   937  	}
   938  
   939  	connection_draining = true
   940  	connection_draining_timeout = 400
   941  }
   942  `
   943  
   944  const testAccAWSELBConfigConnectionDraining_update_timeout = `
   945  resource "aws_elb" "bar" {
   946  	name = "foobar-terraform-test"
   947  	availability_zones = ["us-west-2a"]
   948  
   949  	listener {
   950  		instance_port = 8000
   951  		instance_protocol = "http"
   952  		lb_port = 80
   953  		lb_protocol = "http"
   954  	}
   955  
   956  	connection_draining = true
   957  	connection_draining_timeout = 600
   958  }
   959  `
   960  
   961  const testAccAWSELBConfigConnectionDraining_update_disable = `
   962  resource "aws_elb" "bar" {
   963  	name = "foobar-terraform-test"
   964  	availability_zones = ["us-west-2a"]
   965  
   966  	listener {
   967  		instance_port = 8000
   968  		instance_protocol = "http"
   969  		lb_port = 80
   970  		lb_protocol = "http"
   971  	}
   972  
   973  	connection_draining = false
   974  }
   975  `
   976  
   977  const testAccAWSELBConfigSecurityGroups = `
   978  resource "aws_elb" "bar" {
   979    name = "foobar-terraform-test"
   980    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   981  
   982    listener {
   983      instance_port = 8000
   984      instance_protocol = "http"
   985      lb_port = 80
   986      lb_protocol = "http"
   987    }
   988  
   989    security_groups = ["${aws_security_group.bar.id}"]
   990  }
   991  
   992  resource "aws_security_group" "bar" {
   993    name = "terraform-elb-acceptance-test"
   994    description = "Used in the terraform acceptance tests for the elb resource"
   995  
   996    ingress {
   997      protocol = "tcp"
   998      from_port = 80
   999      to_port = 80
  1000      cidr_blocks = ["0.0.0.0/0"]
  1001    }
  1002  }
  1003  `