Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows zero-value ELB and ALB names #14304

Merged
merged 1 commit into from
May 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions builtin/providers/aws/resource_aws_alb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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" {
Expand Down
47 changes: 46 additions & 1 deletion builtin/providers/aws/resource_aws_elb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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"]
Expand Down
11 changes: 7 additions & 4 deletions builtin/providers/aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down