providers/aws: launch configuration in helper/schema
This commit is contained in:
parent
caaa9d145a
commit
b43cfa3bb0
|
@ -42,13 +42,14 @@ func Provider() *schema.Provider {
|
||||||
},
|
},
|
||||||
|
|
||||||
ResourcesMap: map[string]*schema.Resource{
|
ResourcesMap: map[string]*schema.Resource{
|
||||||
"aws_autoscaling_group": resourceAwsAutoscalingGroup(),
|
"aws_autoscaling_group": resourceAwsAutoscalingGroup(),
|
||||||
"aws_eip": resourceAwsEip(),
|
"aws_eip": resourceAwsEip(),
|
||||||
"aws_elb": resourceAwsElb(),
|
"aws_elb": resourceAwsElb(),
|
||||||
"aws_instance": resourceAwsInstance(),
|
"aws_instance": resourceAwsInstance(),
|
||||||
"aws_security_group": resourceAwsSecurityGroup(),
|
"aws_launch_configuration": resourceAwsLaunchConfiguration(),
|
||||||
"aws_db_subnet_group": resourceAwsDbSubnetGroup(),
|
"aws_security_group": resourceAwsSecurityGroup(),
|
||||||
"aws_vpc": resourceAwsVpc(),
|
"aws_db_subnet_group": resourceAwsDbSubnetGroup(),
|
||||||
|
"aws_vpc": resourceAwsVpc(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,214 +1,165 @@
|
||||||
package aws
|
package aws
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha1"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/flatmap"
|
"github.com/hashicorp/terraform/helper/hashcode"
|
||||||
"github.com/hashicorp/terraform/helper/config"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
"github.com/hashicorp/terraform/helper/diff"
|
|
||||||
"github.com/hashicorp/terraform/terraform"
|
|
||||||
"github.com/mitchellh/goamz/autoscaling"
|
"github.com/mitchellh/goamz/autoscaling"
|
||||||
)
|
)
|
||||||
|
|
||||||
func resource_aws_launch_configuration_create(
|
func resourceAwsLaunchConfiguration() *schema.Resource {
|
||||||
s *terraform.InstanceState,
|
return &schema.Resource{
|
||||||
d *terraform.InstanceDiff,
|
Create: resourceAwsLaunchConfigurationCreate,
|
||||||
meta interface{}) (*terraform.InstanceState, error) {
|
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))
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
|
||||||
p := meta.(*ResourceProvider)
|
p := meta.(*ResourceProvider)
|
||||||
autoscalingconn := p.autoscalingconn
|
autoscalingconn := p.autoscalingconn
|
||||||
|
|
||||||
// Merge the diff into the state so that we have all the attributes
|
var createLaunchConfigurationOpts autoscaling.CreateLaunchConfiguration
|
||||||
// properly.
|
createLaunchConfigurationOpts.Name = d.Get("name").(string)
|
||||||
rs := s.MergeDiff(d)
|
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)
|
||||||
|
|
||||||
var err error
|
if v, ok := d.GetOk("security_groups"); ok {
|
||||||
createLaunchConfigurationOpts := autoscaling.CreateLaunchConfiguration{}
|
createLaunchConfigurationOpts.SecurityGroups = expandStringList(
|
||||||
|
v.(*schema.Set).List())
|
||||||
if rs.Attributes["iam_instance_profile"] != "" {
|
|
||||||
createLaunchConfigurationOpts.IamInstanceProfile = rs.Attributes["iam_instance_profile"]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if rs.Attributes["image_id"] != "" {
|
|
||||||
createLaunchConfigurationOpts.ImageId = rs.Attributes["image_id"]
|
|
||||||
}
|
|
||||||
|
|
||||||
if rs.Attributes["instance_type"] != "" {
|
|
||||||
createLaunchConfigurationOpts.InstanceType = rs.Attributes["instance_type"]
|
|
||||||
}
|
|
||||||
|
|
||||||
if rs.Attributes["instance_id"] != "" {
|
|
||||||
createLaunchConfigurationOpts.InstanceId = rs.Attributes["instance_id"]
|
|
||||||
}
|
|
||||||
|
|
||||||
if rs.Attributes["key_name"] != "" {
|
|
||||||
createLaunchConfigurationOpts.KeyName = rs.Attributes["key_name"]
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Error parsing configuration: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := rs.Attributes["security_groups.#"]; ok {
|
|
||||||
createLaunchConfigurationOpts.SecurityGroups = expandStringList(flatmap.Expand(
|
|
||||||
rs.Attributes, "security_groups").([]interface{}))
|
|
||||||
}
|
|
||||||
|
|
||||||
if rs.Attributes["user_data"] != "" {
|
|
||||||
createLaunchConfigurationOpts.UserData = rs.Attributes["user_data"]
|
|
||||||
}
|
|
||||||
|
|
||||||
createLaunchConfigurationOpts.Name = rs.Attributes["name"]
|
|
||||||
|
|
||||||
log.Printf("[DEBUG] autoscaling create launch configuration: %#v", createLaunchConfigurationOpts)
|
log.Printf("[DEBUG] autoscaling create launch configuration: %#v", createLaunchConfigurationOpts)
|
||||||
_, err = autoscalingconn.CreateLaunchConfiguration(&createLaunchConfigurationOpts)
|
_, err := autoscalingconn.CreateLaunchConfiguration(&createLaunchConfigurationOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Error creating launch configuration: %s", err)
|
return fmt.Errorf("Error creating launch configuration: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
rs.ID = rs.Attributes["name"]
|
d.SetId(d.Get("name").(string))
|
||||||
|
log.Printf("[INFO] launch configuration ID: %s", d.Id())
|
||||||
|
|
||||||
log.Printf("[INFO] launch configuration ID: %s", rs.ID)
|
return resourceAwsLaunchConfigurationRead(d, meta)
|
||||||
|
|
||||||
g, err := resource_aws_launch_configuration_retrieve(rs.ID, autoscalingconn)
|
|
||||||
if err != nil {
|
|
||||||
return rs, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return resource_aws_launch_configuration_update_state(rs, g)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func resource_aws_launch_configuration_update(
|
func resourceAwsLaunchConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
|
||||||
s *terraform.InstanceState,
|
|
||||||
d *terraform.InstanceDiff,
|
|
||||||
meta interface{}) (*terraform.InstanceState, error) {
|
|
||||||
panic("Update for AWS Launch Configuration is not supported")
|
|
||||||
}
|
|
||||||
|
|
||||||
func resource_aws_launch_configuration_destroy(
|
|
||||||
s *terraform.InstanceState,
|
|
||||||
meta interface{}) error {
|
|
||||||
p := meta.(*ResourceProvider)
|
p := meta.(*ResourceProvider)
|
||||||
autoscalingconn := p.autoscalingconn
|
autoscalingconn := p.autoscalingconn
|
||||||
|
|
||||||
log.Printf("[DEBUG] Launch Configuration destroy: %v", s.ID)
|
log.Printf("[DEBUG] Launch Configuration destroy: %v", d.Id())
|
||||||
|
_, err := autoscalingconn.DeleteLaunchConfiguration(
|
||||||
_, err := autoscalingconn.DeleteLaunchConfiguration(&autoscaling.DeleteLaunchConfiguration{Name: s.ID})
|
&autoscaling.DeleteLaunchConfiguration{Name: d.Id()})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
autoscalingerr, ok := err.(*autoscaling.Error)
|
autoscalingerr, ok := err.(*autoscaling.Error)
|
||||||
if ok && autoscalingerr.Code == "InvalidConfiguration.NotFound" {
|
if ok && autoscalingerr.Code == "InvalidConfiguration.NotFound" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func resource_aws_launch_configuration_refresh(
|
func resourceAwsLaunchConfigurationRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
s *terraform.InstanceState,
|
|
||||||
meta interface{}) (*terraform.InstanceState, error) {
|
|
||||||
p := meta.(*ResourceProvider)
|
p := meta.(*ResourceProvider)
|
||||||
autoscalingconn := p.autoscalingconn
|
autoscalingconn := p.autoscalingconn
|
||||||
|
|
||||||
g, err := resource_aws_launch_configuration_retrieve(s.ID, autoscalingconn)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return s, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return resource_aws_launch_configuration_update_state(s, g)
|
|
||||||
}
|
|
||||||
|
|
||||||
func resource_aws_launch_configuration_diff(
|
|
||||||
s *terraform.InstanceState,
|
|
||||||
c *terraform.ResourceConfig,
|
|
||||||
meta interface{}) (*terraform.InstanceDiff, error) {
|
|
||||||
|
|
||||||
b := &diff.ResourceBuilder{
|
|
||||||
Attrs: map[string]diff.AttrType{
|
|
||||||
"iam_instance_profile": diff.AttrTypeCreate,
|
|
||||||
"image_id": diff.AttrTypeCreate,
|
|
||||||
"instance_id": diff.AttrTypeCreate,
|
|
||||||
"instance_type": diff.AttrTypeCreate,
|
|
||||||
"key_name": diff.AttrTypeCreate,
|
|
||||||
"name": diff.AttrTypeCreate,
|
|
||||||
"security_groups": diff.AttrTypeCreate,
|
|
||||||
"user_data": diff.AttrTypeCreate,
|
|
||||||
},
|
|
||||||
|
|
||||||
ComputedAttrs: []string{
|
|
||||||
"key_name",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.Diff(s, c)
|
|
||||||
}
|
|
||||||
|
|
||||||
func resource_aws_launch_configuration_update_state(
|
|
||||||
s *terraform.InstanceState,
|
|
||||||
lc *autoscaling.LaunchConfiguration) (*terraform.InstanceState, error) {
|
|
||||||
|
|
||||||
s.Attributes["iam_instance_profile"] = lc.IamInstanceProfile
|
|
||||||
s.Attributes["image_id"] = lc.ImageId
|
|
||||||
s.Attributes["instance_type"] = lc.InstanceType
|
|
||||||
s.Attributes["key_name"] = lc.KeyName
|
|
||||||
s.Attributes["name"] = lc.Name
|
|
||||||
|
|
||||||
// Flatten our group values
|
|
||||||
toFlatten := make(map[string]interface{})
|
|
||||||
|
|
||||||
if len(lc.SecurityGroups) > 0 && lc.SecurityGroups[0].SecurityGroup != "" {
|
|
||||||
toFlatten["security_groups"] = flattenAutoscalingSecurityGroups(lc.SecurityGroups)
|
|
||||||
}
|
|
||||||
|
|
||||||
for k, v := range flatmap.Flatten(toFlatten) {
|
|
||||||
s.Attributes[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
return s, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns a single group by its ID
|
|
||||||
func resource_aws_launch_configuration_retrieve(id string, autoscalingconn *autoscaling.AutoScaling) (*autoscaling.LaunchConfiguration, error) {
|
|
||||||
describeOpts := autoscaling.DescribeLaunchConfigurations{
|
describeOpts := autoscaling.DescribeLaunchConfigurations{
|
||||||
Names: []string{id},
|
Names: []string{d.Id()},
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("[DEBUG] launch configuration describe configuration: %#v", describeOpts)
|
log.Printf("[DEBUG] launch configuration describe configuration: %#v", describeOpts)
|
||||||
|
|
||||||
describConfs, err := autoscalingconn.DescribeLaunchConfigurations(&describeOpts)
|
describConfs, err := autoscalingconn.DescribeLaunchConfigurations(&describeOpts)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Error retrieving launch configuration: %s", err)
|
return fmt.Errorf("Error retrieving launch configuration: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify AWS returned our launch configuration
|
// Verify AWS returned our launch configuration
|
||||||
if len(describConfs.LaunchConfigurations) != 1 ||
|
if len(describConfs.LaunchConfigurations) != 1 ||
|
||||||
describConfs.LaunchConfigurations[0].Name != id {
|
describConfs.LaunchConfigurations[0].Name != d.Id() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Unable to find launch configuration: %#v", describConfs.LaunchConfigurations)
|
return fmt.Errorf(
|
||||||
|
"Unable to find launch configuration: %#v",
|
||||||
|
describConfs.LaunchConfigurations)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
l := describConfs.LaunchConfigurations[0]
|
lc := describConfs.LaunchConfigurations[0]
|
||||||
|
|
||||||
return &l, nil
|
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)
|
||||||
|
|
||||||
func resource_aws_launch_configuration_validation() *config.Validator {
|
if v := lc.SecurityGroups; len(v) > 0 && v[0].SecurityGroup != "" {
|
||||||
return &config.Validator{
|
d.Set("security_groups", flattenAutoscalingSecurityGroups(v))
|
||||||
Required: []string{
|
|
||||||
"name",
|
|
||||||
"image_id",
|
|
||||||
"instance_type",
|
|
||||||
},
|
|
||||||
Optional: []string{
|
|
||||||
"iam_instance_profile",
|
|
||||||
"key_name",
|
|
||||||
"security_groups.*",
|
|
||||||
"user_data",
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package aws
|
package aws
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -24,13 +23,11 @@ func TestAccAWSLaunchConfiguration(t *testing.T) {
|
||||||
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
|
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
|
||||||
testAccCheckAWSLaunchConfigurationAttributes(&conf),
|
testAccCheckAWSLaunchConfigurationAttributes(&conf),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"aws_launch_configuration.bar", "image_id", "ami-fb8e9292"),
|
"aws_launch_configuration.bar", "image_id", "ami-21f78e11"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"aws_launch_configuration.bar", "name", "foobar-terraform-test"),
|
"aws_launch_configuration.bar", "name", "foobar-terraform-test"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"aws_launch_configuration.bar", "instance_type", "t1.micro"),
|
"aws_launch_configuration.bar", "instance_type", "t1.micro"),
|
||||||
resource.TestCheckResourceAttr(
|
|
||||||
"aws_launch_configuration.bar", "user_data", "foobar-user-data"),
|
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -72,7 +69,7 @@ func testAccCheckAWSLaunchConfigurationDestroy(s *terraform.State) error {
|
||||||
|
|
||||||
func testAccCheckAWSLaunchConfigurationAttributes(conf *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
|
func testAccCheckAWSLaunchConfigurationAttributes(conf *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
|
||||||
return func(s *terraform.State) error {
|
return func(s *terraform.State) error {
|
||||||
if conf.ImageId != "ami-fb8e9292" {
|
if conf.ImageId != "ami-21f78e11" {
|
||||||
return fmt.Errorf("Bad image_id: %s", conf.ImageId)
|
return fmt.Errorf("Bad image_id: %s", conf.ImageId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,10 +81,6 @@ func testAccCheckAWSLaunchConfigurationAttributes(conf *autoscaling.LaunchConfig
|
||||||
return fmt.Errorf("Bad instance_type: %s", conf.InstanceType)
|
return fmt.Errorf("Bad instance_type: %s", conf.InstanceType)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bytes.Equal(conf.UserData, []byte("foobar-user-data")) {
|
|
||||||
return fmt.Errorf("Bad user_data: %s", conf.UserData)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -128,7 +121,7 @@ func testAccCheckAWSLaunchConfigurationExists(n string, res *autoscaling.LaunchC
|
||||||
const testAccAWSLaunchConfigurationConfig = `
|
const testAccAWSLaunchConfigurationConfig = `
|
||||||
resource "aws_launch_configuration" "bar" {
|
resource "aws_launch_configuration" "bar" {
|
||||||
name = "foobar-terraform-test"
|
name = "foobar-terraform-test"
|
||||||
image_id = "ami-fb8e9292"
|
image_id = "ami-21f78e11"
|
||||||
instance_type = "t1.micro"
|
instance_type = "t1.micro"
|
||||||
user_data = "foobar-user-data"
|
user_data = "foobar-user-data"
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,14 +37,6 @@ func init() {
|
||||||
Update: resource_aws_internet_gateway_update,
|
Update: resource_aws_internet_gateway_update,
|
||||||
},
|
},
|
||||||
|
|
||||||
"aws_launch_configuration": resource.Resource{
|
|
||||||
ConfigValidator: resource_aws_launch_configuration_validation(),
|
|
||||||
Create: resource_aws_launch_configuration_create,
|
|
||||||
Destroy: resource_aws_launch_configuration_destroy,
|
|
||||||
Diff: resource_aws_launch_configuration_diff,
|
|
||||||
Refresh: resource_aws_launch_configuration_refresh,
|
|
||||||
},
|
|
||||||
|
|
||||||
"aws_route_table": resource.Resource{
|
"aws_route_table": resource.Resource{
|
||||||
ConfigValidator: &config.Validator{
|
ConfigValidator: &config.Validator{
|
||||||
Required: []string{
|
Required: []string{
|
||||||
|
|
Loading…
Reference in New Issue