From 70b7243dd6606286ec77f7b4448ca5d4f572e0c5 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Tue, 30 Jun 2015 12:54:32 +0100 Subject: [PATCH 1/2] helper: Add resource.PrefixedUniqueId --- helper/resource/id.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/helper/resource/id.go b/helper/resource/id.go index b3af422fc..ea0ae1916 100644 --- a/helper/resource/id.go +++ b/helper/resource/id.go @@ -9,13 +9,18 @@ import ( const UniqueIdPrefix = `terraform-` -// Helper for a resource to generate a unique identifier +// Helper for a resource to generate a unique identifier w/ default prefix +func UniqueId() string { + return PrefixedUniqueId(UniqueIdPrefix) +} + +// Helper for a resource to generate a unique identifier w/ given prefix // // This uses a simple RFC 4122 v4 UUID with some basic cosmetic filters // applied (base32, remove padding, downcase) to make visually distinguishing // identifiers easier. -func UniqueId() string { - return fmt.Sprintf("%s%s", UniqueIdPrefix, +func PrefixedUniqueId(prefix string) string { + return fmt.Sprintf("%s%s", prefix, strings.ToLower( strings.Replace( base32.StdEncoding.EncodeToString(uuidV4()), From 676d490d403f4234b5a2baa579328da28c49d476 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Tue, 30 Jun 2015 13:01:07 +0100 Subject: [PATCH 2/2] provider/aws: Allow elb name to be generated --- builtin/providers/aws/resource_aws_elb.go | 16 +++++++++++++--- .../docs/providers/aws/r/elb.html.markdown | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index b76cc7131..8a5c9b413 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -11,6 +11,7 @@ import ( "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/elb" "github.com/hashicorp/terraform/helper/hashcode" + "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" ) @@ -24,7 +25,8 @@ func resourceAwsElb() *schema.Resource { Schema: map[string]*schema.Schema{ "name": &schema.Schema{ Type: schema.TypeString, - Required: true, + Optional: true, + Computed: true, ForceNew: true, ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { value := v.(string) @@ -211,10 +213,18 @@ func resourceAwsElbCreate(d *schema.ResourceData, meta interface{}) error { return err } + var elbName string + if v, ok := d.GetOk("name"); ok { + elbName = v.(string) + } else { + elbName = resource.PrefixedUniqueId("tf-lb-") + d.Set("name", elbName) + } + tags := tagsFromMapELB(d.Get("tags").(map[string]interface{})) // Provision the elb elbOpts := &elb.CreateLoadBalancerInput{ - LoadBalancerName: aws.String(d.Get("name").(string)), + LoadBalancerName: aws.String(elbName), Listeners: listeners, Tags: tags, } @@ -241,7 +251,7 @@ func resourceAwsElbCreate(d *schema.ResourceData, meta interface{}) error { } // Assign the elb's unique identifier for use later - d.SetId(d.Get("name").(string)) + d.SetId(elbName) log.Printf("[INFO] ELB ID: %s", d.Id()) // Enable partial mode and record what we set diff --git a/website/source/docs/providers/aws/r/elb.html.markdown b/website/source/docs/providers/aws/r/elb.html.markdown index 9c63788d3..824a5507f 100644 --- a/website/source/docs/providers/aws/r/elb.html.markdown +++ b/website/source/docs/providers/aws/r/elb.html.markdown @@ -57,7 +57,7 @@ resource "aws_elb" "bar" { The following arguments are supported: -* `name` - (Required) The name of the ELB +* `name` - (Optional) The name of the ELB. By default generated by terraform. * `availability_zones` - (Required for an EC2-classic ELB) The AZ's to serve traffic in. * `security_groups` - (Optional) A list of security group IDs to assign to the ELB. * `subnets` - (Required for a VPC ELB) A list of subnet IDs to attach to the ELB.