-
Notifications
You must be signed in to change notification settings - Fork 159
/
client.go
459 lines (392 loc) · 12.2 KB
/
client.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// Copyright (C) 2017 ScyllaDB
package mermaidclient
import (
"context"
"crypto/tls"
"net/http"
"net/url"
"os"
"strconv"
"sync"
"time"
api "github.com/go-openapi/runtime/client"
"github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/strfmt"
"github.com/pkg/errors"
"github.com/scylladb/scylla-operator/pkg/mermaidclient/internal/client/operations"
"github.com/scylladb/scylla-operator/pkg/mermaidclient/internal/models"
"github.com/scylladb/scylla-operator/pkg/util/uuid"
)
var disableOpenAPIDebugOnce sync.Once
//go:generate ./internalgen.sh
// Client provides means to interact with Mermaid.
type Client struct {
operations *operations.Client
}
// DefaultTLSConfig specifies default TLS configuration used when creating a new
// client.
var DefaultTLSConfig = func() *tls.Config {
return &tls.Config{
InsecureSkipVerify: true,
}
}
func NewClient(rawURL string, transport http.RoundTripper) (Client, error) {
u, err := url.Parse(rawURL)
if err != nil {
return Client{}, err
}
disableOpenAPIDebugOnce.Do(func() {
middleware.Debug = false
})
if transport == nil {
transport = &http.Transport{
TLSClientConfig: DefaultTLSConfig(),
}
}
httpClient := &http.Client{
Transport: transport,
}
r := api.NewWithClient(u.Host, u.Path, []string{u.Scheme}, httpClient)
// debug can be turned on by SWAGGER_DEBUG or DEBUG env variable
// we change that to SCTOOL_DUMP_HTTP
r.Debug, _ = strconv.ParseBool(os.Getenv("SCTOOL_DUMP_HTTP"))
return Client{operations: operations.New(r, strfmt.Default)}, nil
}
// CreateCluster creates a new cluster.
func (c Client) CreateCluster(ctx context.Context, cluster *Cluster) (string, error) {
resp, err := c.operations.PostClusters(&operations.PostClustersParams{
Context: ctx,
Cluster: cluster,
})
if err != nil {
return "", err
}
clusterID, err := uuidFromLocation(resp.Location)
if err != nil {
return "", errors.Wrap(err, "cannot parse response")
}
return clusterID.String(), nil
}
// GetCluster returns a cluster for a given ID.
func (c Client) GetCluster(ctx context.Context, clusterID string) (*Cluster, error) {
resp, err := c.operations.GetClusterClusterID(&operations.GetClusterClusterIDParams{
Context: ctx,
ClusterID: clusterID,
})
if err != nil {
return nil, err
}
return resp.Payload, nil
}
// UpdateCluster updates cluster.
func (c Client) UpdateCluster(ctx context.Context, cluster *Cluster) error {
_, err := c.operations.PutClusterClusterID(&operations.PutClusterClusterIDParams{ // nolint: errcheck
Context: ctx,
ClusterID: cluster.ID,
Cluster: cluster,
})
return err
}
// DeleteCluster removes cluster.
func (c Client) DeleteCluster(ctx context.Context, clusterID string) error {
_, err := c.operations.DeleteClusterClusterID(&operations.DeleteClusterClusterIDParams{ // nolint: errcheck
Context: ctx,
ClusterID: clusterID,
})
return err
}
// DeleteClusterSecrets removes cluster secrets.
func (c Client) DeleteClusterSecrets(ctx context.Context, clusterID string, cqlCreds, sslUserCert bool) error {
ok := false
p := &operations.DeleteClusterClusterIDParams{
Context: ctx,
ClusterID: clusterID,
}
if cqlCreds {
p.CqlCreds = &cqlCreds
ok = true
}
if sslUserCert {
p.SslUserCert = &sslUserCert
ok = true
}
if !ok {
return nil
}
_, err := c.operations.DeleteClusterClusterID(p) // nolint: errcheck
return err
}
// ListClusters returns clusters.
func (c Client) ListClusters(ctx context.Context) (ClusterSlice, error) {
resp, err := c.operations.GetClusters(&operations.GetClustersParams{
Context: ctx,
})
if err != nil {
return nil, err
}
return resp.Payload, nil
}
// ClusterStatus returns health check progress.
func (c Client) ClusterStatus(ctx context.Context, clusterID string) (ClusterStatus, error) {
resp, err := c.operations.GetClusterClusterIDStatus(&operations.GetClusterClusterIDStatusParams{
Context: ctx,
ClusterID: clusterID,
})
if err != nil {
return nil, err
}
return ClusterStatus(resp.Payload), nil
}
// GetRepairTarget fetches information about repair target.
func (c *Client) GetRepairTarget(ctx context.Context, clusterID string, t *Task) (*RepairTarget, error) {
resp, err := c.operations.GetClusterClusterIDTasksRepairTarget(&operations.GetClusterClusterIDTasksRepairTargetParams{
Context: ctx,
ClusterID: clusterID,
TaskFields: makeTaskUpdate(t),
})
if err != nil {
return nil, err
}
return &RepairTarget{RepairTarget: *resp.Payload}, nil
}
// GetBackupTarget fetches information about repair target.
func (c *Client) GetBackupTarget(ctx context.Context, clusterID string, t *Task) (*BackupTarget, error) {
resp, err := c.operations.GetClusterClusterIDTasksBackupTarget(&operations.GetClusterClusterIDTasksBackupTargetParams{
Context: ctx,
ClusterID: clusterID,
TaskFields: makeTaskUpdate(t),
})
if err != nil {
return nil, err
}
return &BackupTarget{BackupTarget: *resp.Payload}, nil
}
// CreateTask creates a new task.
func (c *Client) CreateTask(ctx context.Context, clusterID string, t *Task) (uuid.UUID, error) {
params := &operations.PostClusterClusterIDTasksParams{
Context: ctx,
ClusterID: clusterID,
TaskFields: makeTaskUpdate(t),
}
resp, err := c.operations.PostClusterClusterIDTasks(params)
if err != nil {
return uuid.Nil, err
}
taskID, err := uuidFromLocation(resp.Location)
if err != nil {
return uuid.Nil, errors.Wrap(err, "cannot parse response")
}
return taskID, nil
}
// GetTask returns a task of a given type and ID.
func (c *Client) GetTask(ctx context.Context, clusterID, taskType string, taskID uuid.UUID) (*Task, error) {
resp, err := c.operations.GetClusterClusterIDTaskTaskTypeTaskID(&operations.GetClusterClusterIDTaskTaskTypeTaskIDParams{
Context: ctx,
ClusterID: clusterID,
TaskType: taskType,
TaskID: taskID.String(),
})
if err != nil {
return nil, err
}
return resp.Payload, nil
}
// GetTaskHistory returns a run history of task of a given type and task ID.
func (c *Client) GetTaskHistory(ctx context.Context, clusterID, taskType string, taskID uuid.UUID, limit int64) (TaskRunSlice, error) {
params := &operations.GetClusterClusterIDTaskTaskTypeTaskIDHistoryParams{
Context: ctx,
ClusterID: clusterID,
TaskType: taskType,
TaskID: taskID.String(),
}
params.Limit = &limit
resp, err := c.operations.GetClusterClusterIDTaskTaskTypeTaskIDHistory(params)
if err != nil {
return nil, err
}
return resp.Payload, nil
}
// StartTask starts executing a task.
func (c *Client) StartTask(ctx context.Context, clusterID, taskType string, taskID uuid.UUID, cont bool) error {
_, err := c.operations.PutClusterClusterIDTaskTaskTypeTaskIDStart(&operations.PutClusterClusterIDTaskTaskTypeTaskIDStartParams{ // nolint: errcheck
Context: ctx,
ClusterID: clusterID,
TaskType: taskType,
TaskID: taskID.String(),
Continue: cont,
})
return err
}
// StopTask stops executing a task.
func (c *Client) StopTask(ctx context.Context, clusterID, taskType string, taskID uuid.UUID, disable bool) error {
_, err := c.operations.PutClusterClusterIDTaskTaskTypeTaskIDStop(&operations.PutClusterClusterIDTaskTaskTypeTaskIDStopParams{ // nolint: errcheck
Context: ctx,
ClusterID: clusterID,
TaskType: taskType,
TaskID: taskID.String(),
Disable: &disable,
})
return err
}
// DeleteTask stops executing a task.
func (c *Client) DeleteTask(ctx context.Context, clusterID, taskType string, taskID uuid.UUID) error {
_, err := c.operations.DeleteClusterClusterIDTaskTaskTypeTaskID(&operations.DeleteClusterClusterIDTaskTaskTypeTaskIDParams{ // nolint: errcheck
Context: ctx,
ClusterID: clusterID,
TaskType: taskType,
TaskID: taskID.String(),
})
return err
}
// UpdateTask updates an existing task unit.
func (c *Client) UpdateTask(ctx context.Context, clusterID string, t *Task) error {
_, err := c.operations.PutClusterClusterIDTaskTaskTypeTaskID(&operations.PutClusterClusterIDTaskTaskTypeTaskIDParams{ // nolint: errcheck
Context: ctx,
ClusterID: clusterID,
TaskType: t.Type,
TaskID: t.ID,
TaskFields: &models.TaskUpdate{
Enabled: t.Enabled,
Name: t.Name,
Schedule: t.Schedule,
Tags: t.Tags,
Properties: t.Properties,
},
})
return err
}
// ListTasks returns uled tasks within a clusterID, optionaly filtered by task type tp.
func (c *Client) ListTasks(ctx context.Context, clusterID, taskType string, all bool, status string) (ExtendedTasks, error) {
resp, err := c.operations.GetClusterClusterIDTasks(&operations.GetClusterClusterIDTasksParams{
Context: ctx,
ClusterID: clusterID,
Type: &taskType,
All: &all,
Status: &status,
})
if err != nil {
return ExtendedTasks{}, err
}
et := ExtendedTasks{
All: all,
}
et.ExtendedTaskSlice = resp.Payload
return et, nil
}
// RepairProgress returns repair progress.
func (c Client) RepairProgress(ctx context.Context, clusterID, taskID, runID string) (RepairProgress, error) {
resp, err := c.operations.GetClusterClusterIDTaskRepairTaskIDRunID(&operations.GetClusterClusterIDTaskRepairTaskIDRunIDParams{
Context: ctx,
ClusterID: clusterID,
TaskID: taskID,
RunID: runID,
})
if err != nil {
return RepairProgress{}, err
}
return RepairProgress{
TaskRunRepairProgress: resp.Payload,
}, nil
}
// BackupProgress returns backup progress.
func (c Client) BackupProgress(ctx context.Context, clusterID, taskID, runID string) (BackupProgress, error) {
tr := &models.TaskRunBackupProgress{
Progress: &models.BackupProgress{
Stage: "INIT",
},
Run: &models.TaskRun{
Status: "NEW",
},
}
resp, err := c.operations.GetClusterClusterIDTaskBackupTaskIDRunID(&operations.GetClusterClusterIDTaskBackupTaskIDRunIDParams{
Context: ctx,
ClusterID: clusterID,
TaskID: taskID,
RunID: runID,
})
if err != nil {
return tr, err
}
if resp.Payload.Progress == nil {
resp.Payload.Progress = tr.Progress
}
if resp.Payload.Run == nil {
resp.Payload.Run = tr.Run
}
return resp.Payload, nil
}
// ListBackups returns listing of available backups.
func (c Client) ListBackups(ctx context.Context, clusterID string,
locations []string, allClusters bool, keyspace []string, minDate, maxDate strfmt.DateTime) (BackupListItems, error) {
p := &operations.GetClusterClusterIDBackupsParams{
Context: ctx,
ClusterID: clusterID,
Locations: locations,
Keyspace: keyspace,
}
if !allClusters {
p.QueryClusterID = &clusterID
}
if !time.Time(minDate).IsZero() {
p.MinDate = &minDate
}
if !time.Time(maxDate).IsZero() {
p.MaxDate = &maxDate
}
resp, err := c.operations.GetClusterClusterIDBackups(p)
if err != nil {
return BackupListItems{}, err
}
return resp.Payload, nil
}
// ListBackupFiles returns a listing of available backup files.
func (c Client) ListBackupFiles(ctx context.Context, clusterID string,
locations []string, allClusters bool, keyspace []string, snapshotTag string) ([]*models.BackupFilesInfo, error) {
p := &operations.GetClusterClusterIDBackupsFilesParams{
Context: ctx,
ClusterID: clusterID,
Locations: locations,
Keyspace: keyspace,
SnapshotTag: snapshotTag,
}
if !allClusters {
p.QueryClusterID = &clusterID
}
resp, err := c.operations.GetClusterClusterIDBackupsFiles(p)
if err != nil {
return nil, err
}
return resp.Payload, nil
}
// DeleteSnapshot deletes backup snapshot with all data associated with it.
func (c Client) DeleteSnapshot(ctx context.Context, clusterID string,
locations []string, snapshotTag string) error {
p := &operations.DeleteClusterClusterIDBackupsParams{
Context: ctx,
ClusterID: clusterID,
Locations: locations,
SnapshotTag: snapshotTag,
}
_, err := c.operations.DeleteClusterClusterIDBackups(p) // nolint: errcheck
return err
}
// Version returns server version.
func (c Client) Version(ctx context.Context) (*models.Version, error) {
resp, err := c.operations.GetVersion(&operations.GetVersionParams{
Context: ctx,
})
if err != nil {
return &models.Version{}, err
}
return resp.Payload, nil
}
// SetRepairIntensity updates ongoing repair intensity.
func (c Client) SetRepairIntensity(ctx context.Context, clusterID string, intensity float64) error {
p := &operations.PostClusterClusterIDRepairsIntensityParams{
Context: ctx,
ClusterID: clusterID,
Intensity: intensity,
}
_, err := c.operations.PostClusterClusterIDRepairsIntensity(p) // nolint: errcheck
return err
}