Skip to content

Commit 5af8c80

Browse files
committed
Fix. Handle missing AMI name when matching against image name.
This commit fixes the issues where in a very rare cases the Amazon Machine Image (AMI) would not have an image name set causing regular expression match to fail with a nil pointer dereference. Also, the logic of if-else statements was simplified (reduced branching since return is used a lot). Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
1 parent 3a3a424 commit 5af8c80

1 file changed

Lines changed: 24 additions & 11 deletions

File tree

‎builtin/providers/aws/data_source_aws_ami.go‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,19 @@ func dataSourceAwsAmiRead(d *schema.ResourceData, meta interface{}) error {
236236
}
237237

238238
var filteredImages []*ec2.Image
239-
if nameRegexOk == true {
239+
if nameRegexOk {
240240
r := regexp.MustCompile(nameRegex.(string))
241241
for _, image := range resp.Images {
242-
if r.MatchString(*image.Name) == true {
242+
// Check for a very rare case where the response would include no
243+
// image name. No name means nothing to attempt a match against,
244+
// therefore we are skipping such image.
245+
if image.Name == nil || *image.Name == "" {
246+
log.Printf("[WARN] Unable to find AMI name to match against "+
247+
"for image ID %q owned by %q, nothing to do.",
248+
*image.ImageId, *image.OwnerId)
249+
continue
250+
}
251+
if r.MatchString(*image.Name) {
243252
filteredImages = append(filteredImages, image)
244253
}
245254
}
@@ -249,19 +258,23 @@ func dataSourceAwsAmiRead(d *schema.ResourceData, meta interface{}) error {
249258

250259
var image *ec2.Image
251260
if len(filteredImages) < 1 {
252-
return fmt.Errorf("Your query returned no results. Please change your filters and try again.")
253-
} else if len(filteredImages) > 1 {
254-
if (d.Get("most_recent").(bool)) == true {
255-
log.Printf("[DEBUG] aws_ami - multiple results found and most_recent is set")
261+
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
262+
}
263+
264+
if len(filteredImages) > 1 {
265+
recent := d.Get("most_recent").(bool)
266+
log.Printf("[DEBUG] aws_ami - multiple results found and `most_recent` is set to: %t", recent)
267+
if recent {
256268
image = mostRecentAmi(filteredImages)
257269
} else {
258-
log.Printf("[DEBUG] aws_ami - multiple results found and most_recent not set")
259-
return fmt.Errorf("Your query returned more than one result. Please try a more specific search, or set most_recent to true.")
270+
return fmt.Errorf("Your query returned more than one result. Please try a more " +
271+
"specific search criteria, or set `most_recent` attribute to true.")
260272
}
261-
} else {
262-
log.Printf("[DEBUG] aws_ami - Single AMI found: %s", *filteredImages[0].ImageId)
263-
image = filteredImages[0]
264273
}
274+
275+
image = filteredImages[0]
276+
log.Printf("[DEBUG] aws_ami - Single AMI found: %s", *image.ImageId)
277+
265278
return amiDescriptionAttributes(d, image)
266279
}
267280

0 commit comments

Comments
 (0)