github.com/bengesoff/terraform@v0.3.1-0.20141018223233-b25a53629922/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  	}
    80  }
    81  
    82  func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
    83  	p := meta.(*ResourceProvider)
    84  	autoscalingconn := p.autoscalingconn
    85  
    86  	var createLaunchConfigurationOpts autoscaling.CreateLaunchConfiguration
    87  	createLaunchConfigurationOpts.Name = d.Get("name").(string)
    88  	createLaunchConfigurationOpts.IamInstanceProfile = d.Get("iam_instance_profile").(string)
    89  	createLaunchConfigurationOpts.ImageId = d.Get("image_id").(string)
    90  	createLaunchConfigurationOpts.InstanceType = d.Get("instance_type").(string)
    91  	createLaunchConfigurationOpts.KeyName = d.Get("key_name").(string)
    92  	createLaunchConfigurationOpts.UserData = d.Get("user_data").(string)
    93  
    94  	if v, ok := d.GetOk("security_groups"); ok {
    95  		createLaunchConfigurationOpts.SecurityGroups = expandStringList(
    96  			v.(*schema.Set).List())
    97  	}
    98  
    99  	log.Printf("[DEBUG] autoscaling create launch configuration: %#v", createLaunchConfigurationOpts)
   100  	_, err := autoscalingconn.CreateLaunchConfiguration(&createLaunchConfigurationOpts)
   101  	if err != nil {
   102  		return fmt.Errorf("Error creating launch configuration: %s", err)
   103  	}
   104  
   105  	d.SetId(d.Get("name").(string))
   106  	log.Printf("[INFO] launch configuration ID: %s", d.Id())
   107  
   108  	// We put a Retry here since sometimes eventual consistency bites
   109  	// us and we need to retry a few times to get the LC to load properly
   110  	return resource.Retry(30*time.Second, func() error {
   111  		return resourceAwsLaunchConfigurationRead(d, meta)
   112  	})
   113  }
   114  
   115  func resourceAwsLaunchConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
   116  	p := meta.(*ResourceProvider)
   117  	autoscalingconn := p.autoscalingconn
   118  
   119  	log.Printf("[DEBUG] Launch Configuration destroy: %v", d.Id())
   120  	_, err := autoscalingconn.DeleteLaunchConfiguration(
   121  		&autoscaling.DeleteLaunchConfiguration{Name: d.Id()})
   122  	if err != nil {
   123  		autoscalingerr, ok := err.(*autoscaling.Error)
   124  		if ok && autoscalingerr.Code == "InvalidConfiguration.NotFound" {
   125  			return nil
   126  		}
   127  
   128  		return err
   129  	}
   130  
   131  	return nil
   132  }
   133  
   134  func resourceAwsLaunchConfigurationRead(d *schema.ResourceData, meta interface{}) error {
   135  	p := meta.(*ResourceProvider)
   136  	autoscalingconn := p.autoscalingconn
   137  
   138  	describeOpts := autoscaling.DescribeLaunchConfigurations{
   139  		Names: []string{d.Id()},
   140  	}
   141  
   142  	log.Printf("[DEBUG] launch configuration describe configuration: %#v", describeOpts)
   143  	describConfs, err := autoscalingconn.DescribeLaunchConfigurations(&describeOpts)
   144  	if err != nil {
   145  		return fmt.Errorf("Error retrieving launch configuration: %s", err)
   146  	}
   147  	if len(describConfs.LaunchConfigurations) == 0 {
   148  		d.SetId("")
   149  		return nil
   150  	}
   151  
   152  	// Verify AWS returned our launch configuration
   153  	if describConfs.LaunchConfigurations[0].Name != d.Id() {
   154  		return fmt.Errorf(
   155  			"Unable to find launch configuration: %#v",
   156  			describConfs.LaunchConfigurations)
   157  	}
   158  
   159  	lc := describConfs.LaunchConfigurations[0]
   160  
   161  	d.Set("key_name", lc.KeyName)
   162  	d.Set("iam_instance_profile", lc.IamInstanceProfile)
   163  	d.Set("image_id", lc.ImageId)
   164  	d.Set("instance_type", lc.InstanceType)
   165  	d.Set("name", lc.Name)
   166  	d.Set("security_groups", lc.SecurityGroups)
   167  
   168  	return nil
   169  }