github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/sort.go (about)

     1  package aws
     2  
     3  import (
     4  	"sort"
     5  	"time"
     6  
     7  	"github.com/aws/aws-sdk-go/service/ec2"
     8  )
     9  
    10  type imageSort []*ec2.Image
    11  type snapshotSort []*ec2.Snapshot
    12  
    13  func (a imageSort) Len() int {
    14  	return len(a)
    15  }
    16  
    17  func (a imageSort) Swap(i, j int) {
    18  	a[i], a[j] = a[j], a[i]
    19  }
    20  
    21  func (a imageSort) Less(i, j int) bool {
    22  	itime, _ := time.Parse(time.RFC3339, *a[i].CreationDate)
    23  	jtime, _ := time.Parse(time.RFC3339, *a[j].CreationDate)
    24  	return itime.Unix() < jtime.Unix()
    25  }
    26  
    27  // Sort images by creation date, in descending order.
    28  func sortImages(images []*ec2.Image) []*ec2.Image {
    29  	sortedImages := images
    30  	sort.Sort(sort.Reverse(imageSort(sortedImages)))
    31  	return sortedImages
    32  }
    33  
    34  func (a snapshotSort) Len() int {
    35  	return len(a)
    36  }
    37  
    38  func (a snapshotSort) Swap(i, j int) {
    39  	a[i], a[j] = a[j], a[i]
    40  }
    41  
    42  func (a snapshotSort) Less(i, j int) bool {
    43  	itime := *a[i].StartTime
    44  	jtime := *a[j].StartTime
    45  	return itime.Unix() < jtime.Unix()
    46  }
    47  
    48  // Sort snapshots by creation date, in descending order.
    49  func sortSnapshots(snapshots []*ec2.Snapshot) []*ec2.Snapshot {
    50  	sortedSnapshots := snapshots
    51  	sort.Sort(sort.Reverse(snapshotSort(sortedSnapshots)))
    52  	return sortedSnapshots
    53  }