forked from civo/civogo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_size.go
69 lines (60 loc) · 1.88 KB
/
instance_size.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
package civogo
import (
"bytes"
"encoding/json"
"fmt"
"strings"
)
// InstanceSize represents an available size for instances to launch
type InstanceSize struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
NiceName string `json:"nice_name,omitempty"`
CPUCores int `json:"cpu_cores,omitempty"`
RAMMegabytes int `json:"ram_mb,omitempty"`
DiskGigabytes int `json:"disk_gb,omitempty"`
Description string `json:"description,omitempty"`
Selectable bool `json:"selectable,omitempty"`
}
// ListInstanceSizes returns all availble sizes of instances
func (c *Client) ListInstanceSizes() ([]InstanceSize, error) {
resp, err := c.SendGetRequest("/v2/sizes")
if err != nil {
return nil, decodeERROR(err)
}
sizes := make([]InstanceSize, 0)
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&sizes); err != nil {
return nil, err
}
return sizes, nil
}
// FindInstanceSizes finds a instance size name by either part of the ID or part of the name
func (c *Client) FindInstanceSizes(search string) (*InstanceSize, error) {
instanceSize, err := c.ListInstanceSizes()
if err != nil {
return nil, decodeERROR(err)
}
exactMatch := false
partialMatchesCount := 0
result := InstanceSize{}
for _, value := range instanceSize {
if value.Name == search || value.ID == search {
exactMatch = true
result = value
} else if strings.Contains(value.Name, search) || strings.Contains(value.ID, search) {
if !exactMatch {
result = value
partialMatchesCount++
}
}
}
if exactMatch || partialMatchesCount == 1 {
return &result, nil
} else if partialMatchesCount > 1 {
err := fmt.Errorf("unable to find %s because there were multiple matches", search)
return nil, MultipleMatchesError.wrap(err)
} else {
err := fmt.Errorf("unable to find %s, zero matches", search)
return nil, ZeroMatchesError.wrap(err)
}
}