github.com/arvindram03/terraform@v0.3.7-0.20150212015210-408f838db36d/builtin/providers/aws/resource_aws_launch_configuration.go (about)

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