-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusters.go
190 lines (167 loc) · 5.89 KB
/
clusters.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package ecogo
import (
"context"
"fmt"
"net/http"
"path"
"time"
)
var (
listClustersPath = "/v1/cluster/%s/%s/list"
createClusterPath = "/v1/cluster/%s/%s"
versionPath = "/version"
apiV1Path = "/v1/cluster/%s/%s/%d"
repairClusterPath = path.Join(apiV1Path, "repair")
makeClusterHAPath = path.Join(apiV1Path, "ha")
healthyClusterPath = path.Join(apiV1Path, "health")
)
var _ ClusterServiceI = &ClusterService{}
type ClusterServiceI interface {
ServerVersion(ctx context.Context) (string, error)
List(ctx context.Context) ([]ClusterShortInfo, error)
Create(ctx context.Context, opt *ClusterOpt) (uint, error)
Request(ctx context.Context, clusterID uint) (*Cluster, error)
Delete(ctx context.Context, clusterID uint) error
Repair(ctx context.Context, clusterID uint) error
MakeHA(ctx context.Context, clusterID uint) error
Healthy(ctx context.Context, clusterID uint) error
//ClusterCert(ctx context.Context, clusterID uint)
}
type ClusterService struct {
client *Client
}
type ClusterShortInfo struct {
ID uint `json:"id" yaml:"id"`
Processing bool `json:"processing" yaml:"processing"`
Name string `json:"name" yaml:"name"`
State string `json:"state" yaml:"state"`
Status string `json:"status" yaml:"status"`
Existed string `json:"existed" yaml:"existed"`
Created time.Time `json:"created" yaml:"created"`
}
type Cluster struct {
ID uint `json:"id" yaml:"id"`
ProjectID int `json:"projectID" yaml:"projectID"`
RegionID int `json:"regionID" yaml:"regionID"`
Processing bool `json:"processing" yaml:"processing"`
HA bool `json:"ha" yaml:"ha"`
InternalLB bool `json:"internalLB" yaml:"internalLB"`
Endpoint string `json:"endpoint" yaml:"endpoint"`
Version string `json:"version" yaml:"version"`
Name string `json:"name" yaml:"name"`
NetworkID string `json:"networkID" yaml:"networkID"`
SubnetID string `json:"subnetID" yaml:"subnetID"`
State string `json:"state" yaml:"state"`
Status string `json:"status" yaml:"status"`
Existed string `json:"existed" yaml:"existed"`
Created time.Time `json:"created" yaml:"created"`
MastersPool Pool `json:"mastersPool" yaml:"mastersPool"`
WorkersPools []Pool `json:"workersPools" yaml:"workersPools"`
}
type ClusterOpt struct {
HA bool `json:"ha" yaml:"ha"`
InternalLB bool `json:"internalLB" yaml:"internalLB"`
//AuthType string `json:"authType" yaml:"authType"`
Name string `json:"name" yaml:"name"`
Version string `json:"version" yaml:"version"`
APILbFlavor string `json:"apiLbFlavor" yaml:"APILbFlavor"`
NetworkID string `json:"networkID" yaml:"networkID"`
SubnetID string `json:"subnetID" yaml:"subnetID"`
MasterOpt PoolOpt `json:"masterOpt" yaml:"masterOpt"`
WorkerOpts []PoolOpt `json:"workerOpts" yaml:"workerOpts"`
}
func (s *ClusterService) ServerVersion(ctx context.Context) (string, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, versionPath, nil)
if err != nil {
return "", err
}
var version string
if _, err = s.client.Do(ctx, req, &version); err != nil {
return "", err
}
return version, nil
}
func (s *ClusterService) List(ctx context.Context) ([]ClusterShortInfo, error) {
requestPath := fmt.Sprintf(listClustersPath, s.client.projectID, s.client.regionID)
req, err := s.client.NewRequest(ctx, http.MethodGet, requestPath, nil)
if err != nil {
return nil, err
}
var clusters []ClusterShortInfo
if _, err = s.client.Do(ctx, req, &clusters); err != nil {
return nil, err
}
return clusters, nil
}
func (s *ClusterService) Create(ctx context.Context, opt *ClusterOpt) (uint, error) {
requestPath := fmt.Sprintf(createClusterPath, s.client.projectID, s.client.regionID)
req, err := s.client.NewRequest(ctx, http.MethodPost, requestPath, opt)
if err != nil {
return 0, err
}
var id uint
if _, err = s.client.Do(ctx, req, &id); err != nil {
return 0, err
}
return id, err
}
func (s *ClusterService) Request(ctx context.Context, clusterID uint) (*Cluster, error) {
requestPath := fmt.Sprintf(apiV1Path, s.client.projectID, s.client.regionID, clusterID)
req, err := s.client.NewRequest(ctx, http.MethodGet, requestPath, nil)
if err != nil {
return nil, err
}
var cluster Cluster
if _, err = s.client.Do(ctx, req, &cluster); err != nil {
return nil, err
}
return &cluster, err
}
func (s *ClusterService) Delete(ctx context.Context, clusterID uint) error {
requestPath := fmt.Sprintf(apiV1Path, s.client.projectID, s.client.regionID, clusterID)
req, err := s.client.NewRequest(ctx, http.MethodDelete, requestPath, nil)
if err != nil {
return err
}
var resp string
if _, err = s.client.Do(ctx, req, &resp); err != nil {
return err
}
return err
}
func (s *ClusterService) Repair(ctx context.Context, clusterID uint) error {
requestPath := fmt.Sprintf(repairClusterPath, s.client.projectID, s.client.regionID, clusterID)
req, err := s.client.NewRequest(ctx, http.MethodPost, requestPath, nil)
if err != nil {
return err
}
var resp string
if _, err = s.client.Do(ctx, req, &resp); err != nil {
return err
}
return err
}
func (s *ClusterService) MakeHA(ctx context.Context, clusterID uint) error {
requestPath := fmt.Sprintf(makeClusterHAPath, s.client.projectID, s.client.regionID, clusterID)
req, err := s.client.NewRequest(ctx, http.MethodPost, requestPath, nil)
if err != nil {
return err
}
var resp string
if _, err = s.client.Do(ctx, req, &resp); err != nil {
return err
}
return err
}
func (s *ClusterService) Healthy(ctx context.Context, clusterID uint) error {
requestPath := fmt.Sprintf(healthyClusterPath, s.client.projectID, s.client.regionID, clusterID)
req, err := s.client.NewRequest(ctx, http.MethodGet, requestPath, nil)
if err != nil {
return err
}
var resp string
if _, err = s.client.Do(ctx, req, &resp); err != nil {
return err
}
return err
}