From 20ad3e0b2f17c5a81e0dd0c102be2fc111f0a706 Mon Sep 17 00:00:00 2001 From: stack72 Date: Tue, 6 Sep 2016 09:14:52 +0100 Subject: [PATCH 1/3] provider/aws: Allow `aws_alb` to have the name auto-generated In order to satisify scenarios where a lifecycle is block is used, we would need the AWS ALB name field to be autogenerated. WE follow the same work as AWS ELB, we prefix it with `tl-lb-` ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSALB_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/09/08 12:43:40 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSALB_ -timeout 120m === RUN TestAccAWSALB_basic --- PASS: TestAccAWSALB_basic (79.81s) === RUN TestAccAWSALB_generatedName --- PASS: TestAccAWSALB_generatedName (93.81s) === RUN TestAccAWSALB_namePrefix --- PASS: TestAccAWSALB_namePrefix (73.48s) === RUN TestAccAWSALB_tags --- PASS: TestAccAWSALB_tags (181.32s) === RUN TestAccAWSALB_noSecurityGroup --- PASS: TestAccAWSALB_noSecurityGroup (66.03s) === RUN TestAccAWSALB_accesslogs --- PASS: TestAccAWSALB_accesslogs (130.82s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 625.285s ``` --- builtin/providers/aws/resource_aws_alb.go | 24 ++- .../providers/aws/resource_aws_alb_test.go | 178 ++++++++++++++++++ .../docs/providers/aws/r/alb.html.markdown | 6 +- 3 files changed, 204 insertions(+), 4 deletions(-) diff --git a/builtin/providers/aws/resource_aws_alb.go b/builtin/providers/aws/resource_aws_alb.go index a7bbbdfb4..0668ed4af 100644 --- a/builtin/providers/aws/resource_aws_alb.go +++ b/builtin/providers/aws/resource_aws_alb.go @@ -8,6 +8,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/elbv2" "github.com/hashicorp/errwrap" + "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" ) @@ -28,8 +29,17 @@ func resourceAwsAlb() *schema.Resource { }, "name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + ConflictsWith: []string{"name_prefix"}, + ValidateFunc: validateElbName, + }, + + "name_prefix": { Type: schema.TypeString, - Required: true, + Optional: true, ForceNew: true, ValidateFunc: validateElbName, }, @@ -111,8 +121,18 @@ func resourceAwsAlb() *schema.Resource { func resourceAwsAlbCreate(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn + var name string + if v, ok := d.GetOk("name"); ok { + name = v.(string) + } else if v, ok := d.GetOk("name_prefix"); ok { + name = resource.PrefixedUniqueId(v.(string)) + } else { + name = resource.PrefixedUniqueId("tf-lb-") + } + d.Set("name", name) + elbOpts := &elbv2.CreateLoadBalancerInput{ - Name: aws.String(d.Get("name").(string)), + Name: aws.String(name), Tags: tagsFromMapELBv2(d.Get("tags").(map[string]interface{})), } diff --git a/builtin/providers/aws/resource_aws_alb_test.go b/builtin/providers/aws/resource_aws_alb_test.go index c70786b99..d6d5a48f8 100644 --- a/builtin/providers/aws/resource_aws_alb_test.go +++ b/builtin/providers/aws/resource_aws_alb_test.go @@ -3,6 +3,7 @@ package aws import ( "errors" "fmt" + "regexp" "testing" "github.com/aws/aws-sdk-go/aws" @@ -45,6 +46,48 @@ func TestAccAWSALB_basic(t *testing.T) { }) } +func TestAccAWSALB_generatedName(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_generatedName(), + 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 + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: "aws_alb.alb_test", + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSALBDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSALBConfig_namePrefix(), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSALBExists("aws_alb.alb_test", &conf), + resource.TestCheckResourceAttrSet("aws_alb.alb_test", "name"), + resource.TestMatchResourceAttr("aws_alb.alb_test", "name", + regexp.MustCompile("^tf-lb")), + ), + }, + }, + }) +} + func TestAccAWSALB_tags(t *testing.T) { var conf elbv2.LoadBalancer albName := fmt.Sprintf("testaccawsalb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) @@ -292,6 +335,141 @@ resource "aws_security_group" "alb_test" { } }`, albName) } + +func testAccAWSALBConfig_generatedName() string { + return fmt.Sprintf(` +resource "aws_alb" "alb_test" { + internal = false + 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_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" { + name_prefix = "tf-lb" + internal = false + 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_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_updatedTags(albName string) string { return fmt.Sprintf(`resource "aws_alb" "alb_test" { name = "%s" diff --git a/website/source/docs/providers/aws/r/alb.html.markdown b/website/source/docs/providers/aws/r/alb.html.markdown index cbba79ab3..c4b258253 100644 --- a/website/source/docs/providers/aws/r/alb.html.markdown +++ b/website/source/docs/providers/aws/r/alb.html.markdown @@ -37,8 +37,10 @@ resource "aws_alb" "test" { The following arguments are supported: -* `name` - (Required) The name of the ALB. This name must be unique within your AWS account, can have a maximum of -32 characters, must contain only alphanumeric characters or hyphens, and must not begin or end with a hyphen. +* `name` - (Optional) The name of the ALB. This name must be unique within your AWS account, can have a maximum of 32 characters, +must contain only alphanumeric characters or hyphens, and must not begin or end with a hyphen. If not specified, +Terraform will autogenerate a name beginning with `tf-lb`. +* `name_prefix` - (Optional) Creates a unique name beginning with the specified prefix. Conflicts with `name`. * `internal` - (Optional) If true, the ALB will be internal. * `security_groups` - (Optional) A list of security group IDs to assign to the ELB. * `access_logs` - (Optional) An Access Logs block. Access Logs documented below. From 1de1bd6576050ee2ba51df2f76fb2d2418faaa11 Mon Sep 17 00:00:00 2001 From: Paul Stack Date: Mon, 12 Sep 2016 10:59:05 +0100 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c10a89cce..76d5360ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 0.7.4 (Unreleased) FEATURES: + * **New Resource:** `aws_codecommit_trigger` [GH-8751] IMPROVEMENTS: * provider/aws: Support 'publish' attribute in lambda_function [GH-8653] From 90589dbf2b4de23a6c87bf34e5306f675f753ae3 Mon Sep 17 00:00:00 2001 From: Paul Stack Date: Mon, 12 Sep 2016 11:05:07 +0100 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76d5360ef..13eb8a5ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ FEATURES: IMPROVEMENTS: * provider/aws: Support 'publish' attribute in lambda_function [GH-8653] * provider/aws: Export AWS ELB service account ARN [GH-8700] + * provider/aws: Allow `aws_alb` to have the name auto-generated [GH-8673] * provider/google: Resources depending on the `network` attribute can now reference the network by `self_link` or `name` [GH-8639] * provider/postgresql: The standard environment variables PGHOST, PGUSER, PGPASSWORD and PGSSLMODE are now supported for provider configuration [GH-8666]