2014-07-15 00:48:22 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-10-02 04:19:36 +02:00
|
|
|
"os"
|
2014-07-15 00:48:22 +02:00
|
|
|
"reflect"
|
2014-12-16 15:21:25 +01:00
|
|
|
"sort"
|
2014-07-15 00:48:22 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/goamz/elb"
|
|
|
|
)
|
|
|
|
|
2014-07-16 23:02:47 +02:00
|
|
|
func TestAccAWSELB_basic(t *testing.T) {
|
2014-07-15 00:48:22 +02:00
|
|
|
var conf elb.LoadBalancer
|
2014-10-02 04:19:36 +02:00
|
|
|
ssl_certificate_id := os.Getenv("AWS_SSL_CERTIFICATE_ID")
|
2014-07-15 00:48:22 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSELBDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSELBConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSELBExists("aws_elb.bar", &conf),
|
|
|
|
testAccCheckAWSELBAttributes(&conf),
|
2014-07-16 23:02:47 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_elb.bar", "name", "foobar-terraform-test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-16 15:21:25 +01:00
|
|
|
"aws_elb.bar", "availability_zones.2487133097", "us-west-2a"),
|
2014-07-16 23:02:47 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-16 15:21:25 +01:00
|
|
|
"aws_elb.bar", "availability_zones.221770259", "us-west-2b"),
|
2014-07-16 23:02:47 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-16 15:21:25 +01:00
|
|
|
"aws_elb.bar", "availability_zones.2050015877", "us-west-2c"),
|
2014-07-16 23:02:47 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"aws_elb.bar", "listener.206423021.instance_port", "8000"),
|
2014-07-16 23:02:47 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"aws_elb.bar", "listener.206423021.instance_protocol", "http"),
|
2014-08-11 01:09:05 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"aws_elb.bar", "listener.206423021.ssl_certificate_id", ssl_certificate_id),
|
2014-07-16 23:02:47 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"aws_elb.bar", "listener.206423021.lb_port", "80"),
|
2014-07-16 23:02:47 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"aws_elb.bar", "listener.206423021.lb_protocol", "http"),
|
2014-12-10 07:07:08 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_elb.bar", "cross_zone_load_balancing", "true"),
|
2014-07-16 23:02:47 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSELB_InstanceAttaching(t *testing.T) {
|
|
|
|
var conf elb.LoadBalancer
|
|
|
|
|
|
|
|
testCheckInstanceAttached := func(count int) resource.TestCheckFunc {
|
|
|
|
return func(*terraform.State) error {
|
|
|
|
if len(conf.Instances) != count {
|
|
|
|
return fmt.Errorf("instance count does not match")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSELBDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSELBConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSELBExists("aws_elb.bar", &conf),
|
|
|
|
testAccCheckAWSELBAttributes(&conf),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSELBConfigNewInstance,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSELBExists("aws_elb.bar", &conf),
|
|
|
|
testCheckInstanceAttached(1),
|
2014-07-15 00:48:22 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-01-15 14:55:25 +01:00
|
|
|
func TestAccAWSELB_AddSubnet(t *testing.T) {
|
|
|
|
var conf elb.LoadBalancer
|
|
|
|
|
|
|
|
testCheckSubnetsAdded := func(count int) resource.TestCheckFunc {
|
|
|
|
return func(*terraform.State) error {
|
|
|
|
if len(conf.Subnets) != count {
|
|
|
|
return fmt.Errorf("subnet count does not match")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSELBDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSELBConfigVPC,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSELBExists("aws_elb.bar", &conf),
|
|
|
|
testAccCheckAWSELBAttributes(&conf),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSELBAddSubnets,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSELBExists("aws_elb.bar", &conf),
|
|
|
|
testCheckSubnetsAdded(2),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-30 16:15:22 +02:00
|
|
|
func TestAccAWSELB_HealthCheck(t *testing.T) {
|
|
|
|
var conf elb.LoadBalancer
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSELBDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSELBConfigHealthCheck,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSELBExists("aws_elb.bar", &conf),
|
|
|
|
testAccCheckAWSELBAttributesHealthCheck(&conf),
|
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"aws_elb.bar", "health_check.3484319807.healthy_threshold", "5"),
|
2014-07-30 16:15:22 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"aws_elb.bar", "health_check.3484319807.unhealthy_threshold", "5"),
|
2014-07-30 16:15:22 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"aws_elb.bar", "health_check.3484319807.target", "HTTP:8000/"),
|
2014-07-30 16:15:22 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"aws_elb.bar", "health_check.3484319807.timeout", "30"),
|
2014-07-30 16:15:22 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"aws_elb.bar", "health_check.3484319807.interval", "60"),
|
2014-07-30 16:15:22 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2014-07-15 00:48:22 +02:00
|
|
|
func testAccCheckAWSELBDestroy(s *terraform.State) error {
|
2014-11-21 17:58:34 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).elbconn
|
2014-07-15 00:48:22 +02:00
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-15 00:48:22 +02:00
|
|
|
if rs.Type != "aws_elb" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancer{
|
2014-09-17 02:44:42 +02:00
|
|
|
Names: []string{rs.Primary.ID},
|
2014-07-15 00:48:22 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if len(describe.LoadBalancers) != 0 &&
|
2014-09-17 02:44:42 +02:00
|
|
|
describe.LoadBalancers[0].LoadBalancerName == rs.Primary.ID {
|
2014-07-15 00:48:22 +02:00
|
|
|
return fmt.Errorf("ELB still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the error
|
|
|
|
providerErr, ok := err.(*elb.Error)
|
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if providerErr.Code != "InvalidLoadBalancerName.NotFound" {
|
|
|
|
return fmt.Errorf("Unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSELBAttributes(conf *elb.LoadBalancer) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-12-16 15:21:25 +01:00
|
|
|
zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"}
|
|
|
|
sort.StringSlice(conf.AvailabilityZones).Sort()
|
|
|
|
if !reflect.DeepEqual(conf.AvailabilityZones, zones) {
|
2014-07-15 00:48:22 +02:00
|
|
|
return fmt.Errorf("bad availability_zones")
|
|
|
|
}
|
|
|
|
|
2014-07-15 00:57:04 +02:00
|
|
|
if conf.LoadBalancerName != "foobar-terraform-test" {
|
2014-07-15 00:48:22 +02:00
|
|
|
return fmt.Errorf("bad name")
|
|
|
|
}
|
|
|
|
|
|
|
|
l := elb.Listener{
|
|
|
|
InstancePort: 8000,
|
2014-07-15 14:49:40 +02:00
|
|
|
InstanceProtocol: "HTTP",
|
2014-07-15 00:48:22 +02:00
|
|
|
LoadBalancerPort: 80,
|
2014-07-15 14:49:40 +02:00
|
|
|
Protocol: "HTTP",
|
2014-07-15 00:48:22 +02:00
|
|
|
}
|
2014-07-15 00:57:04 +02:00
|
|
|
|
2014-07-15 00:48:22 +02:00
|
|
|
if !reflect.DeepEqual(conf.Listeners[0], l) {
|
2014-07-15 14:49:40 +02:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
|
|
|
|
conf.Listeners[0],
|
|
|
|
l)
|
2014-07-15 00:48:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if conf.DNSName == "" {
|
|
|
|
return fmt.Errorf("empty dns_name")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-30 16:15:22 +02:00
|
|
|
func testAccCheckAWSELBAttributesHealthCheck(conf *elb.LoadBalancer) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-12-16 15:21:25 +01:00
|
|
|
zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"}
|
|
|
|
sort.StringSlice(conf.AvailabilityZones).Sort()
|
|
|
|
if !reflect.DeepEqual(conf.AvailabilityZones, zones) {
|
2014-07-30 16:15:22 +02:00
|
|
|
return fmt.Errorf("bad availability_zones")
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.LoadBalancerName != "foobar-terraform-test" {
|
|
|
|
return fmt.Errorf("bad name")
|
|
|
|
}
|
|
|
|
|
|
|
|
check := elb.HealthCheck{
|
|
|
|
Timeout: 30,
|
|
|
|
UnhealthyThreshold: 5,
|
|
|
|
HealthyThreshold: 5,
|
|
|
|
Interval: 60,
|
|
|
|
Target: "HTTP:8000/",
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(conf.HealthCheck, check) {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
|
|
|
|
conf.HealthCheck,
|
|
|
|
check)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.DNSName == "" {
|
|
|
|
return fmt.Errorf("empty dns_name")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-15 00:48:22 +02:00
|
|
|
func testAccCheckAWSELBExists(n string, res *elb.LoadBalancer) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:44:42 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-07-15 00:48:22 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-07-15 00:48:22 +02:00
|
|
|
return fmt.Errorf("No ELB ID is set")
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).elbconn
|
2014-07-15 00:48:22 +02:00
|
|
|
|
|
|
|
describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancer{
|
2014-09-17 02:44:42 +02:00
|
|
|
Names: []string{rs.Primary.ID},
|
2014-07-15 00:48:22 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(describe.LoadBalancers) != 1 ||
|
2014-09-17 02:44:42 +02:00
|
|
|
describe.LoadBalancers[0].LoadBalancerName != rs.Primary.ID {
|
2014-07-15 00:48:22 +02:00
|
|
|
return fmt.Errorf("ELB not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*res = describe.LoadBalancers[0]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccAWSELBConfig = `
|
|
|
|
resource "aws_elb" "bar" {
|
|
|
|
name = "foobar-terraform-test"
|
2014-07-16 23:02:47 +02:00
|
|
|
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
|
2014-07-15 00:48:22 +02:00
|
|
|
|
2014-07-15 00:57:04 +02:00
|
|
|
listener {
|
2014-07-15 00:48:22 +02:00
|
|
|
instance_port = 8000
|
|
|
|
instance_protocol = "http"
|
|
|
|
lb_port = 80
|
|
|
|
lb_protocol = "http"
|
|
|
|
}
|
2014-07-15 00:57:04 +02:00
|
|
|
|
2014-12-10 07:07:08 +01:00
|
|
|
cross_zone_load_balancing = true
|
2014-07-15 00:48:22 +02:00
|
|
|
}
|
|
|
|
`
|
2014-07-16 23:02:47 +02:00
|
|
|
|
|
|
|
const testAccAWSELBConfigNewInstance = `
|
|
|
|
resource "aws_elb" "bar" {
|
|
|
|
name = "foobar-terraform-test"
|
|
|
|
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
|
|
|
|
|
|
|
|
listener {
|
|
|
|
instance_port = 8000
|
|
|
|
instance_protocol = "http"
|
|
|
|
lb_port = 80
|
|
|
|
lb_protocol = "http"
|
|
|
|
}
|
|
|
|
|
|
|
|
instances = ["${aws_instance.foo.id}"]
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
# us-west-2
|
|
|
|
ami = "ami-043a5034"
|
|
|
|
instance_type = "t1.micro"
|
|
|
|
}
|
|
|
|
`
|
2015-01-15 14:55:25 +01:00
|
|
|
const testAccAWSELBConfigVPC = `
|
|
|
|
resource "aws_elb" "bar" {
|
|
|
|
vpc_id = "${aws_vpc.foobar.id}"
|
|
|
|
name = "foobar-terraform-test"
|
|
|
|
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
|
|
|
|
|
|
|
|
listener {
|
|
|
|
instance_port = 8000
|
|
|
|
instance_protocol = "http"
|
|
|
|
lb_port = 80
|
|
|
|
lb_protocol = "http"
|
|
|
|
}
|
|
|
|
|
|
|
|
subnets = ["${aws_subnet.baz.id}"]
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-16 15:07:55 +01:00
|
|
|
resource "aws_subnet" "baz" {
|
2015-01-15 14:55:25 +01:00
|
|
|
vpc_id = "${aws_vpc.foobar.id}"
|
|
|
|
cidr_block = "10.0.69.0/24"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_vpc" "foobar" {
|
|
|
|
cidr_block = "10.0.0.0/16"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccAWSELBAddSubnets = `
|
|
|
|
resource "aws_elb" "bar" {
|
|
|
|
vpc_id = "${aws_vpc.foobar.id}"
|
|
|
|
name = "foobar-terraform-test"
|
|
|
|
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
|
|
|
|
|
|
|
|
listener {
|
|
|
|
instance_port = 8000
|
|
|
|
instance_protocol = "http"
|
|
|
|
lb_port = 80
|
|
|
|
lb_protocol = "http"
|
|
|
|
}
|
|
|
|
|
|
|
|
subnets = ["${aws_subnet.baz.id}",
|
|
|
|
"${aws_subnet.foo.id}"]
|
|
|
|
}
|
|
|
|
|
2015-01-16 15:07:55 +01:00
|
|
|
resource "aws_subnet" "foo" {
|
2015-01-15 14:55:25 +01:00
|
|
|
vpc_id = "${aws_vpc.foobar.id}"
|
|
|
|
cidr_block = "10.0.68.0/24"
|
|
|
|
}
|
|
|
|
|
2015-01-16 15:07:55 +01:00
|
|
|
resource "aws_subnet" "baz" {
|
2015-01-15 14:55:25 +01:00
|
|
|
vpc_id = "${aws_vpc.foobar.id}"
|
|
|
|
cidr_block = "10.0.69.0/24"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_vpc" "foobar" {
|
|
|
|
cidr_block = "10.0.0.0/16"
|
|
|
|
}
|
|
|
|
`
|
2014-07-30 16:15:22 +02:00
|
|
|
|
2014-08-11 01:09:05 +02:00
|
|
|
const testAccAWSELBConfigListenerSSLCertificateId = `
|
|
|
|
resource "aws_elb" "bar" {
|
|
|
|
name = "foobar-terraform-test"
|
|
|
|
availability_zones = ["us-west-2a"]
|
|
|
|
|
|
|
|
listener {
|
|
|
|
instance_port = 8000
|
|
|
|
instance_protocol = "http"
|
2014-10-02 04:19:36 +02:00
|
|
|
ssl_certificate_id = "%s"
|
2014-08-11 01:09:05 +02:00
|
|
|
lb_port = 443
|
|
|
|
lb_protocol = "https"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2014-07-30 16:15:22 +02:00
|
|
|
const testAccAWSELBConfigHealthCheck = `
|
|
|
|
resource "aws_elb" "bar" {
|
|
|
|
name = "foobar-terraform-test"
|
|
|
|
availability_zones = ["us-west-2a"]
|
|
|
|
|
|
|
|
listener {
|
|
|
|
instance_port = 8000
|
|
|
|
instance_protocol = "http"
|
|
|
|
lb_port = 80
|
|
|
|
lb_protocol = "http"
|
|
|
|
}
|
|
|
|
|
|
|
|
health_check {
|
|
|
|
healthy_threshold = 5
|
|
|
|
unhealthy_threshold = 5
|
|
|
|
target = "HTTP:8000/"
|
|
|
|
interval = 60
|
|
|
|
timeout = 30
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|