/
spot_price.go
53 lines (42 loc) · 1.25 KB
/
spot_price.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) 2016-2022 Cristian Măgherușan-Stanciu
// Licensed under the Open Software License version 3.0
package autospotting
import (
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
)
type spotPrices struct {
data []*ec2.SpotPrice
conn connections
}
// fetch queries all spot prices in the current region
func (s *spotPrices) fetch(product string,
duration time.Duration,
availabilityZone *string,
instanceTypes []*string) error {
log.Println(s.conn.region, "Requesting spot prices")
ec2Conn := s.conn.ec2
params := &ec2.DescribeSpotPriceHistoryInput{
ProductDescriptions: []*string{
aws.String(product),
},
StartTime: aws.Time(time.Now().Add(-1 * duration)),
EndTime: aws.Time(time.Now()),
AvailabilityZone: availabilityZone,
InstanceTypes: instanceTypes,
}
data := []*ec2.SpotPrice{}
err := ec2Conn.DescribeSpotPriceHistoryPages(params, func(page *ec2.DescribeSpotPriceHistoryOutput, lastPage bool) bool {
data = append(data, page.SpotPriceHistory...)
debug.Printf("DescribeSpotPriceHistory lastPage: %v", lastPage)
return true
})
if err != nil {
log.Println(s.conn.region, "Failed requesting spot prices:", err.Error())
return err
}
s.data = data
return nil
}