terraform/builtin/providers/aws/resource_aws_launch_configu...

604 lines
17 KiB
Go
Raw Normal View History

package aws
import (
"fmt"
"log"
"math/rand"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func init() {
resource.AddTestSweepers("aws_launch_configuration", &resource.Sweeper{
Name: "aws_launch_configuration",
Dependencies: []string{"aws_autoscaling_group"},
F: testSweepLaunchConfigurations,
})
}
func testSweepLaunchConfigurations(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
}
autoscalingconn := client.(*AWSClient).autoscalingconn
resp, err := autoscalingconn.DescribeLaunchConfigurations(&autoscaling.DescribeLaunchConfigurationsInput{})
if err != nil {
return fmt.Errorf("Error retrieving launch configuration: %s", err)
}
if len(resp.LaunchConfigurations) == 0 {
log.Print("[DEBUG] No aws launch configurations to sweep")
return nil
}
for _, lc := range resp.LaunchConfigurations {
var testOptGroup bool
for _, testName := range []string{"terraform-", "foobar"} {
if strings.HasPrefix(*lc.LaunchConfigurationName, testName) {
testOptGroup = true
}
}
if !testOptGroup {
continue
}
_, err := autoscalingconn.DeleteLaunchConfiguration(
&autoscaling.DeleteLaunchConfigurationInput{
LaunchConfigurationName: lc.LaunchConfigurationName,
})
if err != nil {
autoscalingerr, ok := err.(awserr.Error)
if ok && (autoscalingerr.Code() == "InvalidConfiguration.NotFound" || autoscalingerr.Code() == "ValidationError") {
log.Printf("[DEBUG] Launch configuration (%s) not found", *lc.LaunchConfigurationName)
return nil
}
return err
}
}
return nil
}
func TestAccAWSLaunchConfiguration_basic(t *testing.T) {
var conf autoscaling.LaunchConfiguration
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchConfigurationNoNameConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
testAccCheckAWSLaunchConfigurationGeneratedNamePrefix(
"aws_launch_configuration.bar", "terraform-"),
),
},
{
Config: testAccAWSLaunchConfigurationPrefixNameConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.baz", &conf),
testAccCheckAWSLaunchConfigurationGeneratedNamePrefix(
"aws_launch_configuration.baz", "baz-"),
),
},
},
})
}
func TestAccAWSLaunchConfiguration_withBlockDevices(t *testing.T) {
var conf autoscaling.LaunchConfiguration
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchConfigurationConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
testAccCheckAWSLaunchConfigurationAttributes(&conf),
resource.TestCheckResourceAttr(
"aws_launch_configuration.bar", "image_id", "ami-21f78e11"),
resource.TestCheckResourceAttr(
"aws_launch_configuration.bar", "instance_type", "m1.small"),
resource.TestCheckResourceAttr(
"aws_launch_configuration.bar", "associate_public_ip_address", "true"),
resource.TestCheckResourceAttr(
"aws_launch_configuration.bar", "spot_price", ""),
),
},
},
})
}
func TestAccAWSLaunchConfiguration_updateRootBlockDevice(t *testing.T) {
var conf autoscaling.LaunchConfiguration
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchConfigurationConfigWithRootBlockDevice(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
resource.TestCheckResourceAttr(
"aws_launch_configuration.bar", "root_block_device.0.volume_size", "11"),
),
},
{
Config: testAccAWSLaunchConfigurationConfigWithRootBlockDeviceUpdated(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
resource.TestCheckResourceAttr(
"aws_launch_configuration.bar", "root_block_device.0.volume_size", "20"),
),
},
},
})
}
func TestAccAWSLaunchConfiguration_withSpotPrice(t *testing.T) {
var conf autoscaling.LaunchConfiguration
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchConfigurationWithSpotPriceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
resource.TestCheckResourceAttr(
"aws_launch_configuration.bar", "spot_price", "0.01"),
),
},
},
})
}
func TestAccAWSLaunchConfiguration_withVpcClassicLink(t *testing.T) {
var vpc ec2.Vpc
var group ec2.SecurityGroup
var conf autoscaling.LaunchConfiguration
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchConfigurationConfig_withVpcClassicLink,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.foo", &conf),
testAccCheckVpcExists("aws_vpc.foo", &vpc),
testAccCheckAWSSecurityGroupExists("aws_security_group.foo", &group),
),
},
},
})
}
func TestAccAWSLaunchConfiguration_withIAMProfile(t *testing.T) {
var conf autoscaling.LaunchConfiguration
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchConfigurationConfig_withIAMProfile,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
),
},
},
})
}
2016-01-06 08:36:39 +01:00
func testAccCheckAWSLaunchConfigurationWithEncryption(conf *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Map out the block devices by name, which should be unique.
blockDevices := make(map[string]*autoscaling.BlockDeviceMapping)
for _, blockDevice := range conf.BlockDeviceMappings {
blockDevices[*blockDevice.DeviceName] = blockDevice
}
// Check if the root block device exists.
if _, ok := blockDevices["/dev/sda1"]; !ok {
return fmt.Errorf("block device doesn't exist: /dev/sda1")
} else if blockDevices["/dev/sda1"].Ebs.Encrypted != nil {
return fmt.Errorf("root device should not include value for Encrypted")
}
// Check if the secondary block device exists.
if _, ok := blockDevices["/dev/sdb"]; !ok {
return fmt.Errorf("block device doesn't exist: /dev/sdb")
} else if !*blockDevices["/dev/sdb"].Ebs.Encrypted {
return fmt.Errorf("block device isn't encrypted as expected: /dev/sdb")
}
return nil
}
}
func TestAccAWSLaunchConfiguration_withEncryption(t *testing.T) {
var conf autoscaling.LaunchConfiguration
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
Steps: []resource.TestStep{
{
2016-01-06 08:36:39 +01:00
Config: testAccAWSLaunchConfigurationWithEncryption,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.baz", &conf),
testAccCheckAWSLaunchConfigurationWithEncryption(&conf),
),
},
},
})
}
provider/aws: ForceNew aws_launch_config on ebs_block_device change (#14899) Fixes: #14826 aws_launch_configuration ebs_block_device only had selected properties in the set hash. I removed these to allow any changes to the block device config to force a new resource ``` % make testacc TEST=./builtin/providers/aws/ TESTARGS='-run=TestAccAWSLaunchConfiguration_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/05/29 01:08:55 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws/ -v -run=TestAccAWSLaunchConfiguration_ -timeout 120m === RUN TestAccAWSLaunchConfiguration_importBasic --- PASS: TestAccAWSLaunchConfiguration_importBasic (32.89s) === RUN TestAccAWSLaunchConfiguration_basic --- PASS: TestAccAWSLaunchConfiguration_basic (44.34s) === RUN TestAccAWSLaunchConfiguration_withBlockDevices --- PASS: TestAccAWSLaunchConfiguration_withBlockDevices (28.98s) === RUN TestAccAWSLaunchConfiguration_updateRootBlockDevice --- PASS: TestAccAWSLaunchConfiguration_updateRootBlockDevice (52.23s) === RUN TestAccAWSLaunchConfiguration_withSpotPrice --- PASS: TestAccAWSLaunchConfiguration_withSpotPrice (23.04s) === RUN TestAccAWSLaunchConfiguration_withVpcClassicLink --- PASS: TestAccAWSLaunchConfiguration_withVpcClassicLink (62.30s) === RUN TestAccAWSLaunchConfiguration_withIAMProfile --- PASS: TestAccAWSLaunchConfiguration_withIAMProfile (51.62s) === RUN TestAccAWSLaunchConfiguration_withEncryption --- PASS: TestAccAWSLaunchConfiguration_withEncryption (27.91s) === RUN TestAccAWSLaunchConfiguration_updateEbsBlockDevices --- PASS: TestAccAWSLaunchConfiguration_updateEbsBlockDevices (62.98s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 386.308s ```
2017-05-30 00:16:43 +02:00
func TestAccAWSLaunchConfiguration_updateEbsBlockDevices(t *testing.T) {
var conf autoscaling.LaunchConfiguration
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchConfigurationWithEncryption,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.baz", &conf),
resource.TestCheckResourceAttr(
"aws_launch_configuration.baz", "ebs_block_device.2764618555.volume_size", "9"),
),
},
{
Config: testAccAWSLaunchConfigurationWithEncryptionUpdated,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.baz", &conf),
resource.TestCheckResourceAttr(
"aws_launch_configuration.baz", "ebs_block_device.3859927736.volume_size", "10"),
),
},
},
})
}
func testAccCheckAWSLaunchConfigurationGeneratedNamePrefix(
resource, prefix string) resource.TestCheckFunc {
return func(s *terraform.State) error {
r, ok := s.RootModule().Resources[resource]
if !ok {
return fmt.Errorf("Resource not found")
}
name, ok := r.Primary.Attributes["name"]
if !ok {
return fmt.Errorf("Name attr not found: %#v", r.Primary.Attributes)
}
if !strings.HasPrefix(name, prefix) {
return fmt.Errorf("Name: %q, does not have prefix: %q", name, prefix)
}
return nil
}
}
func testAccCheckAWSLaunchConfigurationDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
2014-09-17 02:44:42 +02:00
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_launch_configuration" {
continue
}
2014-07-14 23:59:08 +02:00
describe, err := conn.DescribeLaunchConfigurations(
&autoscaling.DescribeLaunchConfigurationsInput{
LaunchConfigurationNames: []*string{aws.String(rs.Primary.ID)},
})
if err == nil {
2014-07-14 23:59:08 +02:00
if len(describe.LaunchConfigurations) != 0 &&
*describe.LaunchConfigurations[0].LaunchConfigurationName == rs.Primary.ID {
return fmt.Errorf("Launch Configuration still exists")
}
}
// Verify the error
providerErr, ok := err.(awserr.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-21f78e11" {
return fmt.Errorf("Bad image_id: %s", *conf.ImageId)
}
if !strings.HasPrefix(*conf.LaunchConfigurationName, "terraform-") {
return fmt.Errorf("Bad name: %s", *conf.LaunchConfigurationName)
}
if *conf.InstanceType != "m1.small" {
return fmt.Errorf("Bad instance_type: %s", *conf.InstanceType)
}
// Map out the block devices by name, which should be unique.
blockDevices := make(map[string]*autoscaling.BlockDeviceMapping)
for _, blockDevice := range conf.BlockDeviceMappings {
blockDevices[*blockDevice.DeviceName] = blockDevice
}
// Check if the root block device exists.
if _, ok := blockDevices["/dev/sda1"]; !ok {
2015-12-01 16:31:20 +01:00
return fmt.Errorf("block device doesn't exist: /dev/sda1")
}
// Check if the secondary block device exists.
if _, ok := blockDevices["/dev/sdb"]; !ok {
2015-12-01 16:31:20 +01:00
return fmt.Errorf("block device doesn't exist: /dev/sdb")
}
// Check if the third block device exists.
if _, ok := blockDevices["/dev/sdc"]; !ok {
2015-12-01 16:31:20 +01:00
return fmt.Errorf("block device doesn't exist: /dev/sdc")
}
// Check if the secondary block device exists.
if _, ok := blockDevices["/dev/sdb"]; !ok {
return fmt.Errorf("block device doesn't exist: /dev/sdb")
}
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]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
2014-09-17 02:44:42 +02:00
if rs.Primary.ID == "" {
return fmt.Errorf("No Launch Configuration ID is set")
}
conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
describeOpts := autoscaling.DescribeLaunchConfigurationsInput{
LaunchConfigurationNames: []*string{aws.String(rs.Primary.ID)},
}
describe, err := conn.DescribeLaunchConfigurations(&describeOpts)
if err != nil {
return err
}
if len(describe.LaunchConfigurations) != 1 ||
*describe.LaunchConfigurations[0].LaunchConfigurationName != rs.Primary.ID {
return fmt.Errorf("Launch Configuration Group not found")
}
*res = *describe.LaunchConfigurations[0]
return nil
}
}
func testAccAWSLaunchConfigurationConfigWithRootBlockDevice(rInt int) string {
return fmt.Sprintf(`
resource "aws_launch_configuration" "bar" {
name_prefix = "tf-acc-test-%d"
image_id = "ami-21f78e11"
instance_type = "m1.small"
user_data = "foobar-user-data"
associate_public_ip_address = true
root_block_device {
volume_type = "gp2"
volume_size = 11
}
}
`, rInt)
}
func testAccAWSLaunchConfigurationConfigWithRootBlockDeviceUpdated(rInt int) string {
return fmt.Sprintf(`
resource "aws_launch_configuration" "bar" {
name_prefix = "tf-acc-test-%d"
image_id = "ami-21f78e11"
instance_type = "m1.small"
user_data = "foobar-user-data"
associate_public_ip_address = true
root_block_device {
volume_type = "gp2"
volume_size = 20
}
}
`, rInt)
}
var testAccAWSLaunchConfigurationConfig = fmt.Sprintf(`
resource "aws_launch_configuration" "bar" {
name = "terraform-test-%d"
image_id = "ami-21f78e11"
instance_type = "m1.small"
user_data = "foobar-user-data"
associate_public_ip_address = true
root_block_device {
volume_type = "gp2"
volume_size = 11
}
ebs_block_device {
device_name = "/dev/sdb"
volume_size = 9
}
ebs_block_device {
device_name = "/dev/sdc"
volume_size = 10
volume_type = "io1"
iops = 100
}
ephemeral_block_device {
device_name = "/dev/sde"
virtual_name = "ephemeral0"
}
}
`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
var testAccAWSLaunchConfigurationWithSpotPriceConfig = fmt.Sprintf(`
resource "aws_launch_configuration" "bar" {
name = "terraform-test-%d"
image_id = "ami-21f78e11"
instance_type = "t1.micro"
spot_price = "0.01"
}
`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
const testAccAWSLaunchConfigurationNoNameConfig = `
resource "aws_launch_configuration" "bar" {
image_id = "ami-21f78e11"
instance_type = "t1.micro"
user_data = "foobar-user-data-change"
associate_public_ip_address = false
}
`
const testAccAWSLaunchConfigurationPrefixNameConfig = `
resource "aws_launch_configuration" "baz" {
name_prefix = "baz-"
image_id = "ami-21f78e11"
instance_type = "t1.micro"
user_data = "foobar-user-data-change"
associate_public_ip_address = false
}
`
2016-01-06 08:36:39 +01:00
const testAccAWSLaunchConfigurationWithEncryption = `
resource "aws_launch_configuration" "baz" {
image_id = "ami-5189a661"
instance_type = "t2.micro"
associate_public_ip_address = false
root_block_device {
volume_type = "gp2"
volume_size = 11
}
ebs_block_device {
device_name = "/dev/sdb"
volume_size = 9
encrypted = true
}
}
`
provider/aws: ForceNew aws_launch_config on ebs_block_device change (#14899) Fixes: #14826 aws_launch_configuration ebs_block_device only had selected properties in the set hash. I removed these to allow any changes to the block device config to force a new resource ``` % make testacc TEST=./builtin/providers/aws/ TESTARGS='-run=TestAccAWSLaunchConfiguration_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/05/29 01:08:55 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws/ -v -run=TestAccAWSLaunchConfiguration_ -timeout 120m === RUN TestAccAWSLaunchConfiguration_importBasic --- PASS: TestAccAWSLaunchConfiguration_importBasic (32.89s) === RUN TestAccAWSLaunchConfiguration_basic --- PASS: TestAccAWSLaunchConfiguration_basic (44.34s) === RUN TestAccAWSLaunchConfiguration_withBlockDevices --- PASS: TestAccAWSLaunchConfiguration_withBlockDevices (28.98s) === RUN TestAccAWSLaunchConfiguration_updateRootBlockDevice --- PASS: TestAccAWSLaunchConfiguration_updateRootBlockDevice (52.23s) === RUN TestAccAWSLaunchConfiguration_withSpotPrice --- PASS: TestAccAWSLaunchConfiguration_withSpotPrice (23.04s) === RUN TestAccAWSLaunchConfiguration_withVpcClassicLink --- PASS: TestAccAWSLaunchConfiguration_withVpcClassicLink (62.30s) === RUN TestAccAWSLaunchConfiguration_withIAMProfile --- PASS: TestAccAWSLaunchConfiguration_withIAMProfile (51.62s) === RUN TestAccAWSLaunchConfiguration_withEncryption --- PASS: TestAccAWSLaunchConfiguration_withEncryption (27.91s) === RUN TestAccAWSLaunchConfiguration_updateEbsBlockDevices --- PASS: TestAccAWSLaunchConfiguration_updateEbsBlockDevices (62.98s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 386.308s ```
2017-05-30 00:16:43 +02:00
const testAccAWSLaunchConfigurationWithEncryptionUpdated = `
resource "aws_launch_configuration" "baz" {
image_id = "ami-5189a661"
instance_type = "t2.micro"
associate_public_ip_address = false
root_block_device {
volume_type = "gp2"
volume_size = 11
}
ebs_block_device {
device_name = "/dev/sdb"
volume_size = 10
encrypted = true
}
}
`
const testAccAWSLaunchConfigurationConfig_withVpcClassicLink = `
resource "aws_vpc" "foo" {
cidr_block = "10.0.0.0/16"
enable_classiclink = true
tags {
Name = "testAccAWSLaunchConfigurationConfig_withVpcClassicLink"
}
}
resource "aws_security_group" "foo" {
name = "foo"
vpc_id = "${aws_vpc.foo.id}"
}
resource "aws_launch_configuration" "foo" {
name = "TestAccAWSLaunchConfiguration_withVpcClassicLink"
image_id = "ami-21f78e11"
instance_type = "t1.micro"
vpc_classic_link_id = "${aws_vpc.foo.id}"
vpc_classic_link_security_groups = ["${aws_security_group.foo.id}"]
}
`
const testAccAWSLaunchConfigurationConfig_withIAMProfile = `
resource "aws_iam_role" "role" {
name = "TestAccAWSLaunchConfiguration-withIAMProfile"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_instance_profile" "profile" {
name = "TestAccAWSLaunchConfiguration-withIAMProfile"
roles = ["${aws_iam_role.role.name}"]
}
resource "aws_launch_configuration" "bar" {
image_id = "ami-5189a661"
instance_type = "t2.nano"
iam_instance_profile = "${aws_iam_instance_profile.profile.name}"
}
`