2014-07-10 20:15:58 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2014-10-10 23:50:23 +02:00
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
2014-07-10 20:15:58 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-10-11 21:27:23 +02:00
|
|
|
"time"
|
2014-07-10 20:15:58 +02:00
|
|
|
|
2014-10-10 23:50:23 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
2014-10-11 21:27:23 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-10-10 23:50:23 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-10 20:15:58 +02:00
|
|
|
"github.com/mitchellh/goamz/autoscaling"
|
|
|
|
)
|
|
|
|
|
2014-10-10 23:50:23 +02:00
|
|
|
func resourceAwsLaunchConfiguration() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsLaunchConfigurationCreate,
|
|
|
|
Read: resourceAwsLaunchConfigurationRead,
|
|
|
|
Delete: resourceAwsLaunchConfigurationDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"image_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"instance_type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"iam_instance_profile": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"key_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"user_data": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
StateFunc: func(v interface{}) string {
|
|
|
|
switch v.(type) {
|
|
|
|
case string:
|
|
|
|
hash := sha1.Sum([]byte(v.(string)))
|
|
|
|
return hex.EncodeToString(hash[:])
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"security_groups": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
},
|
2014-11-02 00:21:55 +01:00
|
|
|
|
|
|
|
"associate_public_ip_address": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
},
|
2014-10-10 23:50:23 +02:00
|
|
|
},
|
2014-07-10 20:15:58 +02:00
|
|
|
}
|
2014-10-10 23:50:23 +02:00
|
|
|
}
|
2014-07-10 20:15:58 +02:00
|
|
|
|
2014-10-10 23:50:23 +02:00
|
|
|
func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
|
2014-11-21 17:58:34 +01:00
|
|
|
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
2014-07-10 20:15:58 +02:00
|
|
|
|
2014-10-10 23:50:23 +02:00
|
|
|
var createLaunchConfigurationOpts autoscaling.CreateLaunchConfiguration
|
|
|
|
createLaunchConfigurationOpts.Name = d.Get("name").(string)
|
|
|
|
createLaunchConfigurationOpts.IamInstanceProfile = d.Get("iam_instance_profile").(string)
|
|
|
|
createLaunchConfigurationOpts.ImageId = d.Get("image_id").(string)
|
|
|
|
createLaunchConfigurationOpts.InstanceType = d.Get("instance_type").(string)
|
|
|
|
createLaunchConfigurationOpts.KeyName = d.Get("key_name").(string)
|
|
|
|
createLaunchConfigurationOpts.UserData = d.Get("user_data").(string)
|
2014-11-02 00:21:55 +01:00
|
|
|
createLaunchConfigurationOpts.AssociatePublicIpAddress = d.Get("associate_public_ip_address").(bool)
|
2014-07-10 20:15:58 +02:00
|
|
|
|
2014-10-10 23:50:23 +02:00
|
|
|
if v, ok := d.GetOk("security_groups"); ok {
|
|
|
|
createLaunchConfigurationOpts.SecurityGroups = expandStringList(
|
|
|
|
v.(*schema.Set).List())
|
2014-07-29 23:30:24 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 20:15:58 +02:00
|
|
|
log.Printf("[DEBUG] autoscaling create launch configuration: %#v", createLaunchConfigurationOpts)
|
2014-10-10 23:50:23 +02:00
|
|
|
_, err := autoscalingconn.CreateLaunchConfiguration(&createLaunchConfigurationOpts)
|
2014-07-10 20:15:58 +02:00
|
|
|
if err != nil {
|
2014-10-10 23:50:23 +02:00
|
|
|
return fmt.Errorf("Error creating launch configuration: %s", err)
|
2014-07-10 20:15:58 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:50:23 +02:00
|
|
|
d.SetId(d.Get("name").(string))
|
|
|
|
log.Printf("[INFO] launch configuration ID: %s", d.Id())
|
2014-07-10 20:15:58 +02:00
|
|
|
|
2014-10-11 21:27:23 +02:00
|
|
|
// We put a Retry here since sometimes eventual consistency bites
|
|
|
|
// us and we need to retry a few times to get the LC to load properly
|
2014-10-11 21:57:06 +02:00
|
|
|
return resource.Retry(30*time.Second, func() error {
|
2014-10-11 21:27:23 +02:00
|
|
|
return resourceAwsLaunchConfigurationRead(d, meta)
|
|
|
|
})
|
2014-07-10 20:15:58 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:50:23 +02:00
|
|
|
func resourceAwsLaunchConfigurationRead(d *schema.ResourceData, meta interface{}) error {
|
2014-11-21 17:58:34 +01:00
|
|
|
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
2014-07-10 20:15:58 +02:00
|
|
|
|
|
|
|
describeOpts := autoscaling.DescribeLaunchConfigurations{
|
2014-10-10 23:50:23 +02:00
|
|
|
Names: []string{d.Id()},
|
2014-07-10 20:15:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] launch configuration describe configuration: %#v", describeOpts)
|
|
|
|
describConfs, err := autoscalingconn.DescribeLaunchConfigurations(&describeOpts)
|
|
|
|
if err != nil {
|
2014-10-10 23:50:23 +02:00
|
|
|
return fmt.Errorf("Error retrieving launch configuration: %s", err)
|
2014-07-10 20:15:58 +02:00
|
|
|
}
|
2014-10-18 05:21:18 +02:00
|
|
|
if len(describConfs.LaunchConfigurations) == 0 {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-10 20:15:58 +02:00
|
|
|
|
|
|
|
// Verify AWS returned our launch configuration
|
2014-10-18 05:21:18 +02:00
|
|
|
if describConfs.LaunchConfigurations[0].Name != d.Id() {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Unable to find launch configuration: %#v",
|
|
|
|
describConfs.LaunchConfigurations)
|
2014-07-10 20:15:58 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:50:23 +02:00
|
|
|
lc := describConfs.LaunchConfigurations[0]
|
2014-07-10 20:15:58 +02:00
|
|
|
|
2014-10-10 23:50:23 +02:00
|
|
|
d.Set("key_name", lc.KeyName)
|
|
|
|
d.Set("iam_instance_profile", lc.IamInstanceProfile)
|
|
|
|
d.Set("image_id", lc.ImageId)
|
|
|
|
d.Set("instance_type", lc.InstanceType)
|
|
|
|
d.Set("name", lc.Name)
|
2014-10-17 01:39:58 +02:00
|
|
|
d.Set("security_groups", lc.SecurityGroups)
|
2014-10-10 23:50:23 +02:00
|
|
|
|
|
|
|
return nil
|
2014-07-15 18:36:29 +02:00
|
|
|
}
|
2014-11-21 17:58:34 +01:00
|
|
|
|
|
|
|
func resourceAwsLaunchConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Launch Configuration destroy: %v", d.Id())
|
|
|
|
_, err := autoscalingconn.DeleteLaunchConfiguration(
|
|
|
|
&autoscaling.DeleteLaunchConfiguration{Name: d.Id()})
|
|
|
|
if err != nil {
|
|
|
|
autoscalingerr, ok := err.(*autoscaling.Error)
|
|
|
|
if ok && autoscalingerr.Code == "InvalidConfiguration.NotFound" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|