github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/builtin/providers/aws/resource_aws_launch_configuration.go (about)

     1  package aws
     2  
     3  import (
     4  	"crypto/sha1"
     5  	"encoding/base64"
     6  	"encoding/hex"
     7  	"fmt"
     8  	"log"
     9  	"time"
    10  
    11  	"github.com/hashicorp/aws-sdk-go/aws"
    12  	"github.com/hashicorp/aws-sdk-go/gen/autoscaling"
    13  	"github.com/hashicorp/terraform/helper/hashcode"
    14  	"github.com/hashicorp/terraform/helper/resource"
    15  	"github.com/hashicorp/terraform/helper/schema"
    16  )
    17  
    18  func resourceAwsLaunchConfiguration() *schema.Resource {
    19  	return &schema.Resource{
    20  		Create: resourceAwsLaunchConfigurationCreate,
    21  		Read:   resourceAwsLaunchConfigurationRead,
    22  		Delete: resourceAwsLaunchConfigurationDelete,
    23  
    24  		Schema: map[string]*schema.Schema{
    25  			"name": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"image_id": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  				ForceNew: true,
    35  			},
    36  
    37  			"instance_type": &schema.Schema{
    38  				Type:     schema.TypeString,
    39  				Required: true,
    40  				ForceNew: true,
    41  			},
    42  
    43  			"iam_instance_profile": &schema.Schema{
    44  				Type:     schema.TypeString,
    45  				Optional: true,
    46  				ForceNew: true,
    47  			},
    48  
    49  			"key_name": &schema.Schema{
    50  				Type:     schema.TypeString,
    51  				Optional: true,
    52  				Computed: true,
    53  				ForceNew: true,
    54  			},
    55  
    56  			"user_data": &schema.Schema{
    57  				Type:     schema.TypeString,
    58  				Optional: true,
    59  				ForceNew: true,
    60  				StateFunc: func(v interface{}) string {
    61  					switch v.(type) {
    62  					case string:
    63  						hash := sha1.Sum([]byte(v.(string)))
    64  						return hex.EncodeToString(hash[:])
    65  					default:
    66  						return ""
    67  					}
    68  				},
    69  			},
    70  
    71  			"security_groups": &schema.Schema{
    72  				Type:     schema.TypeSet,
    73  				Optional: true,
    74  				ForceNew: true,
    75  				Elem:     &schema.Schema{Type: schema.TypeString},
    76  				Set: func(v interface{}) int {
    77  					return hashcode.String(v.(string))
    78  				},
    79  			},
    80  
    81  			"associate_public_ip_address": &schema.Schema{
    82  				Type:     schema.TypeBool,
    83  				Optional: true,
    84  				Default:  false,
    85  			},
    86  
    87  			"spot_price": &schema.Schema{
    88  				Type:     schema.TypeString,
    89  				Optional: true,
    90  				ForceNew: true,
    91  			},
    92  		},
    93  	}
    94  }
    95  
    96  func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
    97  	autoscalingconn := meta.(*AWSClient).autoscalingconn
    98  
    99  	var createLaunchConfigurationOpts autoscaling.CreateLaunchConfigurationType
   100  	createLaunchConfigurationOpts.LaunchConfigurationName = aws.String(d.Get("name").(string))
   101  	createLaunchConfigurationOpts.ImageID = aws.String(d.Get("image_id").(string))
   102  	createLaunchConfigurationOpts.InstanceType = aws.String(d.Get("instance_type").(string))
   103  
   104  	if v, ok := d.GetOk("user_data"); ok {
   105  		createLaunchConfigurationOpts.UserData = aws.String(base64.StdEncoding.EncodeToString([]byte(v.(string))))
   106  	}
   107  	if v, ok := d.GetOk("associate_public_ip_address"); ok {
   108  		createLaunchConfigurationOpts.AssociatePublicIPAddress = aws.Boolean(v.(bool))
   109  	}
   110  	if v, ok := d.GetOk("iam_instance_profile"); ok {
   111  		createLaunchConfigurationOpts.IAMInstanceProfile = aws.String(v.(string))
   112  	}
   113  	if v, ok := d.GetOk("key_name"); ok {
   114  		createLaunchConfigurationOpts.KeyName = aws.String(v.(string))
   115  	}
   116  	if v, ok := d.GetOk("spot_price"); ok {
   117  		createLaunchConfigurationOpts.SpotPrice = aws.String(v.(string))
   118  	}
   119  
   120  	if v, ok := d.GetOk("security_groups"); ok {
   121  		createLaunchConfigurationOpts.SecurityGroups = expandStringList(
   122  			v.(*schema.Set).List())
   123  	}
   124  
   125  	log.Printf("[DEBUG] autoscaling create launch configuration: %#v", createLaunchConfigurationOpts)
   126  	err := autoscalingconn.CreateLaunchConfiguration(&createLaunchConfigurationOpts)
   127  	if err != nil {
   128  		return fmt.Errorf("Error creating launch configuration: %s", err)
   129  	}
   130  
   131  	d.SetId(d.Get("name").(string))
   132  	log.Printf("[INFO] launch configuration ID: %s", d.Id())
   133  
   134  	// We put a Retry here since sometimes eventual consistency bites
   135  	// us and we need to retry a few times to get the LC to load properly
   136  	return resource.Retry(30*time.Second, func() error {
   137  		return resourceAwsLaunchConfigurationRead(d, meta)
   138  	})
   139  }
   140  
   141  func resourceAwsLaunchConfigurationRead(d *schema.ResourceData, meta interface{}) error {
   142  	autoscalingconn := meta.(*AWSClient).autoscalingconn
   143  
   144  	describeOpts := autoscaling.LaunchConfigurationNamesType{
   145  		LaunchConfigurationNames: []string{d.Id()},
   146  	}
   147  
   148  	log.Printf("[DEBUG] launch configuration describe configuration: %#v", describeOpts)
   149  	describConfs, err := autoscalingconn.DescribeLaunchConfigurations(&describeOpts)
   150  	if err != nil {
   151  		return fmt.Errorf("Error retrieving launch configuration: %s", err)
   152  	}
   153  	if len(describConfs.LaunchConfigurations) == 0 {
   154  		d.SetId("")
   155  		return nil
   156  	}
   157  
   158  	// Verify AWS returned our launch configuration
   159  	if *describConfs.LaunchConfigurations[0].LaunchConfigurationName != d.Id() {
   160  		return fmt.Errorf(
   161  			"Unable to find launch configuration: %#v",
   162  			describConfs.LaunchConfigurations)
   163  	}
   164  
   165  	lc := describConfs.LaunchConfigurations[0]
   166  
   167  	d.Set("key_name", *lc.KeyName)
   168  	d.Set("image_id", *lc.ImageID)
   169  	d.Set("instance_type", *lc.InstanceType)
   170  	d.Set("name", *lc.LaunchConfigurationName)
   171  
   172  	if lc.IAMInstanceProfile != nil {
   173  		d.Set("iam_instance_profile", *lc.IAMInstanceProfile)
   174  	} else {
   175  		d.Set("iam_instance_profile", nil)
   176  	}
   177  
   178  	if lc.SpotPrice != nil {
   179  		d.Set("spot_price", *lc.SpotPrice)
   180  	} else {
   181  		d.Set("spot_price", nil)
   182  	}
   183  
   184  	if lc.SecurityGroups != nil {
   185  		d.Set("security_groups", lc.SecurityGroups)
   186  	} else {
   187  		d.Set("security_groups", nil)
   188  	}
   189  	return nil
   190  }
   191  
   192  func resourceAwsLaunchConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
   193  	autoscalingconn := meta.(*AWSClient).autoscalingconn
   194  
   195  	log.Printf("[DEBUG] Launch Configuration destroy: %v", d.Id())
   196  	err := autoscalingconn.DeleteLaunchConfiguration(
   197  		&autoscaling.LaunchConfigurationNameType{LaunchConfigurationName: aws.String(d.Id())})
   198  	if err != nil {
   199  		autoscalingerr, ok := err.(aws.APIError)
   200  		if ok && autoscalingerr.Code == "InvalidConfiguration.NotFound" {
   201  			return nil
   202  		}
   203  
   204  		return err
   205  	}
   206  
   207  	return nil
   208  }