Skip to content

Commit

Permalink
Merge pull request #10521 from tomelliff/error-when-launch-template-n…
Browse files Browse the repository at this point in the history
…ot-found-by-data-source

Error when data.aws_launch_template cannot find launch template
  • Loading branch information
anGie44 authored Jul 16, 2020
2 parents 614a260 + 63cf56f commit a89521b
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions aws/data_source_aws_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,33 +399,27 @@ func dataSourceAwsLaunchTemplateRead(d *schema.ResourceData, meta interface{}) e

dlt, err := conn.DescribeLaunchTemplates(params)

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 isAWSErr(err, "InvalidLaunchTemplateId.NotFound", "") ||
isAWSErr(err, "InvalidLaunchTemplateName.NotFoundException", "") {
return fmt.Errorf("Launch Template not found")
}
return fmt.Errorf("Error getting launch template: %w", 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
return fmt.Errorf("error reading launch template: empty output")
}

log.Printf("[DEBUG] Found launch template %s", d.Id())
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)
Expand All @@ -451,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
Expand Down

0 comments on commit a89521b

Please sign in to comment.