From c20ee2f71f54a31269536402b6fb7875174f7e01 Mon Sep 17 00:00:00 2001 From: Ryn Daniels Date: Thu, 19 Sep 2019 11:46:58 +0200 Subject: [PATCH 1/5] Only set associate_public_ip_address if it's explicitly set --- aws/resource_aws_ec2_fleet_test.go | 180 ++++++++++++++++++++++++++++ aws/resource_aws_launch_template.go | 7 +- 2 files changed, 186 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_ec2_fleet_test.go b/aws/resource_aws_ec2_fleet_test.go index b3a44fb3d5d..e95f8da1b2b 100644 --- a/aws/resource_aws_ec2_fleet_test.go +++ b/aws/resource_aws_ec2_fleet_test.go @@ -4,9 +4,12 @@ import ( "errors" "fmt" "strconv" + //"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/ec2" "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" @@ -986,6 +989,183 @@ func TestAccAWSEc2Fleet_Type(t *testing.T) { }) } +// Test for the bug described in https://github.com/terraform-providers/terraform-provider-aws/issues/6777 +// For various reasons that don't seem trivially fixable, this test infrastructure doesn't get destroyed cleanly +// when the testAccCheckAWSEc2FleetHistory check passes. Leaving it commented out for now. +/* +func TestAccAWSEc2Fleet_TemplateMultipleNetworkInterfaces(t *testing.T) { + var fleet1 ec2.FleetData + resourceName := "aws_ec2_fleet.test" + rInt := acctest.RandInt() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSEc2Fleet(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSEc2FleetDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSEc2FleetConfig_multipleNetworkInterfaces(rInt), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSEc2FleetExists(resourceName, &fleet1), + resource.TestCheckResourceAttr(resourceName, "type", "maintain"), + testAccCheckAWSEc2FleetHistory(resourceName, "The associatePublicIPAddress parameter cannot be specified when launching with multiple network interfaces"), + ), + }, + }, + }) +} + +func testAccAWSEc2FleetConfig_multipleNetworkInterfaces(rInt int) string { + return fmt.Sprintf(` +data "aws_ami" "test" { + most_recent = true + + filter { + name = "name" + values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"] + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + + owners = ["099720109477"] # Canonical +} + +resource "aws_vpc" "test" { + cidr_block = "10.1.0.0/16" +} + +resource "aws_internet_gateway" "test" { + vpc_id = "${aws_vpc.test.id}" +} + +resource "aws_subnet" "test" { + cidr_block = "10.1.0.0/24" + vpc_id = "${aws_vpc.test.id}" +} + +resource "aws_security_group" "test" { + name = "security-group-%d" + description = "Testacc SSH security group" + vpc_id = "${aws_vpc.test.id}" + + ingress { + protocol = "tcp" + from_port = 22 + to_port = 22 + cidr_blocks = ["0.0.0.0/0"] + } + egress { + protocol = "-1" + from_port = 0 + to_port = 0 + cidr_blocks = ["0.0.0.0/0"] + } +} + +resource "aws_network_interface" "test" { + subnet_id = "${aws_subnet.test.id}" + security_groups = ["${aws_security_group.test.id}"] +} + +resource "aws_launch_template" "test" { + name = "testacc-lt-%d" + image_id = "${data.aws_ami.test.id}" + + instance_market_options { + spot_options { + spot_instance_type = "persistent" + } + market_type="spot" + } + + network_interfaces { + device_index = 0 + network_interface_id = "${aws_network_interface.test.id}" + } + network_interfaces { + device_index = 1 + subnet_id = "${aws_subnet.test.id}" + } + +} + +resource "aws_ec2_fleet" "test" { + launch_template_config { + launch_template_specification { + launch_template_id = "${aws_launch_template.test.id}" + version = "${aws_launch_template.test.latest_version}" + } + # allow to choose from several instance types if there is no spot capacity for some type + override { + instance_type = "t2.micro" + } + override { + instance_type = "t3.micro" + } + override { + instance_type = "t3.small" + } + } + + target_capacity_specification { + default_target_capacity_type = "spot" + total_target_capacity = 1 + } +} +`, rInt, rInt) +} + +func testAccCheckAWSEc2FleetHistory(resourceName string, errorMsg string) resource.TestCheckFunc { + return func(s *terraform.State) error { + time.Sleep(time.Minute * 2) // We have to wait a bit for the history to get populated. + + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("Not found: %s", resourceName) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No EC2 Fleet ID is set") + } + + conn := testAccProvider.Meta().(*AWSClient).ec2conn + + input := &ec2.DescribeFleetHistoryInput{ + FleetId: aws.String(rs.Primary.ID), + StartTime: aws.Time(time.Now().Add(time.Hour * -2)), + } + + output, err := conn.DescribeFleetHistory(input) + + if err != nil { + return err + } + + if output == nil { + return fmt.Errorf("EC2 Fleet history not found") + } + + if output.HistoryRecords == nil { + return fmt.Errorf("No fleet history records found for fleet %s", rs.Primary.ID) + } + + for _, record := range output.HistoryRecords { + if record == nil { + continue + } + if strings.Contains(aws.StringValue(record.EventInformation.EventDescription), errorMsg) { + return fmt.Errorf("Error %s found in fleet history event", errorMsg) + } + } + + return nil + } +} +*/ + func testAccCheckAWSEc2FleetExists(resourceName string, fleet *ec2.FleetData) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName] diff --git a/aws/resource_aws_launch_template.go b/aws/resource_aws_launch_template.go index 0d91c0d9488..e39821bbccf 100644 --- a/aws/resource_aws_launch_template.go +++ b/aws/resource_aws_launch_template.go @@ -1277,7 +1277,12 @@ func readNetworkInterfacesFromConfig(ni map[string]interface{}) *ec2.LaunchTempl if v, ok := ni["network_interface_id"].(string); ok && v != "" { networkInterface.NetworkInterfaceId = aws.String(v) } else if v, ok := ni["associate_public_ip_address"]; ok { - networkInterface.AssociatePublicIpAddress = aws.Bool(v.(bool)) + if ni["associate_public_ip_address"] == true { + // If there are multiple network interfaces, having this be explicitly set, even to false, will cause problems + // so don't set it unless we actually need to + networkInterface.AssociatePublicIpAddress = aws.Bool(v.(bool)) + } + } if v, ok := ni["private_ip_address"].(string); ok && v != "" { From 690dc991b49d3328583314c9a486a8419fe836b1 Mon Sep 17 00:00:00 2001 From: Ryn Daniels Date: Thu, 21 Nov 2019 13:07:01 +0100 Subject: [PATCH 2/5] Use TypeString instead of TypeBool, add more tests and fix others --- aws/resource_aws_ec2_fleet_test.go | 16 ++--- aws/resource_aws_launch_template.go | 48 ++++++++------- aws/resource_aws_launch_template_test.go | 76 +++++++++++++++++++++++- 3 files changed, 111 insertions(+), 29 deletions(-) diff --git a/aws/resource_aws_ec2_fleet_test.go b/aws/resource_aws_ec2_fleet_test.go index e95f8da1b2b..403f77e51f5 100644 --- a/aws/resource_aws_ec2_fleet_test.go +++ b/aws/resource_aws_ec2_fleet_test.go @@ -4,9 +4,9 @@ import ( "errors" "fmt" "strconv" - //"strings" + "strings" "testing" - //"time" + "time" "github.com/aws/aws-sdk-go/aws" //"github.com/aws/aws-sdk-go/aws/awserr" @@ -990,9 +990,6 @@ func TestAccAWSEc2Fleet_Type(t *testing.T) { } // Test for the bug described in https://github.com/terraform-providers/terraform-provider-aws/issues/6777 -// For various reasons that don't seem trivially fixable, this test infrastructure doesn't get destroyed cleanly -// when the testAccCheckAWSEc2FleetHistory check passes. Leaving it commented out for now. -/* func TestAccAWSEc2Fleet_TemplateMultipleNetworkInterfaces(t *testing.T) { var fleet1 ec2.FleetData resourceName := "aws_ec2_fleet.test" @@ -1083,20 +1080,24 @@ resource "aws_launch_template" "test" { network_interfaces { device_index = 0 + delete_on_termination = true network_interface_id = "${aws_network_interface.test.id}" } network_interfaces { device_index = 1 + delete_on_termination = true subnet_id = "${aws_subnet.test.id}" } } resource "aws_ec2_fleet" "test" { + terminate_instances = true + launch_template_config { launch_template_specification { - launch_template_id = "${aws_launch_template.test.id}" - version = "${aws_launch_template.test.latest_version}" + launch_template_id = "${aws_launch_template.test.id}" + version = "${aws_launch_template.test.latest_version}" } # allow to choose from several instance types if there is no spot capacity for some type override { @@ -1164,7 +1165,6 @@ func testAccCheckAWSEc2FleetHistory(resourceName string, errorMsg string) resour return nil } } -*/ func testAccCheckAWSEc2FleetExists(resourceName string, fleet *ec2.FleetData) resource.TestCheckFunc { return func(s *terraform.State) error { diff --git a/aws/resource_aws_launch_template.go b/aws/resource_aws_launch_template.go index e39821bbccf..c4b7248328b 100644 --- a/aws/resource_aws_launch_template.go +++ b/aws/resource_aws_launch_template.go @@ -357,8 +357,10 @@ func resourceAwsLaunchTemplate() *schema.Resource { Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "associate_public_ip_address": { - Type: schema.TypeBool, - Optional: true, + Type: schema.TypeString, + Optional: true, + DiffSuppressFunc: suppressEquivalentTypeStringBoolean, + ValidateFunc: validateTypeStringNullableBoolean, }, "delete_on_termination": { Type: schema.TypeBool, @@ -921,15 +923,17 @@ func getNetworkInterfaces(n []*ec2.LaunchTemplateInstanceNetworkInterfaceSpecifi var ipv4Addresses []string networkInterface := map[string]interface{}{ - "associate_public_ip_address": aws.BoolValue(v.AssociatePublicIpAddress), - "delete_on_termination": aws.BoolValue(v.DeleteOnTermination), - "description": aws.StringValue(v.Description), - "device_index": aws.Int64Value(v.DeviceIndex), - "ipv4_address_count": aws.Int64Value(v.SecondaryPrivateIpAddressCount), - "ipv6_address_count": aws.Int64Value(v.Ipv6AddressCount), - "network_interface_id": aws.StringValue(v.NetworkInterfaceId), - "private_ip_address": aws.StringValue(v.PrivateIpAddress), - "subnet_id": aws.StringValue(v.SubnetId), + "delete_on_termination": aws.BoolValue(v.DeleteOnTermination), + "description": aws.StringValue(v.Description), + "device_index": aws.Int64Value(v.DeviceIndex), + "ipv4_address_count": aws.Int64Value(v.SecondaryPrivateIpAddressCount), + "ipv6_address_count": aws.Int64Value(v.Ipv6AddressCount), + "network_interface_id": aws.StringValue(v.NetworkInterfaceId), + "private_ip_address": aws.StringValue(v.PrivateIpAddress), + "subnet_id": aws.StringValue(v.SubnetId), + } + if v.AssociatePublicIpAddress != nil { + networkInterface["associate_public_ip_address"] = strconv.FormatBool(aws.BoolValue(v.AssociatePublicIpAddress)) } if len(v.Ipv6Addresses) > 0 { @@ -1149,7 +1153,10 @@ func buildLaunchTemplateData(d *schema.ResourceData) (*ec2.RequestLaunchTemplate continue } niData := ni.(map[string]interface{}) - networkInterface := readNetworkInterfacesFromConfig(niData) + networkInterface, err := readNetworkInterfacesFromConfig(niData) + if err != nil { + return nil, err + } networkInterfaces = append(networkInterfaces, networkInterface) } opts.NetworkInterfaces = networkInterfaces @@ -1256,7 +1263,7 @@ func readEbsBlockDeviceFromConfig(ebs map[string]interface{}) (*ec2.LaunchTempla return ebsDevice, nil } -func readNetworkInterfacesFromConfig(ni map[string]interface{}) *ec2.LaunchTemplateInstanceNetworkInterfaceSpecificationRequest { +func readNetworkInterfacesFromConfig(ni map[string]interface{}) (*ec2.LaunchTemplateInstanceNetworkInterfaceSpecificationRequest, error) { var ipv4Addresses []*ec2.PrivateIpAddressSpecification var ipv6Addresses []*ec2.InstanceIpv6AddressRequest var privateIpAddress string @@ -1276,13 +1283,14 @@ func readNetworkInterfacesFromConfig(ni map[string]interface{}) *ec2.LaunchTempl if v, ok := ni["network_interface_id"].(string); ok && v != "" { networkInterface.NetworkInterfaceId = aws.String(v) - } else if v, ok := ni["associate_public_ip_address"]; ok { - if ni["associate_public_ip_address"] == true { - // If there are multiple network interfaces, having this be explicitly set, even to false, will cause problems - // so don't set it unless we actually need to - networkInterface.AssociatePublicIpAddress = aws.Bool(v.(bool)) - } + } + if v, ok := ni["associate_public_ip_address"]; ok && v.(string) != "" { + vBool, err := strconv.ParseBool(v.(string)) + if err != nil { + return nil, fmt.Errorf("error converting associate_public_ip_address %q from string to boolean: %s", v.(string), err) + } + networkInterface.AssociatePublicIpAddress = aws.Bool(vBool) } if v, ok := ni["private_ip_address"].(string); ok && v != "" { @@ -1325,7 +1333,7 @@ func readNetworkInterfacesFromConfig(ni map[string]interface{}) *ec2.LaunchTempl networkInterface.PrivateIpAddresses = ipv4Addresses } - return networkInterface + return networkInterface, nil } func readIamInstanceProfileFromConfig(iip map[string]interface{}) *ec2.LaunchTemplateIamInstanceProfileSpecificationRequest { diff --git a/aws/resource_aws_launch_template_test.go b/aws/resource_aws_launch_template_test.go index a73a3d9fa12..1e8c05c8eda 100644 --- a/aws/resource_aws_launch_template_test.go +++ b/aws/resource_aws_launch_template_test.go @@ -541,7 +541,7 @@ func TestAccAWSLaunchTemplate_networkInterface(t *testing.T) { testAccCheckAWSLaunchTemplateExists(resourceName, &template), resource.TestCheckResourceAttr(resourceName, "network_interfaces.#", "1"), resource.TestCheckResourceAttrSet(resourceName, "network_interfaces.0.network_interface_id"), - resource.TestCheckResourceAttr(resourceName, "network_interfaces.0.associate_public_ip_address", "false"), + resource.TestCheckResourceAttr(resourceName, "network_interfaces.0.associate_public_ip_address", ""), resource.TestCheckResourceAttr(resourceName, "network_interfaces.0.ipv4_address_count", "2"), ), }, @@ -1077,6 +1077,80 @@ resource "aws_launch_template" "test" { } ` +const testAccAWSLaunchTemplateConfig_associatePublicIpAddressTrue = ` +resource "aws_vpc" "test" { + cidr_block = "10.1.0.0/16" +} + +resource "aws_subnet" "test" { + vpc_id = "${aws_vpc.test.id}" + cidr_block = "10.1.0.0/24" +} + +resource "aws_network_interface" "test" { + subnet_id = "${aws_subnet.test.id}" +} + +resource "aws_launch_template" "test" { + name = "network-interface-launch-template" + + network_interfaces { + network_interface_id = "${aws_network_interface.test.id}" + associate_public_ip_address = true + ipv4_address_count = 2 + } +} +` + +const testAccAWSLaunchTemplateConfig_associatePublicIpAddressFalse = ` +resource "aws_vpc" "test" { + cidr_block = "10.1.0.0/16" +} + +resource "aws_subnet" "test" { + vpc_id = "${aws_vpc.test.id}" + cidr_block = "10.1.0.0/24" +} + +resource "aws_network_interface" "test" { + subnet_id = "${aws_subnet.test.id}" +} + +resource "aws_launch_template" "test" { + name = "network-interface-launch-template" + + network_interfaces { + network_interface_id = "${aws_network_interface.test.id}" + associate_public_ip_address = false + ipv4_address_count = 2 + } +} +` + +const testAccAWSLaunchTemplateConfig_associatePublicIpAddressMissing = ` +resource "aws_vpc" "test" { + cidr_block = "10.1.0.0/16" +} + +resource "aws_subnet" "test" { + vpc_id = "${aws_vpc.test.id}" + cidr_block = "10.1.0.0/24" +} + +resource "aws_network_interface" "test" { + subnet_id = "${aws_subnet.test.id}" +} + +resource "aws_launch_template" "test" { + name = "network-interface-launch-template" + + network_interfaces { + network_interface_id = "${aws_network_interface.test.id}" + ipv4_address_count = 2 + } +} +` + const testAccAWSLaunchTemplateConfig_networkInterface_ipv6Addresses = ` resource "aws_launch_template" "test" { name = "network-interface-ipv6-addresses-launch-template" From 5080007b0d38a09929777149e6433c6568081295 Mon Sep 17 00:00:00 2001 From: Ryn Daniels Date: Thu, 21 Nov 2019 13:08:40 +0100 Subject: [PATCH 3/5] Remove commented out import --- aws/resource_aws_ec2_fleet_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/aws/resource_aws_ec2_fleet_test.go b/aws/resource_aws_ec2_fleet_test.go index 403f77e51f5..2ee8f058d69 100644 --- a/aws/resource_aws_ec2_fleet_test.go +++ b/aws/resource_aws_ec2_fleet_test.go @@ -9,7 +9,6 @@ import ( "time" "github.com/aws/aws-sdk-go/aws" - //"github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" From 52aedd10bf2a60645b992f1bf59a350270169e5d Mon Sep 17 00:00:00 2001 From: Ryn Daniels Date: Thu, 21 Nov 2019 15:00:15 +0100 Subject: [PATCH 4/5] put back the test that rebasing ate --- aws/resource_aws_launch_template_test.go | 48 ++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/aws/resource_aws_launch_template_test.go b/aws/resource_aws_launch_template_test.go index 1e8c05c8eda..7d847c6aa56 100644 --- a/aws/resource_aws_launch_template_test.go +++ b/aws/resource_aws_launch_template_test.go @@ -554,6 +554,54 @@ func TestAccAWSLaunchTemplate_networkInterface(t *testing.T) { }) } +func TestAccAWSLaunchTemplate_associatePublicIPAddress(t *testing.T) { + var template ec2.LaunchTemplate + resourceName := "aws_launch_template.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSLaunchTemplateDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLaunchTemplateConfig_associatePublicIpAddressTrue, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSLaunchTemplateExists(resourceName, &template), + resource.TestCheckResourceAttr(resourceName, "network_interfaces.#", "1"), + resource.TestCheckResourceAttrSet(resourceName, "network_interfaces.0.network_interface_id"), + resource.TestCheckResourceAttr(resourceName, "network_interfaces.0.associate_public_ip_address", "true"), + resource.TestCheckResourceAttr(resourceName, "network_interfaces.0.ipv4_address_count", "2"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSLaunchTemplateConfig_associatePublicIpAddressFalse, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSLaunchTemplateExists(resourceName, &template), + resource.TestCheckResourceAttr(resourceName, "network_interfaces.#", "1"), + resource.TestCheckResourceAttrSet(resourceName, "network_interfaces.0.network_interface_id"), + resource.TestCheckResourceAttr(resourceName, "network_interfaces.0.associate_public_ip_address", "false"), + resource.TestCheckResourceAttr(resourceName, "network_interfaces.0.ipv4_address_count", "2"), + ), + }, + { + Config: testAccAWSLaunchTemplateConfig_associatePublicIpAddressMissing, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSLaunchTemplateExists(resourceName, &template), + resource.TestCheckResourceAttr(resourceName, "network_interfaces.#", "1"), + resource.TestCheckResourceAttrSet(resourceName, "network_interfaces.0.network_interface_id"), + resource.TestCheckResourceAttr(resourceName, "network_interfaces.0.associate_public_ip_address", ""), + resource.TestCheckResourceAttr(resourceName, "network_interfaces.0.ipv4_address_count", "2"), + ), + }, + }, + }) +} + func TestAccAWSLaunchTemplate_networkInterface_ipv6Addresses(t *testing.T) { var template ec2.LaunchTemplate resourceName := "aws_launch_template.test" From f2c04a00716232a7f0b5220f32db78799aabd05e Mon Sep 17 00:00:00 2001 From: Ryn Daniels Date: Wed, 27 Nov 2019 13:55:08 +0100 Subject: [PATCH 5/5] Minor test improvements --- aws/resource_aws_launch_template_test.go | 64 ++++-------------------- 1 file changed, 9 insertions(+), 55 deletions(-) diff --git a/aws/resource_aws_launch_template_test.go b/aws/resource_aws_launch_template_test.go index 7d847c6aa56..c54bee67d35 100644 --- a/aws/resource_aws_launch_template_test.go +++ b/aws/resource_aws_launch_template_test.go @@ -556,6 +556,7 @@ func TestAccAWSLaunchTemplate_networkInterface(t *testing.T) { func TestAccAWSLaunchTemplate_associatePublicIPAddress(t *testing.T) { var template ec2.LaunchTemplate + rName := acctest.RandomWithPrefix("tf-acc-test") resourceName := "aws_launch_template.test" resource.ParallelTest(t, resource.TestCase{ @@ -564,7 +565,7 @@ func TestAccAWSLaunchTemplate_associatePublicIPAddress(t *testing.T) { CheckDestroy: testAccCheckAWSLaunchTemplateDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSLaunchTemplateConfig_associatePublicIpAddressTrue, + Config: testAccAWSLaunchTemplateConfig_associatePublicIpAddress(rName, "true"), Check: resource.ComposeTestCheckFunc( testAccCheckAWSLaunchTemplateExists(resourceName, &template), resource.TestCheckResourceAttr(resourceName, "network_interfaces.#", "1"), @@ -579,7 +580,7 @@ func TestAccAWSLaunchTemplate_associatePublicIPAddress(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccAWSLaunchTemplateConfig_associatePublicIpAddressFalse, + Config: testAccAWSLaunchTemplateConfig_associatePublicIpAddress(rName, "false"), Check: resource.ComposeTestCheckFunc( testAccCheckAWSLaunchTemplateExists(resourceName, &template), resource.TestCheckResourceAttr(resourceName, "network_interfaces.#", "1"), @@ -589,7 +590,7 @@ func TestAccAWSLaunchTemplate_associatePublicIPAddress(t *testing.T) { ), }, { - Config: testAccAWSLaunchTemplateConfig_associatePublicIpAddressMissing, + Config: testAccAWSLaunchTemplateConfig_associatePublicIpAddress(rName, "null"), Check: resource.ComposeTestCheckFunc( testAccCheckAWSLaunchTemplateExists(resourceName, &template), resource.TestCheckResourceAttr(resourceName, "network_interfaces.#", "1"), @@ -1125,32 +1126,8 @@ resource "aws_launch_template" "test" { } ` -const testAccAWSLaunchTemplateConfig_associatePublicIpAddressTrue = ` -resource "aws_vpc" "test" { - cidr_block = "10.1.0.0/16" -} - -resource "aws_subnet" "test" { - vpc_id = "${aws_vpc.test.id}" - cidr_block = "10.1.0.0/24" -} - -resource "aws_network_interface" "test" { - subnet_id = "${aws_subnet.test.id}" -} - -resource "aws_launch_template" "test" { - name = "network-interface-launch-template" - - network_interfaces { - network_interface_id = "${aws_network_interface.test.id}" - associate_public_ip_address = true - ipv4_address_count = 2 - } -} -` - -const testAccAWSLaunchTemplateConfig_associatePublicIpAddressFalse = ` +func testAccAWSLaunchTemplateConfig_associatePublicIpAddress(rName, associatePublicIPAddress string) string { + return fmt.Sprintf(` resource "aws_vpc" "test" { cidr_block = "10.1.0.0/16" } @@ -1165,40 +1142,17 @@ resource "aws_network_interface" "test" { } resource "aws_launch_template" "test" { - name = "network-interface-launch-template" + name = %[1]q network_interfaces { network_interface_id = "${aws_network_interface.test.id}" - associate_public_ip_address = false + associate_public_ip_address = %[2]s ipv4_address_count = 2 } } -` - -const testAccAWSLaunchTemplateConfig_associatePublicIpAddressMissing = ` -resource "aws_vpc" "test" { - cidr_block = "10.1.0.0/16" +`, rName, associatePublicIPAddress) } -resource "aws_subnet" "test" { - vpc_id = "${aws_vpc.test.id}" - cidr_block = "10.1.0.0/24" -} - -resource "aws_network_interface" "test" { - subnet_id = "${aws_subnet.test.id}" -} - -resource "aws_launch_template" "test" { - name = "network-interface-launch-template" - - network_interfaces { - network_interface_id = "${aws_network_interface.test.id}" - ipv4_address_count = 2 - } -} -` - const testAccAWSLaunchTemplateConfig_networkInterface_ipv6Addresses = ` resource "aws_launch_template" "test" { name = "network-interface-ipv6-addresses-launch-template"