github.com/civo/civogo@v0.3.65/pool.go (about) 1 package civogo 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "strings" 8 9 corev1 "k8s.io/api/core/v1" 10 ) 11 12 // KubernetesClusterPoolUpdateConfig is used to create a new cluster pool 13 type KubernetesClusterPoolUpdateConfig struct { 14 ID string `json:"id,omitempty"` 15 Count int `json:"count,omitempty"` 16 Size string `json:"size,omitempty"` 17 Labels map[string]string `json:"labels,omitempty"` 18 Taints []corev1.Taint `json:"taints"` 19 PublicIPNodePool bool `json:"public_ip_node_pool,omitempty"` 20 Region string `json:"region,omitempty"` 21 } 22 23 // ListKubernetesClusterPools returns all the pools for a kubernetes cluster 24 func (c *Client) ListKubernetesClusterPools(cid string) ([]KubernetesPool, error) { 25 resp, err := c.SendGetRequest(fmt.Sprintf("/v2/kubernetes/clusters/%s/pools", cid)) 26 if err != nil { 27 return nil, decodeError(err) 28 } 29 30 pools := make([]KubernetesPool, 0) 31 if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&pools); err != nil { 32 return nil, decodeError(err) 33 } 34 35 return pools, nil 36 } 37 38 // CreateKubernetesClusterPool update a single kubernetes cluster by its full ID 39 func (c *Client) CreateKubernetesClusterPool(id string, i *KubernetesClusterPoolUpdateConfig) (*SimpleResponse, error) { 40 i.Region = c.Region 41 resp, err := c.SendPostRequest(fmt.Sprintf("/v2/kubernetes/clusters/%s/pools", id), i) 42 if err != nil { 43 return nil, decodeError(err) 44 } 45 return c.DecodeSimpleResponse(resp) 46 } 47 48 // GetKubernetesClusterPool returns a pool for a kubernetes cluster 49 func (c *Client) GetKubernetesClusterPool(cid, pid string) (*KubernetesPool, error) { 50 resp, err := c.SendGetRequest(fmt.Sprintf("/v2/kubernetes/clusters/%s/pools/%s", cid, pid)) 51 if err != nil { 52 return nil, decodeError(err) 53 } 54 55 pool := &KubernetesPool{} 56 if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&pool); err != nil { 57 return nil, decodeError(err) 58 } 59 60 return pool, nil 61 } 62 63 // FindKubernetesClusterPool finds a pool by either part of the ID 64 func (c *Client) FindKubernetesClusterPool(cid, search string) (*KubernetesPool, error) { 65 pools, err := c.ListKubernetesClusterPools(cid) 66 if err != nil { 67 return nil, decodeError(err) 68 } 69 70 exactMatch := false 71 partialMatchesCount := 0 72 result := KubernetesPool{} 73 74 for _, value := range pools { 75 if value.ID == search { 76 exactMatch = true 77 result = value 78 } else if strings.Contains(value.ID, search) { 79 if !exactMatch { 80 result = value 81 partialMatchesCount++ 82 } 83 } 84 } 85 86 if exactMatch || partialMatchesCount == 1 { 87 return &result, nil 88 } else if partialMatchesCount > 1 { 89 err := fmt.Errorf("unable to find %s because there were multiple matches", search) 90 return nil, MultipleMatchesError.wrap(err) 91 } else { 92 err := fmt.Errorf("unable to find %s, zero matches", search) 93 return nil, ZeroMatchesError.wrap(err) 94 } 95 } 96 97 // DeleteKubernetesClusterPoolInstance deletes a instance from pool 98 func (c *Client) DeleteKubernetesClusterPoolInstance(cid, pid, id string) (*SimpleResponse, error) { 99 resp, err := c.SendDeleteRequest(fmt.Sprintf("/v2/kubernetes/clusters/%s/pools/%s/instances/%s", cid, pid, id)) 100 if err != nil { 101 return nil, decodeError(err) 102 } 103 104 return c.DecodeSimpleResponse(resp) 105 } 106 107 // UpdateKubernetesClusterPool updates a pool for a kubernetes cluster 108 func (c *Client) UpdateKubernetesClusterPool(cid, pid string, config *KubernetesClusterPoolUpdateConfig) (*KubernetesPool, error) { 109 resp, err := c.SendPutRequest(fmt.Sprintf("/v2/kubernetes/clusters/%s/pools/%s", cid, pid), config) 110 if err != nil { 111 return nil, decodeError(err) 112 } 113 114 pool := &KubernetesPool{} 115 if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&pool); err != nil { 116 return nil, decodeError(err) 117 } 118 119 return pool, nil 120 } 121 122 // DeleteKubernetesClusterPool delete a pool inside the cluster 123 func (c *Client) DeleteKubernetesClusterPool(id, poolID string) (*SimpleResponse, error) { 124 resp, err := c.SendDeleteRequest(fmt.Sprintf("/v2/kubernetes/clusters/%s/pools/%s", id, poolID)) 125 if err != nil { 126 return nil, decodeError(err) 127 } 128 129 return c.DecodeSimpleResponse(resp) 130 }