Skip to content

Commit

Permalink
Merge #9033: Fix crash in aws_ami data source with name_regex
Browse files Browse the repository at this point in the history
  • Loading branch information
apparentlymart committed Sep 24, 2016
2 parents 3a3a424 + 5af8c80 commit 7d2b51e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ BUG FIXES:
* provider/aws: Ensure that origin_access_identity is a required value within the CloudFront distribution s3_config block [GH-8777]
* provider/aws: Corrected Seoul S3 Website Endpoint format [GH-9032]
* provider/aws: Fix failed remove S3 lifecycle_rule [GH-9031]
* provider/aws: Fix crashing bug in `aws_ami` data source when using `name_regex` [GH-9033]


## 0.7.4 (September 19, 2016)
Expand Down
35 changes: 24 additions & 11 deletions builtin/providers/aws/data_source_aws_ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,19 @@ func dataSourceAwsAmiRead(d *schema.ResourceData, meta interface{}) error {
}

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

var image *ec2.Image
if len(filteredImages) < 1 {
return fmt.Errorf("Your query returned no results. Please change your filters and try again.")
} else if len(filteredImages) > 1 {
if (d.Get("most_recent").(bool)) == true {
log.Printf("[DEBUG] aws_ami - multiple results found and most_recent is set")
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
}

if len(filteredImages) > 1 {
recent := d.Get("most_recent").(bool)
log.Printf("[DEBUG] aws_ami - multiple results found and `most_recent` is set to: %t", recent)
if recent {
image = mostRecentAmi(filteredImages)
} else {
log.Printf("[DEBUG] aws_ami - multiple results found and most_recent not set")
return fmt.Errorf("Your query returned more than one result. Please try a more specific search, or set most_recent to true.")
return fmt.Errorf("Your query returned more than one result. Please try a more " +
"specific search criteria, or set `most_recent` attribute to true.")
}
} else {
log.Printf("[DEBUG] aws_ami - Single AMI found: %s", *filteredImages[0].ImageId)
image = filteredImages[0]
}

image = filteredImages[0]
log.Printf("[DEBUG] aws_ami - Single AMI found: %s", *image.ImageId)

return amiDescriptionAttributes(d, image)
}

Expand Down

0 comments on commit 7d2b51e

Please sign in to comment.