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