-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgo-pinot-api.go
981 lines (761 loc) · 29.3 KB
/
go-pinot-api.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
package goPinotAPI
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"log/slog"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"github.com/azaurus1/go-pinot-api/model"
)
type PinotAPIClient struct {
pinotControllerUrl *url.URL
pinotHttp *pinotHttp
Host string
log *slog.Logger
}
func NewPinotAPIClient(opts ...Opt) *PinotAPIClient {
clientCfg, pinotControllerUrl, err := validateOpts(opts...)
if err != nil {
log.Panic(err)
}
return &PinotAPIClient{
pinotControllerUrl: pinotControllerUrl,
pinotHttp: &pinotHttp{
httpClient: &http.Client{},
pinotControllerUrl: pinotControllerUrl,
httpAuthWriter: clientCfg.httpAuthWriter,
},
Host: pinotControllerUrl.Hostname(),
log: clientCfg.logger,
}
}
func (c *PinotAPIClient) FetchData(endpoint string, result any) error {
fullURL := prepareRequestURL(c, endpoint)
request, err := http.NewRequest(http.MethodGet, fullURL.String(), nil)
if err != nil {
return fmt.Errorf("client: could not create request: %w", err)
}
c.log.Debug(fmt.Sprintf("attempting GET %s", fullURL))
resp, err := c.pinotHttp.Do(request)
if err != nil {
c.logErrorResp(resp)
return fmt.Errorf("client: could not send request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
// extract body contents to add to error message
bodyContents, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("client: request failed with status code: %d", resp.StatusCode)
}
return fmt.Errorf("client: request failed with status code: %d, body: %s", resp.StatusCode, string(bodyContents))
}
bodyContents, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("client: could not read response body: %w", err)
}
err = json.NewDecoder(bytes.NewReader(bodyContents)).Decode(result)
if err != nil {
c.log.Debug(fmt.Sprintf("unable to decode response from successful request: %s", err))
return fmt.Errorf("client: could not unmarshal response JSON: %w\n%s", err, string(bodyContents))
}
return nil
}
func (c *PinotAPIClient) FetchPlainText(endpoint string, result *model.PlainTextAPIResponse) error {
fullURL := prepareRequestURL(c, endpoint)
request, err := http.NewRequest(http.MethodGet, fullURL.String(), nil)
if err != nil {
return fmt.Errorf("client: could not create request: %w", err)
}
c.log.Debug(fmt.Sprintf("attempting GET %s", fullURL))
resp, err := c.pinotHttp.Do(request)
if err != nil {
c.logErrorResp(resp)
return fmt.Errorf("client: could not send request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
// extract body contents to add to error message
bodyContents, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("client: request failed with status code: %d", resp.StatusCode)
}
return fmt.Errorf("client: request failed with status code: %d, body: %s", resp.StatusCode, string(bodyContents))
}
bodyContents, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("client: could not read response body: %w", err)
}
c.log.Debug(fmt.Sprintf("response from successful request: %s", string(bodyContents)))
result.Response = string(bodyContents)
return nil
}
func (c *PinotAPIClient) CreateObject(endpoint string, body []byte, result any) error {
fullURL := prepareRequestURL(c, endpoint)
var req *http.Request
var err error
if body == nil {
c.log.Debug("body is nil")
req, err = http.NewRequest(http.MethodPost, fullURL.String(), nil)
req.Header.Set("Content-Type", "application/json")
} else {
req, err = http.NewRequest(http.MethodPost, fullURL.String(), bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
}
if err != nil {
return fmt.Errorf("client: could not create request: %w", err)
}
c.log.Debug(fmt.Sprintf("attempting POST %s", fullURL))
res, err := c.pinotHttp.Do(req)
if err != nil {
c.logErrorResp(res)
return fmt.Errorf("client: could not send request: %w", err)
}
// Check the status code
if res.StatusCode != http.StatusOK {
errRespMessage, err := c.extractErrorMessage(res)
if err != nil {
return fmt.Errorf("client: could not extract error message: %w", err)
}
var errMsg string
// From client perspective, 409 isnt a failed request
if res.StatusCode == 409 {
errMsg = "client: conflict, object exists - "
} else if res.StatusCode == 403 {
errMsg = "client: forbidden - "
} else {
errMsg = "client: "
}
return fmt.Errorf("%srequest failed: status %d\n%s", errMsg, res.StatusCode, errRespMessage)
}
err = json.NewDecoder(res.Body).Decode(&result)
if err != nil {
return fmt.Errorf("client: could not unmarshal JSON: %w", err)
}
return nil
}
func (c *PinotAPIClient) CreateFormDataObject(endpoint string, body []byte, result any) error {
fullURL := c.pinotControllerUrl.JoinPath(endpoint).String()
req, err := http.NewRequest(http.MethodPost, fullURL, bytes.NewBuffer(body))
if err != nil {
return fmt.Errorf("client: could not create request: %w", err)
}
req.Header.Set("Content-Type", "multipart/form-data")
res, err := c.pinotHttp.Do(req)
if err != nil {
c.logErrorResp(res)
return fmt.Errorf("client: could not send request: %w", err)
}
// Check the status code
if res.StatusCode != http.StatusOK {
var errMsg string
// From client perspective, 409 isnt a failed request
if res.StatusCode == 409 {
errMsg = "client: conflict, object exists - "
} else if res.StatusCode == 403 {
errMsg = "client: forbidden - "
} else {
errMsg = "client: "
}
return fmt.Errorf("%srequest failed with status code: %d", errMsg, res.StatusCode)
}
err = json.NewDecoder(res.Body).Decode(&result)
if err != nil {
return fmt.Errorf("client: could not unmarshal JSON: %w", err)
}
return nil
}
func (c *PinotAPIClient) DeleteObject(endpoint string, queryParams map[string]string, result any) error {
fullURL := prepareRequestURL(c, endpoint)
c.encodeParams(fullURL, queryParams)
request, err := http.NewRequest(http.MethodDelete, fullURL.String(), nil)
if err != nil {
return fmt.Errorf("client: could not create request: %w", err)
}
c.log.Debug(fmt.Sprintf("attempting DELETE %s", fullURL.String()))
res, err := c.pinotHttp.Do(request)
if err != nil {
c.logErrorResp(res)
return fmt.Errorf("client: could not send request: %w", err)
}
if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusMultipleChoices {
errRespMessage, err := c.extractErrorMessage(res)
if err != nil {
return fmt.Errorf("client: could not extract error message: %w", err)
}
var errMsg string
// From client perspective, 409 isnt a failed request
switch res.StatusCode {
case http.StatusNotFound:
errMsg = "client: object can not be found - "
default:
errMsg = "client: "
}
return fmt.Errorf("%srequest failed: status %d\n%s", errMsg, res.StatusCode, errRespMessage)
}
err = json.NewDecoder(res.Body).Decode(&result)
if err != nil {
return fmt.Errorf("client: could not unmarshal JSON: %w", err)
}
return nil
}
func (c *PinotAPIClient) UpdateObject(endpoint string, queryParams map[string]string, body []byte, result any) error {
fullURL := c.pinotControllerUrl.JoinPath(endpoint)
c.encodeParams(fullURL, queryParams)
var req *http.Request
var err error
if body == nil {
c.log.Debug("body is nil")
req, err = http.NewRequest(http.MethodPut, fullURL.String(), nil)
} else {
req, err = http.NewRequest(http.MethodPut, fullURL.String(), bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
}
if err != nil {
return fmt.Errorf("client: could not create request: %w", err)
}
c.log.Debug(fmt.Sprintf("attempting PUT %s", fullURL.String()))
res, err := c.pinotHttp.Do(req)
if err != nil {
c.logErrorResp(res)
return fmt.Errorf("client: could not send request: %w", err)
}
if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusMultipleChoices {
errRespMessage, err := c.extractErrorMessage(res)
if err != nil {
return fmt.Errorf("client: could not extract error message: %w", err)
}
var errMsg string
// From client perspective, 409 isnt a failed request
if res.StatusCode == 404 {
errMsg = "client: object can not be found - "
} else {
errMsg = "client: "
}
return fmt.Errorf("%srequest failed: status %d\n%s", errMsg, res.StatusCode, errRespMessage)
}
err = json.NewDecoder(res.Body).Decode(&result)
if err != nil {
return fmt.Errorf("client: could not unmarshal JSON: %w", err)
}
return nil
}
func (c *PinotAPIClient) GetUsers() (*model.GetUsersResponse, error) {
var result model.GetUsersResponse
err := c.FetchData("/users", &result)
return &result, err
}
func (c *PinotAPIClient) GetUser(username string, component string) (*model.User, error) {
var result map[string]model.User
var resultUser model.User
endpoint := fmt.Sprintf("/users/%s?component=%s", username, component)
err := c.FetchData(endpoint, &result)
usernameWithComponent := fmt.Sprintf("%s_%s", username, component)
resultUser = result[usernameWithComponent]
return &resultUser, err
}
func (c *PinotAPIClient) CreateUser(body []byte) (*model.UserActionResponse, error) {
var result model.UserActionResponse
err := c.CreateObject("/users", body, &result)
return &result, err
}
func (c *PinotAPIClient) DeleteUser(username string, component string) (*model.UserActionResponse, error) {
deletionQueryParams := make(map[string]string)
deletionQueryParams["component"] = component
endpoint := fmt.Sprintf("/users/%s", username)
var result model.UserActionResponse
err := c.DeleteObject(endpoint, deletionQueryParams, &result)
return &result, err
}
func (c *PinotAPIClient) UpdateUser(username string, component string, passwordChanged bool, body []byte) (*model.UserActionResponse, error) {
updateQueryParams := make(map[string]string)
updateQueryParams["component"] = component
updateQueryParams["passwordChanged"] = strconv.FormatBool(passwordChanged)
var result model.UserActionResponse
endpoint := fmt.Sprintf("/users/%s", username)
err := c.UpdateObject(endpoint, updateQueryParams, body, &result)
return &result, err
}
func (c *PinotAPIClient) GetTables() (*model.GetTablesResponse, error) {
var result model.GetTablesResponse
err := c.FetchData("/tables", &result)
return &result, err
}
func (c *PinotAPIClient) GetTable(tableName string) (*model.GetTableResponse, error) {
var result model.GetTableResponse
endpoint := fmt.Sprintf("/tables/%s", tableName)
err := c.FetchData(endpoint, &result)
return &result, err
}
// TODO: ValidateTable (?)
// func (c *PinotAPIClient) ValidateTable(body []byte) (*model.UserActionResponse, error) {
// }
func (c *PinotAPIClient) CreateTable(body []byte) (*model.CreateTablesResponse, error) {
result := &model.CreateTablesResponse{}
err := c.CreateObject("/tables", body, result)
return result, err
}
func (c *PinotAPIClient) UpdateTable(tableName string, body []byte) (*model.UserActionResponse, error) {
var result model.UserActionResponse
endpoint := fmt.Sprintf("/tables/%s", tableName)
err := c.UpdateObject(endpoint, nil, body, &result)
return &result, err
}
func (c *PinotAPIClient) DeleteTable(tableName string) (*model.UserActionResponse, error) {
var result model.UserActionResponse
endpoint := fmt.Sprintf("/tables/%s", tableName)
err := c.DeleteObject(endpoint, nil, &result)
return &result, err
}
func (c *PinotAPIClient) CreateTableFromFile(tableConfigFile string) (*model.CreateTablesResponse, error) {
f, err := os.Open(tableConfigFile)
if err != nil {
return nil, fmt.Errorf("unable to open table config file: %w", err)
}
defer f.Close()
var tableConfig model.Table
err = json.NewDecoder(f).Decode(&tableConfig)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal table config: %w", err)
}
tableConfigBytes, err := json.Marshal(tableConfig)
if err != nil {
return nil, fmt.Errorf("unable to marshal table config: %w", err)
}
return c.CreateTable(tableConfigBytes)
}
func (c *PinotAPIClient) GetTableExternalView(tableName string) (*model.GetTableExternalViewResponse, error) {
var result model.GetTableExternalViewResponse
endpoint := fmt.Sprintf("/tables/%s/externalview", tableName)
err := c.FetchData(endpoint, &result)
return &result, err
}
func (c *PinotAPIClient) GetTableIdealState(tableName string) (*model.GetTableIdealStateResponse, error) {
var result model.GetTableIdealStateResponse
endpoint := fmt.Sprintf("/tables/%s/idealstate", tableName)
err := c.FetchData(endpoint, &result)
return &result, err
}
func (c *PinotAPIClient) GetTableIndexes(tableName string) (*model.GetTableIndexesResponse, error) {
var result model.GetTableIndexesResponse
endpoint := fmt.Sprintf("/tables/%s/indexes", tableName)
err := c.FetchData(endpoint, &result)
return &result, err
}
func (c *PinotAPIClient) GetTableInstances(tableName string) (*model.GetTableInstancesResponse, error) {
var result model.GetTableInstancesResponse
endpoint := fmt.Sprintf("/tables/%s/instances", tableName)
err := c.FetchData(endpoint, &result)
return &result, err
}
func (c *PinotAPIClient) GetAllTableLiveBrokers() (*model.GetLiveBrokersResponse, error) {
var result model.GetLiveBrokersResponse
err := c.FetchData("/tables/livebrokers", &result)
return &result, err
}
func (c *PinotAPIClient) GetTableLiveBrokers(tableName string) (*[]string, error) {
var result []string
endpoint := fmt.Sprintf("/tables/%s/livebrokers", tableName)
err := c.FetchData(endpoint, &result)
return &result, err
}
func (c *PinotAPIClient) GetTableMetadata(tableName string) (*model.GetTableMetadataResponse, error) {
var result model.GetTableMetadataResponse
endpoint := fmt.Sprintf("/tables/%s/metadata", tableName)
err := c.FetchData(endpoint, &result)
return &result, err
}
func (c *PinotAPIClient) RebuildBrokerResourceFromHelixTags(tableName string) (*model.UserActionResponse, error) {
var result model.UserActionResponse
endpoint := fmt.Sprintf("/tables/%s/rebuildBrokerResourceFromHelixTags", tableName)
err := c.CreateObject(endpoint, nil, &result)
return &result, err
}
func (c *PinotAPIClient) GetTableSchema(tableName string) (*model.Schema, error) {
var result model.Schema
endpoint := fmt.Sprintf("/tables/%s/schema", tableName)
err := c.FetchData(endpoint, &result)
return &result, err
}
func (c *PinotAPIClient) GetTableSize(tableName string) (*model.GetTableSizeResponse, error) {
var result model.GetTableSizeResponse
endpoint := fmt.Sprintf("/tables/%s/size", tableName)
err := c.FetchData(endpoint, &result)
return &result, err
}
func (c *PinotAPIClient) GetTableState(tableName string, tableType string) (*model.GetTableStateResponse, error) {
var result model.GetTableStateResponse
endpoint := fmt.Sprintf("/tables/%s/state?type=%s", tableName, tableType)
err := c.FetchData(endpoint, &result)
return &result, err
}
func (c *PinotAPIClient) ChangeTableState(tableName string, tableType string, state string) (*model.UserActionResponse, error) {
var result model.UserActionResponse
queryParams := make(map[string]string)
queryParams["state"] = state
queryParams["type"] = tableType
endpoint := fmt.Sprintf("/tables/%s/state", tableName)
err := c.UpdateObject(endpoint, queryParams, nil, &result)
return &result, err
}
func (c *PinotAPIClient) GetTableStats(tableName string) (*model.GetTableStatsResponse, error) {
var result model.GetTableStatsResponse
endpoint := fmt.Sprintf("/tables/%s/stats", tableName)
err := c.FetchData(endpoint, &result)
return &result, err
}
// GetSchemas returns a list of schemas
func (c *PinotAPIClient) GetSchemas() (*model.GetSchemaResponse, error) {
var result model.GetSchemaResponse
err := c.FetchData("/schemas", &result)
return &result, err
}
// GetSchema returns a schema
func (c *PinotAPIClient) GetSchema(schemaName string) (*model.Schema, error) {
var result model.Schema
err := c.FetchData(fmt.Sprintf("/schemas/%s", schemaName), &result)
return &result, err
}
// CreateSchema creates a new schema. if it already exists, it will nothing will happen
func (c *PinotAPIClient) CreateSchema(schema model.Schema) (*model.UserActionResponse, error) {
// validate schema first
schemaResp, err := c.ValidateSchema(schema)
if err != nil {
return nil, fmt.Errorf("unable to validate schema: %w", err)
}
if !schemaResp.Ok {
return nil, fmt.Errorf("schema is invalid: %s", schemaResp.Error)
}
var result model.UserActionResponse
schemaBytes, err := schema.AsBytes()
if err != nil {
return nil, fmt.Errorf("unable to marshal schema: %w", err)
}
err = c.CreateObject("/schemas", schemaBytes, result)
return &result, err
}
func (c *PinotAPIClient) CreateSchemaFromBytes(schemaBytes []byte) (*model.CreateSchemaResponse, error) {
var schema model.Schema
// Validate first
json.Unmarshal(schemaBytes, &schema)
schemaResp, err := c.ValidateSchema(schema)
if err != nil {
return nil, fmt.Errorf("unable to validate schema: %w", err)
}
if !schemaResp.Ok {
return nil, fmt.Errorf("schema is invalid: %s", schemaResp.Error)
}
var result model.CreateSchemaResponse
err = c.CreateObject("/schemas?override=false&force=false", schemaBytes, &result)
return &result, err
}
// CreateSchemaFromFile creates a new schema from a file and uses CreateSchema
func (c *PinotAPIClient) CreateSchemaFromFile(schemaFilePath string) (*model.UserActionResponse, error) {
f, err := os.Open(schemaFilePath)
if err != nil {
return nil, fmt.Errorf("unable to open schema file: %w", err)
}
defer f.Close()
var schema model.Schema
err = json.NewDecoder(f).Decode(&schema)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal schema: %w", err)
}
return c.CreateSchema(schema)
}
// ValidateSchema validates a schema
func (c *PinotAPIClient) ValidateSchema(schema model.Schema) (*model.ValidateSchemaResponse, error) {
schemaBytes, err := schema.AsBytes()
if err != nil {
return nil, fmt.Errorf("unable to marshal schema: %w", err)
}
fullUrl := c.pinotControllerUrl.JoinPath("schemas", "validate").String()
req, err := http.NewRequest("POST", fullUrl, bytes.NewBuffer(schemaBytes))
if err != nil {
return nil, fmt.Errorf("client: could not create request: %w", err)
}
res, err := c.pinotHttp.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("client: could not send request: %w", err)
}
if res.StatusCode == http.StatusInternalServerError {
return nil, fmt.Errorf("client: internal server error")
}
// Invalid schema in body
if res.StatusCode == http.StatusBadRequest {
var result map[string]string
err = json.NewDecoder(res.Body).Decode(&result)
if err != nil {
return nil, fmt.Errorf("client: could not unmarshal JSON: %w", err)
}
return &model.ValidateSchemaResponse{
Ok: false,
Error: result["error"],
}, nil
}
return &model.ValidateSchemaResponse{Ok: true}, nil
}
func (c *PinotAPIClient) UpdateSchemaFromBytes(schemaBytes []byte) (*model.UserActionResponse, error) {
var schema model.Schema
// Validate first
json.Unmarshal(schemaBytes, &schema)
schemaResp, err := c.ValidateSchema(schema)
if err != nil {
return nil, fmt.Errorf("unable to validate schema: %w", err)
}
if !schemaResp.Ok {
return nil, fmt.Errorf("schema is invalid: %s", schemaResp.Error)
}
var result model.UserActionResponse
err = c.CreateObject("/schemas", schemaBytes, &result)
return &result, err
}
func (c *PinotAPIClient) UpdateSchema(schema model.Schema) (*model.UserActionResponse, error) {
var result model.UserActionResponse
schemaBytes, err := schema.AsBytes()
if err != nil {
return nil, fmt.Errorf("unable to marshal schema: %w", err)
}
err = c.CreateObject("/schemas", schemaBytes, result) // Should be PUT?
return &result, err
}
func (c *PinotAPIClient) DeleteSchema(schemaName string) (*model.UserActionResponse, error) {
getTablesRes, err := c.GetTables()
if err != nil {
return nil, fmt.Errorf("unable to get tables names to check: %w", err)
}
for _, tableName := range getTablesRes.Tables {
if tableName == schemaName {
return nil, fmt.Errorf("can not delete schema %s, it is used by table %s", schemaName, tableName)
}
}
// proceed with deletion
var result model.UserActionResponse
err = c.DeleteObject(fmt.Sprintf("/schemas/%s", schemaName), nil, &result)
return &result, err
}
func (c *PinotAPIClient) GetSchemaFieldSpecs() (*model.GetSchemaFieldSpecsResponse, error) {
var result model.GetSchemaFieldSpecsResponse
err := c.FetchData(fmt.Sprintf("/schemas/fieldSpec"), &result)
return &result, err
}
// Segments
// TODO: Implement Create, Get, GetMetadata, Delete
// func (c *PinotAPIClient) CreateSegment(body []byte) (*model.UserActionResponse, error) {
// var result model.UserActionResponse
// err := c.CreateFormDataObject("/v2/segments", body, &result)
// return &result, err
// }
func (c *PinotAPIClient) GetSegments(tableName string) (model.GetSegmentsResponse, error) {
var result model.GetSegmentsResponse
err := c.FetchData(fmt.Sprintf("/segments/%s", tableName), &result)
return result, err
}
// func (c *PinotAPIClient) GetSegmentMetadata(tableName string, segmentName string) (*model.GetSegmentMetadataResponse, error) {
// var result model.GetSegmentMetadataResponse
// err := c.FetchData(fmt.Sprintf("/segments/%s/%s/metadata", tableName, segmentName), &result)
// return &result, err
// }
// func (c *PinotAPIClient) DeleteSegment(tableName string, segmentName string) (*model.UserActionResponse, error) {
// var result model.UserActionResponse
// err := c.DeleteObject(fmt.Sprintf("/segments/%s/choose", tableName, segmentName), nil, &result)
// return &result, err
// }
func (c *PinotAPIClient) ReloadTableSegments(tableName string) (*model.UserActionResponse, error) {
var result model.UserActionResponse
err := c.CreateObject(fmt.Sprintf("/segments/%s/reload", tableName), nil, &result)
return &result, err
}
func (c *PinotAPIClient) ReloadSegment(tableName string, segmentName string) (*model.UserActionResponse, error) {
var result model.UserActionResponse
err := c.CreateObject(fmt.Sprintf("/segments/%s/%s/reload", tableName, segmentName), nil, &result)
return &result, err
}
func (c *PinotAPIClient) ResetTableSegments(tableNameWithType string) (*model.UserActionResponse, error) {
var result model.UserActionResponse
err := c.CreateObject(fmt.Sprintf("/segments/%s/reset", tableNameWithType), nil, &result) // you must provide type in the tableName here e.g. airlineStats_OFFLINE
return &result, err
}
func (c *PinotAPIClient) ResetTableSegment(tableName string, segmentName string) (*model.UserActionResponse, error) {
var result model.UserActionResponse
err := c.CreateObject(fmt.Sprintf("/segments/%s/%s/reset", tableName, segmentName), nil, &result)
return &result, err
}
func (c *PinotAPIClient) GetSegmentTiers(tableName string, tableType string) (*model.GetSegmentTiersResponse, error) {
var result model.GetSegmentTiersResponse
err := c.FetchData(fmt.Sprintf("/segments/%s/tiers?type=%s", tableName, tableType), &result)
return &result, err
}
func (c *PinotAPIClient) GetSegmentCRC(tableName string) (*model.GetSegmentCRCResponse, error) {
var result model.GetSegmentCRCResponse
err := c.FetchData(fmt.Sprintf("/segments/%s/crc", tableName), &result)
return &result, err
}
func (c *PinotAPIClient) GetSegmentMetadata(tableName string) (*model.GetSegmentMetadataResponse, error) {
var result model.GetSegmentMetadataResponse
err := c.FetchData(fmt.Sprintf("/segments/%s/metadata", tableName), &result)
return &result, err
}
func (c *PinotAPIClient) GetSegmentZKMetadata(tableName string) (*model.GetSegmentZKMetadataResponse, error) {
var result model.GetSegmentZKMetadataResponse
err := c.FetchData(fmt.Sprintf("/segments/%s/zkmetadata", tableName), &result)
return &result, err
}
func (c *PinotAPIClient) UpdateSegmentZKTimeInterval(tableNameWithType string) (*model.UserActionResponse, error) {
var result model.UserActionResponse
err := c.CreateObject(fmt.Sprintf("/segments/%s/updateZkTimeInterval", tableNameWithType), nil, &result)
return &result, err
}
// Cluster
func (c *PinotAPIClient) GetClusterInfo() (*model.GetClusterResponse, error) {
var result model.GetClusterResponse
err := c.FetchData("/cluster/info", &result)
return &result, err
}
func (c *PinotAPIClient) GetClusterConfigs() (*model.GetClusterConfigResponse, error) {
var result model.GetClusterConfigResponse
err := c.FetchData("/cluster/configs", &result)
return &result, err
}
func (c *PinotAPIClient) UpdateClusterConfigs(body []byte) (*model.UserActionResponse, error) {
var result model.UserActionResponse
err := c.CreateObject("/cluster/configs", body, &result)
return &result, err
}
func (c *PinotAPIClient) DeleteClusterConfig(configName string) (*model.UserActionResponse, error) {
var result model.UserActionResponse
err := c.DeleteObject(fmt.Sprintf("/cluster/configs/%s", configName), nil, &result)
return &result, err
}
// Tenants
func (c *PinotAPIClient) GetTenants() (*model.GetTenantsResponse, error) {
var result model.GetTenantsResponse
err := c.FetchData("/tenants", &result)
return &result, err
}
func (c *PinotAPIClient) GetTenantInstances(tenantName string) (*model.GetTenantResponse, error) {
var result model.GetTenantResponse
err := c.FetchData(fmt.Sprintf("/tenants/%s", tenantName), &result)
return &result, err
}
func (c *PinotAPIClient) GetTenantTables(tenantName string) (*model.GetTablesResponse, error) {
var result model.GetTablesResponse
err := c.FetchData(fmt.Sprintf("/tenants/%s/tables", tenantName), &result)
return &result, err
}
func (c *PinotAPIClient) GetTenantMetadata(tenantName string) (*model.GetTenantMetadataResponse, error) {
var result model.GetTenantMetadataResponse
err := c.FetchData(fmt.Sprintf("/tenants/%s/metadata", tenantName), &result)
return &result, err
}
func (c *PinotAPIClient) CreateTenant(body []byte) (*model.UserActionResponse, error) {
var result model.UserActionResponse
err := c.CreateObject("/tenants", body, &result)
return &result, err
}
func (c *PinotAPIClient) UpdateTenant(body []byte) (*model.UserActionResponse, error) {
var result model.UserActionResponse
err := c.UpdateObject("/tenants", nil, body, &result)
return &result, err
}
func (c *PinotAPIClient) DeleteTenant(tenantName string, tenantType string) (*model.UserActionResponse, error) {
var result model.UserActionResponse
err := c.DeleteObject(fmt.Sprintf("/tenants/%s?type=%s", tenantName, tenantType), nil, &result)
return &result, err
}
func (c *PinotAPIClient) RebalanceTenant(tenantName string) (*model.UserActionResponse, error) {
var result model.UserActionResponse
err := c.CreateObject(fmt.Sprintf("/tenants/%s/rebalance", tenantName), nil, &result)
return &result, err
}
// Instances
func (c *PinotAPIClient) GetInstances() (*model.GetInstancesResponse, error) {
var result model.GetInstancesResponse
err := c.FetchData("/instances", &result)
return &result, err
}
func (c *PinotAPIClient) GetInstance(instanceName string) (*model.GetInstanceResponse, error) {
var result model.GetInstanceResponse
err := c.FetchData(fmt.Sprintf("/instances/%s", instanceName), &result)
return &result, err
}
func (c *PinotAPIClient) CreateInstance(body []byte) (*model.UserActionResponse, error) {
var result model.UserActionResponse
err := c.CreateObject("/instances", body, &result)
return &result, err
}
func (c *PinotAPIClient) UpdateInstance(instanceName string, body []byte) (*model.UserActionResponse, error) {
var result model.UserActionResponse
err := c.UpdateObject(fmt.Sprintf("/instances/%s", instanceName), nil, body, &result)
return &result, err
}
func (c *PinotAPIClient) DeleteInstance(instanceName string) (*model.UserActionResponse, error) {
var result model.UserActionResponse
err := c.DeleteObject(fmt.Sprintf("/instances/%s", instanceName), nil, &result)
return &result, err
}
func (c *PinotAPIClient) CheckPinotControllerAdminHealth() (*model.PlainTextAPIResponse, error) {
// Returns text/plain
var result model.PlainTextAPIResponse
err := c.FetchPlainText("/pinot-controller/admin", &result)
return &result, err
}
func (c *PinotAPIClient) CheckPinotControllerHealth() (*model.PlainTextAPIResponse, error) {
// Returns text/plain
var result model.PlainTextAPIResponse
err := c.FetchPlainText("/health", &result)
return &result, err
}
func (c *PinotAPIClient) extractErrorMessage(resp *http.Response) (string, error) {
resultBytes, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("unable to decode response from failed request: %s", err)
}
return string(resultBytes), nil
}
func (c *PinotAPIClient) logErrorResp(r *http.Response) {
var responseContent map[string]any
err := json.NewDecoder(r.Body).Decode(&responseContent)
if err != nil {
c.log.Debug(fmt.Sprintf("unable to decode response from failed request: %s", err))
return
}
c.log.Debug(fmt.Sprintf("response from failed request: %s", responseContent))
}
func (c *PinotAPIClient) encodeParams(fullUrl *url.URL, params map[string]string) {
query := fullUrl.Query()
for key, value := range params {
query.Set(key, value)
}
fullUrl.RawQuery = query.Encode()
}
func (c *PinotAPIClient) generateQueryParams(queryString string) map[string]string {
parts := strings.SplitN(queryString, "=", 2)
// Create a map and add the key-value pair
m := make(map[string]string)
if len(parts) == 2 {
m[parts[0]] = parts[1]
}
return m
}
func prepareRequestURL(c *PinotAPIClient, endpoint string) *url.URL {
pathAndQuery := strings.SplitN(endpoint, "?", 2)
var path string
if len(pathAndQuery) > 0 {
path = pathAndQuery[0]
}
var queryString string
if len(pathAndQuery) > 1 {
queryString = pathAndQuery[1]
}
queryMap := c.generateQueryParams(queryString)
fullURL := c.pinotControllerUrl.JoinPath(path)
c.encodeParams(fullURL, queryMap)
return fullURL
}