From 023e56e0a1068770e09d5abfb4fa402443ba4874 Mon Sep 17 00:00:00 2001 From: poornas Date: Tue, 21 Jul 2020 10:10:57 -0700 Subject: [PATCH] Add `mc bucket version` command (#3308) --- cmd/auto-complete.go | 2 + cmd/bucket-main.go | 42 ++++++++ cmd/bucket-version.go | 151 ++++++++++++++++++++++++++++ cmd/client-fs.go | 16 +++ cmd/client-s3.go | 33 ++++++ cmd/client.go | 4 + cmd/main.go | 1 + docs/minio-client-complete-guide.md | 34 ++++++- 8 files changed, 282 insertions(+), 1 deletion(-) create mode 100644 cmd/bucket-main.go create mode 100644 cmd/bucket-version.go diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index 86aa1a4f..44e2dd62 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -260,6 +260,8 @@ var completeCmds = map[string]complete.Predictor{ "/share/list": nil, "/share/upload": s3Completer, + "/bucket/version": s3Completer, + // Admin API commands MinIO only. "/admin/heal": s3Completer, diff --git a/cmd/bucket-main.go b/cmd/bucket-main.go new file mode 100644 index 00000000..07b718c2 --- /dev/null +++ b/cmd/bucket-main.go @@ -0,0 +1,42 @@ +/* + * MinIO Client (C) 2020 MinIO, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cmd + +import "github.com/minio/cli" + +var ( + bucketFlags = []cli.Flag{} +) + +var bucketCmd = cli.Command{ + Name: "bucket", + Usage: "manage bucket", + Action: mainBucket, + Before: setGlobalsFromContext, + HideHelpCommand: true, + Flags: append(bucketFlags, globalFlags...), + Subcommands: []cli.Command{ + bucketVersionCmd, + }, +} + +// mainBucket is the handle for "mc bucket" command. provides sub-commands which allow managing/viewing bucket info. +func mainBucket(ctx *cli.Context) error { + cli.ShowCommandHelp(ctx, ctx.Args().First()) + return nil + // Sub-commands like "info", "encrypt" and "version" have their own main. +} diff --git a/cmd/bucket-version.go b/cmd/bucket-version.go new file mode 100644 index 00000000..5dba6bd4 --- /dev/null +++ b/cmd/bucket-version.go @@ -0,0 +1,151 @@ +/* + * MinIO Client (C) 2020 MinIO, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cmd + +import ( + "fmt" + "strings" + + "github.com/fatih/color" + "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" + "github.com/minio/mc/pkg/probe" + + "github.com/minio/minio/pkg/console" +) + +var bucketVersionCmd = cli.Command{ + Name: "version", + Usage: "manage bucket versioning", + Action: mainBucketVersion, + Before: setGlobalsFromContext, + Flags: globalFlags, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + + USAGE: + {{.HelpName}} TARGET + + FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} + EXAMPLES: + 1. Display bucket versioning status for bucket "mybucket". + {{.Prompt}} {{.HelpName}} myminio/mybucket info + + 2. Enable versioning on bucket "mybucket" for alias "myminio". + {{.Prompt}} {{.HelpName}} myminio/mybucket enable + + 3. Suspend versioning on bucket "mybucket" for alias "myminio". + {{.Prompt}} {{.HelpName}} myminio/mybucket suspend +`, +} + +// checkBucketVersionSyntax - validate all the passed arguments +func checkBucketVersionSyntax(ctx *cli.Context) { + if len(ctx.Args()) < 1 || len(ctx.Args()) > 2 { + cli.ShowCommandHelpAndExit(ctx, "version", 1) // last argument is exit code + } +} + +type bucketVersionMessage struct { + Op string + Status string `json:"status"` + URL string `json:"url"` + Versioning struct { + Status string `json:"status"` + MFADelete string `json:"MFADelete"` + } `json:"versioning"` +} + +func (v bucketVersionMessage) JSON() string { + v.Status = "success" + jsonMessageBytes, e := json.MarshalIndent(v, "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + return string(jsonMessageBytes) +} + +func (v bucketVersionMessage) String() string { + switch v.Op { + case "info": + msg := "" + switch v.Versioning.Status { + case "": + msg = fmt.Sprintf("%s is un-versioned", v.URL) + default: + msg = fmt.Sprintf("%s versioning status is %s", v.URL, strings.ToLower(v.Versioning.Status)) + } + return console.Colorize("BucketVersionMessage", msg) + case "enable": + return console.Colorize("BucketVersionMessage", fmt.Sprintf("%s versioning is enabled", v.URL)) + case "suspend": + return console.Colorize("BucketVersionMessage", fmt.Sprintf("%s versioning is suspended", v.URL)) + } + return "" +} + +// mainBucketVersion is the handler for "mc bucket version" command. +func mainBucketVersion(ctx *cli.Context) error { + checkBucketVersionSyntax(ctx) + + console.SetColor("BucketVersionMessage", color.New(color.FgGreen)) + + // Get the alias parameter from cli + args := ctx.Args() + aliasedURL := args.Get(0) + // Create a new Client + client, err := newClient(aliasedURL) + fatalIf(err, "Unable to initialize connection.") + + if len(args) != 2 { + cli.ShowCommandHelpAndExit(ctx, "version", 1) // last argument is exit code + } + op := "" + switch strings.ToLower(args[1]) { + case "enable": + op = strings.ToLower(args[1]) + case "suspend": + op = strings.ToLower(args[1]) + case "info": + op = strings.ToLower(args[1]) + default: + fatalIf(probe.NewError(fmt.Errorf("Unknown argument %s passed", args[1])), "Invalid argument") + } + + switch op { + case "info": + vConfig, e := client.GetVersioning(globalContext) + fatalIf(e, "Cannot get version info") + vMsg := bucketVersionMessage{ + Op: op, + Status: "success", + URL: aliasedURL, + } + vMsg.Versioning.Status = vConfig.Status + vMsg.Versioning.MFADelete = vConfig.MFADelete + printMsg(vMsg) + case "enable", "suspend": + fatalIf(client.SetVersioning(globalContext, args[1]), "Cannot set versioning status") + printMsg(bucketVersionMessage{ + Op: op, + Status: "success", + URL: aliasedURL, + }) + } + + return nil +} diff --git a/cmd/client-fs.go b/cmd/client-fs.go index 9f419941..1b46ee1b 100644 --- a/cmd/client-fs.go +++ b/cmd/client-fs.go @@ -1128,3 +1128,19 @@ func (f *fsClient) SetLifecycle(ctx context.Context, _ *lifecycle.Configuration) APIType: "filesystem", }) } + +// Get versioning info for a bucket, not implemented. +func (f *fsClient) GetVersioning(ctx context.Context) (minio.BucketVersioningConfiguration, *probe.Error) { + return minio.BucketVersioningConfiguration{}, probe.NewError(APINotImplemented{ + API: "GetVersioning", + APIType: "filesystem", + }) +} + +// SetVersioning - Set versioning configuration on a bucket, not implemented +func (f *fsClient) SetVersioning(ctx context.Context, status string) *probe.Error { + return probe.NewError(APINotImplemented{ + API: "SetVersioning", + APIType: "filesystem", + }) +} diff --git a/cmd/client-s3.go b/cmd/client-s3.go index 37ac1c84..9cfcec48 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -2280,3 +2280,36 @@ func (c *S3Client) SetLifecycle(ctx context.Context, config *lifecycle.Configura return nil } + +// GetVersioning - gets bucket versioning info. +func (c *S3Client) GetVersioning(ctx context.Context) (config minio.BucketVersioningConfiguration, err *probe.Error) { + bucket, _ := c.url2BucketAndObject() + if bucket == "" { + return config, probe.NewError(BucketNameEmpty{}) + } + var e error + config, e = c.api.GetBucketVersioning(ctx, bucket) + if e != nil { + return config, probe.NewError(e) + } + + return config, nil +} + +// SetVersioning - Set versioning configuration on a bucket +func (c *S3Client) SetVersioning(ctx context.Context, status string) *probe.Error { + bucket, _ := c.url2BucketAndObject() + if bucket == "" { + return probe.NewError(BucketNameEmpty{}) + } + var err error + switch status { + case "enable": + err = c.api.EnableVersioning(ctx, bucket) + case "suspend": + err = c.api.SuspendVersioning(ctx, bucket) + default: + return probe.NewError(fmt.Errorf("Invalid versioning status")) + } + return probe.NewError(err) +} diff --git a/cmd/client.go b/cmd/client.go index 2099dcf4..9a6e05b3 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -96,6 +96,10 @@ type Client interface { // Lifecycle operations GetLifecycle(ctx context.Context) (*lifecycle.Configuration, *probe.Error) SetLifecycle(ctx context.Context, config *lifecycle.Configuration) *probe.Error + + // Versioning operations + GetVersioning(ctx context.Context) (minio.BucketVersioningConfiguration, *probe.Error) + SetVersioning(ctx context.Context, status string) *probe.Error } // ClientContent - Content container for content metadata diff --git a/cmd/main.go b/cmd/main.go index bb186979..cb264ca6 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -344,6 +344,7 @@ var appCmds = []cli.Command{ watchCmd, policyCmd, tagCmd, + bucketCmd, adminCmd, configCmd, updateCmd, diff --git a/docs/minio-client-complete-guide.md b/docs/minio-client-complete-guide.md index 79275a85..0acb35fe 100644 --- a/docs/minio-client-complete-guide.md +++ b/docs/minio-client-complete-guide.md @@ -300,7 +300,7 @@ mc version RELEASE.2020-04-25T00-43-23Z | | | | | |:----------------------------------------------------------------------------------------|:---------------------------------------------------------------|:-----------------------------------------------------------|-------------------------------------------| | [**ls** - list buckets and objects](#ls) | [**tree** - list buckets and objects in a tree format](#tree) | [**mb** - make a bucket](#mb) | [**cat** - display object contents](#cat) | -| [**cp** - copy objects](#cp) | [**rb** - remove a bucket](#rb) | [**pipe** - stream STDIN to an object](#pipe) | | +| [**cp** - copy objects](#cp) | [**rb** - remove a bucket](#rb) | [**pipe** - stream STDIN to an object](#pipe) | [**version** - manage bucket version](#version) | | [**share** - generate URL for temporary access to an object](#share) | [**rm** - remove objects](#rm) | [**find** - find files and objects](#find) | | | [**diff** - list differences in object name, size, and date between two buckets](#diff) | [**mirror** - synchronize object(s) to a remote site](#mirror) | [**ilm** - configure bucket lifecycle] (#ilm) | | | [**config** - manage config file](#config) | [**policy** - set public policy on bucket or prefix](#policy) | [**event** - manage events on your buckets](#event) | | @@ -1492,3 +1492,35 @@ Type : file Metadata : Content-Type: application/octet-stream ``` + + +### Command `version` +`version` manages bucket versioning + +``` +USAGE: + mc bucket version TARGET [enable | suspend | info] + +FLAGS: + --help, -h show help +``` + +*Example: Enable versioning on bucket `mybucket`* + +``` +mc bucket version myminio/mybucket enable +myminio/mybucket versioning is enabled +``` + +*Example: Display the versioning configuration for bucket `mybucket`* + +``` +mc bucket version myminio/mybucket info +myminio/mybucket versioning status is enabled + +``` +*Example: Suspend versioning for bucket `mybucket`* +``` +mc bucket version myminio/mybucket suspend +myminio/mybucket versioning is suspended +```