Skip to content

Commit

Permalink
Allow launch configuration names to be computed
Browse files Browse the repository at this point in the history
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...
  • Loading branch information
bobtfish committed Feb 27, 2015
1 parent f680e9a commit 09f5935
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
15 changes: 13 additions & 2 deletions builtin/providers/aws/resource_aws_launch_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},

Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions builtin/providers/aws/resource_aws_launch_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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?!?!?
),
},
},
})
}
Expand Down Expand Up @@ -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
}
`

0 comments on commit 09f5935

Please sign in to comment.