Skip to content

Commit

Permalink
Merge pull request #14004 from terraform-providers/td-singular-data-s…
Browse files Browse the repository at this point in the history
…ource-launch-template

data-source/launch_template: add non-existent acctest
  • Loading branch information
anGie44 authored Jul 17, 2020
2 parents a89521b + b354b8d commit 594a1e1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
41 changes: 22 additions & 19 deletions aws/data_source_aws_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,14 @@ func dataSourceAwsLaunchTemplateRead(d *schema.ResourceData, meta interface{}) e
}

lt := dlt.LaunchTemplates[0]
d.SetId(*lt.LaunchTemplateId)

log.Printf("[DEBUG] Found launch template %s", d.Id())
log.Printf("[DEBUG] Found launch template %s", aws.StringValue(lt.LaunchTemplateId))

d.SetId(aws.StringValue(lt.LaunchTemplateId))
d.Set("name", lt.LaunchTemplateName)
d.Set("latest_version", lt.LatestVersionNumber)
d.Set("default_version", lt.DefaultVersionNumber)
if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(lt.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
return fmt.Errorf("error setting tags: %w", err)
}

arn := arn.ARN{
Expand All @@ -442,14 +441,14 @@ func dataSourceAwsLaunchTemplateRead(d *schema.ResourceData, meta interface{}) e
Versions: []*string{aws.String(version)},
})
if err != nil {
return err
return fmt.Errorf("error reading launch template version (%s) for launch template (%s): %w", version, d.Id(), err)
}

if dltv == nil || len(dltv.LaunchTemplateVersions) == 0 {
return fmt.Errorf("error reading launch template version (%s) for launch template (%s): empty output", version, d.Id())
}

log.Printf("[DEBUG] Received launch template version %q (version %d)", d.Id(), *lt.LatestVersionNumber)
log.Printf("[DEBUG] Received launch template version %q (version %d)", d.Id(), aws.Int64Value(lt.LatestVersionNumber))

ltData := dltv.LaunchTemplateVersions[0].LaunchTemplateData

Expand All @@ -460,59 +459,63 @@ func dataSourceAwsLaunchTemplateRead(d *schema.ResourceData, meta interface{}) e
d.Set("kernel_id", ltData.KernelId)
d.Set("key_name", ltData.KeyName)
d.Set("ram_disk_id", ltData.RamDiskId)
d.Set("security_group_names", aws.StringValueSlice(ltData.SecurityGroups))
if err := d.Set("security_group_names", aws.StringValueSlice(ltData.SecurityGroups)); err != nil {
return fmt.Errorf("error setting security_group_names: %w", err)
}
d.Set("user_data", ltData.UserData)
d.Set("vpc_security_group_ids", aws.StringValueSlice(ltData.SecurityGroupIds))
if err := d.Set("vpc_security_group_ids", aws.StringValueSlice(ltData.SecurityGroupIds)); err != nil {
return fmt.Errorf("error setting vpc_security_group_ids: %w", err)
}
d.Set("ebs_optimized", "")

if ltData.EbsOptimized != nil {
d.Set("ebs_optimized", strconv.FormatBool(aws.BoolValue(ltData.EbsOptimized)))
}

if err := d.Set("block_device_mappings", getBlockDeviceMappings(ltData.BlockDeviceMappings)); err != nil {
return fmt.Errorf("error setting block_device_mappings: %s", err)
return fmt.Errorf("error setting block_device_mappings: %w", err)
}

if strings.HasPrefix(aws.StringValue(ltData.InstanceType), "t2") || strings.HasPrefix(aws.StringValue(ltData.InstanceType), "t3") {
if err := d.Set("credit_specification", getCreditSpecification(ltData.CreditSpecification)); err != nil {
return fmt.Errorf("error setting credit_specification: %s", err)
return fmt.Errorf("error setting credit_specification: %w", err)
}
}

if err := d.Set("elastic_gpu_specifications", getElasticGpuSpecifications(ltData.ElasticGpuSpecifications)); err != nil {
return fmt.Errorf("error setting elastic_gpu_specifications: %s", err)
return fmt.Errorf("error setting elastic_gpu_specifications: %w", err)
}

if err := d.Set("iam_instance_profile", getIamInstanceProfile(ltData.IamInstanceProfile)); err != nil {
return fmt.Errorf("error setting iam_instance_profile: %s", err)
return fmt.Errorf("error setting iam_instance_profile: %w", err)
}

if err := d.Set("instance_market_options", getInstanceMarketOptions(ltData.InstanceMarketOptions)); err != nil {
return fmt.Errorf("error setting instance_market_options: %s", err)
return fmt.Errorf("error setting instance_market_options: %w", err)
}

if err := d.Set("metadata_options", flattenLaunchTemplateInstanceMetadataOptions(ltData.MetadataOptions)); err != nil {
return fmt.Errorf("error setting metadata_options: %s", err)
return fmt.Errorf("error setting metadata_options: %w", err)
}

if err := d.Set("monitoring", getMonitoring(ltData.Monitoring)); err != nil {
return fmt.Errorf("error setting monitoring: %s", err)
return fmt.Errorf("error setting monitoring: %w", err)
}

if err := d.Set("network_interfaces", getNetworkInterfaces(ltData.NetworkInterfaces)); err != nil {
return fmt.Errorf("error setting network_interfaces: %s", err)
return fmt.Errorf("error setting network_interfaces: %w", err)
}

if err := d.Set("placement", getPlacement(ltData.Placement)); err != nil {
return fmt.Errorf("error setting placement: %s", err)
return fmt.Errorf("error setting placement: %w", err)
}

if err := d.Set("hibernation_options", flattenLaunchTemplateHibernationOptions(ltData.HibernationOptions)); err != nil {
return fmt.Errorf("error setting hibernation_options: %s", err)
return fmt.Errorf("error setting hibernation_options: %w", err)
}

if err := d.Set("tag_specifications", getTagSpecifications(ltData.TagSpecifications)); err != nil {
return fmt.Errorf("error setting tag_specifications: %s", err)
return fmt.Errorf("error setting tag_specifications: %w", err)
}

return nil
Expand Down
22 changes: 22 additions & 0 deletions aws/data_source_aws_launch_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
Expand Down Expand Up @@ -159,6 +160,21 @@ func TestAccAWSLaunchTemplateDataSource_associatePublicIPAddress(t *testing.T) {
})
}

func TestAccAWSLaunchTemplateDataSource_NonExistent(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchTemplateDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchTemplateDataSourceConfig_NonExistent,
ExpectError: regexp.MustCompile(`not found`),
},
},
})
}

func testAccAWSLaunchTemplateDataSourceConfig_Basic(rName string) string {
return fmt.Sprintf(`
resource "aws_launch_template" "test" {
Expand Down Expand Up @@ -238,3 +254,9 @@ data "aws_launch_template" "test" {
}
`, rName, associatePublicIPAddress)
}

const testAccAWSLaunchTemplateDataSourceConfig_NonExistent = `
data "aws_launch_template" "test" {
name = "tf-acc-test-nonexistent"
}
`

0 comments on commit 594a1e1

Please sign in to comment.