This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
spotmarket.go
92 lines (77 loc) · 2.55 KB
/
spotmarket.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package packngo
import "path"
const (
spotMarketBasePath = "/market/spot/prices"
spotMarketMetrosPath = "metros"
)
// SpotMarketService expooses Spot Market methods
type SpotMarketService interface {
// Prices gets current spot market prices by facility.
//
// Deprecated: Use PricesByFacility
Prices() (PriceMap, *Response, error)
// PricesByFacility gets current spot market prices by facility. The map is
// indexed by facility code and then plan name.
PricesByFacility() (PriceMap, *Response, error)
// PricesByMetro gets current spot market prices by metro. The map is
// indexed by metro code and then plan name.
PricesByMetro() (PriceMap, *Response, error)
}
// SpotMarketServiceOp implements SpotMarketService
type SpotMarketServiceOp struct {
client *Client
}
// PriceMap is a map of [location][plan]-> float Price
type PriceMap map[string]map[string]float64
// Prices gets current spot market prices by facility.
//
// Deprecated: Use PricesByFacility which this function thinly wraps.
func (s *SpotMarketServiceOp) Prices() (PriceMap, *Response, error) {
return s.PricesByFacility()
}
// PricesByFacility gets current spot market prices by facility. The map is
// indexed by facility code and then plan name.
//
// price := client.SpotMarket.PricesByFacility()["ny5"]["c3.medium.x86"]
func (s *SpotMarketServiceOp) PricesByFacility() (PriceMap, *Response, error) {
root := new(struct {
SMPs map[string]map[string]struct {
Price float64 `json:"price"`
} `json:"spot_market_prices"`
})
resp, err := s.client.DoRequest("GET", spotMarketBasePath, nil, root)
if err != nil {
return nil, resp, err
}
prices := make(PriceMap)
for facility, planMap := range root.SMPs {
prices[facility] = map[string]float64{}
for plan, v := range planMap {
prices[facility][plan] = v.Price
}
}
return prices, resp, err
}
// PricesByMetro gets current spot market prices by metro. The map is
// indexed by metro code and then plan name.
//
// price := client.SpotMarket.PricesByMetro()["sv"]["c3.medium.x86"]
func (s *SpotMarketServiceOp) PricesByMetro() (PriceMap, *Response, error) {
root := new(struct {
SMPs map[string]map[string]struct {
Price float64 `json:"price"`
} `json:"spot_market_prices"`
})
resp, err := s.client.DoRequest("GET", path.Join(spotMarketBasePath, spotMarketMetrosPath), nil, root)
if err != nil {
return nil, resp, err
}
prices := make(PriceMap)
for metro, planMap := range root.SMPs {
prices[metro] = map[string]float64{}
for plan, v := range planMap {
prices[metro][plan] = v.Price
}
}
return prices, resp, err
}