From acd2b247b4f67ff4f826449c9c86bcc6b7e13ede Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Thu, 19 Jan 2017 16:08:45 -0800 Subject: [PATCH 1/2] provider/aws: Wait until ALB is provisioned --- builtin/providers/aws/resource_aws_alb.go | 32 ++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_alb.go b/builtin/providers/aws/resource_aws_alb.go index d8f8fe98b..0efe4566a 100644 --- a/builtin/providers/aws/resource_aws_alb.go +++ b/builtin/providers/aws/resource_aws_alb.go @@ -5,6 +5,7 @@ import ( "log" "regexp" "strconv" + "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/elbv2" @@ -169,9 +170,38 @@ func resourceAwsAlbCreate(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("No load balancers returned following creation of %s", d.Get("name").(string)) } - d.SetId(*resp.LoadBalancers[0].LoadBalancerArn) + lb := resp.LoadBalancers[0] + d.SetId(*lb.LoadBalancerArn) log.Printf("[INFO] ALB ID: %s", d.Id()) + stateConf := &resource.StateChangeConf{ + Pending: []string{"active", "provisioning", "failed"}, + Target: []string{"active"}, + Refresh: func() (interface{}, string, error) { + describeResp, err := elbconn.DescribeLoadBalancers(&elbv2.DescribeLoadBalancersInput{ + LoadBalancerArns: []*string{lb.LoadBalancerArn}, + }) + if err != nil { + return nil, "", err + } + + if len(describeResp.LoadBalancers) != 1 { + return nil, "", fmt.Errorf("No load balancers returned for %s", *lb.LoadBalancerArn) + } + dLb := describeResp.LoadBalancers[0] + + log.Printf("[INFO] ALB state: %s", *dLb.State.Code) + + return describeResp, *dLb.State.Code, nil + }, + Timeout: 5 * time.Minute, + MinTimeout: 3 * time.Second, + } + _, err = stateConf.WaitForState() + if err != nil { + return err + } + return resourceAwsAlbUpdate(d, meta) } From 41da963d07e545cf1fd543806c22f16d9cd3940d Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Fri, 20 Jan 2017 18:48:25 -0800 Subject: [PATCH 2/2] provider/aws: Make ALBs internal or add IGW in tests --- .../aws/data_source_aws_alb_listener_test.go | 10 +++++- .../providers/aws/data_source_aws_alb_test.go | 6 ++-- .../resource_aws_alb_listener_rule_test.go | 2 +- .../aws/resource_aws_alb_listener_test.go | 10 +++++- .../providers/aws/resource_aws_alb_test.go | 32 ++++++++++++------- 5 files changed, 42 insertions(+), 18 deletions(-) diff --git a/builtin/providers/aws/data_source_aws_alb_listener_test.go b/builtin/providers/aws/data_source_aws_alb_listener_test.go index da41e3d4c..5eea80e24 100644 --- a/builtin/providers/aws/data_source_aws_alb_listener_test.go +++ b/builtin/providers/aws/data_source_aws_alb_listener_test.go @@ -74,7 +74,7 @@ func testAccDataSourceAWSALBListenerConfigBasic(albName, targetGroupName string) resource "aws_alb" "alb_test" { name = "%s" - internal = false + internal = true security_groups = ["${aws_security_group.alb_test.id}"] subnets = ["${aws_subnet.alb_test.*.id}"] @@ -221,6 +221,14 @@ resource "aws_vpc" "alb_test" { } } +resource "aws_internet_gateway" "gw" { + vpc_id = "${aws_vpc.alb_test.id}" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + resource "aws_subnet" "alb_test" { count = 2 vpc_id = "${aws_vpc.alb_test.id}" diff --git a/builtin/providers/aws/data_source_aws_alb_test.go b/builtin/providers/aws/data_source_aws_alb_test.go index 2382b6ed2..2626fe74b 100644 --- a/builtin/providers/aws/data_source_aws_alb_test.go +++ b/builtin/providers/aws/data_source_aws_alb_test.go @@ -19,7 +19,7 @@ func TestAccDataSourceAWSALB_basic(t *testing.T) { Config: testAccDataSourceAWSALBConfigBasic(albName), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "name", albName), - resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "internal", "false"), + resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "internal", "true"), resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "subnets.#", "2"), resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "security_groups.#", "1"), resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "tags.%", "1"), @@ -31,7 +31,7 @@ func TestAccDataSourceAWSALB_basic(t *testing.T) { resource.TestCheckResourceAttrSet("data.aws_alb.alb_test_with_arn", "dns_name"), resource.TestCheckResourceAttrSet("data.aws_alb.alb_test_with_arn", "arn"), resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "name", albName), - resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "internal", "false"), + resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "internal", "true"), resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "subnets.#", "2"), resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "security_groups.#", "1"), resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "tags.%", "1"), @@ -51,7 +51,7 @@ func TestAccDataSourceAWSALB_basic(t *testing.T) { func testAccDataSourceAWSALBConfigBasic(albName string) string { return fmt.Sprintf(`resource "aws_alb" "alb_test" { name = "%s" - internal = false + internal = true security_groups = ["${aws_security_group.alb_test.id}"] subnets = ["${aws_subnet.alb_test.*.id}"] diff --git a/builtin/providers/aws/resource_aws_alb_listener_rule_test.go b/builtin/providers/aws/resource_aws_alb_listener_rule_test.go index 683fa3535..e27a0a57d 100644 --- a/builtin/providers/aws/resource_aws_alb_listener_rule_test.go +++ b/builtin/providers/aws/resource_aws_alb_listener_rule_test.go @@ -133,7 +133,7 @@ resource "aws_alb_listener" "front_end" { resource "aws_alb" "alb_test" { name = "%s" - internal = false + internal = true security_groups = ["${aws_security_group.alb_test.id}"] subnets = ["${aws_subnet.alb_test.*.id}"] diff --git a/builtin/providers/aws/resource_aws_alb_listener_test.go b/builtin/providers/aws/resource_aws_alb_listener_test.go index 6b41657df..6fdd84c28 100644 --- a/builtin/providers/aws/resource_aws_alb_listener_test.go +++ b/builtin/providers/aws/resource_aws_alb_listener_test.go @@ -148,7 +148,7 @@ func testAccAWSALBListenerConfig_basic(albName, targetGroupName string) string { resource "aws_alb" "alb_test" { name = "%s" - internal = false + internal = true security_groups = ["${aws_security_group.alb_test.id}"] subnets = ["${aws_subnet.alb_test.*.id}"] @@ -291,6 +291,14 @@ resource "aws_vpc" "alb_test" { } } +resource "aws_internet_gateway" "gw" { + vpc_id = "${aws_vpc.alb_test.id}" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + resource "aws_subnet" "alb_test" { count = 2 vpc_id = "${aws_vpc.alb_test.id}" diff --git a/builtin/providers/aws/resource_aws_alb_test.go b/builtin/providers/aws/resource_aws_alb_test.go index 3b6db4663..3e50be3a0 100644 --- a/builtin/providers/aws/resource_aws_alb_test.go +++ b/builtin/providers/aws/resource_aws_alb_test.go @@ -60,7 +60,7 @@ func TestAccAWSALB_basic(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( testAccCheckAWSALBExists("aws_alb.alb_test", &conf), resource.TestCheckResourceAttr("aws_alb.alb_test", "name", albName), - resource.TestCheckResourceAttr("aws_alb.alb_test", "internal", "false"), + resource.TestCheckResourceAttr("aws_alb.alb_test", "internal", "true"), resource.TestCheckResourceAttr("aws_alb.alb_test", "subnets.#", "2"), resource.TestCheckResourceAttr("aws_alb.alb_test", "security_groups.#", "1"), resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.%", "1"), @@ -197,7 +197,7 @@ func TestAccAWSALB_noSecurityGroup(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( testAccCheckAWSALBExists("aws_alb.alb_test", &conf), resource.TestCheckResourceAttr("aws_alb.alb_test", "name", albName), - resource.TestCheckResourceAttr("aws_alb.alb_test", "internal", "false"), + resource.TestCheckResourceAttr("aws_alb.alb_test", "internal", "true"), resource.TestCheckResourceAttr("aws_alb.alb_test", "subnets.#", "2"), resource.TestCheckResourceAttr("aws_alb.alb_test", "security_groups.#", "1"), resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.%", "1"), @@ -229,7 +229,7 @@ func TestAccAWSALB_accesslogs(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( testAccCheckAWSALBExists("aws_alb.alb_test", &conf), resource.TestCheckResourceAttr("aws_alb.alb_test", "name", albName), - resource.TestCheckResourceAttr("aws_alb.alb_test", "internal", "false"), + resource.TestCheckResourceAttr("aws_alb.alb_test", "internal", "true"), resource.TestCheckResourceAttr("aws_alb.alb_test", "subnets.#", "2"), resource.TestCheckResourceAttr("aws_alb.alb_test", "security_groups.#", "1"), resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.%", "1"), @@ -247,7 +247,7 @@ func TestAccAWSALB_accesslogs(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( testAccCheckAWSALBExists("aws_alb.alb_test", &conf), resource.TestCheckResourceAttr("aws_alb.alb_test", "name", albName), - resource.TestCheckResourceAttr("aws_alb.alb_test", "internal", "false"), + resource.TestCheckResourceAttr("aws_alb.alb_test", "internal", "true"), resource.TestCheckResourceAttr("aws_alb.alb_test", "subnets.#", "2"), resource.TestCheckResourceAttr("aws_alb.alb_test", "security_groups.#", "1"), resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.%", "1"), @@ -269,7 +269,7 @@ func TestAccAWSALB_accesslogs(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( testAccCheckAWSALBExists("aws_alb.alb_test", &conf), resource.TestCheckResourceAttr("aws_alb.alb_test", "name", albName), - resource.TestCheckResourceAttr("aws_alb.alb_test", "internal", "false"), + resource.TestCheckResourceAttr("aws_alb.alb_test", "internal", "true"), resource.TestCheckResourceAttr("aws_alb.alb_test", "subnets.#", "2"), resource.TestCheckResourceAttr("aws_alb.alb_test", "security_groups.#", "1"), resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.%", "1"), @@ -362,7 +362,7 @@ func testAccCheckAWSALBDestroy(s *terraform.State) error { func testAccAWSALBConfig_basic(albName string) string { return fmt.Sprintf(`resource "aws_alb" "alb_test" { name = "%s" - internal = false + internal = true security_groups = ["${aws_security_group.alb_test.id}"] subnets = ["${aws_subnet.alb_test.*.id}"] @@ -429,7 +429,7 @@ resource "aws_security_group" "alb_test" { func testAccAWSALBConfig_generatedName() string { return fmt.Sprintf(` resource "aws_alb" "alb_test" { - internal = false + internal = true security_groups = ["${aws_security_group.alb_test.id}"] subnets = ["${aws_subnet.alb_test.*.id}"] @@ -456,6 +456,14 @@ resource "aws_vpc" "alb_test" { } } +resource "aws_internet_gateway" "gw" { + vpc_id = "${aws_vpc.alb_test.id}" + + tags { + Name = "TestAccAWSALB_basic" + } +} + resource "aws_subnet" "alb_test" { count = 2 vpc_id = "${aws_vpc.alb_test.id}" @@ -497,7 +505,7 @@ func testAccAWSALBConfig_namePrefix() string { return fmt.Sprintf(` resource "aws_alb" "alb_test" { name_prefix = "tf-lb" - internal = false + internal = true security_groups = ["${aws_security_group.alb_test.id}"] subnets = ["${aws_subnet.alb_test.*.id}"] @@ -563,7 +571,7 @@ resource "aws_security_group" "alb_test" { func testAccAWSALBConfig_updatedTags(albName string) string { return fmt.Sprintf(`resource "aws_alb" "alb_test" { name = "%s" - internal = false + internal = true security_groups = ["${aws_security_group.alb_test.id}"] subnets = ["${aws_subnet.alb_test.*.id}"] @@ -631,7 +639,7 @@ resource "aws_security_group" "alb_test" { func testAccAWSALBConfig_accessLogs(enabled bool, albName, bucketName string) string { return fmt.Sprintf(`resource "aws_alb" "alb_test" { name = "%s" - internal = false + internal = true security_groups = ["${aws_security_group.alb_test.id}"] subnets = ["${aws_subnet.alb_test.*.id}"] @@ -742,7 +750,7 @@ resource "aws_security_group" "alb_test" { func testAccAWSALBConfig_nosg(albName string) string { return fmt.Sprintf(`resource "aws_alb" "alb_test" { name = "%s" - internal = false + internal = true subnets = ["${aws_subnet.alb_test.*.id}"] idle_timeout = 30 @@ -784,7 +792,7 @@ resource "aws_subnet" "alb_test" { func testAccAWSALBConfig_updateSecurityGroups(albName string) string { return fmt.Sprintf(`resource "aws_alb" "alb_test" { name = "%s" - internal = false + internal = true security_groups = ["${aws_security_group.alb_test.id}", "${aws_security_group.alb_test_2.id}"] subnets = ["${aws_subnet.alb_test.*.id}"]