github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_elb_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"reflect"
     7  	"regexp"
     8  	"sort"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/aws/aws-sdk-go/aws"
    13  	"github.com/aws/aws-sdk-go/aws/awserr"
    14  	"github.com/aws/aws-sdk-go/service/elb"
    15  	"github.com/hashicorp/terraform/helper/acctest"
    16  	"github.com/hashicorp/terraform/helper/resource"
    17  	"github.com/hashicorp/terraform/terraform"
    18  )
    19  
    20  func TestAccAWSELB_basic(t *testing.T) {
    21  	var conf elb.LoadBalancerDescription
    22  
    23  	resource.Test(t, resource.TestCase{
    24  		PreCheck:      func() { testAccPreCheck(t) },
    25  		IDRefreshName: "aws_elb.bar",
    26  		Providers:     testAccProviders,
    27  		CheckDestroy:  testAccCheckAWSELBDestroy,
    28  		Steps: []resource.TestStep{
    29  			{
    30  				Config: testAccAWSELBConfig,
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
    33  					testAccCheckAWSELBAttributes(&conf),
    34  					resource.TestCheckResourceAttr(
    35  						"aws_elb.bar", "availability_zones.#", "3"),
    36  					resource.TestCheckResourceAttr(
    37  						"aws_elb.bar", "availability_zones.2487133097", "us-west-2a"),
    38  					resource.TestCheckResourceAttr(
    39  						"aws_elb.bar", "availability_zones.221770259", "us-west-2b"),
    40  					resource.TestCheckResourceAttr(
    41  						"aws_elb.bar", "availability_zones.2050015877", "us-west-2c"),
    42  					resource.TestCheckResourceAttr(
    43  						"aws_elb.bar", "subnets.#", "3"),
    44  					// NOTE: Subnet IDs are different across AWS accounts and cannot be checked.
    45  					resource.TestCheckResourceAttr(
    46  						"aws_elb.bar", "listener.206423021.instance_port", "8000"),
    47  					resource.TestCheckResourceAttr(
    48  						"aws_elb.bar", "listener.206423021.instance_protocol", "http"),
    49  					resource.TestCheckResourceAttr(
    50  						"aws_elb.bar", "listener.206423021.lb_port", "80"),
    51  					resource.TestCheckResourceAttr(
    52  						"aws_elb.bar", "listener.206423021.lb_protocol", "http"),
    53  					resource.TestCheckResourceAttr(
    54  						"aws_elb.bar", "cross_zone_load_balancing", "true"),
    55  				),
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func TestAccAWSELB_fullCharacterRange(t *testing.T) {
    62  	var conf elb.LoadBalancerDescription
    63  
    64  	lbName := fmt.Sprintf("Tf-%d",
    65  		rand.New(rand.NewSource(time.Now().UnixNano())).Int())
    66  
    67  	resource.Test(t, resource.TestCase{
    68  		PreCheck:      func() { testAccPreCheck(t) },
    69  		IDRefreshName: "aws_elb.foo",
    70  		Providers:     testAccProviders,
    71  		CheckDestroy:  testAccCheckAWSELBDestroy,
    72  		Steps: []resource.TestStep{
    73  			{
    74  				Config: fmt.Sprintf(testAccAWSELBFullRangeOfCharacters, lbName),
    75  				Check: resource.ComposeTestCheckFunc(
    76  					testAccCheckAWSELBExists("aws_elb.foo", &conf),
    77  					resource.TestCheckResourceAttr(
    78  						"aws_elb.foo", "name", lbName),
    79  				),
    80  			},
    81  		},
    82  	})
    83  }
    84  
    85  func TestAccAWSELB_AccessLogs_enabled(t *testing.T) {
    86  	var conf elb.LoadBalancerDescription
    87  
    88  	rName := fmt.Sprintf("terraform-access-logs-bucket-%d", acctest.RandInt())
    89  
    90  	resource.Test(t, resource.TestCase{
    91  		PreCheck:      func() { testAccPreCheck(t) },
    92  		IDRefreshName: "aws_elb.foo",
    93  		Providers:     testAccProviders,
    94  		CheckDestroy:  testAccCheckAWSELBDestroy,
    95  		Steps: []resource.TestStep{
    96  			{
    97  				Config: testAccAWSELBAccessLogs,
    98  				Check: resource.ComposeTestCheckFunc(
    99  					testAccCheckAWSELBExists("aws_elb.foo", &conf),
   100  				),
   101  			},
   102  
   103  			{
   104  				Config: testAccAWSELBAccessLogsOn(rName),
   105  				Check: resource.ComposeTestCheckFunc(
   106  					testAccCheckAWSELBExists("aws_elb.foo", &conf),
   107  					resource.TestCheckResourceAttr(
   108  						"aws_elb.foo", "access_logs.#", "1"),
   109  					resource.TestCheckResourceAttr(
   110  						"aws_elb.foo", "access_logs.0.bucket", rName),
   111  					resource.TestCheckResourceAttr(
   112  						"aws_elb.foo", "access_logs.0.interval", "5"),
   113  					resource.TestCheckResourceAttr(
   114  						"aws_elb.foo", "access_logs.0.enabled", "true"),
   115  				),
   116  			},
   117  
   118  			{
   119  				Config: testAccAWSELBAccessLogs,
   120  				Check: resource.ComposeTestCheckFunc(
   121  					testAccCheckAWSELBExists("aws_elb.foo", &conf),
   122  					resource.TestCheckResourceAttr(
   123  						"aws_elb.foo", "access_logs.#", "0"),
   124  				),
   125  			},
   126  		},
   127  	})
   128  }
   129  
   130  func TestAccAWSELB_AccessLogs_disabled(t *testing.T) {
   131  	var conf elb.LoadBalancerDescription
   132  
   133  	rName := fmt.Sprintf("terraform-access-logs-bucket-%d", acctest.RandInt())
   134  
   135  	resource.Test(t, resource.TestCase{
   136  		PreCheck:      func() { testAccPreCheck(t) },
   137  		IDRefreshName: "aws_elb.foo",
   138  		Providers:     testAccProviders,
   139  		CheckDestroy:  testAccCheckAWSELBDestroy,
   140  		Steps: []resource.TestStep{
   141  			{
   142  				Config: testAccAWSELBAccessLogs,
   143  				Check: resource.ComposeTestCheckFunc(
   144  					testAccCheckAWSELBExists("aws_elb.foo", &conf),
   145  				),
   146  			},
   147  
   148  			{
   149  				Config: testAccAWSELBAccessLogsDisabled(rName),
   150  				Check: resource.ComposeTestCheckFunc(
   151  					testAccCheckAWSELBExists("aws_elb.foo", &conf),
   152  					resource.TestCheckResourceAttr(
   153  						"aws_elb.foo", "access_logs.#", "1"),
   154  					resource.TestCheckResourceAttr(
   155  						"aws_elb.foo", "access_logs.0.bucket", rName),
   156  					resource.TestCheckResourceAttr(
   157  						"aws_elb.foo", "access_logs.0.interval", "5"),
   158  					resource.TestCheckResourceAttr(
   159  						"aws_elb.foo", "access_logs.0.enabled", "false"),
   160  				),
   161  			},
   162  
   163  			{
   164  				Config: testAccAWSELBAccessLogs,
   165  				Check: resource.ComposeTestCheckFunc(
   166  					testAccCheckAWSELBExists("aws_elb.foo", &conf),
   167  					resource.TestCheckResourceAttr(
   168  						"aws_elb.foo", "access_logs.#", "0"),
   169  				),
   170  			},
   171  		},
   172  	})
   173  }
   174  
   175  func TestAccAWSELB_namePrefix(t *testing.T) {
   176  	var conf elb.LoadBalancerDescription
   177  	nameRegex := regexp.MustCompile("^test-")
   178  
   179  	resource.Test(t, resource.TestCase{
   180  		PreCheck:      func() { testAccPreCheck(t) },
   181  		IDRefreshName: "aws_elb.test",
   182  		Providers:     testAccProviders,
   183  		CheckDestroy:  testAccCheckAWSELBDestroy,
   184  		Steps: []resource.TestStep{
   185  			resource.TestStep{
   186  				Config: testAccAWSELB_namePrefix,
   187  				Check: resource.ComposeTestCheckFunc(
   188  					testAccCheckAWSELBExists("aws_elb.test", &conf),
   189  					resource.TestMatchResourceAttr(
   190  						"aws_elb.test", "name", nameRegex),
   191  				),
   192  			},
   193  		},
   194  	})
   195  }
   196  
   197  func TestAccAWSELB_generatedName(t *testing.T) {
   198  	var conf elb.LoadBalancerDescription
   199  	generatedNameRegexp := regexp.MustCompile("^tf-lb-")
   200  
   201  	resource.Test(t, resource.TestCase{
   202  		PreCheck:      func() { testAccPreCheck(t) },
   203  		IDRefreshName: "aws_elb.foo",
   204  		Providers:     testAccProviders,
   205  		CheckDestroy:  testAccCheckAWSELBDestroy,
   206  		Steps: []resource.TestStep{
   207  			{
   208  				Config: testAccAWSELBGeneratedName,
   209  				Check: resource.ComposeTestCheckFunc(
   210  					testAccCheckAWSELBExists("aws_elb.foo", &conf),
   211  					resource.TestMatchResourceAttr(
   212  						"aws_elb.foo", "name", generatedNameRegexp),
   213  				),
   214  			},
   215  		},
   216  	})
   217  }
   218  
   219  func TestAccAWSELB_availabilityZones(t *testing.T) {
   220  	var conf elb.LoadBalancerDescription
   221  
   222  	resource.Test(t, resource.TestCase{
   223  		PreCheck:      func() { testAccPreCheck(t) },
   224  		IDRefreshName: "aws_elb.bar",
   225  		Providers:     testAccProviders,
   226  		CheckDestroy:  testAccCheckAWSELBDestroy,
   227  		Steps: []resource.TestStep{
   228  			{
   229  				Config: testAccAWSELBConfig,
   230  				Check: resource.ComposeTestCheckFunc(
   231  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   232  					resource.TestCheckResourceAttr(
   233  						"aws_elb.bar", "availability_zones.#", "3"),
   234  					resource.TestCheckResourceAttr(
   235  						"aws_elb.bar", "availability_zones.2487133097", "us-west-2a"),
   236  					resource.TestCheckResourceAttr(
   237  						"aws_elb.bar", "availability_zones.221770259", "us-west-2b"),
   238  					resource.TestCheckResourceAttr(
   239  						"aws_elb.bar", "availability_zones.2050015877", "us-west-2c"),
   240  				),
   241  			},
   242  
   243  			{
   244  				Config: testAccAWSELBConfig_AvailabilityZonesUpdate,
   245  				Check: resource.ComposeTestCheckFunc(
   246  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   247  					resource.TestCheckResourceAttr(
   248  						"aws_elb.bar", "availability_zones.#", "2"),
   249  					resource.TestCheckResourceAttr(
   250  						"aws_elb.bar", "availability_zones.2487133097", "us-west-2a"),
   251  					resource.TestCheckResourceAttr(
   252  						"aws_elb.bar", "availability_zones.221770259", "us-west-2b"),
   253  				),
   254  			},
   255  		},
   256  	})
   257  }
   258  
   259  func TestAccAWSELB_tags(t *testing.T) {
   260  	var conf elb.LoadBalancerDescription
   261  	var td elb.TagDescription
   262  
   263  	resource.Test(t, resource.TestCase{
   264  		PreCheck:      func() { testAccPreCheck(t) },
   265  		IDRefreshName: "aws_elb.bar",
   266  		Providers:     testAccProviders,
   267  		CheckDestroy:  testAccCheckAWSELBDestroy,
   268  		Steps: []resource.TestStep{
   269  			{
   270  				Config: testAccAWSELBConfig,
   271  				Check: resource.ComposeTestCheckFunc(
   272  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   273  					testAccCheckAWSELBAttributes(&conf),
   274  					testAccLoadTags(&conf, &td),
   275  					testAccCheckELBTags(&td.Tags, "bar", "baz"),
   276  				),
   277  			},
   278  
   279  			{
   280  				Config: testAccAWSELBConfig_TagUpdate,
   281  				Check: resource.ComposeTestCheckFunc(
   282  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   283  					testAccCheckAWSELBAttributes(&conf),
   284  					testAccLoadTags(&conf, &td),
   285  					testAccCheckELBTags(&td.Tags, "foo", "bar"),
   286  					testAccCheckELBTags(&td.Tags, "new", "type"),
   287  				),
   288  			},
   289  		},
   290  	})
   291  }
   292  
   293  func TestAccAWSELB_iam_server_cert(t *testing.T) {
   294  	var conf elb.LoadBalancerDescription
   295  	// var td elb.TagDescription
   296  	testCheck := func(*terraform.State) error {
   297  		if len(conf.ListenerDescriptions) != 1 {
   298  			return fmt.Errorf(
   299  				"TestAccAWSELB_iam_server_cert expected 1 listener, got %d",
   300  				len(conf.ListenerDescriptions))
   301  		}
   302  		return nil
   303  	}
   304  	resource.Test(t, resource.TestCase{
   305  		PreCheck:      func() { testAccPreCheck(t) },
   306  		IDRefreshName: "aws_elb.bar",
   307  		Providers:     testAccProviders,
   308  		CheckDestroy:  testAccCheckAWSELBDestroy,
   309  		Steps: []resource.TestStep{
   310  			{
   311  				Config: testAccELBIAMServerCertConfig(
   312  					fmt.Sprintf("tf-acctest-%s", acctest.RandString(10))),
   313  				Check: resource.ComposeTestCheckFunc(
   314  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   315  					testCheck,
   316  				),
   317  			},
   318  		},
   319  	})
   320  }
   321  
   322  func TestAccAWSELB_swap_subnets(t *testing.T) {
   323  	var conf elb.LoadBalancerDescription
   324  
   325  	resource.Test(t, resource.TestCase{
   326  		PreCheck:      func() { testAccPreCheck(t) },
   327  		IDRefreshName: "aws_elb.ourapp",
   328  		Providers:     testAccProviders,
   329  		CheckDestroy:  testAccCheckAWSELBDestroy,
   330  		Steps: []resource.TestStep{
   331  			{
   332  				Config: testAccAWSELBConfig_subnets,
   333  				Check: resource.ComposeTestCheckFunc(
   334  					testAccCheckAWSELBExists("aws_elb.ourapp", &conf),
   335  					resource.TestCheckResourceAttr(
   336  						"aws_elb.ourapp", "subnets.#", "2"),
   337  				),
   338  			},
   339  
   340  			{
   341  				Config: testAccAWSELBConfig_subnet_swap,
   342  				Check: resource.ComposeTestCheckFunc(
   343  					testAccCheckAWSELBExists("aws_elb.ourapp", &conf),
   344  					resource.TestCheckResourceAttr(
   345  						"aws_elb.ourapp", "subnets.#", "2"),
   346  				),
   347  			},
   348  		},
   349  	})
   350  }
   351  
   352  func testAccLoadTags(conf *elb.LoadBalancerDescription, td *elb.TagDescription) resource.TestCheckFunc {
   353  	return func(s *terraform.State) error {
   354  		conn := testAccProvider.Meta().(*AWSClient).elbconn
   355  
   356  		describe, err := conn.DescribeTags(&elb.DescribeTagsInput{
   357  			LoadBalancerNames: []*string{conf.LoadBalancerName},
   358  		})
   359  
   360  		if err != nil {
   361  			return err
   362  		}
   363  		if len(describe.TagDescriptions) > 0 {
   364  			*td = *describe.TagDescriptions[0]
   365  		}
   366  		return nil
   367  	}
   368  }
   369  
   370  func TestAccAWSELB_InstanceAttaching(t *testing.T) {
   371  	var conf elb.LoadBalancerDescription
   372  
   373  	testCheckInstanceAttached := func(count int) resource.TestCheckFunc {
   374  		return func(*terraform.State) error {
   375  			if len(conf.Instances) != count {
   376  				return fmt.Errorf("instance count does not match")
   377  			}
   378  			return nil
   379  		}
   380  	}
   381  
   382  	resource.Test(t, resource.TestCase{
   383  		PreCheck:      func() { testAccPreCheck(t) },
   384  		IDRefreshName: "aws_elb.bar",
   385  		Providers:     testAccProviders,
   386  		CheckDestroy:  testAccCheckAWSELBDestroy,
   387  		Steps: []resource.TestStep{
   388  			{
   389  				Config: testAccAWSELBConfig,
   390  				Check: resource.ComposeTestCheckFunc(
   391  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   392  					testAccCheckAWSELBAttributes(&conf),
   393  				),
   394  			},
   395  
   396  			{
   397  				Config: testAccAWSELBConfigNewInstance,
   398  				Check: resource.ComposeTestCheckFunc(
   399  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   400  					testCheckInstanceAttached(1),
   401  				),
   402  			},
   403  		},
   404  	})
   405  }
   406  
   407  func TestAccAWSELBUpdate_Listener(t *testing.T) {
   408  	var conf elb.LoadBalancerDescription
   409  
   410  	resource.Test(t, resource.TestCase{
   411  		PreCheck:      func() { testAccPreCheck(t) },
   412  		IDRefreshName: "aws_elb.bar",
   413  		Providers:     testAccProviders,
   414  		CheckDestroy:  testAccCheckAWSELBDestroy,
   415  		Steps: []resource.TestStep{
   416  			{
   417  				Config: testAccAWSELBConfig,
   418  				Check: resource.ComposeTestCheckFunc(
   419  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   420  					testAccCheckAWSELBAttributes(&conf),
   421  					resource.TestCheckResourceAttr(
   422  						"aws_elb.bar", "listener.206423021.instance_port", "8000"),
   423  				),
   424  			},
   425  
   426  			{
   427  				Config: testAccAWSELBConfigListener_update,
   428  				Check: resource.ComposeTestCheckFunc(
   429  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   430  					resource.TestCheckResourceAttr(
   431  						"aws_elb.bar", "listener.3931999347.instance_port", "8080"),
   432  				),
   433  			},
   434  		},
   435  	})
   436  }
   437  
   438  func TestAccAWSELB_HealthCheck(t *testing.T) {
   439  	var conf elb.LoadBalancerDescription
   440  
   441  	resource.Test(t, resource.TestCase{
   442  		PreCheck:      func() { testAccPreCheck(t) },
   443  		IDRefreshName: "aws_elb.bar",
   444  		Providers:     testAccProviders,
   445  		CheckDestroy:  testAccCheckAWSELBDestroy,
   446  		Steps: []resource.TestStep{
   447  			{
   448  				Config: testAccAWSELBConfigHealthCheck,
   449  				Check: resource.ComposeTestCheckFunc(
   450  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   451  					testAccCheckAWSELBAttributesHealthCheck(&conf),
   452  					resource.TestCheckResourceAttr(
   453  						"aws_elb.bar", "health_check.0.healthy_threshold", "5"),
   454  					resource.TestCheckResourceAttr(
   455  						"aws_elb.bar", "health_check.0.unhealthy_threshold", "5"),
   456  					resource.TestCheckResourceAttr(
   457  						"aws_elb.bar", "health_check.0.target", "HTTP:8000/"),
   458  					resource.TestCheckResourceAttr(
   459  						"aws_elb.bar", "health_check.0.timeout", "30"),
   460  					resource.TestCheckResourceAttr(
   461  						"aws_elb.bar", "health_check.0.interval", "60"),
   462  				),
   463  			},
   464  		},
   465  	})
   466  }
   467  
   468  func TestAccAWSELBUpdate_HealthCheck(t *testing.T) {
   469  	resource.Test(t, resource.TestCase{
   470  		PreCheck:      func() { testAccPreCheck(t) },
   471  		IDRefreshName: "aws_elb.bar",
   472  		Providers:     testAccProviders,
   473  		CheckDestroy:  testAccCheckAWSELBDestroy,
   474  		Steps: []resource.TestStep{
   475  			{
   476  				Config: testAccAWSELBConfigHealthCheck,
   477  				Check: resource.ComposeTestCheckFunc(
   478  					resource.TestCheckResourceAttr(
   479  						"aws_elb.bar", "health_check.0.healthy_threshold", "5"),
   480  				),
   481  			},
   482  			{
   483  				Config: testAccAWSELBConfigHealthCheck_update,
   484  				Check: resource.ComposeTestCheckFunc(
   485  					resource.TestCheckResourceAttr(
   486  						"aws_elb.bar", "health_check.0.healthy_threshold", "10"),
   487  				),
   488  			},
   489  		},
   490  	})
   491  }
   492  
   493  func TestAccAWSELB_Timeout(t *testing.T) {
   494  	var conf elb.LoadBalancerDescription
   495  
   496  	resource.Test(t, resource.TestCase{
   497  		PreCheck:      func() { testAccPreCheck(t) },
   498  		IDRefreshName: "aws_elb.bar",
   499  		Providers:     testAccProviders,
   500  		CheckDestroy:  testAccCheckAWSELBDestroy,
   501  		Steps: []resource.TestStep{
   502  			{
   503  				Config: testAccAWSELBConfigIdleTimeout,
   504  				Check: resource.ComposeTestCheckFunc(
   505  					testAccCheckAWSELBExists("aws_elb.bar", &conf),
   506  					resource.TestCheckResourceAttr(
   507  						"aws_elb.bar", "idle_timeout", "200",
   508  					),
   509  				),
   510  			},
   511  		},
   512  	})
   513  }
   514  
   515  func TestAccAWSELBUpdate_Timeout(t *testing.T) {
   516  	resource.Test(t, resource.TestCase{
   517  		PreCheck:      func() { testAccPreCheck(t) },
   518  		IDRefreshName: "aws_elb.bar",
   519  		Providers:     testAccProviders,
   520  		CheckDestroy:  testAccCheckAWSELBDestroy,
   521  		Steps: []resource.TestStep{
   522  			{
   523  				Config: testAccAWSELBConfigIdleTimeout,
   524  				Check: resource.ComposeTestCheckFunc(
   525  					resource.TestCheckResourceAttr(
   526  						"aws_elb.bar", "idle_timeout", "200",
   527  					),
   528  				),
   529  			},
   530  			{
   531  				Config: testAccAWSELBConfigIdleTimeout_update,
   532  				Check: resource.ComposeTestCheckFunc(
   533  					resource.TestCheckResourceAttr(
   534  						"aws_elb.bar", "idle_timeout", "400",
   535  					),
   536  				),
   537  			},
   538  		},
   539  	})
   540  }
   541  
   542  func TestAccAWSELB_ConnectionDraining(t *testing.T) {
   543  	resource.Test(t, resource.TestCase{
   544  		PreCheck:      func() { testAccPreCheck(t) },
   545  		IDRefreshName: "aws_elb.bar",
   546  		Providers:     testAccProviders,
   547  		CheckDestroy:  testAccCheckAWSELBDestroy,
   548  		Steps: []resource.TestStep{
   549  			{
   550  				Config: testAccAWSELBConfigConnectionDraining,
   551  				Check: resource.ComposeTestCheckFunc(
   552  					resource.TestCheckResourceAttr(
   553  						"aws_elb.bar", "connection_draining", "true",
   554  					),
   555  					resource.TestCheckResourceAttr(
   556  						"aws_elb.bar", "connection_draining_timeout", "400",
   557  					),
   558  				),
   559  			},
   560  		},
   561  	})
   562  }
   563  
   564  func TestAccAWSELBUpdate_ConnectionDraining(t *testing.T) {
   565  	resource.Test(t, resource.TestCase{
   566  		PreCheck:      func() { testAccPreCheck(t) },
   567  		IDRefreshName: "aws_elb.bar",
   568  		Providers:     testAccProviders,
   569  		CheckDestroy:  testAccCheckAWSELBDestroy,
   570  		Steps: []resource.TestStep{
   571  			{
   572  				Config: testAccAWSELBConfigConnectionDraining,
   573  				Check: resource.ComposeTestCheckFunc(
   574  					resource.TestCheckResourceAttr(
   575  						"aws_elb.bar", "connection_draining", "true",
   576  					),
   577  					resource.TestCheckResourceAttr(
   578  						"aws_elb.bar", "connection_draining_timeout", "400",
   579  					),
   580  				),
   581  			},
   582  			{
   583  				Config: testAccAWSELBConfigConnectionDraining_update_timeout,
   584  				Check: resource.ComposeTestCheckFunc(
   585  					resource.TestCheckResourceAttr(
   586  						"aws_elb.bar", "connection_draining", "true",
   587  					),
   588  					resource.TestCheckResourceAttr(
   589  						"aws_elb.bar", "connection_draining_timeout", "600",
   590  					),
   591  				),
   592  			},
   593  			{
   594  				Config: testAccAWSELBConfigConnectionDraining_update_disable,
   595  				Check: resource.ComposeTestCheckFunc(
   596  					resource.TestCheckResourceAttr(
   597  						"aws_elb.bar", "connection_draining", "false",
   598  					),
   599  				),
   600  			},
   601  		},
   602  	})
   603  }
   604  
   605  func TestAccAWSELB_SecurityGroups(t *testing.T) {
   606  	resource.Test(t, resource.TestCase{
   607  		PreCheck:      func() { testAccPreCheck(t) },
   608  		IDRefreshName: "aws_elb.bar",
   609  		Providers:     testAccProviders,
   610  		CheckDestroy:  testAccCheckAWSELBDestroy,
   611  		Steps: []resource.TestStep{
   612  			{
   613  				Config: testAccAWSELBConfig,
   614  				Check: resource.ComposeTestCheckFunc(
   615  					// ELBs get a default security group
   616  					resource.TestCheckResourceAttr(
   617  						"aws_elb.bar", "security_groups.#", "1",
   618  					),
   619  				),
   620  			},
   621  			{
   622  				Config: testAccAWSELBConfigSecurityGroups,
   623  				Check: resource.ComposeTestCheckFunc(
   624  					// Count should still be one as we swap in a custom security group
   625  					resource.TestCheckResourceAttr(
   626  						"aws_elb.bar", "security_groups.#", "1",
   627  					),
   628  				),
   629  			},
   630  		},
   631  	})
   632  }
   633  
   634  // Unit test for listeners hash
   635  func TestResourceAwsElbListenerHash(t *testing.T) {
   636  	cases := map[string]struct {
   637  		Left  map[string]interface{}
   638  		Right map[string]interface{}
   639  		Match bool
   640  	}{
   641  		"protocols are case insensitive": {
   642  			map[string]interface{}{
   643  				"instance_port":     80,
   644  				"instance_protocol": "TCP",
   645  				"lb_port":           80,
   646  				"lb_protocol":       "TCP",
   647  			},
   648  			map[string]interface{}{
   649  				"instance_port":     80,
   650  				"instance_protocol": "Tcp",
   651  				"lb_port":           80,
   652  				"lb_protocol":       "tcP",
   653  			},
   654  			true,
   655  		},
   656  	}
   657  
   658  	for tn, tc := range cases {
   659  		leftHash := resourceAwsElbListenerHash(tc.Left)
   660  		rightHash := resourceAwsElbListenerHash(tc.Right)
   661  		if leftHash == rightHash != tc.Match {
   662  			t.Fatalf("%s: expected match: %t, but did not get it", tn, tc.Match)
   663  		}
   664  	}
   665  }
   666  
   667  func TestResourceAWSELB_validateElbNameCannotBeginWithHyphen(t *testing.T) {
   668  	var elbName = "-Testing123"
   669  	_, errors := validateElbName(elbName, "SampleKey")
   670  
   671  	if len(errors) != 1 {
   672  		t.Fatalf("Expected the ELB Name to trigger a validation error")
   673  	}
   674  }
   675  
   676  func TestResourceAWSELB_validateElbNameCannotBeLongerThen32Characters(t *testing.T) {
   677  	var elbName = "Testing123dddddddddddddddddddvvvv"
   678  	_, errors := validateElbName(elbName, "SampleKey")
   679  
   680  	if len(errors) != 1 {
   681  		t.Fatalf("Expected the ELB Name to trigger a validation error")
   682  	}
   683  }
   684  
   685  func TestResourceAWSELB_validateElbNameCannotHaveSpecialCharacters(t *testing.T) {
   686  	var elbName = "Testing123%%"
   687  	_, errors := validateElbName(elbName, "SampleKey")
   688  
   689  	if len(errors) != 1 {
   690  		t.Fatalf("Expected the ELB Name to trigger a validation error")
   691  	}
   692  }
   693  
   694  func TestResourceAWSELB_validateElbNameCannotEndWithHyphen(t *testing.T) {
   695  	var elbName = "Testing123-"
   696  	_, errors := validateElbName(elbName, "SampleKey")
   697  
   698  	if len(errors) != 1 {
   699  		t.Fatalf("Expected the ELB Name to trigger a validation error")
   700  	}
   701  }
   702  
   703  func TestResourceAWSELB_validateAccessLogsInterval(t *testing.T) {
   704  	type testCases struct {
   705  		Value    int
   706  		ErrCount int
   707  	}
   708  
   709  	invalidCases := []testCases{
   710  		{
   711  			Value:    0,
   712  			ErrCount: 1,
   713  		},
   714  		{
   715  			Value:    10,
   716  			ErrCount: 1,
   717  		},
   718  		{
   719  			Value:    -1,
   720  			ErrCount: 1,
   721  		},
   722  	}
   723  
   724  	for _, tc := range invalidCases {
   725  		_, errors := validateAccessLogsInterval(tc.Value, "interval")
   726  		if len(errors) != tc.ErrCount {
   727  			t.Fatalf("Expected %q to trigger a validation error.", tc.Value)
   728  		}
   729  	}
   730  
   731  }
   732  
   733  func TestResourceAWSELB_validateListenerProtocol(t *testing.T) {
   734  	type testCases struct {
   735  		Value    string
   736  		ErrCount int
   737  	}
   738  
   739  	invalidCases := []testCases{
   740  		{
   741  			Value:    "",
   742  			ErrCount: 1,
   743  		},
   744  		{
   745  			Value:    "incorrect",
   746  			ErrCount: 1,
   747  		},
   748  		{
   749  			Value:    "HTTP:",
   750  			ErrCount: 1,
   751  		},
   752  	}
   753  
   754  	for _, tc := range invalidCases {
   755  		_, errors := validateListenerProtocol(tc.Value, "protocol")
   756  		if len(errors) != tc.ErrCount {
   757  			t.Fatalf("Expected %q to trigger a validation error.", tc.Value)
   758  		}
   759  	}
   760  
   761  	validCases := []testCases{
   762  		{
   763  			Value:    "TCP",
   764  			ErrCount: 0,
   765  		},
   766  		{
   767  			Value:    "ssl",
   768  			ErrCount: 0,
   769  		},
   770  		{
   771  			Value:    "HTTP",
   772  			ErrCount: 0,
   773  		},
   774  	}
   775  
   776  	for _, tc := range validCases {
   777  		_, errors := validateListenerProtocol(tc.Value, "protocol")
   778  		if len(errors) != tc.ErrCount {
   779  			t.Fatalf("Expected %q not to trigger a validation error.", tc.Value)
   780  		}
   781  	}
   782  }
   783  
   784  func TestResourceAWSELB_validateHealthCheckTarget(t *testing.T) {
   785  	type testCase struct {
   786  		Value    string
   787  		ErrCount int
   788  	}
   789  
   790  	randomRunes := func(n int) string {
   791  		rand.Seed(time.Now().UTC().UnixNano())
   792  
   793  		// A complete set of modern Katakana characters.
   794  		runes := []rune("アイウエオ" +
   795  			"カキクケコガギグゲゴサシスセソザジズゼゾ" +
   796  			"タチツテトダヂヅデドナニヌネノハヒフヘホ" +
   797  			"バビブベボパピプペポマミムメモヤユヨラリ" +
   798  			"ルレロワヰヱヲン")
   799  
   800  		s := make([]rune, n)
   801  		for i := range s {
   802  			s[i] = runes[rand.Intn(len(runes))]
   803  		}
   804  		return string(s)
   805  	}
   806  
   807  	validCases := []testCase{
   808  		{
   809  			Value:    "TCP:1234",
   810  			ErrCount: 0,
   811  		},
   812  		{
   813  			Value:    "http:80/test",
   814  			ErrCount: 0,
   815  		},
   816  		{
   817  			Value:    fmt.Sprintf("HTTP:8080/%s", randomRunes(5)),
   818  			ErrCount: 0,
   819  		},
   820  		{
   821  			Value:    "SSL:8080",
   822  			ErrCount: 0,
   823  		},
   824  	}
   825  
   826  	for _, tc := range validCases {
   827  		_, errors := validateHeathCheckTarget(tc.Value, "target")
   828  		if len(errors) != tc.ErrCount {
   829  			t.Fatalf("Expected %q not to trigger a validation error.", tc.Value)
   830  		}
   831  	}
   832  
   833  	invalidCases := []testCase{
   834  		{
   835  			Value:    "",
   836  			ErrCount: 1,
   837  		},
   838  		{
   839  			Value:    "TCP:",
   840  			ErrCount: 1,
   841  		},
   842  		{
   843  			Value:    "TCP:1234/",
   844  			ErrCount: 1,
   845  		},
   846  		{
   847  			Value:    "SSL:8080/",
   848  			ErrCount: 1,
   849  		},
   850  		{
   851  			Value:    "HTTP:8080",
   852  			ErrCount: 1,
   853  		},
   854  		{
   855  			Value:    "incorrect-value",
   856  			ErrCount: 1,
   857  		},
   858  		{
   859  			Value:    "TCP:123456",
   860  			ErrCount: 1,
   861  		},
   862  		{
   863  			Value:    "incorrect:80/",
   864  			ErrCount: 1,
   865  		},
   866  		{
   867  			Value:    fmt.Sprintf("HTTP:8080/%s%s", randomString(512), randomRunes(512)),
   868  			ErrCount: 1,
   869  		},
   870  	}
   871  
   872  	for _, tc := range invalidCases {
   873  		_, errors := validateHeathCheckTarget(tc.Value, "target")
   874  		if len(errors) != tc.ErrCount {
   875  			t.Fatalf("Expected %q to trigger a validation error.", tc.Value)
   876  		}
   877  	}
   878  }
   879  
   880  func testAccCheckAWSELBDestroy(s *terraform.State) error {
   881  	conn := testAccProvider.Meta().(*AWSClient).elbconn
   882  
   883  	for _, rs := range s.RootModule().Resources {
   884  		if rs.Type != "aws_elb" {
   885  			continue
   886  		}
   887  
   888  		describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancersInput{
   889  			LoadBalancerNames: []*string{aws.String(rs.Primary.ID)},
   890  		})
   891  
   892  		if err == nil {
   893  			if len(describe.LoadBalancerDescriptions) != 0 &&
   894  				*describe.LoadBalancerDescriptions[0].LoadBalancerName == rs.Primary.ID {
   895  				return fmt.Errorf("ELB still exists")
   896  			}
   897  		}
   898  
   899  		// Verify the error
   900  		providerErr, ok := err.(awserr.Error)
   901  		if !ok {
   902  			return err
   903  		}
   904  
   905  		if providerErr.Code() != "LoadBalancerNotFound" {
   906  			return fmt.Errorf("Unexpected error: %s", err)
   907  		}
   908  	}
   909  
   910  	return nil
   911  }
   912  
   913  func testAccCheckAWSELBAttributes(conf *elb.LoadBalancerDescription) resource.TestCheckFunc {
   914  	return func(s *terraform.State) error {
   915  		zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"}
   916  		azs := make([]string, 0, len(conf.AvailabilityZones))
   917  		for _, x := range conf.AvailabilityZones {
   918  			azs = append(azs, *x)
   919  		}
   920  		sort.StringSlice(azs).Sort()
   921  		if !reflect.DeepEqual(azs, zones) {
   922  			return fmt.Errorf("bad availability_zones")
   923  		}
   924  
   925  		l := elb.Listener{
   926  			InstancePort:     aws.Int64(int64(8000)),
   927  			InstanceProtocol: aws.String("HTTP"),
   928  			LoadBalancerPort: aws.Int64(int64(80)),
   929  			Protocol:         aws.String("HTTP"),
   930  		}
   931  
   932  		if !reflect.DeepEqual(conf.ListenerDescriptions[0].Listener, &l) {
   933  			return fmt.Errorf(
   934  				"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   935  				conf.ListenerDescriptions[0].Listener,
   936  				l)
   937  		}
   938  
   939  		if *conf.DNSName == "" {
   940  			return fmt.Errorf("empty dns_name")
   941  		}
   942  
   943  		return nil
   944  	}
   945  }
   946  
   947  func testAccCheckAWSELBAttributesHealthCheck(conf *elb.LoadBalancerDescription) resource.TestCheckFunc {
   948  	return func(s *terraform.State) error {
   949  		zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"}
   950  		azs := make([]string, 0, len(conf.AvailabilityZones))
   951  		for _, x := range conf.AvailabilityZones {
   952  			azs = append(azs, *x)
   953  		}
   954  		sort.StringSlice(azs).Sort()
   955  		if !reflect.DeepEqual(azs, zones) {
   956  			return fmt.Errorf("bad availability_zones")
   957  		}
   958  
   959  		check := &elb.HealthCheck{
   960  			Timeout:            aws.Int64(int64(30)),
   961  			UnhealthyThreshold: aws.Int64(int64(5)),
   962  			HealthyThreshold:   aws.Int64(int64(5)),
   963  			Interval:           aws.Int64(int64(60)),
   964  			Target:             aws.String("HTTP:8000/"),
   965  		}
   966  
   967  		if !reflect.DeepEqual(conf.HealthCheck, check) {
   968  			return fmt.Errorf(
   969  				"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
   970  				conf.HealthCheck,
   971  				check)
   972  		}
   973  
   974  		if *conf.DNSName == "" {
   975  			return fmt.Errorf("empty dns_name")
   976  		}
   977  
   978  		return nil
   979  	}
   980  }
   981  
   982  func testAccCheckAWSELBExists(n string, res *elb.LoadBalancerDescription) resource.TestCheckFunc {
   983  	return func(s *terraform.State) error {
   984  		rs, ok := s.RootModule().Resources[n]
   985  		if !ok {
   986  			return fmt.Errorf("Not found: %s", n)
   987  		}
   988  
   989  		if rs.Primary.ID == "" {
   990  			return fmt.Errorf("No ELB ID is set")
   991  		}
   992  
   993  		conn := testAccProvider.Meta().(*AWSClient).elbconn
   994  
   995  		describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancersInput{
   996  			LoadBalancerNames: []*string{aws.String(rs.Primary.ID)},
   997  		})
   998  
   999  		if err != nil {
  1000  			return err
  1001  		}
  1002  
  1003  		if len(describe.LoadBalancerDescriptions) != 1 ||
  1004  			*describe.LoadBalancerDescriptions[0].LoadBalancerName != rs.Primary.ID {
  1005  			return fmt.Errorf("ELB not found")
  1006  		}
  1007  
  1008  		*res = *describe.LoadBalancerDescriptions[0]
  1009  
  1010  		// Confirm source_security_group_id for ELBs in a VPC
  1011  		// 	See https://github.com/hashicorp/terraform/pull/3780
  1012  		if res.VPCId != nil {
  1013  			sgid := rs.Primary.Attributes["source_security_group_id"]
  1014  			if sgid == "" {
  1015  				return fmt.Errorf("Expected to find source_security_group_id for ELB, but was empty")
  1016  			}
  1017  		}
  1018  
  1019  		return nil
  1020  	}
  1021  }
  1022  
  1023  const testAccAWSELBConfig = `
  1024  resource "aws_elb" "bar" {
  1025    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
  1026  
  1027    listener {
  1028      instance_port = 8000
  1029      instance_protocol = "http"
  1030      lb_port = 80
  1031      // Protocol should be case insensitive
  1032      lb_protocol = "HttP"
  1033    }
  1034  
  1035  	tags {
  1036  		bar = "baz"
  1037  	}
  1038  
  1039    cross_zone_load_balancing = true
  1040  }
  1041  `
  1042  
  1043  const testAccAWSELBFullRangeOfCharacters = `
  1044  resource "aws_elb" "foo" {
  1045    name = "%s"
  1046    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
  1047  
  1048    listener {
  1049      instance_port = 8000
  1050      instance_protocol = "http"
  1051      lb_port = 80
  1052      lb_protocol = "http"
  1053    }
  1054  }
  1055  `
  1056  
  1057  const testAccAWSELBAccessLogs = `
  1058  resource "aws_elb" "foo" {
  1059    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
  1060  
  1061    listener {
  1062      instance_port = 8000
  1063      instance_protocol = "http"
  1064      lb_port = 80
  1065      lb_protocol = "http"
  1066    }
  1067  }
  1068  `
  1069  
  1070  func testAccAWSELBAccessLogsOn(r string) string {
  1071  	return fmt.Sprintf(`
  1072  # an S3 bucket configured for Access logs
  1073  # The 797873946194 is the AWS ID for us-west-2, so this test
  1074  # must be ran in us-west-2
  1075  resource "aws_s3_bucket" "acceslogs_bucket" {
  1076    bucket = "%s"
  1077    acl = "private"
  1078    force_destroy = true
  1079    policy = <<EOF
  1080  {
  1081    "Id": "Policy1446577137248",
  1082    "Statement": [
  1083      {
  1084        "Action": "s3:PutObject",
  1085        "Effect": "Allow",
  1086        "Principal": {
  1087          "AWS": "arn:aws:iam::797873946194:root"
  1088        },
  1089        "Resource": "arn:aws:s3:::%s/*",
  1090        "Sid": "Stmt1446575236270"
  1091      }
  1092    ],
  1093    "Version": "2012-10-17"
  1094  }
  1095  EOF
  1096  }
  1097  
  1098  resource "aws_elb" "foo" {
  1099    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
  1100  
  1101    listener {
  1102      instance_port = 8000
  1103      instance_protocol = "http"
  1104      lb_port = 80
  1105      lb_protocol = "http"
  1106    }
  1107  
  1108  	access_logs {
  1109  		interval = 5
  1110  		bucket = "${aws_s3_bucket.acceslogs_bucket.bucket}"
  1111  	}
  1112  }
  1113  `, r, r)
  1114  }
  1115  
  1116  func testAccAWSELBAccessLogsDisabled(r string) string {
  1117  	return fmt.Sprintf(`
  1118  # an S3 bucket configured for Access logs
  1119  # The 797873946194 is the AWS ID for us-west-2, so this test
  1120  # must be ran in us-west-2
  1121  resource "aws_s3_bucket" "acceslogs_bucket" {
  1122    bucket = "%s"
  1123    acl = "private"
  1124    force_destroy = true
  1125    policy = <<EOF
  1126  {
  1127    "Id": "Policy1446577137248",
  1128    "Statement": [
  1129      {
  1130        "Action": "s3:PutObject",
  1131        "Effect": "Allow",
  1132        "Principal": {
  1133          "AWS": "arn:aws:iam::797873946194:root"
  1134        },
  1135        "Resource": "arn:aws:s3:::%s/*",
  1136        "Sid": "Stmt1446575236270"
  1137      }
  1138    ],
  1139    "Version": "2012-10-17"
  1140  }
  1141  EOF
  1142  }
  1143  
  1144  resource "aws_elb" "foo" {
  1145    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
  1146  
  1147    listener {
  1148      instance_port = 8000
  1149      instance_protocol = "http"
  1150      lb_port = 80
  1151      lb_protocol = "http"
  1152    }
  1153  
  1154  	access_logs {
  1155  		interval = 5
  1156  		bucket = "${aws_s3_bucket.acceslogs_bucket.bucket}"
  1157  		enabled = false
  1158  	}
  1159  }
  1160  `, r, r)
  1161  }
  1162  
  1163  const testAccAWSELB_namePrefix = `
  1164  resource "aws_elb" "test" {
  1165    name_prefix = "test-"
  1166    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
  1167  
  1168    listener {
  1169      instance_port = 8000
  1170      instance_protocol = "http"
  1171      lb_port = 80
  1172      lb_protocol = "http"
  1173    }
  1174  }
  1175  `
  1176  
  1177  const testAccAWSELBGeneratedName = `
  1178  resource "aws_elb" "foo" {
  1179    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
  1180  
  1181    listener {
  1182      instance_port = 8000
  1183      instance_protocol = "http"
  1184      lb_port = 80
  1185      lb_protocol = "http"
  1186    }
  1187  }
  1188  `
  1189  
  1190  const testAccAWSELBConfig_AvailabilityZonesUpdate = `
  1191  resource "aws_elb" "bar" {
  1192    availability_zones = ["us-west-2a", "us-west-2b"]
  1193  
  1194    listener {
  1195      instance_port = 8000
  1196      instance_protocol = "http"
  1197      lb_port = 80
  1198      lb_protocol = "http"
  1199    }
  1200  }
  1201  `
  1202  
  1203  const testAccAWSELBConfig_TagUpdate = `
  1204  resource "aws_elb" "bar" {
  1205    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
  1206  
  1207    listener {
  1208      instance_port = 8000
  1209      instance_protocol = "http"
  1210      lb_port = 80
  1211      lb_protocol = "http"
  1212    }
  1213  
  1214  	tags {
  1215  		foo = "bar"
  1216  		new = "type"
  1217  	}
  1218  
  1219    cross_zone_load_balancing = true
  1220  }
  1221  `
  1222  
  1223  const testAccAWSELBConfigNewInstance = `
  1224  resource "aws_elb" "bar" {
  1225    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
  1226  
  1227    listener {
  1228      instance_port = 8000
  1229      instance_protocol = "http"
  1230      lb_port = 80
  1231      lb_protocol = "http"
  1232    }
  1233  
  1234    instances = ["${aws_instance.foo.id}"]
  1235  }
  1236  
  1237  resource "aws_instance" "foo" {
  1238  	# us-west-2
  1239  	ami = "ami-043a5034"
  1240  	instance_type = "t1.micro"
  1241  }
  1242  `
  1243  
  1244  const testAccAWSELBConfigListenerSSLCertificateId = `
  1245  resource "aws_elb" "bar" {
  1246    availability_zones = ["us-west-2a"]
  1247  
  1248    listener {
  1249      instance_port = 8000
  1250      instance_protocol = "http"
  1251      ssl_certificate_id = "%s"
  1252      lb_port = 443
  1253      lb_protocol = "https"
  1254    }
  1255  }
  1256  `
  1257  
  1258  const testAccAWSELBConfigHealthCheck = `
  1259  resource "aws_elb" "bar" {
  1260    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
  1261  
  1262    listener {
  1263      instance_port = 8000
  1264      instance_protocol = "http"
  1265      lb_port = 80
  1266      lb_protocol = "http"
  1267    }
  1268  
  1269    health_check {
  1270      healthy_threshold = 5
  1271      unhealthy_threshold = 5
  1272      target = "HTTP:8000/"
  1273      interval = 60
  1274      timeout = 30
  1275    }
  1276  }
  1277  `
  1278  
  1279  const testAccAWSELBConfigHealthCheck_update = `
  1280  resource "aws_elb" "bar" {
  1281    availability_zones = ["us-west-2a"]
  1282  
  1283    listener {
  1284      instance_port = 8000
  1285      instance_protocol = "http"
  1286      lb_port = 80
  1287      lb_protocol = "http"
  1288    }
  1289  
  1290    health_check {
  1291      healthy_threshold = 10
  1292      unhealthy_threshold = 5
  1293      target = "HTTP:8000/"
  1294      interval = 60
  1295      timeout = 30
  1296    }
  1297  }
  1298  `
  1299  
  1300  const testAccAWSELBConfigListener_update = `
  1301  resource "aws_elb" "bar" {
  1302    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
  1303  
  1304    listener {
  1305      instance_port = 8080
  1306      instance_protocol = "http"
  1307      lb_port = 80
  1308      lb_protocol = "http"
  1309    }
  1310  }
  1311  `
  1312  
  1313  const testAccAWSELBConfigIdleTimeout = `
  1314  resource "aws_elb" "bar" {
  1315  	availability_zones = ["us-west-2a"]
  1316  
  1317  	listener {
  1318  		instance_port = 8000
  1319  		instance_protocol = "http"
  1320  		lb_port = 80
  1321  		lb_protocol = "http"
  1322  	}
  1323  
  1324  	idle_timeout = 200
  1325  }
  1326  `
  1327  
  1328  const testAccAWSELBConfigIdleTimeout_update = `
  1329  resource "aws_elb" "bar" {
  1330  	availability_zones = ["us-west-2a"]
  1331  
  1332  	listener {
  1333  		instance_port = 8000
  1334  		instance_protocol = "http"
  1335  		lb_port = 80
  1336  		lb_protocol = "http"
  1337  	}
  1338  
  1339  	idle_timeout = 400
  1340  }
  1341  `
  1342  
  1343  const testAccAWSELBConfigConnectionDraining = `
  1344  resource "aws_elb" "bar" {
  1345  	availability_zones = ["us-west-2a"]
  1346  
  1347  	listener {
  1348  		instance_port = 8000
  1349  		instance_protocol = "http"
  1350  		lb_port = 80
  1351  		lb_protocol = "http"
  1352  	}
  1353  
  1354  	connection_draining = true
  1355  	connection_draining_timeout = 400
  1356  }
  1357  `
  1358  
  1359  const testAccAWSELBConfigConnectionDraining_update_timeout = `
  1360  resource "aws_elb" "bar" {
  1361  	availability_zones = ["us-west-2a"]
  1362  
  1363  	listener {
  1364  		instance_port = 8000
  1365  		instance_protocol = "http"
  1366  		lb_port = 80
  1367  		lb_protocol = "http"
  1368  	}
  1369  
  1370  	connection_draining = true
  1371  	connection_draining_timeout = 600
  1372  }
  1373  `
  1374  
  1375  const testAccAWSELBConfigConnectionDraining_update_disable = `
  1376  resource "aws_elb" "bar" {
  1377  	availability_zones = ["us-west-2a"]
  1378  
  1379  	listener {
  1380  		instance_port = 8000
  1381  		instance_protocol = "http"
  1382  		lb_port = 80
  1383  		lb_protocol = "http"
  1384  	}
  1385  
  1386  	connection_draining = false
  1387  }
  1388  `
  1389  
  1390  const testAccAWSELBConfigSecurityGroups = `
  1391  resource "aws_elb" "bar" {
  1392    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
  1393  
  1394    listener {
  1395      instance_port = 8000
  1396      instance_protocol = "http"
  1397      lb_port = 80
  1398      lb_protocol = "http"
  1399    }
  1400  
  1401    security_groups = ["${aws_security_group.bar.id}"]
  1402  }
  1403  
  1404  resource "aws_security_group" "bar" {
  1405    ingress {
  1406      protocol = "tcp"
  1407      from_port = 80
  1408      to_port = 80
  1409      cidr_blocks = ["0.0.0.0/0"]
  1410    }
  1411  
  1412  	tags {
  1413  		Name = "tf_elb_sg_test"
  1414  	}
  1415  }
  1416  `
  1417  
  1418  // This IAM Server config is lifted from
  1419  // builtin/providers/aws/resource_aws_iam_server_certificate_test.go
  1420  func testAccELBIAMServerCertConfig(certName string) string {
  1421  	return fmt.Sprintf(`
  1422  resource "aws_iam_server_certificate" "test_cert" {
  1423    name = "%s"
  1424    certificate_body = <<EOF
  1425  -----BEGIN CERTIFICATE-----
  1426  MIIDBjCCAe4CCQCGWwBmOiHQdTANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJB
  1427  VTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0
  1428  cyBQdHkgTHRkMB4XDTE2MDYyMTE2MzM0MVoXDTE3MDYyMTE2MzM0MVowRTELMAkG
  1429  A1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNVBAoTGEludGVybmV0
  1430  IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
  1431  AL+LFlsCJG5txZp4yuu+lQnuUrgBXRG+irQqcTXlV91Bp5hpmRIyhnGCtWxxDBUL
  1432  xrh4WN3VV/0jDzKT976oLgOy3hj56Cdqf+JlZ1qgMN5bHB3mm3aVWnrnsLbBsfwZ
  1433  SEbk3Kht/cE1nK2toNVW+rznS3m+eoV3Zn/DUNwGlZr42hGNs6ETn2jURY78ETqR
  1434  mW47xvjf86eIo7vULHJaY6xyarPqkL8DZazOmvY06hUGvGwGBny7gugfXqDG+I8n
  1435  cPBsGJGSAmHmVV8o0RCB9UjY+TvSMQRpEDoVlvyrGuglsD8to/4+7UcsuDGlRYN6
  1436  jmIOC37mOi/jwRfWL1YUa4MCAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAPDxTH0oQ
  1437  JjKXoJgkmQxurB81RfnK/NrswJVzWbOv6ejcbhwh+/ZgJTMc15BrYcxU6vUW1V/i
  1438  Z7APU0qJ0icECACML+a2fRI7YdLCTiPIOmY66HY8MZHAn3dGjU5TeiUflC0n0zkP
  1439  mxKJe43kcYLNDItbfvUDo/GoxTXrC3EFVZyU0RhFzoVJdODlTHXMVFCzcbQEBrBJ
  1440  xKdShCEc8nFMneZcGFeEU488ntZoWzzms8/QpYrKa5S0Sd7umEU2Kwu4HTkvUFg/
  1441  CqDUFjhydXxYRsxXBBrEiLOE5BdtJR1sH/QHxIJe23C9iHI2nS1NbLziNEApLwC4
  1442  GnSud83VUo9G9w==
  1443  -----END CERTIFICATE-----
  1444  EOF
  1445  
  1446  	private_key =  <<EOF
  1447  -----BEGIN RSA PRIVATE KEY-----
  1448  MIIEowIBAAKCAQEAv4sWWwIkbm3FmnjK676VCe5SuAFdEb6KtCpxNeVX3UGnmGmZ
  1449  EjKGcYK1bHEMFQvGuHhY3dVX/SMPMpP3vqguA7LeGPnoJ2p/4mVnWqAw3lscHeab
  1450  dpVaeuewtsGx/BlIRuTcqG39wTWcra2g1Vb6vOdLeb56hXdmf8NQ3AaVmvjaEY2z
  1451  oROfaNRFjvwROpGZbjvG+N/zp4iju9QsclpjrHJqs+qQvwNlrM6a9jTqFQa8bAYG
  1452  fLuC6B9eoMb4jydw8GwYkZICYeZVXyjREIH1SNj5O9IxBGkQOhWW/Ksa6CWwPy2j
  1453  /j7tRyy4MaVFg3qOYg4LfuY6L+PBF9YvVhRrgwIDAQABAoIBAFqJ4h1Om+3e0WK8
  1454  6h4YzdYN4ue7LUTv7hxPW4gASlH5cMDoWURywX3yLNN/dBiWom4b5NWmvJqY8dwU
  1455  eSyTznxNFhJ0PjozaxOWnw4FXlQceOPhV2bsHgKudadNU1Y4lSN9lpe+tg2Xy+GE
  1456  ituM66RTKCf502w3DioiJpx6OEkxuhrnsQAWNcGB0MnTukm2f+629V+04R5MT5V1
  1457  nY+5Phx2BpHgYzWBKh6Px1puu7xFv5SMQda1ndlPIKb4cNp0yYn+1lHNjbOE7QL/
  1458  oEpWgrauS5Zk/APK33v/p3wVYHrKocIFHlPiCW0uIJJLsOZDY8pQXpTlc+/xGLLy
  1459  WBu4boECgYEA6xO+1UNh6ndJ3xGuNippH+ucTi/uq1+0tG1bd63v+75tn5l4LyY2
  1460  CWHRaWVlVn+WnDslkQTJzFD68X+9M7Cc4oP6WnhTyPamG7HlGv5JxfFHTC9GOKmz
  1461  sSc624BDmqYJ7Xzyhe5kc3iHzqG/L72ZF1aijZdrodQMSY1634UX6aECgYEA0Jdr
  1462  cBPSN+mgmEY6ogN5h7sO5uNV3TQQtW2IslfWZn6JhSRF4Rf7IReng48CMy9ZhFBy
  1463  Q7H2I1pDGjEC9gQHhgVfm+FyMSVqXfCHEW/97pvvu9ougHA0MhPep1twzTGrqg+K
  1464  f3PLW8hVkGyCrTfWgbDlPsHgsocA/wTaQOheaqMCgYBat5z+WemQfQZh8kXDm2xE
  1465  KD2Cota9BcsLkeQpdFNXWC6f167cqydRSZFx1fJchhJOKjkeFLX3hgzBY6VVLEPu
  1466  2jWj8imLNTv3Fhiu6RD5NVppWRkFRuAUbmo1SPNN2+Oa5YwGCXB0a0Alip/oQYex
  1467  zPogIB4mLlmrjNCtL4SB4QKBgCEHKMrZSJrz0irqS9RlanPUaZqjenAJE3A2xMNA
  1468  Z0FZXdsIEEyA6JGn1i1dkoKaR7lMp5sSbZ/RZfiatBZSMwLEjQv4mYUwoHP5Ztma
  1469  +wEyDbaX6G8L1Sfsv3+OWgETkVPfHBXsNtH0mZ/BnrtgsQVeBh52wmZiPAUlNo26
  1470  fWCzAoGBAJOjqovLelLWzyQGqPFx/MwuI56UFXd1CmFlCIvF2WxCFmk3tlExoCN1
  1471  HqSpt92vsgYgV7+lAb4U7Uy/v012gwiU1LK+vyAE9geo3pTjG73BNzG4H547xtbY
  1472  dg+Sd4Wjm89UQoUUoiIcstY7FPbqfBtYKfh4RYHAHV2BwDFqzZCM
  1473  -----END RSA PRIVATE KEY-----
  1474  EOF
  1475  }
  1476  
  1477  resource "aws_elb" "bar" {
  1478    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
  1479  
  1480    listener {
  1481      instance_port = 8000
  1482      instance_protocol = "https"
  1483      lb_port = 80
  1484      // Protocol should be case insensitive
  1485      lb_protocol = "HttPs"
  1486      ssl_certificate_id = "${aws_iam_server_certificate.test_cert.arn}"
  1487    }
  1488  
  1489  	tags {
  1490  		bar = "baz"
  1491  	}
  1492  
  1493    cross_zone_load_balancing = true
  1494  }
  1495  `, certName)
  1496  }
  1497  
  1498  const testAccAWSELBConfig_subnets = `
  1499  provider "aws" {
  1500    region = "us-west-2"
  1501  }
  1502  
  1503  resource "aws_vpc" "azelb" {
  1504    cidr_block           = "10.1.0.0/16"
  1505    enable_dns_hostnames = true
  1506  
  1507    tags {
  1508      Name = "subnet-vpc"
  1509    }
  1510  }
  1511  
  1512  resource "aws_subnet" "public_a_one" {
  1513    vpc_id = "${aws_vpc.azelb.id}"
  1514  
  1515    cidr_block        = "10.1.1.0/24"
  1516    availability_zone = "us-west-2a"
  1517  }
  1518  
  1519  resource "aws_subnet" "public_b_one" {
  1520    vpc_id = "${aws_vpc.azelb.id}"
  1521  
  1522    cidr_block        = "10.1.7.0/24"
  1523    availability_zone = "us-west-2b"
  1524  }
  1525  
  1526  resource "aws_subnet" "public_a_two" {
  1527    vpc_id = "${aws_vpc.azelb.id}"
  1528  
  1529    cidr_block        = "10.1.2.0/24"
  1530    availability_zone = "us-west-2a"
  1531  }
  1532  
  1533  resource "aws_elb" "ourapp" {
  1534    name = "terraform-asg-deployment-example"
  1535  
  1536    subnets = [
  1537      "${aws_subnet.public_a_one.id}",
  1538      "${aws_subnet.public_b_one.id}",
  1539    ]
  1540  
  1541    listener {
  1542      instance_port     = 80
  1543      instance_protocol = "http"
  1544      lb_port           = 80
  1545      lb_protocol       = "http"
  1546    }
  1547  
  1548    depends_on = ["aws_internet_gateway.gw"]
  1549  }
  1550  
  1551  resource "aws_internet_gateway" "gw" {
  1552    vpc_id = "${aws_vpc.azelb.id}"
  1553  
  1554    tags {
  1555      Name = "main"
  1556    }
  1557  }
  1558  `
  1559  
  1560  const testAccAWSELBConfig_subnet_swap = `
  1561  provider "aws" {
  1562    region = "us-west-2"
  1563  }
  1564  
  1565  resource "aws_vpc" "azelb" {
  1566    cidr_block           = "10.1.0.0/16"
  1567    enable_dns_hostnames = true
  1568  
  1569    tags {
  1570      Name = "subnet-vpc"
  1571    }
  1572  }
  1573  
  1574  resource "aws_subnet" "public_a_one" {
  1575    vpc_id = "${aws_vpc.azelb.id}"
  1576  
  1577    cidr_block        = "10.1.1.0/24"
  1578    availability_zone = "us-west-2a"
  1579  }
  1580  
  1581  resource "aws_subnet" "public_b_one" {
  1582    vpc_id = "${aws_vpc.azelb.id}"
  1583  
  1584    cidr_block        = "10.1.7.0/24"
  1585    availability_zone = "us-west-2b"
  1586  }
  1587  
  1588  resource "aws_subnet" "public_a_two" {
  1589    vpc_id = "${aws_vpc.azelb.id}"
  1590  
  1591    cidr_block        = "10.1.2.0/24"
  1592    availability_zone = "us-west-2a"
  1593  }
  1594  
  1595  resource "aws_elb" "ourapp" {
  1596    name = "terraform-asg-deployment-example"
  1597  
  1598    subnets = [
  1599      "${aws_subnet.public_a_two.id}",
  1600      "${aws_subnet.public_b_one.id}",
  1601    ]
  1602  
  1603    listener {
  1604      instance_port     = 80
  1605      instance_protocol = "http"
  1606      lb_port           = 80
  1607      lb_protocol       = "http"
  1608    }
  1609  
  1610    depends_on = ["aws_internet_gateway.gw"]
  1611  }
  1612  
  1613  resource "aws_internet_gateway" "gw" {
  1614    vpc_id = "${aws_vpc.azelb.id}"
  1615  
  1616    tags {
  1617      Name = "main"
  1618    }
  1619  }
  1620  `