github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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 TestAccAWSELB_iam_server_cert(t *testing.T) {
   183  	var conf elb.LoadBalancerDescription
   184  	// var td elb.TagDescription
   185  	testCheck := func(*terraform.State) error {
   186  		if len(conf.ListenerDescriptions) != 1 {
   187  			return fmt.Errorf(
   188  				"TestAccAWSELB_iam_server_cert expected 1 listener, got %d",
   189  				len(conf.ListenerDescriptions))
   190  		}
   191  		return nil
   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: testAccELBIAMServerCertConfig,
   200  				Check: resource.ComposeTestCheckFunc(
   201  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   202  					testCheck,
   203  				),
   204  			},
   205  		},
   206  	})
   207  }
   208  
   209  func testAccLoadTags(conf *elb.LoadBalancerDescription, td *elb.TagDescription) resource.TestCheckFunc {
   210  	return func(s *terraform.State) error {
   211  		conn := testAccProvider.Meta().(*AWSClient).elbconn
   212  
   213  		describe, err := conn.DescribeTags(&elb.DescribeTagsInput{
   214  			LoadBalancerNames: []*string{conf.LoadBalancerName},
   215  		})
   216  
   217  		if err != nil {
   218  			return err
   219  		}
   220  		if len(describe.TagDescriptions) > 0 {
   221  			*td = *describe.TagDescriptions[0]
   222  		}
   223  		return nil
   224  	}
   225  }
   226  
   227  func TestAccAWSELB_InstanceAttaching(t *testing.T) {
   228  	var conf elb.LoadBalancerDescription
   229  
   230  	testCheckInstanceAttached := func(count int) resource.TestCheckFunc {
   231  		return func(*terraform.State) error {
   232  			if len(conf.Instances) != count {
   233  				return fmt.Errorf("instance count does not match")
   234  			}
   235  			return nil
   236  		}
   237  	}
   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  				),
   250  			},
   251  
   252  			resource.TestStep{
   253  				Config: testAccAWSELBConfigNewInstance,
   254  				Check: resource.ComposeTestCheckFunc(
   255  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   256  					testCheckInstanceAttached(1),
   257  				),
   258  			},
   259  		},
   260  	})
   261  }
   262  
   263  func TestAccAWSELBUpdate_Listener(t *testing.T) {
   264  	var conf elb.LoadBalancerDescription
   265  
   266  	resource.Test(t, resource.TestCase{
   267  		PreCheck:     func() { testAccPreCheck(t) },
   268  		Providers:    testAccProviders,
   269  		CheckDestroy: testAccCheckAWSELBDestroy,
   270  		Steps: []resource.TestStep{
   271  			resource.TestStep{
   272  				Config: testAccAWSELBConfig,
   273  				Check: resource.ComposeTestCheckFunc(
   274  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   275  					testAccCheckAWSELBAttributes(&conf),
   276  					resource.TestCheckResourceAttr(
   277  						"aws_elb.bar", "listener.206423021.instance_port", "8000"),
   278  				),
   279  			},
   280  
   281  			resource.TestStep{
   282  				Config: testAccAWSELBConfigListener_update,
   283  				Check: resource.ComposeTestCheckFunc(
   284  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   285  					resource.TestCheckResourceAttr(
   286  						"aws_elb.bar", "listener.3931999347.instance_port", "8080"),
   287  				),
   288  			},
   289  		},
   290  	})
   291  }
   292  
   293  func TestAccAWSELB_HealthCheck(t *testing.T) {
   294  	var conf elb.LoadBalancerDescription
   295  
   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  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   305  					testAccCheckAWSELBAttributesHealthCheck(&conf),
   306  					resource.TestCheckResourceAttr(
   307  						"aws_elb.bar", "health_check.3484319807.healthy_threshold", "5"),
   308  					resource.TestCheckResourceAttr(
   309  						"aws_elb.bar", "health_check.3484319807.unhealthy_threshold", "5"),
   310  					resource.TestCheckResourceAttr(
   311  						"aws_elb.bar", "health_check.3484319807.target", "HTTP:8000/"),
   312  					resource.TestCheckResourceAttr(
   313  						"aws_elb.bar", "health_check.3484319807.timeout", "30"),
   314  					resource.TestCheckResourceAttr(
   315  						"aws_elb.bar", "health_check.3484319807.interval", "60"),
   316  				),
   317  			},
   318  		},
   319  	})
   320  }
   321  
   322  func TestAccAWSELBUpdate_HealthCheck(t *testing.T) {
   323  	resource.Test(t, resource.TestCase{
   324  		PreCheck:     func() { testAccPreCheck(t) },
   325  		Providers:    testAccProviders,
   326  		CheckDestroy: testAccCheckAWSELBDestroy,
   327  		Steps: []resource.TestStep{
   328  			resource.TestStep{
   329  				Config: testAccAWSELBConfigHealthCheck,
   330  				Check: resource.ComposeTestCheckFunc(
   331  					resource.TestCheckResourceAttr(
   332  						"aws_elb.bar", "health_check.3484319807.healthy_threshold", "5"),
   333  				),
   334  			},
   335  			resource.TestStep{
   336  				Config: testAccAWSELBConfigHealthCheck_update,
   337  				Check: resource.ComposeTestCheckFunc(
   338  					resource.TestCheckResourceAttr(
   339  						"aws_elb.bar", "health_check.2648756019.healthy_threshold", "10"),
   340  				),
   341  			},
   342  		},
   343  	})
   344  }
   345  
   346  func TestAccAWSELB_Timeout(t *testing.T) {
   347  	var conf elb.LoadBalancerDescription
   348  
   349  	resource.Test(t, resource.TestCase{
   350  		PreCheck:     func() { testAccPreCheck(t) },
   351  		Providers:    testAccProviders,
   352  		CheckDestroy: testAccCheckAWSELBDestroy,
   353  		Steps: []resource.TestStep{
   354  			resource.TestStep{
   355  				Config: testAccAWSELBConfigIdleTimeout,
   356  				Check: resource.ComposeTestCheckFunc(
   357  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   358  					resource.TestCheckResourceAttr(
   359  						"aws_elb.bar", "idle_timeout", "200",
   360  					),
   361  				),
   362  			},
   363  		},
   364  	})
   365  }
   366  
   367  func TestAccAWSELBUpdate_Timeout(t *testing.T) {
   368  	resource.Test(t, resource.TestCase{
   369  		PreCheck:     func() { testAccPreCheck(t) },
   370  		Providers:    testAccProviders,
   371  		CheckDestroy: testAccCheckAWSELBDestroy,
   372  		Steps: []resource.TestStep{
   373  			resource.TestStep{
   374  				Config: testAccAWSELBConfigIdleTimeout,
   375  				Check: resource.ComposeTestCheckFunc(
   376  					resource.TestCheckResourceAttr(
   377  						"aws_elb.bar", "idle_timeout", "200",
   378  					),
   379  				),
   380  			},
   381  			resource.TestStep{
   382  				Config: testAccAWSELBConfigIdleTimeout_update,
   383  				Check: resource.ComposeTestCheckFunc(
   384  					resource.TestCheckResourceAttr(
   385  						"aws_elb.bar", "idle_timeout", "400",
   386  					),
   387  				),
   388  			},
   389  		},
   390  	})
   391  }
   392  
   393  func TestAccAWSELB_ConnectionDraining(t *testing.T) {
   394  	resource.Test(t, resource.TestCase{
   395  		PreCheck:     func() { testAccPreCheck(t) },
   396  		Providers:    testAccProviders,
   397  		CheckDestroy: testAccCheckAWSELBDestroy,
   398  		Steps: []resource.TestStep{
   399  			resource.TestStep{
   400  				Config: testAccAWSELBConfigConnectionDraining,
   401  				Check: resource.ComposeTestCheckFunc(
   402  					resource.TestCheckResourceAttr(
   403  						"aws_elb.bar", "connection_draining", "true",
   404  					),
   405  					resource.TestCheckResourceAttr(
   406  						"aws_elb.bar", "connection_draining_timeout", "400",
   407  					),
   408  				),
   409  			},
   410  		},
   411  	})
   412  }
   413  
   414  func TestAccAWSELBUpdate_ConnectionDraining(t *testing.T) {
   415  	resource.Test(t, resource.TestCase{
   416  		PreCheck:     func() { testAccPreCheck(t) },
   417  		Providers:    testAccProviders,
   418  		CheckDestroy: testAccCheckAWSELBDestroy,
   419  		Steps: []resource.TestStep{
   420  			resource.TestStep{
   421  				Config: testAccAWSELBConfigConnectionDraining,
   422  				Check: resource.ComposeTestCheckFunc(
   423  					resource.TestCheckResourceAttr(
   424  						"aws_elb.bar", "connection_draining", "true",
   425  					),
   426  					resource.TestCheckResourceAttr(
   427  						"aws_elb.bar", "connection_draining_timeout", "400",
   428  					),
   429  				),
   430  			},
   431  			resource.TestStep{
   432  				Config: testAccAWSELBConfigConnectionDraining_update_timeout,
   433  				Check: resource.ComposeTestCheckFunc(
   434  					resource.TestCheckResourceAttr(
   435  						"aws_elb.bar", "connection_draining", "true",
   436  					),
   437  					resource.TestCheckResourceAttr(
   438  						"aws_elb.bar", "connection_draining_timeout", "600",
   439  					),
   440  				),
   441  			},
   442  			resource.TestStep{
   443  				Config: testAccAWSELBConfigConnectionDraining_update_disable,
   444  				Check: resource.ComposeTestCheckFunc(
   445  					resource.TestCheckResourceAttr(
   446  						"aws_elb.bar", "connection_draining", "false",
   447  					),
   448  				),
   449  			},
   450  		},
   451  	})
   452  }
   453  
   454  func TestAccAWSELB_SecurityGroups(t *testing.T) {
   455  	resource.Test(t, resource.TestCase{
   456  		PreCheck:     func() { testAccPreCheck(t) },
   457  		Providers:    testAccProviders,
   458  		CheckDestroy: testAccCheckAWSELBDestroy,
   459  		Steps: []resource.TestStep{
   460  			resource.TestStep{
   461  				Config: testAccAWSELBConfig,
   462  				Check: resource.ComposeTestCheckFunc(
   463  					resource.TestCheckResourceAttr(
   464  						"aws_elb.bar", "security_groups.#", "0",
   465  					),
   466  				),
   467  			},
   468  			resource.TestStep{
   469  				Config: testAccAWSELBConfigSecurityGroups,
   470  				Check: resource.ComposeTestCheckFunc(
   471  					resource.TestCheckResourceAttr(
   472  						"aws_elb.bar", "security_groups.#", "1",
   473  					),
   474  				),
   475  			},
   476  		},
   477  	})
   478  }
   479  
   480  // Unit test for listeners hash
   481  func TestResourceAwsElbListenerHash(t *testing.T) {
   482  	cases := map[string]struct {
   483  		Left  map[string]interface{}
   484  		Right map[string]interface{}
   485  		Match bool
   486  	}{
   487  		"protocols are case insensitive": {
   488  			map[string]interface{}{
   489  				"instance_port":     80,
   490  				"instance_protocol": "TCP",
   491  				"lb_port":           80,
   492  				"lb_protocol":       "TCP",
   493  			},
   494  			map[string]interface{}{
   495  				"instance_port":     80,
   496  				"instance_protocol": "Tcp",
   497  				"lb_port":           80,
   498  				"lb_protocol":       "tcP",
   499  			},
   500  			true,
   501  		},
   502  	}
   503  
   504  	for tn, tc := range cases {
   505  		leftHash := resourceAwsElbListenerHash(tc.Left)
   506  		rightHash := resourceAwsElbListenerHash(tc.Right)
   507  		if leftHash == rightHash != tc.Match {
   508  			t.Fatalf("%s: expected match: %t, but did not get it", tn, tc.Match)
   509  		}
   510  	}
   511  }
   512  
   513  func TestResourceAWSELB_validateElbNameCannotBeginWithHyphen(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 TestResourceAWSELB_validateElbNameCannotBeLongerThen32Characters(t *testing.T) {
   523  	var elbName = "Testing123dddddddddddddddddddvvvv"
   524  	_, errors := validateElbName(elbName, "SampleKey")
   525  
   526  	if len(errors) != 1 {
   527  		t.Fatalf("Expected the ELB Name to trigger a validation error")
   528  	}
   529  }
   530  
   531  func TestResourceAWSELB_validateElbNameCannotHaveSpecialCharacters(t *testing.T) {
   532  	var elbName = "Testing123%%"
   533  	_, errors := validateElbName(elbName, "SampleKey")
   534  
   535  	if len(errors) != 1 {
   536  		t.Fatalf("Expected the ELB Name to trigger a validation error")
   537  	}
   538  }
   539  
   540  func TestResourceAWSELB_validateElbNameCannotEndWithHyphen(t *testing.T) {
   541  	var elbName = "Testing123-"
   542  	_, errors := validateElbName(elbName, "SampleKey")
   543  
   544  	if len(errors) != 1 {
   545  		t.Fatalf("Expected the ELB Name to trigger a validation error")
   546  	}
   547  }
   548  
   549  func testAccCheckAWSELBDestroy(s *terraform.State) error {
   550  	conn := testAccProvider.Meta().(*AWSClient).elbconn
   551  
   552  	for _, rs := range s.RootModule().Resources {
   553  		if rs.Type != "aws_elb" {
   554  			continue
   555  		}
   556  
   557  		describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancersInput{
   558  			LoadBalancerNames: []*string{aws.String(rs.Primary.ID)},
   559  		})
   560  
   561  		if err == nil {
   562  			if len(describe.LoadBalancerDescriptions) != 0 &&
   563  				*describe.LoadBalancerDescriptions[0].LoadBalancerName == rs.Primary.ID {
   564  				return fmt.Errorf("ELB still exists")
   565  			}
   566  		}
   567  
   568  		// Verify the error
   569  		providerErr, ok := err.(awserr.Error)
   570  		if !ok {
   571  			return err
   572  		}
   573  
   574  		if providerErr.Code() != "InvalidLoadBalancerName.NotFound" {
   575  			return fmt.Errorf("Unexpected error: %s", err)
   576  		}
   577  	}
   578  
   579  	return nil
   580  }
   581  
   582  func testAccCheckAWSELBAttributes(conf *elb.LoadBalancerDescription) resource.TestCheckFunc {
   583  	return func(s *terraform.State) error {
   584  		zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"}
   585  		azs := make([]string, 0, len(conf.AvailabilityZones))
   586  		for _, x := range conf.AvailabilityZones {
   587  			azs = append(azs, *x)
   588  		}
   589  		sort.StringSlice(azs).Sort()
   590  		if !reflect.DeepEqual(azs, zones) {
   591  			return fmt.Errorf("bad availability_zones")
   592  		}
   593  
   594  		if *conf.LoadBalancerName != "foobar-terraform-test" {
   595  			return fmt.Errorf("bad name")
   596  		}
   597  
   598  		l := elb.Listener{
   599  			InstancePort:     aws.Int64(int64(8000)),
   600  			InstanceProtocol: aws.String("HTTP"),
   601  			LoadBalancerPort: aws.Int64(int64(80)),
   602  			Protocol:         aws.String("HTTP"),
   603  		}
   604  
   605  		if !reflect.DeepEqual(conf.ListenerDescriptions[0].Listener, &l) {
   606  			return fmt.Errorf(
   607  				"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   608  				conf.ListenerDescriptions[0].Listener,
   609  				l)
   610  		}
   611  
   612  		if *conf.DNSName == "" {
   613  			return fmt.Errorf("empty dns_name")
   614  		}
   615  
   616  		return nil
   617  	}
   618  }
   619  
   620  func testAccCheckAWSELBAttributesHealthCheck(conf *elb.LoadBalancerDescription) resource.TestCheckFunc {
   621  	return func(s *terraform.State) error {
   622  		zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"}
   623  		azs := make([]string, 0, len(conf.AvailabilityZones))
   624  		for _, x := range conf.AvailabilityZones {
   625  			azs = append(azs, *x)
   626  		}
   627  		sort.StringSlice(azs).Sort()
   628  		if !reflect.DeepEqual(azs, zones) {
   629  			return fmt.Errorf("bad availability_zones")
   630  		}
   631  
   632  		if *conf.LoadBalancerName != "foobar-terraform-test" {
   633  			return fmt.Errorf("bad name")
   634  		}
   635  
   636  		check := &elb.HealthCheck{
   637  			Timeout:            aws.Int64(int64(30)),
   638  			UnhealthyThreshold: aws.Int64(int64(5)),
   639  			HealthyThreshold:   aws.Int64(int64(5)),
   640  			Interval:           aws.Int64(int64(60)),
   641  			Target:             aws.String("HTTP:8000/"),
   642  		}
   643  
   644  		if !reflect.DeepEqual(conf.HealthCheck, check) {
   645  			return fmt.Errorf(
   646  				"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   647  				conf.HealthCheck,
   648  				check)
   649  		}
   650  
   651  		if *conf.DNSName == "" {
   652  			return fmt.Errorf("empty dns_name")
   653  		}
   654  
   655  		return nil
   656  	}
   657  }
   658  
   659  func testAccCheckAWSELBExists(n string, res *elb.LoadBalancerDescription) resource.TestCheckFunc {
   660  	return func(s *terraform.State) error {
   661  		rs, ok := s.RootModule().Resources[n]
   662  		if !ok {
   663  			return fmt.Errorf("Not found: %s", n)
   664  		}
   665  
   666  		if rs.Primary.ID == "" {
   667  			return fmt.Errorf("No ELB ID is set")
   668  		}
   669  
   670  		conn := testAccProvider.Meta().(*AWSClient).elbconn
   671  
   672  		describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancersInput{
   673  			LoadBalancerNames: []*string{aws.String(rs.Primary.ID)},
   674  		})
   675  
   676  		if err != nil {
   677  			return err
   678  		}
   679  
   680  		if len(describe.LoadBalancerDescriptions) != 1 ||
   681  			*describe.LoadBalancerDescriptions[0].LoadBalancerName != rs.Primary.ID {
   682  			return fmt.Errorf("ELB not found")
   683  		}
   684  
   685  		*res = *describe.LoadBalancerDescriptions[0]
   686  
   687  		// Confirm source_security_group_id for ELBs in a VPC
   688  		// 	See https://github.com/hashicorp/terraform/pull/3780
   689  		if res.VPCId != nil {
   690  			sgid := rs.Primary.Attributes["source_security_group_id"]
   691  			if sgid == "" {
   692  				return fmt.Errorf("Expected to find source_security_group_id for ELB, but was empty")
   693  			}
   694  		}
   695  
   696  		return nil
   697  	}
   698  }
   699  
   700  const testAccAWSELBConfig = `
   701  resource "aws_elb" "bar" {
   702    name = "foobar-terraform-test"
   703    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   704  
   705    listener {
   706      instance_port = 8000
   707      instance_protocol = "http"
   708      lb_port = 80
   709      // Protocol should be case insensitive
   710      lb_protocol = "HttP"
   711    }
   712  
   713  	tags {
   714  		bar = "baz"
   715  	}
   716  
   717    cross_zone_load_balancing = true
   718  }
   719  `
   720  
   721  const testAccAWSELBFullRangeOfCharacters = `
   722  resource "aws_elb" "foo" {
   723    name = "FoobarTerraform-test123"
   724    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   725  
   726    listener {
   727      instance_port = 8000
   728      instance_protocol = "http"
   729      lb_port = 80
   730      lb_protocol = "http"
   731    }
   732  }
   733  `
   734  
   735  const testAccAWSELBAccessLogs = `
   736  resource "aws_elb" "foo" {
   737    name = "FoobarTerraform-test123"
   738    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   739  
   740    listener {
   741      instance_port = 8000
   742      instance_protocol = "http"
   743      lb_port = 80
   744      lb_protocol = "http"
   745    }
   746  }
   747  `
   748  const testAccAWSELBAccessLogsOn = `
   749  # an S3 bucket configured for Access logs
   750  # The 797873946194 is the AWS ID for us-west-2, so this test
   751  # must be ran in us-west-2
   752  resource "aws_s3_bucket" "acceslogs_bucket" {
   753    bucket = "terraform-access-logs-bucket"
   754    acl = "private"
   755    force_destroy = true
   756    policy = <<EOF
   757  {
   758    "Id": "Policy1446577137248",
   759    "Statement": [
   760      {
   761        "Action": "s3:PutObject",
   762        "Effect": "Allow",
   763        "Principal": {
   764          "AWS": "arn:aws:iam::797873946194:root"
   765        },
   766        "Resource": "arn:aws:s3:::terraform-access-logs-bucket/*",
   767        "Sid": "Stmt1446575236270"
   768      }
   769    ],
   770    "Version": "2012-10-17"
   771  }
   772  EOF
   773  }
   774  
   775  resource "aws_elb" "foo" {
   776    name = "FoobarTerraform-test123"
   777    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   778  
   779    listener {
   780      instance_port = 8000
   781      instance_protocol = "http"
   782      lb_port = 80
   783      lb_protocol = "http"
   784    }
   785  
   786  	access_logs {
   787  		interval = 5
   788  		bucket = "${aws_s3_bucket.acceslogs_bucket.bucket}"
   789  	}
   790  }
   791  `
   792  
   793  const testAccAWSELBGeneratedName = `
   794  resource "aws_elb" "foo" {
   795    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   796  
   797    listener {
   798      instance_port = 8000
   799      instance_protocol = "http"
   800      lb_port = 80
   801      lb_protocol = "http"
   802    }
   803  }
   804  `
   805  
   806  const testAccAWSELBConfig_TagUpdate = `
   807  resource "aws_elb" "bar" {
   808    name = "foobar-terraform-test"
   809    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   810  
   811    listener {
   812      instance_port = 8000
   813      instance_protocol = "http"
   814      lb_port = 80
   815      lb_protocol = "http"
   816    }
   817  
   818  	tags {
   819  		foo = "bar"
   820  		new = "type"
   821  	}
   822  
   823    cross_zone_load_balancing = true
   824  }
   825  `
   826  
   827  const testAccAWSELBConfigNewInstance = `
   828  resource "aws_elb" "bar" {
   829    name = "foobar-terraform-test"
   830    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   831  
   832    listener {
   833      instance_port = 8000
   834      instance_protocol = "http"
   835      lb_port = 80
   836      lb_protocol = "http"
   837    }
   838  
   839    instances = ["${aws_instance.foo.id}"]
   840  }
   841  
   842  resource "aws_instance" "foo" {
   843  	# us-west-2
   844  	ami = "ami-043a5034"
   845  	instance_type = "t1.micro"
   846  }
   847  `
   848  
   849  const testAccAWSELBConfigListenerSSLCertificateId = `
   850  resource "aws_elb" "bar" {
   851    name = "foobar-terraform-test"
   852    availability_zones = ["us-west-2a"]
   853  
   854    listener {
   855      instance_port = 8000
   856      instance_protocol = "http"
   857      ssl_certificate_id = "%s"
   858      lb_port = 443
   859      lb_protocol = "https"
   860    }
   861  }
   862  `
   863  
   864  const testAccAWSELBConfigHealthCheck = `
   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    health_check {
   877      healthy_threshold = 5
   878      unhealthy_threshold = 5
   879      target = "HTTP:8000/"
   880      interval = 60
   881      timeout = 30
   882    }
   883  }
   884  `
   885  
   886  const testAccAWSELBConfigHealthCheck_update = `
   887  resource "aws_elb" "bar" {
   888    name = "foobar-terraform-test"
   889    availability_zones = ["us-west-2a"]
   890  
   891    listener {
   892      instance_port = 8000
   893      instance_protocol = "http"
   894      lb_port = 80
   895      lb_protocol = "http"
   896    }
   897  
   898    health_check {
   899      healthy_threshold = 10
   900      unhealthy_threshold = 5
   901      target = "HTTP:8000/"
   902      interval = 60
   903      timeout = 30
   904    }
   905  }
   906  `
   907  
   908  const testAccAWSELBConfigListener_update = `
   909  resource "aws_elb" "bar" {
   910    name = "foobar-terraform-test"
   911    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   912  
   913    listener {
   914      instance_port = 8080
   915      instance_protocol = "http"
   916      lb_port = 80
   917      lb_protocol = "http"
   918    }
   919  }
   920  `
   921  
   922  const testAccAWSELBConfigIdleTimeout = `
   923  resource "aws_elb" "bar" {
   924  	name = "foobar-terraform-test"
   925  	availability_zones = ["us-west-2a"]
   926  
   927  	listener {
   928  		instance_port = 8000
   929  		instance_protocol = "http"
   930  		lb_port = 80
   931  		lb_protocol = "http"
   932  	}
   933  
   934  	idle_timeout = 200
   935  }
   936  `
   937  
   938  const testAccAWSELBConfigIdleTimeout_update = `
   939  resource "aws_elb" "bar" {
   940  	name = "foobar-terraform-test"
   941  	availability_zones = ["us-west-2a"]
   942  
   943  	listener {
   944  		instance_port = 8000
   945  		instance_protocol = "http"
   946  		lb_port = 80
   947  		lb_protocol = "http"
   948  	}
   949  
   950  	idle_timeout = 400
   951  }
   952  `
   953  
   954  const testAccAWSELBConfigConnectionDraining = `
   955  resource "aws_elb" "bar" {
   956  	name = "foobar-terraform-test"
   957  	availability_zones = ["us-west-2a"]
   958  
   959  	listener {
   960  		instance_port = 8000
   961  		instance_protocol = "http"
   962  		lb_port = 80
   963  		lb_protocol = "http"
   964  	}
   965  
   966  	connection_draining = true
   967  	connection_draining_timeout = 400
   968  }
   969  `
   970  
   971  const testAccAWSELBConfigConnectionDraining_update_timeout = `
   972  resource "aws_elb" "bar" {
   973  	name = "foobar-terraform-test"
   974  	availability_zones = ["us-west-2a"]
   975  
   976  	listener {
   977  		instance_port = 8000
   978  		instance_protocol = "http"
   979  		lb_port = 80
   980  		lb_protocol = "http"
   981  	}
   982  
   983  	connection_draining = true
   984  	connection_draining_timeout = 600
   985  }
   986  `
   987  
   988  const testAccAWSELBConfigConnectionDraining_update_disable = `
   989  resource "aws_elb" "bar" {
   990  	name = "foobar-terraform-test"
   991  	availability_zones = ["us-west-2a"]
   992  
   993  	listener {
   994  		instance_port = 8000
   995  		instance_protocol = "http"
   996  		lb_port = 80
   997  		lb_protocol = "http"
   998  	}
   999  
  1000  	connection_draining = false
  1001  }
  1002  `
  1003  
  1004  const testAccAWSELBConfigSecurityGroups = `
  1005  resource "aws_elb" "bar" {
  1006    name = "foobar-terraform-test"
  1007    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
  1008  
  1009    listener {
  1010      instance_port = 8000
  1011      instance_protocol = "http"
  1012      lb_port = 80
  1013      lb_protocol = "http"
  1014    }
  1015  
  1016    security_groups = ["${aws_security_group.bar.id}"]
  1017  }
  1018  
  1019  resource "aws_security_group" "bar" {
  1020    name = "terraform-elb-acceptance-test"
  1021    description = "Used in the terraform acceptance tests for the elb resource"
  1022  
  1023    ingress {
  1024      protocol = "tcp"
  1025      from_port = 80
  1026      to_port = 80
  1027      cidr_blocks = ["0.0.0.0/0"]
  1028    }
  1029  }
  1030  `
  1031  
  1032  // This IAM Server config is lifted from
  1033  // builtin/providers/aws/resource_aws_iam_server_certificate_test.go
  1034  var testAccELBIAMServerCertConfig = `
  1035  resource "aws_iam_server_certificate" "test_cert" {
  1036    name = "terraform-test-cert"
  1037    certificate_body = <<EOF
  1038  -----BEGIN CERTIFICATE-----
  1039  MIIDCDCCAfACAQEwDQYJKoZIhvcNAQELBQAwgY4xCzAJBgNVBAYTAlVTMREwDwYD
  1040  VQQIDAhOZXcgWW9yazERMA8GA1UEBwwITmV3IFlvcmsxFjAUBgNVBAoMDUJhcmVm
  1041  b290IExhYnMxGDAWBgNVBAMMD0phc29uIEJlcmxpbnNreTEnMCUGCSqGSIb3DQEJ
  1042  ARYYamFzb25AYmFyZWZvb3Rjb2RlcnMuY29tMB4XDTE1MDYyMTA1MzcwNVoXDTE2
  1043  MDYyMDA1MzcwNVowgYgxCzAJBgNVBAYTAlVTMREwDwYDVQQIDAhOZXcgWW9yazEL
  1044  MAkGA1UEBwwCTlkxFjAUBgNVBAoMDUJhcmVmb290IExhYnMxGDAWBgNVBAMMD0ph
  1045  c29uIEJlcmxpbnNreTEnMCUGCSqGSIb3DQEJARYYamFzb25AYmFyZWZvb3Rjb2Rl
  1046  cnMuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD2AVGKRIx+EFM0kkg7
  1047  6GoJv9uy0biEDHB4phQBqnDIf8J8/gq9eVvQrR5jJC9Uz4zp5wG/oLZlGuF92/jD
  1048  bI/yS+DOAjrh30vN79Au74jGN2Cw8fIak40iDUwjZaczK2Gkna54XIO9pqMcbQ6Q
  1049  mLUkQXsqlJ7Q4X2kL3b9iMsXcQIDAQABMA0GCSqGSIb3DQEBCwUAA4IBAQCDGNvU
  1050  eioQMVPNlmmxW3+Rwo0Kl+/HtUOmqUDKUDvJnelxulBr7O8w75N/Z7h7+aBJCUkt
  1051  tz+DwATZswXtsal6TuzHHpAhpFql82jQZVE8OYkrX84XKRQpm8ZnbyZObMdXTJWk
  1052  ArC/rGVIWsvhlbgGM8zu7a3zbeuAESZ8Bn4ZbJxnoaRK8p36/alvzAwkgzSf3oUX
  1053  HtU4LrdunevBs6/CbKCWrxYcvNCy8EcmHitqCfQL5nxCCXpgf/Mw1vmIPTwbPSJq
  1054  oUkh5yjGRKzhh7QbG1TlFX6zUp4vb+UJn5+g4edHrqivRSjIqYrC45ygVMOABn21
  1055  hpMXOlZL+YXfR4Kp
  1056  -----END CERTIFICATE-----
  1057  EOF
  1058  
  1059    certificate_chain = <<EOF
  1060  -----BEGIN CERTIFICATE-----
  1061  MIID8TCCAtmgAwIBAgIJAKX2xeCkfFcbMA0GCSqGSIb3DQEBCwUAMIGOMQswCQYD
  1062  VQQGEwJVUzERMA8GA1UECAwITmV3IFlvcmsxETAPBgNVBAcMCE5ldyBZb3JrMRYw
  1063  FAYDVQQKDA1CYXJlZm9vdCBMYWJzMRgwFgYDVQQDDA9KYXNvbiBCZXJsaW5za3kx
  1064  JzAlBgkqhkiG9w0BCQEWGGphc29uQGJhcmVmb290Y29kZXJzLmNvbTAeFw0xNTA2
  1065  MjEwNTM2MDZaFw0yNTA2MTgwNTM2MDZaMIGOMQswCQYDVQQGEwJVUzERMA8GA1UE
  1066  CAwITmV3IFlvcmsxETAPBgNVBAcMCE5ldyBZb3JrMRYwFAYDVQQKDA1CYXJlZm9v
  1067  dCBMYWJzMRgwFgYDVQQDDA9KYXNvbiBCZXJsaW5za3kxJzAlBgkqhkiG9w0BCQEW
  1068  GGphc29uQGJhcmVmb290Y29kZXJzLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEP
  1069  ADCCAQoCggEBAMteFbwfLz7NyQn3eDxxw22l1ZPBrzfPON0HOAq8nHat4kT4A2cI
  1070  45kCtxKMzCVoG84tXoX/rbjGkez7lz9lEfvEuSh+I+UqinFA/sefhcE63foVMZu1
  1071  2t6O3+utdxBvOYJwAQaiGW44x0h6fTyqDv6Gc5Ml0uoIVeMWPhT1MREoOcPDz1gb
  1072  Ep3VT2aqFULLJedP37qbzS4D04rn1tS7pcm3wYivRyjVNEvs91NsWEvvE1WtS2Cl
  1073  2RBt+ihXwq4UNB9UPYG75+FuRcQQvfqameyweyKT9qBmJLELMtYa/KTCYvSch4JY
  1074  YVPAPOlhFlO4BcTto/gpBes2WEAWZtE/jnECAwEAAaNQME4wHQYDVR0OBBYEFOna
  1075  aiYnm5583EY7FT/mXwTBuLZgMB8GA1UdIwQYMBaAFOnaaiYnm5583EY7FT/mXwTB
  1076  uLZgMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBABp/dKQ489CCzzB1
  1077  IX78p6RFAdda4e3lL6uVjeS3itzFIIiKvdf1/txhmsEeCEYz0El6aMnXLkpk7jAr
  1078  kCwlAOOz2R2hlA8k8opKTYX4IQQau8DATslUFAFOvRGOim/TD/Yuch+a/VF2VQKz
  1079  L2lUVi5Hjp9KvWe2HQYPjnJaZs/OKAmZQ4uP547dqFrTz6sWfisF1rJ60JH70cyM
  1080  qjZQp/xYHTZIB8TCPvLgtVIGFmd/VAHVBFW2p9IBwtSxBIsEPwYQOV3XbwhhmGIv
  1081  DWx5TpnEzH7ZM33RNbAKcdwOBxdRY+SI/ua5hYCm4QngAqY69lEuk4zXZpdDLPq1
  1082  qxxQx0E=
  1083  -----END CERTIFICATE-----
  1084  EOF
  1085  
  1086  	private_key =  <<EOF
  1087  -----BEGIN RSA PRIVATE KEY-----
  1088  MIICXQIBAAKBgQD2AVGKRIx+EFM0kkg76GoJv9uy0biEDHB4phQBqnDIf8J8/gq9
  1089  eVvQrR5jJC9Uz4zp5wG/oLZlGuF92/jDbI/yS+DOAjrh30vN79Au74jGN2Cw8fIa
  1090  k40iDUwjZaczK2Gkna54XIO9pqMcbQ6QmLUkQXsqlJ7Q4X2kL3b9iMsXcQIDAQAB
  1091  AoGALmVBQ5p6BKx/hMKx7NqAZSZSAP+clQrji12HGGlUq/usanZfAC0LK+f6eygv
  1092  5QbfxJ1UrxdYTukq7dm2qOSooOMUuukWInqC6ztjdLwH70CKnl0bkNB3/NkW2VNc
  1093  32YiUuZCM9zaeBuEUclKNs+dhD2EeGdJF8KGntWGOTU/M4ECQQD9gdYb38PvaMdu
  1094  opM3sKJF5n9pMoLDleBpCGqq3nD3DFn0V6PHQAwn30EhRN+7BbUEpde5PmfoIdAR
  1095  uDlj/XPlAkEA+GyY1e4uU9rz+1K4ubxmtXTp9ZIR2LsqFy5L/MS5hqX2zq5GGq8g
  1096  jZYDxnxPEUrxaWQH4nh0qdu3skUBi4a0nQJBAKJaqLkpUd7eB/t++zHLWeHSgP7q
  1097  bny8XABod4f+9fICYwntpuJQzngqrxeTeIXaXdggLkxg/0LXhN4UUg0LoVECQQDE
  1098  Pi1h2dyY+37/CzLH7q+IKopjJneYqQmv9C+sxs70MgjM7liM3ckub9IdqrdfJr+c
  1099  DJw56APo5puvZNm6mbf1AkBVMDyfdOOyoHpJjrhmZWo6QqynujfwErrBYQ0sZQ3l
  1100  O57Z0RUNQ8DRyymhLd2t5nAHTfpcFA1sBeKE6CziLbZB
  1101  -----END RSA PRIVATE KEY-----
  1102  EOF
  1103  }
  1104  
  1105  resource "aws_elb" "bar" {
  1106    name = "foobar-terraform-test"
  1107    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
  1108  
  1109    listener {
  1110      instance_port = 8000
  1111      instance_protocol = "https"
  1112      lb_port = 80
  1113      // Protocol should be case insensitive
  1114      lb_protocol = "HttPs"
  1115      ssl_certificate_id = "${aws_iam_server_certificate.test_cert.arn}"
  1116    }
  1117  
  1118  	tags {
  1119  		bar = "baz"
  1120  	}
  1121  
  1122    cross_zone_load_balancing = true
  1123  }
  1124  `