2014-07-14 20:10:13 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2014-07-30 01:09:55 +02:00
|
|
|
"bytes"
|
2014-07-14 20:10:13 +02:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/goamz/autoscaling"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAWSLaunchConfiguration(t *testing.T) {
|
|
|
|
var conf autoscaling.LaunchConfiguration
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSLaunchConfigurationConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
|
|
|
|
testAccCheckAWSLaunchConfigurationAttributes(&conf),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_launch_configuration.bar", "image_id", "ami-fb8e9292"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_launch_configuration.bar", "name", "foobar-terraform-test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_launch_configuration.bar", "instance_type", "t1.micro"),
|
2014-07-29 23:30:24 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_launch_configuration.bar", "user_data", "foobar-user-data"),
|
2014-07-14 20:10:13 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSLaunchConfigurationDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.autoscalingconn
|
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-14 20:10:13 +02:00
|
|
|
if rs.Type != "aws_launch_configuration" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-07-14 23:59:08 +02:00
|
|
|
describe, err := conn.DescribeLaunchConfigurations(
|
2014-07-14 20:10:13 +02:00
|
|
|
&autoscaling.DescribeLaunchConfigurations{
|
2014-09-17 02:44:42 +02:00
|
|
|
Names: []string{rs.Primary.ID},
|
2014-07-14 20:10:13 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
2014-07-14 23:59:08 +02:00
|
|
|
if len(describe.LaunchConfigurations) != 0 &&
|
2014-09-17 02:44:42 +02:00
|
|
|
describe.LaunchConfigurations[0].Name == rs.Primary.ID {
|
2014-07-14 20:10:13 +02:00
|
|
|
return fmt.Errorf("Launch Configuration still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the error
|
|
|
|
providerErr, ok := err.(*autoscaling.Error)
|
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if providerErr.Code != "InvalidLaunchConfiguration.NotFound" {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSLaunchConfigurationAttributes(conf *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if conf.ImageId != "ami-fb8e9292" {
|
|
|
|
return fmt.Errorf("Bad image_id: %s", conf.ImageId)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.Name != "foobar-terraform-test" {
|
|
|
|
return fmt.Errorf("Bad name: %s", conf.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.InstanceType != "t1.micro" {
|
|
|
|
return fmt.Errorf("Bad instance_type: %s", conf.InstanceType)
|
|
|
|
}
|
|
|
|
|
2014-07-30 01:09:55 +02:00
|
|
|
if !bytes.Equal(conf.UserData, []byte("foobar-user-data")) {
|
2014-07-29 23:30:24 +02:00
|
|
|
return fmt.Errorf("Bad user_data: %s", conf.UserData)
|
|
|
|
}
|
|
|
|
|
2014-07-14 20:10:13 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSLaunchConfigurationExists(n string, res *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:44:42 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-07-14 20:10:13 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-07-14 20:10:13 +02:00
|
|
|
return fmt.Errorf("No Launch Configuration ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.autoscalingconn
|
|
|
|
|
|
|
|
describeOpts := autoscaling.DescribeLaunchConfigurations{
|
2014-09-17 02:44:42 +02:00
|
|
|
Names: []string{rs.Primary.ID},
|
2014-07-14 20:10:13 +02:00
|
|
|
}
|
|
|
|
describe, err := conn.DescribeLaunchConfigurations(&describeOpts)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(describe.LaunchConfigurations) != 1 ||
|
2014-09-17 02:44:42 +02:00
|
|
|
describe.LaunchConfigurations[0].Name != rs.Primary.ID {
|
2014-07-14 20:10:13 +02:00
|
|
|
return fmt.Errorf("Launch Configuration Group not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*res = describe.LaunchConfigurations[0]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccAWSLaunchConfigurationConfig = `
|
|
|
|
resource "aws_launch_configuration" "bar" {
|
|
|
|
name = "foobar-terraform-test"
|
|
|
|
image_id = "ami-fb8e9292"
|
|
|
|
instance_type = "t1.micro"
|
2014-07-29 23:30:24 +02:00
|
|
|
user_data = "foobar-user-data"
|
2014-07-14 20:10:13 +02:00
|
|
|
}
|
|
|
|
`
|