Merge pull request #14304 from paybyphone/zero-value-aws-lb-names

Allows zero-value ELB and ALB names
This commit is contained in:
Jake Champlin 2017-05-09 13:16:41 -04:00 committed by GitHub
commit 770b861508
3 changed files with 149 additions and 5 deletions

View File

@ -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) { func TestAccAWSALB_namePrefix(t *testing.T) {
var conf elbv2.LoadBalancer 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 { func testAccAWSALBConfig_namePrefix() string {
return fmt.Sprintf(` return fmt.Sprintf(`
resource "aws_alb" "alb_test" { resource "aws_alb" "alb_test" {

View File

@ -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) { func TestAccAWSELB_availabilityZones(t *testing.T) {
var conf elb.LoadBalancerDescription 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" var elbName = "Testing123dddddddddddddddddddvvvv"
_, errors := validateElbName(elbName, "SampleKey") _, 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 = ` const testAccAWSELBConfig_AvailabilityZonesUpdate = `
resource "aws_elb" "bar" { resource "aws_elb" "bar" {
availability_zones = ["us-west-2a", "us-west-2b"] availability_zones = ["us-west-2a", "us-west-2b"]

View File

@ -161,15 +161,18 @@ func validateStreamViewType(v interface{}, k string) (ws []string, errors []erro
func validateElbName(v interface{}, k string) (ws []string, errors []error) { func validateElbName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string) value := v.(string)
if !regexp.MustCompile(`^[0-9A-Za-z-]+$`).MatchString(value) { if len(value) == 0 {
errors = append(errors, fmt.Errorf( return // short-circuit
"only alphanumeric characters and hyphens allowed in %q: %q",
k, value))
} }
if len(value) > 32 { if len(value) > 32 {
errors = append(errors, fmt.Errorf( errors = append(errors, fmt.Errorf(
"%q cannot be longer than 32 characters: %q", k, value)) "%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) { if regexp.MustCompile(`^-`).MatchString(value) {
errors = append(errors, fmt.Errorf( errors = append(errors, fmt.Errorf(
"%q cannot begin with a hyphen: %q", k, value)) "%q cannot begin with a hyphen: %q", k, value))