diff --git a/builtin/providers/aws/resource_aws_alb_test.go b/builtin/providers/aws/resource_aws_alb_test.go index 807d1f4e5..5e766d74b 100644 --- a/builtin/providers/aws/resource_aws_alb_test.go +++ b/builtin/providers/aws/resource_aws_alb_test.go @@ -98,6 +98,26 @@ func TestAccAWSALB_generatedName(t *testing.T) { }) } +func TestAccAWSALB_generatesNameForZeroValue(t *testing.T) { + var conf elbv2.LoadBalancer + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: "aws_alb.alb_test", + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSALBDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSALBConfig_zeroValueName(), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSALBExists("aws_alb.alb_test", &conf), + resource.TestCheckResourceAttrSet("aws_alb.alb_test", "name"), + ), + }, + }, + }) +} + func TestAccAWSALB_namePrefix(t *testing.T) { var conf elbv2.LoadBalancer @@ -848,6 +868,82 @@ resource "aws_security_group" "alb_test" { }`) } +func testAccAWSALBConfig_zeroValueName() string { + return fmt.Sprintf(` +resource "aws_alb" "alb_test" { + name = "" + internal = true + security_groups = ["${aws_security_group.alb_test.id}"] + subnets = ["${aws_subnet.alb_test.*.id}"] + + idle_timeout = 30 + enable_deletion_protection = false + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +variable "subnets" { + default = ["10.0.1.0/24", "10.0.2.0/24"] + type = "list" +} + +data "aws_availability_zones" "available" {} + +resource "aws_vpc" "alb_test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +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}" + cidr_block = "${element(var.subnets, count.index)}" + map_public_ip_on_launch = true + availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_security_group" "alb_test" { + name = "allow_all_alb_test" + description = "Used for ALB Testing" + vpc_id = "${aws_vpc.alb_test.id}" + + ingress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + tags { + TestName = "TestAccAWSALB_basic" + } +}`) +} + func testAccAWSALBConfig_namePrefix() string { return fmt.Sprintf(` resource "aws_alb" "alb_test" { diff --git a/builtin/providers/aws/resource_aws_elb_test.go b/builtin/providers/aws/resource_aws_elb_test.go index bc6856f35..60ac96476 100644 --- a/builtin/providers/aws/resource_aws_elb_test.go +++ b/builtin/providers/aws/resource_aws_elb_test.go @@ -216,6 +216,28 @@ func TestAccAWSELB_generatedName(t *testing.T) { }) } +func TestAccAWSELB_generatesNameForZeroValue(t *testing.T) { + var conf elb.LoadBalancerDescription + generatedNameRegexp := regexp.MustCompile("^tf-lb-") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: "aws_elb.foo", + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSELBDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSELB_zeroValueName, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSELBExists("aws_elb.foo", &conf), + resource.TestMatchResourceAttr( + "aws_elb.foo", "name", generatedNameRegexp), + ), + }, + }, + }) +} + func TestAccAWSELB_availabilityZones(t *testing.T) { var conf elb.LoadBalancerDescription @@ -673,7 +695,16 @@ func TestResourceAWSELB_validateElbNameCannotBeginWithHyphen(t *testing.T) { } } -func TestResourceAWSELB_validateElbNameCannotBeLongerThen32Characters(t *testing.T) { +func TestResourceAWSELB_validateElbNameCanBeAnEmptyString(t *testing.T) { + var elbName = "" + _, errors := validateElbName(elbName, "SampleKey") + + if len(errors) != 0 { + t.Fatalf("Expected the ELB Name to pass validation") + } +} + +func TestResourceAWSELB_validateElbNameCannotBeLongerThan32Characters(t *testing.T) { var elbName = "Testing123dddddddddddddddddddvvvv" _, errors := validateElbName(elbName, "SampleKey") @@ -1187,6 +1218,20 @@ resource "aws_elb" "foo" { } ` +const testAccAWSELB_zeroValueName = ` +resource "aws_elb" "foo" { + name = "" + availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] + + listener { + instance_port = 8000 + instance_protocol = "http" + lb_port = 80 + lb_protocol = "http" + } +} +` + const testAccAWSELBConfig_AvailabilityZonesUpdate = ` resource "aws_elb" "bar" { availability_zones = ["us-west-2a", "us-west-2b"] diff --git a/builtin/providers/aws/validators.go b/builtin/providers/aws/validators.go index f53df93b5..f09745491 100644 --- a/builtin/providers/aws/validators.go +++ b/builtin/providers/aws/validators.go @@ -161,15 +161,18 @@ func validateStreamViewType(v interface{}, k string) (ws []string, errors []erro func validateElbName(v interface{}, k string) (ws []string, errors []error) { value := v.(string) - if !regexp.MustCompile(`^[0-9A-Za-z-]+$`).MatchString(value) { - errors = append(errors, fmt.Errorf( - "only alphanumeric characters and hyphens allowed in %q: %q", - k, value)) + if len(value) == 0 { + return // short-circuit } if len(value) > 32 { errors = append(errors, fmt.Errorf( "%q cannot be longer than 32 characters: %q", k, value)) } + if !regexp.MustCompile(`^[0-9A-Za-z-]+$`).MatchString(value) { + errors = append(errors, fmt.Errorf( + "only alphanumeric characters and hyphens allowed in %q: %q", + k, value)) + } if regexp.MustCompile(`^-`).MatchString(value) { errors = append(errors, fmt.Errorf( "%q cannot begin with a hyphen: %q", k, value))