Add mc bucket version command (#3308)

This commit is contained in:
poornas
2020-07-21 10:10:57 -07:00
committed by GitHub
parent c8a3b7bff0
commit 023e56e0a1
8 changed files with 282 additions and 1 deletions
+2
View File
@@ -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,
+42
View File
@@ -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.
}
+151
View File
@@ -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
}
+16
View File
@@ -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",
})
}
+33
View File
@@ -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)
}
+4
View File
@@ -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
+1
View File
@@ -344,6 +344,7 @@ var appCmds = []cli.Command{
watchCmd,
policyCmd,
tagCmd,
bucketCmd,
adminCmd,
configCmd,
updateCmd,
+33 -1
View File
@@ -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
```
<a name="version"></a>
### 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
```