2014-07-14 20:10:13 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2015-02-20 23:26:43 +01:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
|
|
|
"github.com/awslabs/aws-sdk-go/gen/autoscaling"
|
2014-07-14 20:10:13 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
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(
|
2014-10-10 23:50:23 +02:00
|
|
|
"aws_launch_configuration.bar", "image_id", "ami-21f78e11"),
|
2014-07-14 20:10:13 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_launch_configuration.bar", "name", "foobar-terraform-test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_launch_configuration.bar", "instance_type", "t1.micro"),
|
2014-11-24 05:26:03 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_launch_configuration.bar", "associate_public_ip_address", "true"),
|
2015-01-23 22:51:25 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_launch_configuration.bar", "spot_price", ""),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: TestAccAWSLaunchConfigurationWithSpotPriceConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
|
|
|
|
testAccCheckAWSLaunchConfigurationAttributes(&conf),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_launch_configuration.bar", "spot_price", "0.01"),
|
2014-07-14 20:10:13 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSLaunchConfigurationDestroy(s *terraform.State) error {
|
2014-11-21 17:58:34 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
|
2014-07-14 20:10:13 +02:00
|
|
|
|
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(
|
2015-02-20 23:26:43 +01:00
|
|
|
&autoscaling.LaunchConfigurationNamesType{
|
|
|
|
LaunchConfigurationNames: []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 &&
|
2015-02-20 23:26:43 +01:00
|
|
|
*describe.LaunchConfigurations[0].LaunchConfigurationName == rs.Primary.ID {
|
2014-07-14 20:10:13 +02:00
|
|
|
return fmt.Errorf("Launch Configuration still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the error
|
2015-02-20 23:26:43 +01:00
|
|
|
providerErr, ok := err.(aws.APIError)
|
2014-07-14 20:10:13 +02:00
|
|
|
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 {
|
2015-02-20 23:26:43 +01:00
|
|
|
if *conf.ImageID != "ami-21f78e11" {
|
|
|
|
return fmt.Errorf("Bad image_id: %s", *conf.ImageID)
|
2014-07-14 20:10:13 +02:00
|
|
|
}
|
|
|
|
|
2015-02-20 23:26:43 +01:00
|
|
|
if *conf.LaunchConfigurationName != "foobar-terraform-test" {
|
|
|
|
return fmt.Errorf("Bad name: %s", *conf.LaunchConfigurationName)
|
2014-07-14 20:10:13 +02:00
|
|
|
}
|
|
|
|
|
2015-02-20 23:26:43 +01:00
|
|
|
if *conf.InstanceType != "t1.micro" {
|
|
|
|
return fmt.Errorf("Bad instance_type: %s", *conf.InstanceType)
|
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")
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
|
2014-07-14 20:10:13 +02:00
|
|
|
|
2015-02-20 23:26:43 +01:00
|
|
|
describeOpts := autoscaling.LaunchConfigurationNamesType{
|
|
|
|
LaunchConfigurationNames: []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 ||
|
2015-02-20 23:26:43 +01:00
|
|
|
*describe.LaunchConfigurations[0].LaunchConfigurationName != 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"
|
2014-10-10 23:50:23 +02:00
|
|
|
image_id = "ami-21f78e11"
|
2014-07-14 20:10:13 +02:00
|
|
|
instance_type = "t1.micro"
|
2014-07-29 23:30:24 +02:00
|
|
|
user_data = "foobar-user-data"
|
2014-11-24 05:26:03 +01:00
|
|
|
associate_public_ip_address = true
|
2014-07-14 20:10:13 +02:00
|
|
|
}
|
|
|
|
`
|
2015-01-23 22:51:25 +01:00
|
|
|
|
|
|
|
const TestAccAWSLaunchConfigurationWithSpotPriceConfig = `
|
|
|
|
resource "aws_launch_configuration" "bar" {
|
|
|
|
name = "foobar-terraform-test"
|
|
|
|
image_id = "ami-21f78e11"
|
|
|
|
instance_type = "t1.micro"
|
|
|
|
user_data = "foobar-user-data"
|
|
|
|
associate_public_ip_address = true
|
|
|
|
spot_price = "0.01"
|
|
|
|
}
|
|
|
|
`
|