From 1797c1e00c93fef298fda99bcbeb469693ced914 Mon Sep 17 00:00:00 2001 From: Tom Elliff Date: Wed, 16 Oct 2019 16:21:55 +0100 Subject: [PATCH 1/2] Error when data.aws_launch_template cannot find launch template Spotted when raising https://github.com/terraform-providers/terraform-provider-aws/issues/10071. --- aws/data_source_aws_launch_template.go | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/aws/data_source_aws_launch_template.go b/aws/data_source_aws_launch_template.go index a632e26ef32..6e607a0a2b3 100644 --- a/aws/data_source_aws_launch_template.go +++ b/aws/data_source_aws_launch_template.go @@ -349,29 +349,10 @@ func dataSourceAwsLaunchTemplateRead(d *schema.ResourceData, meta interface{}) e LaunchTemplateNames: []*string{aws.String(d.Get("name").(string))}, }) - if isAWSErr(err, ec2.LaunchTemplateErrorCodeLaunchTemplateIdDoesNotExist, "") { - log.Printf("[WARN] launch template (%s) not found - removing from state", d.Id()) - d.SetId("") - return nil - } - - // AWS SDK constant above is currently incorrect - if isAWSErr(err, "InvalidLaunchTemplateId.NotFound", "") { - log.Printf("[WARN] launch template (%s) not found - removing from state", d.Id()) - d.SetId("") - return nil - } - if err != nil { return fmt.Errorf("Error getting launch template: %s", err) } - if dlt == nil || len(dlt.LaunchTemplates) == 0 { - log.Printf("[WARN] launch template (%s) not found - removing from state", d.Id()) - d.SetId("") - return nil - } - log.Printf("[DEBUG] Found launch template %s", d.Id()) lt := dlt.LaunchTemplates[0] From 63cf56f98e149bb05b4341a7d88953ad7a14b463 Mon Sep 17 00:00:00 2001 From: Angie Pinilla Date: Thu, 16 Jul 2020 16:12:16 -0400 Subject: [PATCH 2/2] adjust error messaging --- aws/data_source_aws_launch_template.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/aws/data_source_aws_launch_template.go b/aws/data_source_aws_launch_template.go index b1b4124b1a8..a8cc54c2d74 100644 --- a/aws/data_source_aws_launch_template.go +++ b/aws/data_source_aws_launch_template.go @@ -400,13 +400,26 @@ func dataSourceAwsLaunchTemplateRead(d *schema.ResourceData, meta interface{}) e dlt, err := conn.DescribeLaunchTemplates(params) if err != nil { - return fmt.Errorf("Error getting launch template: %s", err) + if isAWSErr(err, "InvalidLaunchTemplateId.NotFound", "") || + isAWSErr(err, "InvalidLaunchTemplateName.NotFoundException", "") { + return fmt.Errorf("Launch Template not found") + } + return fmt.Errorf("Error getting launch template: %w", err) } - log.Printf("[DEBUG] Found launch template %s", d.Id()) + if dlt == nil || len(dlt.LaunchTemplates) == 0 { + return fmt.Errorf("error reading launch template: empty output") + } + + if len(dlt.LaunchTemplates) > 1 { + return fmt.Errorf("Search returned %d result(s), please revise so only one is returned", len(dlt.LaunchTemplates)) + } lt := dlt.LaunchTemplates[0] d.SetId(*lt.LaunchTemplateId) + + log.Printf("[DEBUG] Found launch template %s", d.Id()) + d.Set("name", lt.LaunchTemplateName) d.Set("latest_version", lt.LatestVersionNumber) d.Set("default_version", lt.DefaultVersionNumber) @@ -432,6 +445,10 @@ func dataSourceAwsLaunchTemplateRead(d *schema.ResourceData, meta interface{}) e return 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) ltData := dltv.LaunchTemplateVersions[0].LaunchTemplateData