From ea1803417f80a743fc6c7bb261d864c38628cf8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=9E=E9=9B=AA=E6=97=A0=E6=83=85?= Date: Wed, 26 Aug 2020 23:52:46 +0800 Subject: [PATCH] Use constants for gateway names to avoid bugs caused by spelling. (#10355) --- cmd/bucket-metadata-sys.go | 10 +++++----- cmd/gateway-interface.go | 9 ++++++++- cmd/gateway-main.go | 6 +++--- cmd/gateway/azure/gateway-azure.go | 5 ++--- cmd/gateway/gcs/gateway-gcs.go | 10 ++++------ cmd/gateway/hdfs/gateway-hdfs.go | 8 +++----- cmd/gateway/nas/gateway-nas.go | 10 +++------- cmd/gateway/s3/gateway-s3.go | 8 ++------ cmd/metrics.go | 2 +- 9 files changed, 31 insertions(+), 37 deletions(-) diff --git a/cmd/bucket-metadata-sys.go b/cmd/bucket-metadata-sys.go index c4f4345c9..56a471d03 100644 --- a/cmd/bucket-metadata-sys.go +++ b/cmd/bucket-metadata-sys.go @@ -81,7 +81,7 @@ func (sys *BucketMetadataSys) Update(bucket string, configFile string, configDat // This code is needed only for gateway implementations. switch configFile { case bucketSSEConfig: - if globalGatewayName == "nas" { + if globalGatewayName == NASBackendGateway { meta, err := loadBucketMetadata(GlobalContext, objAPI, bucket) if err != nil { return err @@ -90,7 +90,7 @@ func (sys *BucketMetadataSys) Update(bucket string, configFile string, configDat return meta.Save(GlobalContext, objAPI) } case bucketLifecycleConfig: - if globalGatewayName == "nas" { + if globalGatewayName == NASBackendGateway { meta, err := loadBucketMetadata(GlobalContext, objAPI, bucket) if err != nil { return err @@ -99,7 +99,7 @@ func (sys *BucketMetadataSys) Update(bucket string, configFile string, configDat return meta.Save(GlobalContext, objAPI) } case bucketTaggingConfig: - if globalGatewayName == "nas" { + if globalGatewayName == NASBackendGateway { meta, err := loadBucketMetadata(GlobalContext, objAPI, bucket) if err != nil { return err @@ -108,7 +108,7 @@ func (sys *BucketMetadataSys) Update(bucket string, configFile string, configDat return meta.Save(GlobalContext, objAPI) } case bucketNotificationConfig: - if globalGatewayName == "nas" { + if globalGatewayName == NASBackendGateway { meta, err := loadBucketMetadata(GlobalContext, objAPI, bucket) if err != nil { return err @@ -273,7 +273,7 @@ func (sys *BucketMetadataSys) GetLifecycleConfig(bucket string) (*lifecycle.Life // GetNotificationConfig returns configured notification config // The returned object may not be modified. func (sys *BucketMetadataSys) GetNotificationConfig(bucket string) (*event.Config, error) { - if globalIsGateway && globalGatewayName == "nas" { + if globalIsGateway && globalGatewayName == NASBackendGateway { // Only needed in case of NAS gateway. objAPI := newObjectLayerWithoutSafeModeFn() if objAPI == nil { diff --git a/cmd/gateway-interface.go b/cmd/gateway-interface.go index 4b92dcc3c..21b81ff67 100644 --- a/cmd/gateway-interface.go +++ b/cmd/gateway-interface.go @@ -21,7 +21,14 @@ import ( ) // GatewayMinioSysTmp prefix is used in Azure/GCS gateway for save metadata sent by Initialize Multipart Upload API. -const GatewayMinioSysTmp = "minio.sys.tmp/" +const ( + GatewayMinioSysTmp = "minio.sys.tmp/" + AzureBackendGateway = "azure" + GCSBackendGateway = "gcs" + HDFSBackendGateway = "hdfs" + NASBackendGateway = "nas" + S3BackendGateway = "s3" +) // Gateway represents a gateway backend. type Gateway interface { diff --git a/cmd/gateway-main.go b/cmd/gateway-main.go index fc42501d4..3c237ffcc 100644 --- a/cmd/gateway-main.go +++ b/cmd/gateway-main.go @@ -262,8 +262,8 @@ func StartGateway(ctx *cli.Context, gw Gateway) { } // Currently only NAS and S3 gateway support encryption headers. - encryptionEnabled := gatewayName == "s3" || gatewayName == "nas" - allowSSEKMS := gatewayName == "s3" // Only S3 can support SSE-KMS (as pass-through) + encryptionEnabled := gatewayName == S3BackendGateway || gatewayName == NASBackendGateway + allowSSEKMS := gatewayName == S3BackendGateway // Only S3 can support SSE-KMS (as pass-through) // Add API router. registerAPIRouter(router, encryptionEnabled, allowSSEKMS) @@ -310,7 +310,7 @@ func StartGateway(ctx *cli.Context, gw Gateway) { // Calls all New() for all sub-systems. newAllSubsystems() - if gatewayName == "nas" { + if gatewayName == NASBackendGateway { buckets, err := newObject.ListBuckets(GlobalContext) if err != nil { logger.Fatal(err, "Unable to list buckets") diff --git a/cmd/gateway/azure/gateway-azure.go b/cmd/gateway/azure/gateway-azure.go index ac64d0085..b47c9438a 100644 --- a/cmd/gateway/azure/gateway-azure.go +++ b/cmd/gateway/azure/gateway-azure.go @@ -73,7 +73,6 @@ const ( azureBlockSize = 100 * humanize.MiByte azureS3MinPartSize = 5 * humanize.MiByte metadataObjectNameTemplate = minio.GatewayMinioSysTmp + "multipart/v1/%s.%x/azure.json" - azureBackend = "azure" azureMarkerPrefix = "{minio}" metadataPartNamePrefix = minio.GatewayMinioSysTmp + "multipart/v1/%s.%x" maxPartsCount = 10000 @@ -113,7 +112,7 @@ EXAMPLES: ` minio.RegisterGatewayCommand(cli.Command{ - Name: azureBackend, + Name: minio.AzureBackendGateway, Usage: "Microsoft Azure Blob Storage", Action: azureGatewayMain, CustomHelpTemplate: azureGatewayTemplate, @@ -170,7 +169,7 @@ type Azure struct { // Name implements Gateway interface. func (g *Azure) Name() string { - return azureBackend + return minio.AzureBackendGateway } // NewGatewayLayer initializes azure blob storage client and returns AzureObjects. diff --git a/cmd/gateway/gcs/gateway-gcs.go b/cmd/gateway/gcs/gateway-gcs.go index 1bbb7a960..79d774557 100644 --- a/cmd/gateway/gcs/gateway-gcs.go +++ b/cmd/gateway/gcs/gateway-gcs.go @@ -91,8 +91,6 @@ const ( // Project ID key in credentials.json gcsProjectIDKey = "project_id" - - gcsBackend = "gcs" ) func init() { @@ -132,7 +130,7 @@ EXAMPLES: ` minio.RegisterGatewayCommand(cli.Command{ - Name: gcsBackend, + Name: minio.GCSBackendGateway, Usage: "Google Cloud Storage", Action: gcsGatewayMain, CustomHelpTemplate: gcsGatewayTemplate, @@ -145,13 +143,13 @@ func gcsGatewayMain(ctx *cli.Context) { projectID := ctx.Args().First() if projectID == "" && os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") == "" { logger.LogIf(minio.GlobalContext, errGCSProjectIDNotFound, logger.Application) - cli.ShowCommandHelpAndExit(ctx, "gcs", 1) + cli.ShowCommandHelpAndExit(ctx, minio.GCSBackendGateway, 1) } if projectID != "" && !isValidGCSProjectIDFormat(projectID) { reqInfo := (&logger.ReqInfo{}).AppendTags("projectID", ctx.Args().First()) contxt := logger.SetReqInfo(minio.GlobalContext, reqInfo) logger.LogIf(contxt, errGCSInvalidProjectID, logger.Application) - cli.ShowCommandHelpAndExit(ctx, "gcs", 1) + cli.ShowCommandHelpAndExit(ctx, minio.GCSBackendGateway, 1) } minio.StartGateway(ctx, &GCS{projectID}) @@ -164,7 +162,7 @@ type GCS struct { // Name returns the name of gcs ObjectLayer. func (g *GCS) Name() string { - return gcsBackend + return minio.GCSBackendGateway } // NewGatewayLayer returns gcs ObjectLayer. diff --git a/cmd/gateway/hdfs/gateway-hdfs.go b/cmd/gateway/hdfs/gateway-hdfs.go index ec654d55c..6160dca0f 100644 --- a/cmd/gateway/hdfs/gateway-hdfs.go +++ b/cmd/gateway/hdfs/gateway-hdfs.go @@ -47,8 +47,6 @@ import ( ) const ( - hdfsBackend = "hdfs" - hdfsSeparator = minio.SlashSeparator ) @@ -84,7 +82,7 @@ EXAMPLES: ` minio.RegisterGatewayCommand(cli.Command{ - Name: hdfsBackend, + Name: minio.HDFSBackendGateway, Usage: "Hadoop Distributed File System (HDFS)", Action: hdfsGatewayMain, CustomHelpTemplate: hdfsGatewayTemplate, @@ -96,7 +94,7 @@ EXAMPLES: func hdfsGatewayMain(ctx *cli.Context) { // Validate gateway arguments. if ctx.Args().First() == "help" { - cli.ShowCommandHelpAndExit(ctx, hdfsBackend, 1) + cli.ShowCommandHelpAndExit(ctx, minio.HDFSBackendGateway, 1) } minio.StartGateway(ctx, &HDFS{args: ctx.Args()}) @@ -109,7 +107,7 @@ type HDFS struct { // Name implements Gateway interface. func (g *HDFS) Name() string { - return hdfsBackend + return minio.HDFSBackendGateway } func getKerberosClient() (*krb.Client, error) { diff --git a/cmd/gateway/nas/gateway-nas.go b/cmd/gateway/nas/gateway-nas.go index eca3949cd..42f5683b4 100644 --- a/cmd/gateway/nas/gateway-nas.go +++ b/cmd/gateway/nas/gateway-nas.go @@ -24,10 +24,6 @@ import ( "github.com/minio/minio/pkg/auth" ) -const ( - nasBackend = "nas" -) - func init() { const nasGatewayTemplate = `NAME: {{.HelpName}} - {{.Usage}} @@ -61,7 +57,7 @@ EXAMPLES: ` minio.RegisterGatewayCommand(cli.Command{ - Name: nasBackend, + Name: minio.NASBackendGateway, Usage: "Network-attached storage (NAS)", Action: nasGatewayMain, CustomHelpTemplate: nasGatewayTemplate, @@ -73,7 +69,7 @@ EXAMPLES: func nasGatewayMain(ctx *cli.Context) { // Validate gateway arguments. if !ctx.Args().Present() || ctx.Args().First() == "help" { - cli.ShowCommandHelpAndExit(ctx, nasBackend, 1) + cli.ShowCommandHelpAndExit(ctx, minio.NASBackendGateway, 1) } minio.StartGateway(ctx, &NAS{ctx.Args().First()}) @@ -86,7 +82,7 @@ type NAS struct { // Name implements Gateway interface. func (g *NAS) Name() string { - return nasBackend + return minio.NASBackendGateway } // NewGatewayLayer returns nas gatewaylayer. diff --git a/cmd/gateway/s3/gateway-s3.go b/cmd/gateway/s3/gateway-s3.go index 37db6e276..69681d946 100644 --- a/cmd/gateway/s3/gateway-s3.go +++ b/cmd/gateway/s3/gateway-s3.go @@ -40,10 +40,6 @@ import ( "github.com/minio/minio/pkg/bucket/policy" ) -const ( - s3Backend = "s3" -) - func init() { const s3GatewayTemplate = `NAME: {{.HelpName}} - {{.Usage}} @@ -76,7 +72,7 @@ EXAMPLES: ` minio.RegisterGatewayCommand(cli.Command{ - Name: s3Backend, + Name: minio.S3BackendGateway, Usage: "Amazon Simple Storage Service (S3)", Action: s3GatewayMain, CustomHelpTemplate: s3GatewayTemplate, @@ -109,7 +105,7 @@ type S3 struct { // Name implements Gateway interface. func (g *S3) Name() string { - return s3Backend + return minio.S3BackendGateway } const letterBytes = "abcdefghijklmnopqrstuvwxyz01234569" diff --git a/cmd/metrics.go b/cmd/metrics.go index ec8b3b179..46e08611a 100644 --- a/cmd/metrics.go +++ b/cmd/metrics.go @@ -157,7 +157,7 @@ func healingMetricsPrometheus(ch chan<- prometheus.Metric) { // collects gateway specific metrics for MinIO instance in Prometheus specific format // and sends to given channel func gatewayMetricsPrometheus(ch chan<- prometheus.Metric) { - if !globalIsGateway || (globalGatewayName != "s3" && globalGatewayName != "azure" && globalGatewayName != "gcs") { + if !globalIsGateway || (globalGatewayName != S3BackendGateway && globalGatewayName != AzureBackendGateway && globalGatewayName != GCSBackendGateway) { return }