-
Notifications
You must be signed in to change notification settings - Fork 40
/
metadata_handler.go
2995 lines (2588 loc) · 94.6 KB
/
metadata_handler.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
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gater
import (
"bytes"
"encoding/base64"
"encoding/xml"
"fmt"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
"github.com/cosmos/gogoproto/jsonpb"
"github.com/ethereum/go-ethereum/common"
"github.com/gorilla/mux"
"github.com/bnb-chain/greenfield-storage-provider/base/types/gfsperrors"
"github.com/bnb-chain/greenfield-storage-provider/modular/metadata/types"
"github.com/bnb-chain/greenfield-storage-provider/pkg/log"
"github.com/bnb-chain/greenfield-storage-provider/pkg/metrics"
"github.com/bnb-chain/greenfield-storage-provider/util"
"github.com/bnb-chain/greenfield/types/resource"
resource_types "github.com/bnb-chain/greenfield/types/resource"
"github.com/bnb-chain/greenfield/types/s3util"
payment_types "github.com/bnb-chain/greenfield/x/payment/types"
permission_types "github.com/bnb-chain/greenfield/x/permission/types"
sp_types "github.com/bnb-chain/greenfield/x/sp/types"
storage_types "github.com/bnb-chain/greenfield/x/storage/types"
virtual_types "github.com/bnb-chain/greenfield/x/virtualgroup/types"
)
const (
MaximumGetGroupListLimit = 1000
MaximumGetGroupListOffset = 100000
MaximumListObjectsAndBucketsSize = 1000
MaximumIDSize = 100
DefaultGetGroupListLimit = 50
DefaultGetGroupListOffset = 0
HandlerSuccess = "success"
HandlerFailure = "failure"
HandlerLevel = "handler"
)
func MetadataHandlerFailureMetrics(err error, startTime time.Time, handlerName string) {
gfspErr := gfsperrors.MakeGfSpError(err)
code := gfspErr.HttpStatusCode
metrics.ReqCounter.WithLabelValues(GatewayTotalFailure).Inc()
metrics.ReqTime.WithLabelValues(GatewayTotalFailure).Observe(time.Since(startTime).Seconds())
metrics.MetadataReqTime.WithLabelValues(HandlerFailure, HandlerLevel, handlerName, strconv.Itoa(int(code))).Observe(time.Since(startTime).Seconds())
}
func MetadataHandlerSuccessMetrics(startTime time.Time, handlerName string) {
metrics.ReqCounter.WithLabelValues(GatewayTotalSuccess).Inc()
metrics.ReqTime.WithLabelValues(GatewayTotalSuccess).Observe(time.Since(startTime).Seconds())
metrics.MetadataReqTime.WithLabelValues(HandlerSuccess, HandlerLevel, handlerName, strconv.Itoa(http.StatusOK)).Observe(time.Since(startTime).Seconds())
}
// getUserBucketsHandler handle get object request
func (g *GateModular) getUserBucketsHandler(w http.ResponseWriter, r *http.Request) {
var (
requestIncludeRemoved string
includedRemoved bool
queryParams url.Values
err error
respBytes []byte
reqCtx *RequestContext
)
startTime := time.Now()
defer func() {
reqCtx.Cancel()
handlerName := mux.CurrentRoute(r).GetName()
if err != nil {
reqCtx.SetError(gfsperrors.MakeGfSpError(err))
log.CtxErrorw(reqCtx.Context(), "failed to get user buckets", reqCtx.String())
MakeErrorResponse(w, err)
MetadataHandlerFailureMetrics(err, startTime, handlerName)
} else {
MetadataHandlerSuccessMetrics(startTime, handlerName)
}
}()
reqCtx, _ = NewRequestContext(r, g)
queryParams = reqCtx.request.URL.Query()
requestIncludeRemoved = queryParams.Get(ListObjectsIncludeRemovedQuery)
if ok := common.IsHexAddress(r.Header.Get(GnfdUserAddressHeader)); !ok {
log.Errorw("failed to check account id", "account_id", reqCtx.account, "error", err)
err = ErrInvalidHeader
return
}
if requestIncludeRemoved != "" {
includedRemoved, err = strconv.ParseBool(requestIncludeRemoved)
if err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to parse requestRemoved", "request_include_removed", requestIncludeRemoved, "error", err)
err = ErrInvalidQuery
return
}
}
resp, err := g.baseApp.GfSpClient().GetUserBuckets(reqCtx.Context(), r.Header.Get(GnfdUserAddressHeader), includedRemoved)
if err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to get user buckets", "error", err)
return
}
grpcResponse := &types.GfSpGetUserBucketsResponse{
Buckets: resp,
}
respBytes, err = xml.Marshal(grpcResponse)
if err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to get user buckets", "error", err)
return
}
respBytes = processVGFInfoBucketXmlResponse(respBytes, grpcResponse.Buckets)
w.Header().Set(ContentTypeHeader, ContentTypeXMLHeaderValue)
w.Write(respBytes)
}
// listObjectsByBucketNameHandler handle list objects by bucket name request
func (g *GateModular) listObjectsByBucketNameHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
respBytes []byte
maxKeys uint64
reqCtx *RequestContext
ok bool
requestBucketName string
requestMaxKeys string
requestStartAfter string
requestContinuationToken string
requestDelimiter string
requestPrefix string
requestIncludeRemoved string
continuationToken string
includedRemoved bool
decodedContinuationToken []byte
queryParams url.Values
)
startTime := time.Now()
defer func() {
reqCtx.Cancel()
handlerName := mux.CurrentRoute(r).GetName()
if err != nil {
reqCtx.SetError(gfsperrors.MakeGfSpError(err))
log.CtxErrorw(reqCtx.Context(), "failed to list objects by bucket name", reqCtx.String())
MakeErrorResponse(w, err)
MetadataHandlerFailureMetrics(err, startTime, handlerName)
} else {
MetadataHandlerSuccessMetrics(startTime, handlerName)
}
}()
reqCtx, _ = NewRequestContext(r, g)
queryParams = reqCtx.request.URL.Query()
requestBucketName = reqCtx.bucketName
requestMaxKeys = queryParams.Get(ListObjectsMaxKeysQuery)
requestStartAfter = queryParams.Get(ListObjectsStartAfterQuery)
requestContinuationToken = queryParams.Get(ListObjectsContinuationTokenQuery)
requestDelimiter = queryParams.Get(ListObjectsDelimiterQuery)
requestPrefix = queryParams.Get(ListObjectsPrefixQuery)
requestIncludeRemoved = queryParams.Get(ListObjectsIncludeRemovedQuery)
if requestDelimiter != "" && requestDelimiter != "/" {
log.CtxErrorw(reqCtx.Context(), "failed to check delimiter", "delimiter", requestDelimiter, "error", err)
err = ErrInvalidQuery
return
}
if err = s3util.CheckValidBucketName(requestBucketName); err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to check bucket name", "bucket_name", requestBucketName, "error", err)
err = ErrInvalidQuery
return
}
if requestMaxKeys != "" {
if maxKeys, err = util.StringToUint64(requestMaxKeys); err != nil || maxKeys == 0 {
log.CtxErrorw(reqCtx.Context(), "failed to parse or check maxKeys", "max_keys", requestMaxKeys, "error", err)
err = ErrInvalidQuery
return
}
}
if requestStartAfter != "" {
if err = s3util.CheckValidObjectName(requestStartAfter); err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to check startAfter", "start_after", requestStartAfter, "error", err)
err = ErrInvalidQuery
return
}
}
if requestContinuationToken != "" {
decodedContinuationToken, err = base64.StdEncoding.DecodeString(requestContinuationToken)
if err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to check requestContinuationToken", "continuation_token", requestContinuationToken, "error", err)
err = ErrInvalidQuery
return
}
continuationToken = string(decodedContinuationToken)
if err = s3util.CheckValidObjectName(continuationToken); err != nil {
log.Errorw("failed to check requestContinuationToken", "continuation_token", continuationToken, "error", err)
err = ErrInvalidQuery
return
}
if !strings.HasPrefix(continuationToken, requestPrefix) {
log.Errorw("failed to check requestContinuationToken", "continuation_token", continuationToken, "prefix", requestPrefix, "error", err)
err = ErrInvalidQuery
return
}
}
if requestIncludeRemoved != "" {
includedRemoved, err = strconv.ParseBool(requestIncludeRemoved)
if err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to parse requestRemoved", "request_include_removed", requestIncludeRemoved, "error", err)
err = ErrInvalidQuery
return
}
}
if ok = checkValidObjectPrefix(requestPrefix); !ok {
log.CtxErrorw(reqCtx.Context(), "failed to check requestPrefix", "prefix", requestPrefix, "error", err)
err = ErrInvalidQuery
return
}
if requestContinuationToken == "" {
continuationToken = requestStartAfter
}
objects,
keyCount,
maxKeys,
isTruncated,
nextContinuationToken,
name,
prefix,
delimiter,
commonPrefixes,
continuationToken,
err :=
g.baseApp.GfSpClient().ListObjectsByBucketName(
reqCtx.Context(),
requestBucketName,
"",
maxKeys,
requestStartAfter,
continuationToken,
requestDelimiter,
requestPrefix,
includedRemoved)
if err != nil {
log.Errorf("failed to list objects by bucket name", "error", err)
return
}
grpcResponse := &types.GfSpListObjectsByBucketNameResponse{
Objects: objects,
KeyCount: keyCount,
MaxKeys: maxKeys,
IsTruncated: isTruncated,
NextContinuationToken: nextContinuationToken,
Name: name,
Prefix: prefix,
Delimiter: delimiter,
CommonPrefixes: commonPrefixes,
ContinuationToken: continuationToken,
}
respBytes, err = xml.Marshal(grpcResponse)
if err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to get user buckets", "error", err)
return
}
respBytes = processObjectsXmlResponse(respBytes, grpcResponse.Objects)
w.Header().Set(ContentTypeHeader, ContentTypeXMLHeaderValue)
w.Write(respBytes)
}
// getObjectMetaHandler handle get object metadata request
func (g *GateModular) getObjectMetaHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
respBytes []byte
reqCtx *RequestContext
)
startTime := time.Now()
defer func() {
reqCtx.Cancel()
handlerName := mux.CurrentRoute(r).GetName()
if err != nil {
reqCtx.SetError(gfsperrors.MakeGfSpError(err))
log.CtxErrorw(reqCtx.Context(), "failed to get object meta", reqCtx.String())
MakeErrorResponse(w, err)
MetadataHandlerFailureMetrics(err, startTime, handlerName)
} else {
MetadataHandlerSuccessMetrics(startTime, handlerName)
}
}()
reqCtx, _ = NewRequestContext(r, g)
if err = s3util.CheckValidBucketName(reqCtx.bucketName); err != nil {
log.Errorw("failed to check bucket name", "bucket_name", reqCtx.bucketName, "error", err)
err = ErrInvalidQuery
return
}
if err = s3util.CheckValidObjectName(reqCtx.objectName); err != nil {
log.Errorw("failed to check object name", "object_name", reqCtx.objectName, "error", err)
err = ErrInvalidQuery
return
}
resp, err := g.baseApp.GfSpClient().GetObjectMeta(reqCtx.Context(), reqCtx.objectName, reqCtx.bucketName, true)
if err != nil {
log.Errorf("failed to get object meta", "error", err)
return
}
grpcResponse := &types.GfSpGetObjectMetaResponse{
Object: resp,
}
respBytes, err = xml.Marshal(grpcResponse)
if err != nil {
log.Errorf("failed to get object meta", "error", err)
return
}
var objects = []*types.Object{grpcResponse.Object}
respBytes = processObjectsXmlResponse(respBytes, objects)
w.Header().Set(ContentTypeHeader, ContentTypeXMLHeaderValue)
w.Write(respBytes)
}
// getBucketMetaHandler handle get bucket metadata request
func (g *GateModular) getBucketMetaHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
respBytes []byte
reqCtx *RequestContext
)
startTime := time.Now()
defer func() {
reqCtx.Cancel()
handlerName := mux.CurrentRoute(r).GetName()
if err != nil {
reqCtx.SetError(gfsperrors.MakeGfSpError(err))
log.CtxErrorw(reqCtx.Context(), "failed to get bucket meta", reqCtx.String())
MakeErrorResponse(w, err)
MetadataHandlerFailureMetrics(err, startTime, handlerName)
} else {
MetadataHandlerSuccessMetrics(startTime, handlerName)
}
}()
reqCtx, _ = NewRequestContext(r, g)
if err = s3util.CheckValidBucketName(reqCtx.bucketName); err != nil {
log.Errorw("failed to check bucket name", "bucket_name", reqCtx.bucketName, "error", err)
err = ErrInvalidQuery
return
}
bucket, streamRecord, err := g.baseApp.GfSpClient().GetBucketMeta(reqCtx.Context(), reqCtx.bucketName, true)
if err != nil {
log.Errorf("failed to get bucket metadata", "error", err)
return
}
grpcResponse := &types.GfSpGetBucketMetaResponse{
Bucket: bucket,
StreamRecord: streamRecord,
}
respBytes, err = xml.Marshal(grpcResponse)
if err != nil {
log.Errorf("failed to get bucket metadata", "error", err)
return
}
var bucketsWithPayment = []*types.GfSpGetBucketMetaResponse{grpcResponse}
respBytes = processBucketsWithPaymentResponse(respBytes, bucketsWithPayment)
w.Header().Set(ContentTypeHeader, ContentTypeXMLHeaderValue)
w.Write(respBytes)
}
// verifyPermissionHandler handle verify permission request
func (g *GateModular) verifyPermissionHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
operator string
objectName string
actionType string
action int32
respBytes []byte
queryParams url.Values
effect *permission_types.Effect
reqCtx *RequestContext
)
startTime := time.Now()
defer func() {
reqCtx.Cancel()
handlerName := mux.CurrentRoute(r).GetName()
if err != nil {
reqCtx.SetError(gfsperrors.MakeGfSpError(err))
log.CtxErrorw(reqCtx.Context(), "failed to verify permission", reqCtx.String())
MakeErrorResponse(w, err)
MetadataHandlerFailureMetrics(err, startTime, handlerName)
} else {
MetadataHandlerSuccessMetrics(startTime, handlerName)
}
}()
reqCtx, _ = NewRequestContext(r, g)
queryParams = reqCtx.request.URL.Query()
objectName = queryParams.Get(VerifyPermissionObjectQuery)
operator = reqCtx.vars[VerifyPermissionOperator]
actionType = reqCtx.vars[VerifyPermissionActionType]
if err = s3util.CheckValidBucketName(reqCtx.bucketName); err != nil {
log.Errorw("failed to check bucket name", "bucket_name", reqCtx.bucketName, "error", err)
err = ErrInvalidQuery
return
}
if objectName != "" {
if err = s3util.CheckValidObjectName(objectName); err != nil {
log.Errorw("failed to check object name", "object_name", objectName, "error", err)
err = ErrInvalidQuery
return
}
}
if ok := common.IsHexAddress(operator); !ok {
log.Errorw("failed to check operator", "operator", operator, "error", err)
err = ErrInvalidQuery
return
}
if actionType == "" {
log.Errorw("failed to check action type", "action_type", actionType, "error", err)
err = ErrInvalidQuery
return
}
action, err = util.StringToInt32(actionType)
if err != nil {
log.Errorw("failed to check action type", "action_type", actionType, "error", err)
err = ErrInvalidQuery
return
}
effect, err = g.baseApp.GfSpClient().VerifyPermission(reqCtx.Context(), operator, reqCtx.bucketName, objectName, permission_types.ActionType(action))
if err != nil {
log.Errorf("failed to verify permission", "error", err)
return
}
grpcResponse := &storage_types.QueryVerifyPermissionResponse{Effect: *effect}
respBytes, err = xml.Marshal(grpcResponse)
if err != nil {
log.Errorf("failed to verify permission", "error", err)
return
}
w.Header().Set(ContentTypeHeader, ContentTypeXMLHeaderValue)
w.Write(respBytes)
}
// getGroupListHandler handle get group list request
func (g *GateModular) getGroupListHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
respBytes []byte
queryParams url.Values
limitStr string
offsetStr string
name string
prefix string
sourceType string
limit int64
offset int64
count int64
groups []*types.Group
reqCtx *RequestContext
)
startTime := time.Now()
defer func() {
reqCtx.Cancel()
handlerName := mux.CurrentRoute(r).GetName()
if err != nil {
reqCtx.SetError(gfsperrors.MakeGfSpError(err))
log.CtxErrorw(reqCtx.Context(), "failed to get group list", reqCtx.String())
MakeErrorResponse(w, err)
MetadataHandlerFailureMetrics(err, startTime, handlerName)
} else {
MetadataHandlerSuccessMetrics(startTime, handlerName)
}
}()
reqCtx, _ = NewRequestContext(r, g)
queryParams = reqCtx.request.URL.Query()
sourceType = queryParams.Get(GetGroupListSourceTypeQuery)
limitStr = queryParams.Get(GetGroupListLimitQuery)
offsetStr = queryParams.Get(GetGroupListOffsetQuery)
name = queryParams.Get(GetGroupListNameQuery)
prefix = queryParams.Get(GetGroupListPrefixQuery)
if name == "" {
log.CtxErrorw(reqCtx.Context(), "failed to check name", "name", name, "error", err)
err = ErrInvalidQuery
return
}
if prefix == "" {
log.CtxErrorw(reqCtx.Context(), "failed to check prefix", "prefix", name, "error", err)
err = ErrInvalidQuery
return
}
if limitStr != "" {
if limit, err = util.StringToInt64(limitStr); err != nil || limit <= 0 {
log.CtxErrorw(reqCtx.Context(), "failed to parse or check limit", "limit", limitStr, "error", err)
err = ErrInvalidQuery
return
}
if limit > 1000 {
limit = MaximumGetGroupListLimit
}
} else {
limit = DefaultGetGroupListLimit
}
if offsetStr != "" {
if offset, err = util.StringToInt64(offsetStr); err != nil || offset < 0 || offset > MaximumGetGroupListOffset {
log.CtxErrorw(reqCtx.Context(), "failed to parse or check offset", "offset", offsetStr, "error", err)
err = ErrInvalidQuery
return
}
} else {
offset = DefaultGetGroupListOffset
}
if sourceType != "" {
if _, ok := storage_types.SourceType_value[sourceType]; !ok {
log.CtxErrorw(reqCtx.Context(), "failed to parse or check source type", "source-type", sourceType, "error", err)
err = ErrInvalidQuery
return
}
}
groups, count, err = g.baseApp.GfSpClient().GetGroupList(reqCtx.Context(), name, prefix, sourceType, limit, offset, false)
if err != nil {
log.Errorf("failed to get group list", "error", err)
return
}
grpcResponse := &types.GfSpGetGroupListResponse{
Groups: groups,
Count: count,
}
respBytes, err = xml.Marshal(grpcResponse)
if err != nil {
log.Errorf("failed to get group list", "error", err)
return
}
respBytes = processGroupsXmlResponse(respBytes, grpcResponse.Groups)
w.Header().Set(ContentTypeHeader, ContentTypeXMLHeaderValue)
w.Write(respBytes)
}
type GfSpListObjectsByIDsResponse types.GfSpListObjectsByIDsResponse
type ObjectEntry struct {
Id uint64
Value *types.Object
}
func (m GfSpListObjectsByIDsResponse) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if len(m.Objects) == 0 {
return nil
}
err := e.EncodeToken(start)
if err != nil {
return err
}
for k, v := range m.Objects {
e.Encode(ObjectEntry{Id: k, Value: v})
}
return e.EncodeToken(start.End())
}
// listObjectsByIDsHandler list objects by object ids
func (g *GateModular) listObjectsByIDsHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
respBytes []byte
objects map[uint64]*types.Object
objectIDMap map[uint64]bool
ok bool
requestObjectIDs string
objectID uint64
idsStr []string
objectIDs []uint64
reqCtx *RequestContext
queryParams url.Values
)
startTime := time.Now()
defer func() {
reqCtx.Cancel()
handlerName := mux.CurrentRoute(r).GetName()
if err != nil {
reqCtx.SetError(gfsperrors.MakeGfSpError(err))
log.CtxErrorw(reqCtx.Context(), "failed to list objects by ids", reqCtx.String())
MakeErrorResponse(w, err)
MetadataHandlerFailureMetrics(err, startTime, handlerName)
} else {
MetadataHandlerSuccessMetrics(startTime, handlerName)
}
}()
reqCtx, _ = NewRequestContext(r, g)
queryParams = reqCtx.request.URL.Query()
requestObjectIDs = queryParams.Get(IDsQuery)
// extract IDs from the input value, e.g., &id=1,2,3,4
idsStr = strings.Split(requestObjectIDs, ",")
for _, idStr := range idsStr {
objectID, err = util.StringToUint64(idStr)
if err != nil {
log.Errorf("failed to check ids", "error", err)
err = ErrInvalidQuery
return
}
objectIDs = append(objectIDs, objectID)
}
if len(objectIDs) == 0 || len(objectIDs) > MaximumIDSize {
log.Errorf("failed to check ids", "error", err)
err = ErrInvalidQuery
return
}
// check if the input IDs have duplicate values
objectIDMap = make(map[uint64]bool)
for _, id := range objectIDs {
if _, ok = objectIDMap[id]; ok {
// repeat id keys in request
log.Errorf("failed to check ids", "error", err)
err = ErrInvalidQuery
return
}
objectIDMap[id] = true
}
objects, err = g.baseApp.GfSpClient().ListObjectsByIDs(reqCtx.Context(), objectIDs, false)
if err != nil {
log.Errorf("failed to list objects by ids", "error", err)
return
}
grpcResponse := &types.GfSpListObjectsByIDsResponse{Objects: objects}
respBytes, err = xml.Marshal((*GfSpListObjectsByIDsResponse)(grpcResponse))
if err != nil {
log.Errorf("failed to list objects by ids", "error", err)
return
}
respBytes = processObjectsMapXmlResponse(respBytes, grpcResponse.Objects)
w.Header().Set(ContentTypeHeader, ContentTypeXMLHeaderValue)
w.Write(respBytes)
}
type GfSpListBucketsByIDsResponse types.GfSpListBucketsByIDsResponse
type BucketEntry struct {
Id uint64
Value *types.Bucket
}
func (m GfSpListBucketsByIDsResponse) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if len(m.Buckets) == 0 {
return nil
}
err := e.EncodeToken(start)
if err != nil {
return err
}
for k, v := range m.Buckets {
e.Encode(BucketEntry{Id: k, Value: v})
}
return e.EncodeToken(start.End())
}
// listBucketsByIDsHandler list buckets by bucket ids
func (g *GateModular) listBucketsByIDsHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
respBytes []byte
buckets map[uint64]*types.Bucket
bucketIDMap map[uint64]bool
ok bool
requestBucketIDs string
bucketID uint64
idsStr []string
bucketIDs []uint64
reqCtx *RequestContext
queryParams url.Values
)
startTime := time.Now()
defer func() {
reqCtx.Cancel()
handlerName := mux.CurrentRoute(r).GetName()
if err != nil {
reqCtx.SetError(gfsperrors.MakeGfSpError(err))
log.CtxErrorw(reqCtx.Context(), "failed to list buckets by ids", reqCtx.String())
MakeErrorResponse(w, err)
MetadataHandlerFailureMetrics(err, startTime, handlerName)
} else {
MetadataHandlerSuccessMetrics(startTime, handlerName)
}
}()
reqCtx, _ = NewRequestContext(r, g)
queryParams = reqCtx.request.URL.Query()
requestBucketIDs = queryParams.Get(IDsQuery)
// extract IDs from the input value, e.g., &id=1,2,3,4
idsStr = strings.Split(requestBucketIDs, ",")
for _, idStr := range idsStr {
bucketID, err = util.StringToUint64(idStr)
if err != nil {
log.Errorf("failed to check ids", "error", err)
err = ErrInvalidQuery
return
}
bucketIDs = append(bucketIDs, bucketID)
}
if len(bucketIDs) == 0 || len(bucketIDs) > MaximumIDSize {
log.Errorf("failed to check ids", "error", err)
err = ErrInvalidQuery
return
}
// check if the input IDs have duplicate values
bucketIDMap = make(map[uint64]bool)
for _, id := range bucketIDs {
if _, ok = bucketIDMap[id]; ok {
// repeat id keys in request
log.Errorf("failed to check ids", "error", err)
err = ErrInvalidQuery
return
}
bucketIDMap[id] = true
}
buckets, err = g.baseApp.GfSpClient().ListBucketsByIDs(reqCtx.Context(), bucketIDs, false)
if err != nil {
log.Errorf("failed to list buckets by ids", "error", err)
return
}
grpcResponse := &types.GfSpListBucketsByIDsResponse{Buckets: buckets}
respBytes, err = xml.Marshal((*GfSpListBucketsByIDsResponse)(grpcResponse))
if err != nil {
log.Errorf("failed to list buckets by ids", "error", err)
return
}
respBytes = processBucketsMapXmlResponse(respBytes, grpcResponse.Buckets)
w.Header().Set(ContentTypeHeader, ContentTypeXMLHeaderValue)
w.Write(respBytes)
}
// getPaymentByBucketIDHandler get payment by bucket id
func (g *GateModular) getPaymentByBucketIDHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
respBytes []byte
payment *payment_types.StreamRecord
bucketIDStr string
bucketID int64
queryParams url.Values
reqCtx *RequestContext
)
startTime := time.Now()
defer func() {
reqCtx.Cancel()
handlerName := mux.CurrentRoute(r).GetName()
if err != nil {
reqCtx.SetError(gfsperrors.MakeGfSpError(err))
log.CtxErrorw(reqCtx.Context(), "failed to get payment by bucket id", reqCtx.String())
MakeErrorResponse(w, err)
MetadataHandlerFailureMetrics(err, startTime, handlerName)
} else {
MetadataHandlerSuccessMetrics(startTime, handlerName)
}
}()
reqCtx, _ = NewRequestContext(r, g)
queryParams = reqCtx.request.URL.Query()
bucketIDStr = queryParams.Get(BucketIDQuery)
if bucketID, err = util.StringToInt64(bucketIDStr); err != nil || bucketID < 0 {
log.CtxErrorw(reqCtx.Context(), "failed to parse or check bucket id", "bucket-id", bucketIDStr, "error", err)
err = ErrInvalidQuery
return
}
payment, err = g.baseApp.GfSpClient().GetPaymentByBucketID(reqCtx.Context(), bucketID, false)
if err != nil {
log.Errorf("failed to get payment by bucket id", "error", err)
return
}
grpcResponse := &types.GfSpGetPaymentByBucketIDResponse{StreamRecord: payment}
respBytes, err = xml.Marshal(grpcResponse)
if err != nil {
log.Errorf("failed to get payment by bucket id", "error", err)
return
}
w.Header().Set(ContentTypeHeader, ContentTypeXMLHeaderValue)
w.Write(respBytes)
}
// getPaymentByBucketNameHandler handle get payment by bucket name request
func (g *GateModular) getPaymentByBucketNameHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
respBytes []byte
reqCtx *RequestContext
bucketName string
payment *payment_types.StreamRecord
)
startTime := time.Now()
defer func() {
reqCtx.Cancel()
handlerName := mux.CurrentRoute(r).GetName()
if err != nil {
reqCtx.SetError(gfsperrors.MakeGfSpError(err))
log.CtxErrorw(reqCtx.Context(), "failed to get payment by bucket name", reqCtx.String())
MakeErrorResponse(w, err)
MetadataHandlerFailureMetrics(err, startTime, handlerName)
} else {
MetadataHandlerSuccessMetrics(startTime, handlerName)
}
}()
reqCtx, _ = NewRequestContext(r, g)
bucketName = reqCtx.bucketName
if err = s3util.CheckValidBucketName(bucketName); err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to check bucket name", "bucket-name", bucketName, "error", err)
err = ErrInvalidQuery
return
}
payment, err = g.baseApp.GfSpClient().GetPaymentByBucketName(reqCtx.Context(), bucketName, false)
if err != nil {
log.Errorf("failed to get payment by bucket name", "error", err)
return
}
grpcResponse := &types.GfSpGetPaymentByBucketNameResponse{StreamRecord: payment}
respBytes, err = xml.Marshal(grpcResponse)
if err != nil {
log.Errorf("failed to get payment by bucket name", "error", err)
return
}
w.Header().Set(ContentTypeHeader, ContentTypeXMLHeaderValue)
w.Write(respBytes)
}
// getBucketByBucketNameHandler handle get bucket by bucket name request
func (g *GateModular) getBucketByBucketNameHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
respBytes []byte
reqCtx *RequestContext
bucketName string
bucket *types.Bucket
)
startTime := time.Now()
defer func() {
reqCtx.Cancel()
handlerName := mux.CurrentRoute(r).GetName()
if err != nil {
reqCtx.SetError(gfsperrors.MakeGfSpError(err))
log.CtxErrorw(reqCtx.Context(), "failed to get bucket by bucket name", reqCtx.String())
MakeErrorResponse(w, err)
MetadataHandlerFailureMetrics(err, startTime, handlerName)
} else {
MetadataHandlerSuccessMetrics(startTime, handlerName)
}
}()
reqCtx, _ = NewRequestContext(r, g)
bucketName = reqCtx.bucketName
if err = s3util.CheckValidBucketName(bucketName); err != nil {
err = ErrInvalidQuery
log.CtxErrorw(reqCtx.Context(), "failed to check bucket name", "bucket-name", bucketName, "error", err)
return
}
bucket, err = g.baseApp.GfSpClient().GetBucketByBucketName(reqCtx.Context(), bucketName, false)
if err != nil {
log.Errorf("failed to get bucket by bucket name", "error", err)
return
}
grpcResponse := &types.GfSpGetBucketByBucketNameResponse{Bucket: bucket}
respBytes, err = xml.Marshal(grpcResponse)
if err != nil {
log.Errorf("failed to get bucket by bucket name", "error", err)
return
}
w.Header().Set(ContentTypeHeader, ContentTypeXMLHeaderValue)
w.Write(respBytes)
}
// getBucketByBucketIDHandler handle get bucket by bucket id
func (g *GateModular) getBucketByBucketIDHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
respBytes []byte
bucket *types.Bucket
bucketIDStr string
bucketID int64
queryParams url.Values
reqCtx *RequestContext
)
startTime := time.Now()
defer func() {
reqCtx.Cancel()
handlerName := mux.CurrentRoute(r).GetName()
if err != nil {
reqCtx.SetError(gfsperrors.MakeGfSpError(err))
log.CtxErrorw(reqCtx.Context(), "failed to get bucket by bucket id", reqCtx.String())
MakeErrorResponse(w, err)
MetadataHandlerFailureMetrics(err, startTime, handlerName)
} else {
MetadataHandlerSuccessMetrics(startTime, handlerName)
}
}()
reqCtx, _ = NewRequestContext(r, g)
queryParams = reqCtx.request.URL.Query()
bucketIDStr = queryParams.Get(BucketIDQuery)
if bucketID, err = util.StringToInt64(bucketIDStr); err != nil || bucketID < 0 {
log.CtxErrorw(reqCtx.Context(), "failed to parse or check bucket id", "bucket-id", bucketIDStr, "error", err)
err = ErrInvalidQuery
return
}
bucket, err = g.baseApp.GfSpClient().GetBucketByBucketID(reqCtx.Context(), bucketID, false)
if err != nil {