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

Add name_prefix to aws_alb_target_group #13442

Merged
merged 1 commit into from
Apr 7, 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
32 changes: 21 additions & 11 deletions builtin/providers/aws/resource_aws_alb_target_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)

Expand All @@ -37,10 +38,18 @@ func resourceAwsAlbTargetGroup() *schema.Resource {
},

"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name_prefix"},
ValidateFunc: validateAwsAlbTargetGroupName,
},
"name_prefix": {
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: true,
ValidateFunc: validateAwsAlbTargetGroupName,
ValidateFunc: validateAwsAlbTargetGroupNamePrefix,
},

"port": {
Expand Down Expand Up @@ -172,8 +181,17 @@ func resourceAwsAlbTargetGroup() *schema.Resource {
func resourceAwsAlbTargetGroupCreate(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbv2conn

var groupName string
if v, ok := d.GetOk("name"); ok {
groupName = v.(string)
} else if v, ok := d.GetOk("name_prefix"); ok {
groupName = resource.PrefixedUniqueId(v.(string))
} else {
groupName = resource.PrefixedUniqueId("tf-")
}

params := &elbv2.CreateTargetGroupInput{
Name: aws.String(d.Get("name").(string)),
Name: aws.String(groupName),
Port: aws.Int64(int64(d.Get("port").(int))),
Protocol: aws.String(d.Get("protocol").(string)),
VpcId: aws.String(d.Get("vpc_id").(string)),
Expand Down Expand Up @@ -463,14 +481,6 @@ func validateAwsAlbTargetGroupHealthCheckProtocol(v interface{}, k string) (ws [
return
}

func validateAwsAlbTargetGroupName(v interface{}, k string) (ws []string, errors []error) {
name := v.(string)
if len(name) > 32 {
errors = append(errors, fmt.Errorf("%q (%q) cannot be longer than '32' characters", k, name))
}
return
}

func validateAwsAlbTargetGroupPort(v interface{}, k string) (ws []string, errors []error) {
port := v.(int)
if port < 1 || port > 65536 {
Expand Down
65 changes: 65 additions & 0 deletions builtin/providers/aws/resource_aws_alb_target_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"errors"
"fmt"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -85,6 +86,45 @@ func TestAccAWSALBTargetGroup_basic(t *testing.T) {
})
}

func TestAccAWSALBTargetGroup_namePrefix(t *testing.T) {
var conf elbv2.TargetGroup

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_alb_target_group.test",
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSALBTargetGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSALBTargetGroupConfig_namePrefix,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf),
resource.TestMatchResourceAttr("aws_alb_target_group.test", "name", regexp.MustCompile("^tf-")),
),
},
},
})
}

func TestAccAWSALBTargetGroup_generatedName(t *testing.T) {
var conf elbv2.TargetGroup

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_alb_target_group.test",
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSALBTargetGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSALBTargetGroupConfig_generatedName,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf),
),
},
},
})
}

func TestAccAWSALBTargetGroup_changeNameForceNew(t *testing.T) {
var before, after elbv2.TargetGroup
targetGroupNameBefore := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
Expand Down Expand Up @@ -715,3 +755,28 @@ resource "aws_vpc" "test" {
}
}`, targetGroupName, stickinessBlock)
}

const testAccAWSALBTargetGroupConfig_namePrefix = `
resource "aws_alb_target_group" "test" {
name_prefix = "tf-"
port = 80
protocol = "HTTP"
vpc_id = "${aws_vpc.test.id}"
}

resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
}
`

const testAccAWSALBTargetGroupConfig_generatedName = `
resource "aws_alb_target_group" "test" {
port = 80
protocol = "HTTP"
vpc_id = "${aws_vpc.test.id}"
}

resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
}
`
16 changes: 16 additions & 0 deletions builtin/providers/aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -1154,3 +1154,19 @@ func validateDbOptionGroupNamePrefix(v interface{}, k string) (ws []string, erro
}
return
}

func validateAwsAlbTargetGroupName(v interface{}, k string) (ws []string, errors []error) {
name := v.(string)
if len(name) > 32 {
errors = append(errors, fmt.Errorf("%q (%q) cannot be longer than '32' characters", k, name))
}
return
}

func validateAwsAlbTargetGroupNamePrefix(v interface{}, k string) (ws []string, errors []error) {
name := v.(string)
if len(name) > 32 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be 6, not 32

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ashb - what makes you think it should be 6?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That the message on the next line says 6! (I came here because I got this error upon trying name_prefix:

  • aws_alb_target_group.mod: Error creating ALB Target Group: ValidationError: Target group name 'ash-test-airflow-webserver0019dc824d8cf5fe93abbb8785' cannot be longer than '32' characters

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errors = append(errors, fmt.Errorf("%q (%q) cannot be longer than '6' characters", k, name))
}
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ resource "aws_vpc" "main" {

The following arguments are supported:

* `name` - (Required) The name of the target group.
* `name` - (Optional, Forces new resource) The name of the target group. If omitted, Terraform will assign a random, unique name.
* `name_prefix` - (Optional, Forces new resource) Creates a unique name beginning with the specified prefix. Conflicts with `name`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please include the character limits here in the documentation? Also, why the 6 character limit for the prefix?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 6 character limit is because name_prefix generates 26 characters of randomness and the maximum length for the name imposed by AWS is 32 characters. See #12947.

* `port` - (Required) The port on which targets receive traffic, unless overridden when registering a specific target.
* `protocol` - (Required) The protocol to use for routing traffic to the targets.
* `vpc_id` - (Required) The identifier of the VPC in which to create the target group.
Expand Down