-
Notifications
You must be signed in to change notification settings - Fork 24
/
pool.go
130 lines (109 loc) · 4.03 KB
/
pool.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package civogo
import (
"bytes"
"encoding/json"
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
)
// KubernetesClusterPoolUpdateConfig is used to create a new cluster pool
type KubernetesClusterPoolUpdateConfig struct {
ID string `json:"id,omitempty"`
Count *int `json:"count,omitempty"`
Size string `json:"size,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Taints []corev1.Taint `json:"taints"`
PublicIPNodePool bool `json:"public_ip_node_pool,omitempty"`
Region string `json:"region,omitempty"`
}
// ListKubernetesClusterPools returns all the pools for a kubernetes cluster
func (c *Client) ListKubernetesClusterPools(cid string) ([]KubernetesPool, error) {
resp, err := c.SendGetRequest(fmt.Sprintf("/v2/kubernetes/clusters/%s/pools", cid))
if err != nil {
return nil, decodeError(err)
}
pools := make([]KubernetesPool, 0)
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&pools); err != nil {
return nil, decodeError(err)
}
return pools, nil
}
// CreateKubernetesClusterPool update a single kubernetes cluster by its full ID
func (c *Client) CreateKubernetesClusterPool(id string, i *KubernetesClusterPoolConfig) (*SimpleResponse, error) {
i.Region = c.Region
resp, err := c.SendPostRequest(fmt.Sprintf("/v2/kubernetes/clusters/%s/pools", id), i)
if err != nil {
return nil, decodeError(err)
}
return c.DecodeSimpleResponse(resp)
}
// GetKubernetesClusterPool returns a pool for a kubernetes cluster
func (c *Client) GetKubernetesClusterPool(cid, pid string) (*KubernetesPool, error) {
resp, err := c.SendGetRequest(fmt.Sprintf("/v2/kubernetes/clusters/%s/pools/%s", cid, pid))
if err != nil {
return nil, decodeError(err)
}
pool := &KubernetesPool{}
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&pool); err != nil {
return nil, decodeError(err)
}
return pool, nil
}
// FindKubernetesClusterPool finds a pool by either part of the ID
func (c *Client) FindKubernetesClusterPool(cid, search string) (*KubernetesPool, error) {
pools, err := c.ListKubernetesClusterPools(cid)
if err != nil {
return nil, decodeError(err)
}
exactMatch := false
partialMatchesCount := 0
result := KubernetesPool{}
for _, value := range pools {
if value.ID == search {
exactMatch = true
result = value
} else if 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)
}
}
// DeleteKubernetesClusterPoolInstance deletes a instance from pool
func (c *Client) DeleteKubernetesClusterPoolInstance(cid, pid, id string) (*SimpleResponse, error) {
resp, err := c.SendDeleteRequest(fmt.Sprintf("/v2/kubernetes/clusters/%s/pools/%s/instances/%s", cid, pid, id))
if err != nil {
return nil, decodeError(err)
}
return c.DecodeSimpleResponse(resp)
}
// UpdateKubernetesClusterPool updates a pool for a kubernetes cluster
func (c *Client) UpdateKubernetesClusterPool(cid, pid string, config *KubernetesClusterPoolUpdateConfig) (*KubernetesPool, error) {
resp, err := c.SendPutRequest(fmt.Sprintf("/v2/kubernetes/clusters/%s/pools/%s", cid, pid), config)
if err != nil {
return nil, decodeError(err)
}
pool := &KubernetesPool{}
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&pool); err != nil {
return nil, decodeError(err)
}
return pool, nil
}
// DeleteKubernetesClusterPool delete a pool inside the cluster
func (c *Client) DeleteKubernetesClusterPool(id, poolID string) (*SimpleResponse, error) {
resp, err := c.SendDeleteRequest(fmt.Sprintf("/v2/kubernetes/clusters/%s/pools/%s", id, poolID))
if err != nil {
return nil, decodeError(err)
}
return c.DecodeSimpleResponse(resp)
}