From ca15acf1d3381b85000cf09f898811d9d9bf1d83 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Thu, 30 Nov 2023 11:28:29 +0300 Subject: [PATCH] [#257] router: Use named constants Signed-off-by: Denis Kirillov --- api/middleware/metrics.go | 36 +++--- api/router.go | 231 +++++++++++++++++++------------------- 2 files changed, 134 insertions(+), 133 deletions(-) diff --git a/api/middleware/metrics.go b/api/middleware/metrics.go index cba2124..a0b860c 100644 --- a/api/middleware/metrics.go +++ b/api/middleware/metrics.go @@ -107,27 +107,27 @@ func stats(f http.HandlerFunc, resolveCID cidResolveFunc, appMetrics *metrics.Ap func requestTypeFromAPI(api string) metrics.RequestType { switch api { - case "Options", "HeadObject", "HeadBucket": + case OptionsOperation, HeadObjectOperation, HeadBucketOperation: return metrics.HEADRequest - case "CreateMultipartUpload", "UploadPartCopy", "UploadPart", "CompleteMultipartUpload", - "PutObjectACL", "PutObjectTagging", "CopyObject", "PutObjectRetention", "PutObjectLegalHold", - "PutObject", "PutBucketCors", "PutBucketACL", "PutBucketLifecycle", "PutBucketEncryption", - "PutBucketPolicy", "PutBucketObjectLockConfig", "PutBucketTagging", "PutBucketVersioning", - "PutBucketNotification", "CreateBucket", "PostObject": + case CreateMultipartUploadOperation, UploadPartCopyOperation, UploadPartOperation, CompleteMultipartUploadOperation, + PutObjectACLOperation, PutObjectTaggingOperation, CopyObjectOperation, PutObjectRetentionOperation, PutObjectLegalHoldOperation, + PutObjectOperation, PutBucketCorsOperation, PutBucketACLOperation, PutBucketLifecycleOperation, PutBucketEncryptionOperation, + PutBucketPolicyOperation, PutBucketObjectLockConfigOperation, PutBucketTaggingOperation, PutBucketVersioningOperation, + PutBucketNotificationOperation, CreateBucketOperation, PostObjectOperation: return metrics.PUTRequest - case "ListObjectParts", "ListMultipartUploads", "ListObjectsV2M", "ListObjectsV2", "ListBucketVersions", - "ListObjectsV1", "ListBuckets": + case ListPartsOperation, ListMultipartUploadsOperation, ListObjectsV2MOperation, ListObjectsV2Operation, + ListObjectsV1Operation, ListBucketsOperation: return metrics.LISTRequest - case "GetObjectACL", "GetObjectTagging", "SelectObjectContent", "GetObjectRetention", "getobjectlegalhold", - "GetObjectAttributes", "GetObject", "GetBucketLocation", "GetBucketPolicy", - "GetBucketLifecycle", "GetBucketEncryption", "GetBucketCors", "GetBucketACL", - "GetBucketWebsite", "GetBucketAccelerate", "GetBucketRequestPayment", "GetBucketLogging", - "GetBucketReplication", "GetBucketTagging", "GetBucketObjectLockConfig", - "GetBucketVersioning", "GetBucketNotification", "ListenBucketNotification": + case GetObjectACLOperation, GetObjectTaggingOperation, SelectObjectContentOperation, GetObjectRetentionOperation, GetObjectLegalHoldOperation, + GetObjectAttributesOperation, GetObjectOperation, GetBucketLocationOperation, GetBucketPolicyOperation, + GetBucketLifecycleOperation, GetBucketEncryptionOperation, GetBucketCorsOperation, GetBucketACLOperation, + GetBucketWebsiteOperation, GetBucketAccelerateOperation, GetBucketRequestPaymentOperation, GetBucketLoggingOperation, + GetBucketReplicationOperation, GetBucketTaggingOperation, GetBucketObjectLockConfigOperation, + GetBucketVersioningOperation, GetBucketNotificationOperation, ListenBucketNotificationOperation: return metrics.GETRequest - case "AbortMultipartUpload", "DeleteObjectTagging", "DeleteObject", "DeleteBucketCors", - "DeleteBucketWebsite", "DeleteBucketTagging", "DeleteMultipleObjects", "DeleteBucketPolicy", - "DeleteBucketLifecycle", "DeleteBucketEncryption", "DeleteBucket": + case AbortMultipartUploadOperation, DeleteObjectTaggingOperation, DeleteObjectOperation, DeleteBucketCorsOperation, + DeleteBucketWebsiteOperation, DeleteBucketTaggingOperation, DeleteMultipleObjectsOperation, DeleteBucketPolicyOperation, + DeleteBucketLifecycleOperation, DeleteBucketEncryptionOperation, DeleteBucketOperation: return metrics.DELETERequest default: return metrics.UNKNOWNRequest @@ -137,7 +137,7 @@ func requestTypeFromAPI(api string) metrics.RequestType { // resolveCID forms CIDResolveFunc using BucketResolveFunc. func resolveCID(log *zap.Logger, resolveBucket BucketResolveFunc) cidResolveFunc { return func(ctx context.Context, reqInfo *ReqInfo) (cnrID string) { - if reqInfo.BucketName == "" || reqInfo.API == "CreateBucket" || reqInfo.API == "" { + if reqInfo.BucketName == "" || reqInfo.API == CreateBucketOperation || reqInfo.API == "" { return "" } diff --git a/api/router.go b/api/router.go index f6c76bf..e6c7b66 100644 --- a/api/router.go +++ b/api/router.go @@ -10,6 +10,7 @@ import ( s3middleware "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/metrics" + "git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" "go.uber.org/zap" @@ -194,139 +195,139 @@ func bucketRouter(h Handler, log *zap.Logger) chi.Router { bktRouter.Options("/", h.Preflight) - bktRouter.Head("/", named("HeadBucket", h.HeadBucketHandler)) + bktRouter.Head("/", named(s3middleware.HeadBucketOperation, h.HeadBucketHandler)) // GET method handlers bktRouter.Group(func(r chi.Router) { r.Method(http.MethodGet, "/", NewHandlerFilter(). Add(NewFilter(). - Queries("uploads"). - Handler(named("ListMultipartUploads", h.ListMultipartUploadsHandler))). + Queries(s3middleware.UploadsQuery). + Handler(named(s3middleware.ListMultipartUploadsOperation, h.ListMultipartUploadsHandler))). Add(NewFilter(). - Queries("location"). - Handler(named("GetBucketLocation", h.GetBucketLocationHandler))). + Queries(s3middleware.LocationQuery). + Handler(named(s3middleware.GetBucketLocationOperation, h.GetBucketLocationHandler))). Add(NewFilter(). - Queries("policy"). - Handler(named("GetBucketPolicy", h.GetBucketPolicyHandler))). + Queries(s3middleware.PolicyQuery). + Handler(named(s3middleware.GetBucketPolicyOperation, h.GetBucketPolicyHandler))). Add(NewFilter(). - Queries("lifecycle"). - Handler(named("GetBucketLifecycle", h.GetBucketLifecycleHandler))). + Queries(s3middleware.LifecycleQuery). + Handler(named(s3middleware.GetBucketLifecycleOperation, h.GetBucketLifecycleHandler))). Add(NewFilter(). - Queries("encryption"). - Handler(named("GetBucketEncryption", h.GetBucketEncryptionHandler))). + Queries(s3middleware.EncryptionQuery). + Handler(named(s3middleware.GetBucketEncryptionOperation, h.GetBucketEncryptionHandler))). Add(NewFilter(). - Queries("cors"). - Handler(named("GetBucketCors", h.GetBucketCorsHandler))). + Queries(s3middleware.CorsQuery). + Handler(named(s3middleware.GetBucketCorsOperation, h.GetBucketCorsHandler))). Add(NewFilter(). - Queries("acl"). - Handler(named("GetBucketACL", h.GetBucketACLHandler))). + Queries(s3middleware.ACLQuery). + Handler(named(s3middleware.GetBucketACLOperation, h.GetBucketACLHandler))). Add(NewFilter(). - Queries("website"). - Handler(named("GetBucketWebsite", h.GetBucketWebsiteHandler))). + Queries(s3middleware.WebsiteQuery). + Handler(named(s3middleware.GetBucketWebsiteOperation, h.GetBucketWebsiteHandler))). Add(NewFilter(). - Queries("accelerate"). - Handler(named("GetBucketAccelerate", h.GetBucketAccelerateHandler))). + Queries(s3middleware.AccelerateQuery). + Handler(named(s3middleware.GetBucketAccelerateOperation, h.GetBucketAccelerateHandler))). Add(NewFilter(). - Queries("requestPayment"). - Handler(named("GetBucketRequestPayment", h.GetBucketRequestPaymentHandler))). + Queries(s3middleware.RequestPaymentQuery). + Handler(named(s3middleware.GetBucketRequestPaymentOperation, h.GetBucketRequestPaymentHandler))). Add(NewFilter(). - Queries("logging"). - Handler(named("GetBucketLogging", h.GetBucketLoggingHandler))). + Queries(s3middleware.LoggingQuery). + Handler(named(s3middleware.GetBucketLoggingOperation, h.GetBucketLoggingHandler))). Add(NewFilter(). - Queries("replication"). - Handler(named("GetBucketReplication", h.GetBucketReplicationHandler))). + Queries(s3middleware.ReplicationQuery). + Handler(named(s3middleware.GetBucketReplicationOperation, h.GetBucketReplicationHandler))). Add(NewFilter(). - Queries("tagging"). - Handler(named("GetBucketTagging", h.GetBucketTaggingHandler))). + Queries(s3middleware.TaggingQuery). + Handler(named(s3middleware.GetBucketTaggingOperation, h.GetBucketTaggingHandler))). Add(NewFilter(). - Queries("object-lock"). - Handler(named("GetBucketObjectLockConfig", h.GetBucketObjectLockConfigHandler))). + Queries(s3middleware.ObjectLockQuery). + Handler(named(s3middleware.GetBucketObjectLockConfigOperation, h.GetBucketObjectLockConfigHandler))). Add(NewFilter(). - Queries("versioning"). - Handler(named("GetBucketVersioning", h.GetBucketVersioningHandler))). + Queries(s3middleware.VersioningQuery). + Handler(named(s3middleware.GetBucketVersioningOperation, h.GetBucketVersioningHandler))). Add(NewFilter(). - Queries("notification"). - Handler(named("GetBucketNotification", h.GetBucketNotificationHandler))). + Queries(s3middleware.NotificationQuery). + Handler(named(s3middleware.GetBucketNotificationOperation, h.GetBucketNotificationHandler))). Add(NewFilter(). - Queries("events"). - Handler(named("ListenBucketNotification", h.ListenBucketNotificationHandler))). + Queries(s3middleware.EventsQuery). + Handler(named(s3middleware.ListenBucketNotificationOperation, h.ListenBucketNotificationHandler))). Add(NewFilter(). - QueriesMatch("list-type", "2", "metadata", "true"). - Handler(named("ListObjectsV2M", h.ListObjectsV2MHandler))). + QueriesMatch(s3middleware.ListTypeQuery, "2", s3middleware.MetadataQuery, "true"). + Handler(named(s3middleware.ListObjectsV2MOperation, h.ListObjectsV2MHandler))). Add(NewFilter(). - QueriesMatch("list-type", "2"). - Handler(named("ListObjectsV2", h.ListObjectsV2Handler))). + QueriesMatch(s3middleware.ListTypeQuery, "2"). + Handler(named(s3middleware.ListObjectsV2Operation, h.ListObjectsV2Handler))). Add(NewFilter(). - Queries("versions"). - Handler(named("ListBucketObjectVersions", h.ListBucketObjectVersionsHandler))). - DefaultHandler(named("ListObjectsV1", h.ListObjectsV1Handler))) + Queries(s3middleware.VersionsQuery). + Handler(named(s3middleware.ListBucketObjectVersionsOperation, h.ListBucketObjectVersionsHandler))). + DefaultHandler(named(s3middleware.ListObjectsV1Operation, h.ListObjectsV1Handler))) }) // PUT method handlers bktRouter.Group(func(r chi.Router) { r.Method(http.MethodPut, "/", NewHandlerFilter(). Add(NewFilter(). - Queries("cors"). - Handler(named("PutBucketCors", h.PutBucketCorsHandler))). + Queries(s3middleware.CorsQuery). + Handler(named(s3middleware.PutBucketCorsOperation, h.PutBucketCorsHandler))). Add(NewFilter(). - Queries("acl"). - Handler(named("PutBucketACL", h.PutBucketACLHandler))). + Queries(s3middleware.ACLQuery). + Handler(named(s3middleware.PutBucketACLOperation, h.PutBucketACLHandler))). Add(NewFilter(). - Queries("lifecycle"). - Handler(named("PutBucketLifecycle", h.PutBucketLifecycleHandler))). + Queries(s3middleware.LifecycleQuery). + Handler(named(s3middleware.PutBucketLifecycleOperation, h.PutBucketLifecycleHandler))). Add(NewFilter(). - Queries("encryption"). - Handler(named("PutBucketEncryption", h.PutBucketEncryptionHandler))). + Queries(s3middleware.EncryptionQuery). + Handler(named(s3middleware.PutBucketEncryptionOperation, h.PutBucketEncryptionHandler))). Add(NewFilter(). - Queries("policy"). - Handler(named("PutBucketPolicy", h.PutBucketPolicyHandler))). + Queries(s3middleware.PolicyQuery). + Handler(named(s3middleware.PutBucketPolicyOperation, h.PutBucketPolicyHandler))). Add(NewFilter(). - Queries("object-lock"). - Handler(named("PutBucketObjectLockConfig", h.PutBucketObjectLockConfigHandler))). + Queries(s3middleware.ObjectLockQuery). + Handler(named(s3middleware.PutBucketObjectLockConfigOperation, h.PutBucketObjectLockConfigHandler))). Add(NewFilter(). - Queries("tagging"). - Handler(named("PutBucketTagging", h.PutBucketTaggingHandler))). + Queries(s3middleware.TaggingQuery). + Handler(named(s3middleware.PutBucketTaggingOperation, h.PutBucketTaggingHandler))). Add(NewFilter(). - Queries("versioning"). - Handler(named("PutBucketVersioning", h.PutBucketVersioningHandler))). + Queries(s3middleware.VersioningQuery). + Handler(named(s3middleware.PutBucketVersioningOperation, h.PutBucketVersioningHandler))). Add(NewFilter(). - Queries("notification"). - Handler(named("PutBucketNotification", h.PutBucketNotificationHandler))). - DefaultHandler(named("CreateBucket", h.CreateBucketHandler))) + Queries(s3middleware.NotificationQuery). + Handler(named(s3middleware.PutBucketNotificationOperation, h.PutBucketNotificationHandler))). + DefaultHandler(named(s3middleware.CreateBucketOperation, h.CreateBucketHandler))) }) // POST method handlers bktRouter.Group(func(r chi.Router) { r.Method(http.MethodPost, "/", NewHandlerFilter(). Add(NewFilter(). - Queries("delete"). - Handler(named("DeleteMultipleObjects", h.DeleteMultipleObjectsHandler))). + Queries(s3middleware.DeleteQuery). + Handler(named(s3middleware.DeleteMultipleObjectsOperation, h.DeleteMultipleObjectsHandler))). // todo consider add filter to match header for defaultHandler: hdrContentType, "multipart/form-data*" - DefaultHandler(named("PostObject", h.PostObject))) + DefaultHandler(named(s3middleware.PostObjectOperation, h.PostObject))) }) // DELETE method handlers bktRouter.Group(func(r chi.Router) { r.Method(http.MethodDelete, "/", NewHandlerFilter(). Add(NewFilter(). - Queries("cors"). - Handler(named("DeleteBucketCors", h.DeleteBucketCorsHandler))). + Queries(s3middleware.CorsQuery). + Handler(named(s3middleware.DeleteBucketCorsOperation, h.DeleteBucketCorsHandler))). Add(NewFilter(). - Queries("website"). - Handler(named("DeleteBucketWebsite", h.DeleteBucketWebsiteHandler))). + Queries(s3middleware.WebsiteQuery). + Handler(named(s3middleware.DeleteBucketWebsiteOperation, h.DeleteBucketWebsiteHandler))). Add(NewFilter(). - Queries("tagging"). - Handler(named("DeleteBucketTagging", h.DeleteBucketTaggingHandler))). + Queries(s3middleware.TaggingQuery). + Handler(named(s3middleware.DeleteBucketTaggingOperation, h.DeleteBucketTaggingHandler))). Add(NewFilter(). - Queries("policy"). - Handler(named("PutBucketPolicy", h.PutBucketPolicyHandler))). + Queries(s3middleware.PolicyQuery). + Handler(named(s3middleware.PutBucketPolicyOperation, h.PutBucketPolicyHandler))). Add(NewFilter(). - Queries("lifecycle"). - Handler(named("PutBucketLifecycle", h.PutBucketLifecycleHandler))). + Queries(s3middleware.LifecycleQuery). + Handler(named(s3middleware.PutBucketLifecycleOperation, h.PutBucketLifecycleHandler))). Add(NewFilter(). - Queries("encryption"). - Handler(named("DeleteBucketEncryption", h.DeleteBucketEncryptionHandler))). - DefaultHandler(named("DeleteBucket", h.DeleteBucketHandler))) + Queries(s3middleware.EncryptionQuery). + Handler(named(s3middleware.DeleteBucketEncryptionOperation, h.DeleteBucketEncryptionHandler))). + DefaultHandler(named(s3middleware.DeleteBucketOperation, h.DeleteBucketHandler))) }) attachErrorHandler(bktRouter) @@ -338,30 +339,30 @@ func objectRouter(h Handler, l *zap.Logger) chi.Router { objRouter := chi.NewRouter() objRouter.Use(s3middleware.AddObjectName(l)) - objRouter.Head("/*", named("HeadObject", h.HeadObjectHandler)) + objRouter.Head("/*", named(s3middleware.HeadObjectOperation, h.HeadObjectHandler)) // GET method handlers objRouter.Group(func(r chi.Router) { r.Method(http.MethodGet, "/*", NewHandlerFilter(). Add(NewFilter(). - Queries("uploadId"). - Handler(named("ListParts", h.ListPartsHandler))). + Queries(s3middleware.UploadIDQuery). + Handler(named(s3middleware.ListPartsOperation, h.ListPartsHandler))). Add(NewFilter(). - Queries("acl"). - Handler(named("GetObjectACL", h.GetObjectACLHandler))). + Queries(s3middleware.ACLQuery). + Handler(named(s3middleware.GetObjectACLOperation, h.GetObjectACLHandler))). Add(NewFilter(). - Queries("tagging"). - Handler(named("GetObjectTagging", h.GetObjectTaggingHandler))). + Queries(s3middleware.TaggingQuery). + Handler(named(s3middleware.GetObjectTaggingOperation, h.GetObjectTaggingHandler))). Add(NewFilter(). - Queries("retention"). - Handler(named("GetObjectRetention", h.GetObjectRetentionHandler))). + Queries(s3middleware.RetentionQuery). + Handler(named(s3middleware.GetObjectRetentionOperation, h.GetObjectRetentionHandler))). Add(NewFilter(). - Queries("legal-hold"). - Handler(named("GetObjectLegalHold", h.GetObjectLegalHoldHandler))). + Queries(s3middleware.LegalHoldQuery). + Handler(named(s3middleware.GetObjectLegalHoldOperation, h.GetObjectLegalHoldHandler))). Add(NewFilter(). - Queries("attributes"). - Handler(named("GetObjectAttributes", h.GetObjectAttributesHandler))). - DefaultHandler(named("GetObject", h.GetObjectHandler))) + Queries(s3middleware.AttributesQuery). + Handler(named(s3middleware.GetObjectAttributesOperation, h.GetObjectAttributesHandler))). + DefaultHandler(named(s3middleware.GetObjectOperation, h.GetObjectHandler))) }) // PUT method handlers @@ -369,51 +370,51 @@ func objectRouter(h Handler, l *zap.Logger) chi.Router { r.Method(http.MethodPut, "/*", NewHandlerFilter(). Add(NewFilter(). Headers(AmzCopySource). - Queries("partNumber", "uploadId"). - Handler(named("UploadPartCopy", h.UploadPartCopy))). + Queries(s3middleware.PartNumberQuery, s3middleware.UploadIDQuery). + Handler(named(s3middleware.UploadPartCopyOperation, h.UploadPartCopy))). Add(NewFilter(). - Queries("partNumber", "uploadId"). - Handler(named("UploadPart", h.UploadPartHandler))). + Queries(s3middleware.PartNumberQuery, s3middleware.UploadIDQuery). + Handler(named(s3middleware.UploadPartOperation, h.UploadPartHandler))). Add(NewFilter(). - Queries("acl"). - Handler(named("PutObjectACL", h.PutObjectACLHandler))). + Queries(s3middleware.ACLQuery). + Handler(named(s3middleware.PutObjectACLOperation, h.PutObjectACLHandler))). Add(NewFilter(). - Queries("tagging"). - Handler(named("PutObjectTagging", h.PutObjectTaggingHandler))). + Queries(s3middleware.TaggingQuery). + Handler(named(s3middleware.PutObjectTaggingOperation, h.PutObjectTaggingHandler))). Add(NewFilter(). Headers(AmzCopySource). - Handler(named("CopyObject", h.CopyObjectHandler))). + Handler(named(s3middleware.CopyObjectOperation, h.CopyObjectHandler))). Add(NewFilter(). - Queries("retention"). - Handler(named("PutObjectRetention", h.PutObjectRetentionHandler))). + Queries(s3middleware.RetentionQuery). + Handler(named(s3middleware.PutObjectRetentionOperation, h.PutObjectRetentionHandler))). Add(NewFilter(). - Queries("legal-hold"). - Handler(named("PutObjectLegalHold", h.PutObjectLegalHoldHandler))). - DefaultHandler(named("PutObject", h.PutObjectHandler))) + Queries(s3middleware.LegalHoldQuery). + Handler(named(s3middleware.PutObjectLegalHoldOperation, h.PutObjectLegalHoldHandler))). + DefaultHandler(named(s3middleware.PutObjectOperation, h.PutObjectHandler))) }) // POST method handlers objRouter.Group(func(r chi.Router) { r.Method(http.MethodPost, "/*", NewHandlerFilter(). Add(NewFilter(). - Queries("uploadId"). - Handler(named("CompleteMultipartUpload", h.CompleteMultipartUploadHandler))). + Queries(s3middleware.UploadIDQuery). + Handler(named(s3middleware.CompleteMultipartUploadOperation, h.CompleteMultipartUploadHandler))). Add(NewFilter(). - Queries("uploads"). - Handler(named("CreateMultipartUpload", h.CreateMultipartUploadHandler))). - DefaultHandler(named("SelectObjectContent", h.SelectObjectContentHandler))) + Queries(s3middleware.UploadsQuery). + Handler(named(s3middleware.CreateMultipartUploadOperation, h.CreateMultipartUploadHandler))). + DefaultHandler(named(s3middleware.SelectObjectContentOperation, h.SelectObjectContentHandler))) }) // DELETE method handlers objRouter.Group(func(r chi.Router) { r.Method(http.MethodDelete, "/*", NewHandlerFilter(). Add(NewFilter(). - Queries("uploadId"). - Handler(named("AbortMultipartUpload", h.AbortMultipartUploadHandler))). + Queries(s3middleware.UploadIDQuery). + Handler(named(s3middleware.AbortMultipartUploadOperation, h.AbortMultipartUploadHandler))). Add(NewFilter(). - Queries("tagging"). - Handler(named("DeleteObjectTagging", h.DeleteObjectTaggingHandler))). - DefaultHandler(named("DeleteObject", h.DeleteObjectHandler))) + Queries(s3middleware.TaggingQuery). + Handler(named(s3middleware.DeleteObjectTaggingOperation, h.DeleteObjectTaggingHandler))). + DefaultHandler(named(s3middleware.DeleteObjectOperation, h.DeleteObjectHandler))) }) attachErrorHandler(objRouter)