/
availability.go
50 lines (42 loc) · 1.09 KB
/
availability.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
package api
import (
"encoding/json"
"fmt"
"io/ioutil"
)
type InstanceTypeAvailability string
var (
InstanceTypeAvailable InstanceTypeAvailability = "available"
InstanceTypeScarce InstanceTypeAvailability = "scarce"
InstanceTypeShortage InstanceTypeAvailability = "shortage"
)
type ServerAvailability struct {
Availability InstanceTypeAvailability `json:"availability"`
}
type ServerAvailabilities map[string]ServerAvailability
func (a ServerAvailabilities) CommercialTypes() []string {
types := []string{}
for k, _ := range a {
types = append(types, k)
}
return types
}
type availabilityResponse struct {
Servers ServerAvailabilities
}
func (s *API) GetServerAvailabilities() (ServerAvailabilities, error) {
resp, err := s.response("GET", fmt.Sprintf("%s/products/servers/availability", s.computeAPI), nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
content := availabilityResponse{}
if err := json.Unmarshal(bs, &content); err != nil {
return nil, err
}
return content.Servers, nil
}