From 09f5935993d05c11b589b8fcdd709d833463dde3 Mon Sep 17 00:00:00 2001 From: Tomas Doran Date: Fri, 27 Feb 2015 10:15:12 -0800 Subject: [PATCH] Allow launch configuration names to be computed This allows you to set lifecycle create_before_destroy = true and fixes #532 as then we'll make a new launch config, change the launch config on the ASG, and *then* delete the old launch config. Also tried adding tests which unfortunately don't seem to fail... --- .../aws/resource_aws_launch_configuration.go | 15 +++++++++++++-- .../resource_aws_launch_configuration_test.go | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/builtin/providers/aws/resource_aws_launch_configuration.go b/builtin/providers/aws/resource_aws_launch_configuration.go index e6b2f3742..84edd9fd4 100644 --- a/builtin/providers/aws/resource_aws_launch_configuration.go +++ b/builtin/providers/aws/resource_aws_launch_configuration.go @@ -24,7 +24,8 @@ func resourceAwsLaunchConfiguration() *schema.Resource { Schema: map[string]*schema.Schema{ "name": &schema.Schema{ Type: schema.TypeString, - Required: true, + Optional: true, + Computed: true, ForceNew: true, }, @@ -122,13 +123,23 @@ func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface v.(*schema.Set).List()) } + if v, ok := d.GetOk("name"); ok { + createLaunchConfigurationOpts.LaunchConfigurationName = aws.String(v.(string)) + d.SetId(d.Get("name").(string)) + } else { + hash := sha1.Sum([]byte(fmt.Sprintf("%#v", createLaunchConfigurationOpts))) + config_name := fmt.Sprintf("terraform-%s", base64.URLEncoding.EncodeToString(hash[:])) + log.Printf("[DEBUG] Computed Launch config name: %s", config_name) + createLaunchConfigurationOpts.LaunchConfigurationName = aws.String(config_name) + d.SetId(config_name) + } + log.Printf("[DEBUG] autoscaling create launch configuration: %#v", createLaunchConfigurationOpts) err := autoscalingconn.CreateLaunchConfiguration(&createLaunchConfigurationOpts) if err != nil { return fmt.Errorf("Error creating launch configuration: %s", err) } - d.SetId(d.Get("name").(string)) log.Printf("[INFO] launch configuration ID: %s", d.Id()) // We put a Retry here since sometimes eventual consistency bites diff --git a/builtin/providers/aws/resource_aws_launch_configuration_test.go b/builtin/providers/aws/resource_aws_launch_configuration_test.go index 500d3ca07..eb557f08e 100644 --- a/builtin/providers/aws/resource_aws_launch_configuration_test.go +++ b/builtin/providers/aws/resource_aws_launch_configuration_test.go @@ -45,6 +45,16 @@ func TestAccAWSLaunchConfiguration(t *testing.T) { "aws_launch_configuration.bar", "spot_price", "0.01"), ), }, + + resource.TestStep{ + Config: testAccAWSLaunchConfigurationNoNameConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf), + testAccCheckAWSLaunchConfigurationAttributes(&conf), + resource.TestCheckResourceAttr( + "aws_launch_configuration.bar", "name", "terraform-foo"), // FIXME - This should fail?!?!? + ), + }, }, }) } @@ -153,3 +163,12 @@ resource "aws_launch_configuration" "bar" { spot_price = "0.01" } ` + +const testAccAWSLaunchConfigurationNoNameConfig = ` +resource "aws_launch_configuration" "bar" { + image_id = "ami-21f78e12" + instance_type = "t1.micro" + user_data = "foobar-user-data-change" + associate_public_ip_address = false +} +`