Repository navigation
Expand file tree
/
Copy pathresources.go
More file actions
360 lines (307 loc) · 10.3 KB
/
Copy pathresources.go
File metadata and controls
360 lines (307 loc) · 10.3 KB
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
/*
Copyright 2025 The Antfly Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"context"
"fmt"
"net/http"
"github.com/antflydb/antfly/pkg/client/oapi"
"github.com/antflydb/antfly/pkg/libaf/json"
)
// CreateTable creates a new table
func (c *AntflyClient) CreateTable(ctx context.Context, tableName string, req CreateTableRequest) error {
resp, err := c.client.CreateTable(ctx, tableName, req)
if err != nil {
return fmt.Errorf("creating table: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode >= 300 {
return fmt.Errorf("creating table: %w", readErrorResponse(resp))
}
return nil
}
// DropTable drops an existing table
func (c *AntflyClient) DropTable(ctx context.Context, tableName string) error {
resp, err := c.client.DropTable(ctx, tableName)
if err != nil {
return fmt.Errorf("dropping table: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode >= 300 {
return fmt.Errorf("dropping table: %w", readErrorResponse(resp))
}
return nil
}
func (c *AntflyClient) GetTable(ctx context.Context, tableName string) (*TableStatus, error) {
resp, err := c.client.GetTable(ctx, tableName)
if err != nil {
return nil, fmt.Errorf("getting table: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode >= 300 {
return nil, fmt.Errorf("getting table: %w", readErrorResponse(resp))
}
// Parse the response
var table TableStatus
if err := json.NewDecoder(resp.Body).Decode(&table); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}
return &table, nil
}
// ListTables lists all tables
func (c *AntflyClient) ListTables(ctx context.Context) ([]TableStatus, error) {
resp, err := c.client.ListTables(ctx, &oapi.ListTablesParams{})
if err != nil {
return nil, fmt.Errorf("listing tables: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode >= 300 {
return nil, fmt.Errorf("listing tables: %w", readErrorResponse(resp))
}
// Parse the response
var tables []TableStatus
if err := json.NewDecoder(resp.Body).Decode(&tables); err != nil {
return nil, fmt.Errorf("parsing list tables response: %w", err)
}
return tables, nil
}
// CreateIndex creates a new index on a table
func (c *AntflyClient) CreateIndex(ctx context.Context, tableName, indexName string, config IndexConfig) error {
resp, err := c.client.CreateIndex(ctx, tableName, indexName, config)
if err != nil {
return fmt.Errorf("creating index: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode >= 300 {
return fmt.Errorf("creating index: %w", readErrorResponse(resp))
}
return nil
}
// DropIndex drops an index from a table
func (c *AntflyClient) DropIndex(ctx context.Context, tableName, indexName string) error {
resp, err := c.client.DropIndex(ctx, tableName, indexName)
if err != nil {
return fmt.Errorf("dropping index: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode >= 300 {
return fmt.Errorf("dropping index: %w", readErrorResponse(resp))
}
return nil
}
// ListIndexes lists all indexes for a table
func (c *AntflyClient) ListIndexes(ctx context.Context, tableName string) (map[string]IndexStatus, error) {
resp, err := c.client.ListIndexes(ctx, tableName)
if err != nil {
return nil, fmt.Errorf("listing indexes: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode >= 300 {
return nil, fmt.Errorf("listing indexes: %w", readErrorResponse(resp))
}
// Parse the response - API returns an array, we convert to a map keyed by index name
var indexList []IndexStatus
if err := json.NewDecoder(resp.Body).Decode(&indexList); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}
// Convert array to map keyed by index name
indexes := make(map[string]IndexStatus, len(indexList))
for _, idx := range indexList {
indexes[idx.Config.Name] = idx
}
return indexes, nil
}
// GetIndex gets a specific index for a table
func (c *AntflyClient) GetIndex(ctx context.Context, tableName, indexName string) (*IndexStatus, error) {
resp, err := c.client.GetIndex(ctx, tableName, indexName)
if err != nil {
return nil, fmt.Errorf("getting index: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode >= 300 {
return nil, fmt.Errorf("getting index: %w", readErrorResponse(resp))
}
// Parse the response
var index IndexStatus
if err := json.NewDecoder(resp.Body).Decode(&index); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}
return &index, nil
}
// Backup backs up a table
func (c *AntflyClient) Backup(ctx context.Context, tableName, backupID, location string) error {
if tableName == "" {
return fmt.Errorf("empty table name")
}
req := oapi.BackupRequest{
BackupId: backupID,
Location: location,
}
resp, err := c.client.BackupTable(ctx, tableName, req)
if err != nil {
return fmt.Errorf("backup request failed: %w", err)
}
defer func() { _ = resp.Body.Close() }()
// API might return 201 Created or 202 Accepted
if resp.StatusCode >= 300 && resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusAccepted {
return fmt.Errorf("backup failed: %w", readErrorResponse(resp))
}
return nil
}
// Restore restores a table from a backup
func (c *AntflyClient) Restore(ctx context.Context, tableName, backupID, location string) error {
if tableName == "" {
return fmt.Errorf("empty table name")
}
req := oapi.RestoreRequest{
BackupId: backupID,
Location: location,
}
resp, err := c.client.RestoreTable(ctx, tableName, req)
if err != nil {
return fmt.Errorf("restore request failed: %w", err)
}
defer func() { _ = resp.Body.Close() }()
// Restore API might return 202 Accepted
if resp.StatusCode >= 300 && resp.StatusCode != http.StatusAccepted {
return fmt.Errorf("restore failed: %w", readErrorResponse(resp))
}
return nil
}
// ClusterBackupResult represents the result of a cluster backup operation
type ClusterBackupResult struct {
BackupID string
Status string
Tables []TableBackupStatus
}
// TableBackupStatus represents backup status for a single table
type TableBackupStatus struct {
Name string
Status string
Error string
}
// ClusterBackup backs up multiple tables or all tables in the cluster
func (c *AntflyClient) ClusterBackup(ctx context.Context, backupID, location string, tableNames []string) (*ClusterBackupResult, error) {
req := oapi.ClusterBackupRequest{
BackupId: backupID,
Location: location,
TableNames: tableNames,
}
resp, err := c.client.Backup(ctx, req)
if err != nil {
return nil, fmt.Errorf("cluster backup request failed: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode >= 300 {
return nil, fmt.Errorf("cluster backup failed: %w", readErrorResponse(resp))
}
var result oapi.ClusterBackupResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}
tables := make([]TableBackupStatus, len(result.Tables))
for i, t := range result.Tables {
tables[i] = TableBackupStatus{
Name: t.Name,
Status: string(t.Status),
Error: t.Error,
}
}
return &ClusterBackupResult{
BackupID: result.BackupId,
Status: string(result.Status),
Tables: tables,
}, nil
}
// ClusterRestoreResult represents the result of a cluster restore operation
type ClusterRestoreResult struct {
Status string
Tables []TableRestoreStatus
}
// TableRestoreStatus represents restore status for a single table
type TableRestoreStatus struct {
Name string
Status string
Error string
}
// ClusterRestore restores multiple tables from a cluster backup
func (c *AntflyClient) ClusterRestore(ctx context.Context, backupID, location string, tableNames []string, restoreMode string) (*ClusterRestoreResult, error) {
req := oapi.ClusterRestoreRequest{
BackupId: backupID,
Location: location,
TableNames: tableNames,
}
if restoreMode != "" {
req.RestoreMode = oapi.ClusterRestoreRequestRestoreMode(restoreMode)
}
resp, err := c.client.Restore(ctx, req)
if err != nil {
return nil, fmt.Errorf("cluster restore request failed: %w", err)
}
defer func() { _ = resp.Body.Close() }()
// Restore returns 202 Accepted
if resp.StatusCode >= 300 && resp.StatusCode != http.StatusAccepted {
return nil, fmt.Errorf("cluster restore failed: %w", readErrorResponse(resp))
}
var result oapi.ClusterRestoreResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}
tables := make([]TableRestoreStatus, len(result.Tables))
for i, t := range result.Tables {
tables[i] = TableRestoreStatus{
Name: t.Name,
Status: string(t.Status),
Error: t.Error,
}
}
return &ClusterRestoreResult{
Status: string(result.Status),
Tables: tables,
}, nil
}
// BackupInfo represents metadata about a stored backup
type BackupInfo struct {
BackupID string
Timestamp string
AntflyVersion string
Tables []string
}
// ListBackups lists available cluster backups at the specified location
func (c *AntflyClient) ListBackups(ctx context.Context, location string) ([]BackupInfo, error) {
params := &oapi.ListBackupsParams{
Location: location,
}
resp, err := c.client.ListBackups(ctx, params)
if err != nil {
return nil, fmt.Errorf("list backups request failed: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode >= 300 {
return nil, fmt.Errorf("list backups failed: %w", readErrorResponse(resp))
}
var result oapi.BackupListResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}
backups := make([]BackupInfo, len(result.Backups))
for i, b := range result.Backups {
backups[i] = BackupInfo{
BackupID: b.BackupId,
Timestamp: b.Timestamp.Format("2006-01-02T15:04:05Z07:00"),
AntflyVersion: b.AntflyVersion,
Tables: b.Tables,
}
}
return backups, nil
}