-
Notifications
You must be signed in to change notification settings - Fork 26
/
kfcluster.go
150 lines (127 loc) · 4.14 KB
/
kfcluster.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package civogo
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"time"
)
// KfCluster represents a cluster with Kubeflow installed.
type KfCluster struct {
ID string `json:"id"`
Name string `json:"name"`
NetworkID string `json:"network_id"`
FirewallID string `json:"firewall_id,omitempty"`
Size string `json:"size,omitempty"`
KubeflowReady string `json:"kubeflow_ready,omitempty"`
DashboardURL string `json:"dashboard_url,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
}
// CreateKfClusterReq is the request for creating a KfCluster.
type CreateKfClusterReq struct {
Name string `json:"name" validate:"required"`
NetworkID string `json:"network_id" validate:"required"`
FirewallID string `json:"firewall_id,omitempty"`
Size string `json:"size,omitempty"`
Region string `json:"region,omitempty"`
}
// UpdateKfClusterReq is the request for updating a KfCluster.
type UpdateKfClusterReq struct {
Name string `json:"name,omitempty"`
Region string `json:"region,omitempty"`
// Size string `json:"size"`
}
// PaginatedKfClusters returns a paginated list of KfCluster object
type PaginatedKfClusters struct {
Page int `json:"page"`
PerPage int `json:"per_page"`
Pages int `json:"pages"`
Items []KfCluster `json:"items"`
}
// ListKfClusters returns all applications in that specific region
func (c *Client) ListKfClusters() (*PaginatedKfClusters, error) {
resp, err := c.SendGetRequest("/v2/kfclusters")
if err != nil {
return nil, decodeError(err)
}
kfc := &PaginatedKfClusters{}
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&kfc); err != nil {
return nil, decodeError(err)
}
return kfc, nil
}
// GetKfCluster returns a kubeflow cluster by ID
func (c *Client) GetKfCluster(id string) (*KfCluster, error) {
resp, err := c.SendGetRequest(fmt.Sprintf("/v2/kfclusters/%s", id))
if err != nil {
return nil, decodeError(err)
}
kfc := &KfCluster{}
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&kfc); err != nil {
return nil, decodeError(err)
}
return kfc, nil
}
// FindKfCluster finds a kubeflow cluster by either part of the ID or part of the name
func (c *Client) FindKfCluster(search string) (*KfCluster, error) {
kfClusters, err := c.ListKfClusters()
if err != nil {
return nil, decodeError(err)
}
exactMatch := false
partialMatchesCount := 0
result := KfCluster{}
for _, value := range kfClusters.Items {
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)
}
}
// CreateKfCluster creates a new kubeflow cluster
func (c *Client) CreateKfCluster(req CreateKfClusterReq) (*KfCluster, error) {
req.Region = c.Region
body, err := c.SendPostRequest("/v2/kfclusters", req)
if err != nil {
return nil, decodeError(err)
}
var kfc KfCluster
if err := json.NewDecoder(bytes.NewReader(body)).Decode(&kfc); err != nil {
return nil, err
}
return &kfc, nil
}
// UpdateKfCluster updates a kubeflow cluster
func (c *Client) UpdateKfCluster(id string, kfc *UpdateKfClusterReq) (*KfCluster, error) {
body, err := c.SendPutRequest(fmt.Sprintf("/v2/kfclusters/%s", id), kfc)
if err != nil {
return nil, decodeError(err)
}
updatedKfCluster := &KfCluster{}
if err := json.NewDecoder(bytes.NewReader(body)).Decode(updatedKfCluster); err != nil {
return nil, err
}
return updatedKfCluster, nil
}
// DeleteKfCluster deletes an application
func (c *Client) DeleteKfCluster(id string) (*SimpleResponse, error) {
resp, err := c.SendDeleteRequest(fmt.Sprintf("/v2/kfclusters/%s", id))
if err != nil {
return nil, decodeError(err)
}
return c.DecodeSimpleResponse(resp)
}