-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/aws: Sort AMI and snapshot IDs (#13866)
As a follow up to #13844, this pull request sorts the AMIs and snapshots returned from the aws_ami_ids and aws_ebs_snapshot_ids data sources, respectively.
- Loading branch information
1 parent
9f99fc4
commit d721ff6
Showing
9 changed files
with
211 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package aws | ||
|
||
import ( | ||
"sort" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/service/ec2" | ||
) | ||
|
||
type imageSort []*ec2.Image | ||
type snapshotSort []*ec2.Snapshot | ||
|
||
func (a imageSort) Len() int { | ||
return len(a) | ||
} | ||
|
||
func (a imageSort) Swap(i, j int) { | ||
a[i], a[j] = a[j], a[i] | ||
} | ||
|
||
func (a imageSort) Less(i, j int) bool { | ||
itime, _ := time.Parse(time.RFC3339, *a[i].CreationDate) | ||
jtime, _ := time.Parse(time.RFC3339, *a[j].CreationDate) | ||
return itime.Unix() < jtime.Unix() | ||
} | ||
|
||
// Sort images by creation date, in descending order. | ||
func sortImages(images []*ec2.Image) []*ec2.Image { | ||
sortedImages := images | ||
sort.Sort(sort.Reverse(imageSort(sortedImages))) | ||
return sortedImages | ||
} | ||
|
||
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() | ||
} | ||
|
||
// Sort snapshots by creation date, in descending order. | ||
func sortSnapshots(snapshots []*ec2.Snapshot) []*ec2.Snapshot { | ||
sortedSnapshots := snapshots | ||
sort.Sort(sort.Reverse(snapshotSort(sortedSnapshots))) | ||
return sortedSnapshots | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters