provider/aws: Add `most_recent` to the ebs_snapshot data source (#10986)
This commit is contained in:
parent
414b566d2e
commit
ae3144a876
|
@ -2,6 +2,8 @@ package aws
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"sort"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/service/ec2"
|
"github.com/aws/aws-sdk-go/service/ec2"
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
|
@ -14,7 +16,12 @@ func dataSourceAwsEbsSnapshot() *schema.Resource {
|
||||||
Schema: map[string]*schema.Schema{
|
Schema: map[string]*schema.Schema{
|
||||||
//selection criteria
|
//selection criteria
|
||||||
"filter": dataSourceFiltersSchema(),
|
"filter": dataSourceFiltersSchema(),
|
||||||
|
"most_recent": {
|
||||||
|
Type: schema.TypeBool,
|
||||||
|
Optional: true,
|
||||||
|
Default: false,
|
||||||
|
ForceNew: true,
|
||||||
|
},
|
||||||
"owners": {
|
"owners": {
|
||||||
Type: schema.TypeList,
|
Type: schema.TypeList,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
|
@ -110,16 +117,41 @@ func dataSourceAwsEbsSnapshotRead(d *schema.ResourceData, meta interface{}) erro
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var snapshot *ec2.Snapshot
|
||||||
if len(resp.Snapshots) < 1 {
|
if len(resp.Snapshots) < 1 {
|
||||||
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
|
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(resp.Snapshots) > 1 {
|
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
|
//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 {
|
func snapshotDescriptionAttributes(d *schema.ResourceData, snapshot *ec2.Snapshot) error {
|
||||||
|
|
|
@ -69,6 +69,7 @@ resource "aws_ebs_snapshot" "snapshot" {
|
||||||
}
|
}
|
||||||
|
|
||||||
data "aws_ebs_snapshot" "snapshot" {
|
data "aws_ebs_snapshot" "snapshot" {
|
||||||
|
most_recent = true
|
||||||
snapshot_ids = ["${aws_ebs_snapshot.snapshot.id}"]
|
snapshot_ids = ["${aws_ebs_snapshot.snapshot.id}"]
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
@ -88,6 +89,7 @@ resource "aws_ebs_snapshot" "snapshot" {
|
||||||
}
|
}
|
||||||
|
|
||||||
data "aws_ebs_snapshot" "snapshot" {
|
data "aws_ebs_snapshot" "snapshot" {
|
||||||
|
most_recent = true
|
||||||
snapshot_ids = ["${aws_ebs_snapshot.snapshot.id}"]
|
snapshot_ids = ["${aws_ebs_snapshot.snapshot.id}"]
|
||||||
filter {
|
filter {
|
||||||
name = "volume-size"
|
name = "volume-size"
|
||||||
|
|
|
@ -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" {
|
data "aws_ebs_snapshot" "ebs_volume" {
|
||||||
|
most_recent = true
|
||||||
owners = ["self"]
|
owners = ["self"]
|
||||||
filter {
|
filter {
|
||||||
name = "volume-size"
|
name = "volume-size"
|
||||||
|
@ -30,6 +31,8 @@ data "aws_ebs_snapshot" "ebs_volume" {
|
||||||
|
|
||||||
The following arguments are supported:
|
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.
|
* `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.
|
* `snapshot_ids` - (Optional) Returns information on a specific snapshot_id.
|
||||||
|
|
Loading…
Reference in New Issue