This repository has been archived by the owner on Jul 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
spot_instances.go
66 lines (58 loc) · 1.85 KB
/
spot_instances.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
54
55
56
57
58
59
60
61
62
63
64
65
66
package ec2
import (
"encoding/xml"
"log"
"net/url"
"strconv"
"time"
)
type SpotPrice struct {
InstanceType string `xml:"instanceType"` // m1.small</instanceType>
ProductDescription string `xml:"productDescription"` // Linux/UNIX</productDescription>
SpotPrice float64 `xml:"spotPrice"` // 0.287</spotPrice>
Timestamp time.Time `xml:"timestamp"` // 2009-12-04T20:56:05.000Z</timestamp>
AvailabilityZone string `xml:"availabilityZone"` // us-east-1a</availabilityZone>
}
type DescribeSpotPriceHistoryResponse struct {
SpotPrices []*SpotPrice `xml:"spotPriceHistorySet>item"`
}
const TIME_FORMAT = "2006-01-02T15:04:05.999Z"
type SpotPriceFilter struct {
InstanceTypes []string
AvailabilityZones []string
ProductDescriptions []string
StartTime time.Time
EndTime time.Time
}
const DESC_LINUX_UNIX = "Linux/UNIX"
func (client *Client) DescribeSpotPriceHistory(filter *SpotPriceFilter) (prices []*SpotPrice, e error) {
query := queryForAction("DescribeSpotPriceHistory")
if filter == nil {
filter = &SpotPriceFilter{}
}
values := url.Values{}
for i, instanceType := range filter.InstanceTypes {
values.Add("InstanceType."+strconv.Itoa(i+1), instanceType)
}
for i, desc := range filter.ProductDescriptions {
values.Add("ProductDescription."+strconv.Itoa(i+1), desc)
}
if !filter.StartTime.IsZero() {
values.Add("StartTime", filter.StartTime.Format(TIME_FORMAT))
}
if !filter.EndTime.IsZero() {
values.Add("EndTime", filter.EndTime.Format(TIME_FORMAT))
}
query += "&" + values.Encode()
log.Println(query)
raw, e := client.DoSignedRequest("GET", client.Endpoint(), query, nil)
if e != nil {
return prices, e
}
rsp := &DescribeSpotPriceHistoryResponse{}
e = xml.Unmarshal(raw.Content, rsp)
if e != nil {
return prices, e
}
return rsp.SpotPrices, nil
}