forked from equinixmetal-archive/packngo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotmarket.go
39 lines (32 loc) · 975 Bytes
/
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
package packngo
const spotMarketBasePath = "/market/spot/prices"
// SpotMarketService expooses Spot Market methods
type SpotMarketService interface {
Prices() (PriceMap, *Response, error)
}
// SpotMarketServiceOp implements SpotMarketService
type SpotMarketServiceOp struct {
client *Client
}
// PriceMap is a map of [facility][plan]-> float Price
type PriceMap map[string]map[string]float64
// Prices gets current PriceMap from the API
func (s *SpotMarketServiceOp) Prices() (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
}