2014-07-10 20:15:58 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2014-10-10 23:50:23 +02:00
|
|
|
"crypto/sha1"
|
2015-02-20 23:26:43 +01:00
|
|
|
"encoding/base64"
|
2014-10-10 23:50:23 +02:00
|
|
|
"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
|
|
|
|
2015-02-25 22:29:11 +01:00
|
|
|
"github.com/hashicorp/aws-sdk-go/aws"
|
|
|
|
"github.com/hashicorp/aws-sdk-go/gen/autoscaling"
|
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
|
|
|
)
|
|
|
|
|
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,
|
2015-02-27 19:15:12 +01:00
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
2014-10-10 23:50:23 +02:00
|
|
|
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,
|
|
|
|
},
|
2015-01-22 05:48:04 +01:00
|
|
|
|
|
|
|
"spot_price": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
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
|
|
|
|
2015-02-20 23:26:43 +01:00
|
|
|
var createLaunchConfigurationOpts autoscaling.CreateLaunchConfigurationType
|
|
|
|
createLaunchConfigurationOpts.LaunchConfigurationName = aws.String(d.Get("name").(string))
|
|
|
|
createLaunchConfigurationOpts.ImageID = aws.String(d.Get("image_id").(string))
|
|
|
|
createLaunchConfigurationOpts.InstanceType = aws.String(d.Get("instance_type").(string))
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("user_data"); ok {
|
|
|
|
createLaunchConfigurationOpts.UserData = aws.String(base64.StdEncoding.EncodeToString([]byte(v.(string))))
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("associate_public_ip_address"); ok {
|
|
|
|
createLaunchConfigurationOpts.AssociatePublicIPAddress = aws.Boolean(v.(bool))
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("iam_instance_profile"); ok {
|
|
|
|
createLaunchConfigurationOpts.IAMInstanceProfile = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("key_name"); ok {
|
|
|
|
createLaunchConfigurationOpts.KeyName = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("spot_price"); ok {
|
|
|
|
createLaunchConfigurationOpts.SpotPrice = aws.String(v.(string))
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2015-02-27 19:15:12 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-07-10 20:15:58 +02:00
|
|
|
log.Printf("[DEBUG] autoscaling create launch configuration: %#v", createLaunchConfigurationOpts)
|
2015-02-20 23:26:43 +01: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
|
|
|
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
|
|
|
|
2015-02-20 23:26:43 +01:00
|
|
|
describeOpts := autoscaling.LaunchConfigurationNamesType{
|
|
|
|
LaunchConfigurationNames: []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
|
2015-02-20 23:26:43 +01:00
|
|
|
if *describConfs.LaunchConfigurations[0].LaunchConfigurationName != d.Id() {
|
2014-10-18 05:21:18 +02:00
|
|
|
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
|
|
|
|
2015-02-20 23:26:43 +01:00
|
|
|
d.Set("key_name", *lc.KeyName)
|
|
|
|
d.Set("image_id", *lc.ImageID)
|
|
|
|
d.Set("instance_type", *lc.InstanceType)
|
|
|
|
d.Set("name", *lc.LaunchConfigurationName)
|
2014-10-10 23:50:23 +02:00
|
|
|
|
2015-02-20 23:26:43 +01:00
|
|
|
if lc.IAMInstanceProfile != nil {
|
|
|
|
d.Set("iam_instance_profile", *lc.IAMInstanceProfile)
|
|
|
|
} else {
|
|
|
|
d.Set("iam_instance_profile", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if lc.SpotPrice != nil {
|
|
|
|
d.Set("spot_price", *lc.SpotPrice)
|
|
|
|
} else {
|
|
|
|
d.Set("spot_price", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if lc.SecurityGroups != nil {
|
|
|
|
d.Set("security_groups", lc.SecurityGroups)
|
|
|
|
} else {
|
|
|
|
d.Set("security_groups", nil)
|
|
|
|
}
|
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())
|
2015-02-20 23:26:43 +01:00
|
|
|
err := autoscalingconn.DeleteLaunchConfiguration(
|
|
|
|
&autoscaling.LaunchConfigurationNameType{LaunchConfigurationName: aws.String(d.Id())})
|
2014-11-21 17:58:34 +01:00
|
|
|
if err != nil {
|
2015-02-20 23:26:43 +01:00
|
|
|
autoscalingerr, ok := err.(aws.APIError)
|
2014-11-21 17:58:34 +01:00
|
|
|
if ok && autoscalingerr.Code == "InvalidConfiguration.NotFound" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|