diff --git a/builtin/providers/aws/data_source_aws_ebs_snapshot.go b/builtin/providers/aws/data_source_aws_ebs_snapshot.go index 6a7db912b..b16f61f22 100644 --- a/builtin/providers/aws/data_source_aws_ebs_snapshot.go +++ b/builtin/providers/aws/data_source_aws_ebs_snapshot.go @@ -2,6 +2,8 @@ package aws import ( "fmt" + "log" + "sort" "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/terraform/helper/schema" @@ -14,7 +16,12 @@ func dataSourceAwsEbsSnapshot() *schema.Resource { Schema: map[string]*schema.Schema{ //selection criteria "filter": dataSourceFiltersSchema(), - + "most_recent": { + Type: schema.TypeBool, + Optional: true, + Default: false, + ForceNew: true, + }, "owners": { Type: schema.TypeList, Optional: true, @@ -110,16 +117,41 @@ func dataSourceAwsEbsSnapshotRead(d *schema.ResourceData, meta interface{}) erro return err } + var snapshot *ec2.Snapshot if len(resp.Snapshots) < 1 { return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.") } if len(resp.Snapshots) > 1 { - return fmt.Errorf("Your query returned more than one result. Please try a more specific search criteria.") + recent := d.Get("most_recent").(bool) + log.Printf("[DEBUG] aws_ebs_snapshot - multiple results found and `most_recent` is set to: %t", recent) + if recent { + snapshot = mostRecentSnapshot(resp.Snapshots) + } else { + return fmt.Errorf("Your query returned more than one result. Please try a more specific search criteria.") + } + } else { + snapshot = resp.Snapshots[0] } //Single Snapshot found so set to state - return snapshotDescriptionAttributes(d, resp.Snapshots[0]) + return snapshotDescriptionAttributes(d, snapshot) +} + +type snapshotSort []*ec2.Snapshot + +func (a snapshotSort) Len() int { return len(a) } +func (a snapshotSort) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a snapshotSort) Less(i, j int) bool { + itime := *a[i].StartTime + jtime := *a[j].StartTime + return itime.Unix() < jtime.Unix() +} + +func mostRecentSnapshot(snapshots []*ec2.Snapshot) *ec2.Snapshot { + sortedSnapshots := snapshots + sort.Sort(snapshotSort(sortedSnapshots)) + return sortedSnapshots[len(sortedSnapshots)-1] } func snapshotDescriptionAttributes(d *schema.ResourceData, snapshot *ec2.Snapshot) error { diff --git a/builtin/providers/aws/data_source_aws_ebs_snapshot_test.go b/builtin/providers/aws/data_source_aws_ebs_snapshot_test.go index f9e8179f8..682e758cc 100644 --- a/builtin/providers/aws/data_source_aws_ebs_snapshot_test.go +++ b/builtin/providers/aws/data_source_aws_ebs_snapshot_test.go @@ -69,6 +69,7 @@ resource "aws_ebs_snapshot" "snapshot" { } data "aws_ebs_snapshot" "snapshot" { + most_recent = true snapshot_ids = ["${aws_ebs_snapshot.snapshot.id}"] } ` @@ -88,6 +89,7 @@ resource "aws_ebs_snapshot" "snapshot" { } data "aws_ebs_snapshot" "snapshot" { + most_recent = true snapshot_ids = ["${aws_ebs_snapshot.snapshot.id}"] filter { name = "volume-size" diff --git a/website/source/docs/providers/aws/d/ebs_snapshot.html.markdown b/website/source/docs/providers/aws/d/ebs_snapshot.html.markdown index 39a55b509..35827431c 100644 --- a/website/source/docs/providers/aws/d/ebs_snapshot.html.markdown +++ b/website/source/docs/providers/aws/d/ebs_snapshot.html.markdown @@ -14,6 +14,7 @@ Use this data source to get information about an EBS Snapshot for use when provi ``` data "aws_ebs_snapshot" "ebs_volume" { + most_recent = true owners = ["self"] filter { name = "volume-size" @@ -30,6 +31,8 @@ data "aws_ebs_snapshot" "ebs_volume" { The following arguments are supported: +* `most_recent` - (Optional) If more than one result is returned, use the most recent snapshot. + * `owners` - (Optional) Returns the snapshots owned by the specified owner id. Multiple owners can be specified. * `snapshot_ids` - (Optional) Returns information on a specific snapshot_id.