diff --git a/cmd/client-fs.go b/cmd/client-fs.go index 4cf8e56e..623fe218 100644 --- a/cmd/client-fs.go +++ b/cmd/client-fs.go @@ -1091,7 +1091,7 @@ func (f *fsClient) AddUserAgent(_, _ string) { } // Get Object Tags -func (f *fsClient) GetTags(ctx context.Context) (map[string]string, *probe.Error) { +func (f *fsClient) GetTags(ctx context.Context, _ string) (map[string]string, *probe.Error) { return nil, probe.NewError(APINotImplemented{ API: "GetObjectTagging", APIType: "filesystem", @@ -1099,7 +1099,7 @@ func (f *fsClient) GetTags(ctx context.Context) (map[string]string, *probe.Error } // Set Object tags -func (f *fsClient) SetTags(ctx context.Context, tags string) *probe.Error { +func (f *fsClient) SetTags(ctx context.Context, versionID, tags string) *probe.Error { return probe.NewError(APINotImplemented{ API: "SetObjectTagging", APIType: "filesystem", @@ -1107,7 +1107,7 @@ func (f *fsClient) SetTags(ctx context.Context, tags string) *probe.Error { } // Delete object tags -func (f *fsClient) DeleteTags(ctx context.Context) *probe.Error { +func (f *fsClient) DeleteTags(ctx context.Context, versionID string) *probe.Error { return probe.NewError(APINotImplemented{ API: "DeleteObjectTagging", APIType: "filesystem", diff --git a/cmd/client-s3.go b/cmd/client-s3.go index 3f589cde..752ec9b9 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -2338,13 +2338,17 @@ func (c *S3Client) GetObjectLockConfig(ctx context.Context) (minio.RetentionMode } // GetTags - Get tags of bucket or object. -func (c *S3Client) GetTags(ctx context.Context) (map[string]string, *probe.Error) { +func (c *S3Client) GetTags(ctx context.Context, versionID string) (map[string]string, *probe.Error) { bucketName, objectName := c.url2BucketAndObject() if bucketName == "" { return nil, probe.NewError(BucketNameEmpty{}) } if objectName == "" { + if versionID != "" { + return nil, probe.NewError(errors.New("getting bucket tags does not support versioning parameters")) + } + tags, err := c.api.GetBucketTagging(ctx, bucketName) if err != nil { return nil, probe.NewError(err) @@ -2353,7 +2357,7 @@ func (c *S3Client) GetTags(ctx context.Context) (map[string]string, *probe.Error return tags.ToMap(), nil } - tags, err := c.api.GetObjectTagging(ctx, bucketName, objectName, minio.GetObjectTaggingOptions{}) + tags, err := c.api.GetObjectTagging(ctx, bucketName, objectName, minio.GetObjectTaggingOptions{VersionID: versionID}) if err != nil { return nil, probe.NewError(err) } @@ -2362,7 +2366,7 @@ func (c *S3Client) GetTags(ctx context.Context) (map[string]string, *probe.Error } // SetTags - Set tags of bucket or object. -func (c *S3Client) SetTags(ctx context.Context, tagString string) *probe.Error { +func (c *S3Client) SetTags(ctx context.Context, versionID, tagString string) *probe.Error { bucketName, objectName := c.url2BucketAndObject() if bucketName == "" { return probe.NewError(BucketNameEmpty{}) @@ -2374,9 +2378,12 @@ func (c *S3Client) SetTags(ctx context.Context, tagString string) *probe.Error { } if objectName == "" { + if versionID != "" { + return probe.NewError(errors.New("setting bucket tags does not support versioning parameters")) + } err = c.api.SetBucketTagging(ctx, bucketName, tags) } else { - err = c.api.PutObjectTagging(ctx, bucketName, objectName, tags, minio.PutObjectTaggingOptions{}) + err = c.api.PutObjectTagging(ctx, bucketName, objectName, tags, minio.PutObjectTaggingOptions{VersionID: versionID}) } if err != nil { @@ -2387,7 +2394,7 @@ func (c *S3Client) SetTags(ctx context.Context, tagString string) *probe.Error { } // DeleteTags - Delete tags of bucket or object -func (c *S3Client) DeleteTags(ctx context.Context) *probe.Error { +func (c *S3Client) DeleteTags(ctx context.Context, versionID string) *probe.Error { bucketName, objectName := c.url2BucketAndObject() if bucketName == "" { return probe.NewError(BucketNameEmpty{}) @@ -2395,9 +2402,12 @@ func (c *S3Client) DeleteTags(ctx context.Context) *probe.Error { var err error if objectName == "" { + if versionID != "" { + return probe.NewError(errors.New("setting bucket tags does not support versioning parameters")) + } err = c.api.RemoveBucketTagging(ctx, bucketName) } else { - err = c.api.RemoveObjectTagging(ctx, bucketName, objectName, minio.RemoveObjectTaggingOptions{}) + err = c.api.RemoveObjectTagging(ctx, bucketName, objectName, minio.RemoveObjectTaggingOptions{VersionID: versionID}) } if err != nil { diff --git a/cmd/client.go b/cmd/client.go index bfc3f3da..f9e69e55 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -128,9 +128,9 @@ type Client interface { AddUserAgent(app, version string) // Tagging operations - GetTags(ctx context.Context) (map[string]string, *probe.Error) - SetTags(ctx context.Context, tags string) *probe.Error - DeleteTags(ctx context.Context) *probe.Error + GetTags(ctx context.Context, versionID string) (map[string]string, *probe.Error) + SetTags(ctx context.Context, versionID, tags string) *probe.Error + DeleteTags(ctx context.Context, versionID string) *probe.Error // Lifecycle operations GetLifecycle(ctx context.Context) (*lifecycle.Configuration, *probe.Error) diff --git a/cmd/tag-list.go b/cmd/tag-list.go index c8e5d918..7fd5d0c2 100644 --- a/cmd/tag-list.go +++ b/cmd/tag-list.go @@ -22,6 +22,7 @@ import ( "fmt" "sort" "strings" + "time" "github.com/fatih/color" "github.com/minio/cli" @@ -31,12 +32,27 @@ import ( "github.com/minio/minio/pkg/console" ) +var tagListFlags = []cli.Flag{ + cli.StringFlag{ + Name: "version-id, vid", + Usage: "List tags of particular object version", + }, + cli.StringFlag{ + Name: "rewind", + Usage: "Go back in time", + }, + cli.BoolFlag{ + Name: "versions", + Usage: "Select multiple versions for each object", + }, +} + var tagListCmd = cli.Command{ Name: "list", Usage: "list tags of a bucket or an object", Action: mainListTag, Before: setGlobalsFromContext, - Flags: globalFlags, + Flags: append(tagListFlags, globalFlags...), CustomHelpTemplate: `NAME: {{.HelpName}} - {{.Usage}} @@ -53,22 +69,29 @@ EXAMPLES: 1. List the tags assigned to an object. {{.Prompt}} {{.HelpName}} myminio/testbucket/testobject - 2. List the tags assigned to an object in JSON format. + 2. List the tags assigned to particular version of an object. + {{.Prompt}} {{.HelpName}} --version-id "ieQq7aXsyhlhDt47YURGlrucYY3GxWHa" myminio/testbucket/testobject + + 3. List the tags assigned to an object versions that are older than one week. + {{.Prompt}} {{.HelpName}} --versions --rewind 7d myminio/testbucket/testobject + + 4. List the tags assigned to an object in JSON format. {{.Prompt}} {{.HelpName}} --json myminio/testbucket/testobject - 3. List the tags assigned to a bucket. + 5. List the tags assigned to a bucket. {{.Prompt}} {{.HelpName}} myminio/testbucket - 4. List the tags assigned to a bucket in JSON format. + 6. List the tags assigned to a bucket in JSON format. {{.Prompt}} {{.HelpName}} --json s3/testbucket `, } // tagListMessage structure for displaying tag type tagListMessage struct { - Tags map[string]string `json:"tagset,omitempty"` - Status string `json:"status"` - URL string `json:"url"` + Tags map[string]string `json:"tagset,omitempty"` + Status string `json:"status"` + URL string `json:"url"` + VersionID string `json:"versionID"` } func (t tagListMessage) JSON() string { @@ -90,8 +113,9 @@ func (t tagListMessage) String() string { maxKeyLen += 2 // add len(" :") strs := []string{ - fmt.Sprintf("%v%*v %v", console.Colorize("Name", "Name"), maxKeyLen-4, ":", console.Colorize("Name", t.URL)), + fmt.Sprintf("%v%*v %v", console.Colorize("Name", "Name"), maxKeyLen-4, ":", console.Colorize("Name", t.URL+" ("+t.VersionID+")")), } + for _, key := range keys { strs = append( strs, @@ -99,41 +123,83 @@ func (t tagListMessage) String() string { ) } + if len(keys) == 0 { + strs = append(strs, console.Colorize("NoTags", "No tags found")) + } + return strings.Join(strs, "\n") } +// parseTagListSyntax performs command-line input validation for tag list command. +func parseTagListSyntax(ctx *cli.Context) (targetURL, versionID string, timeRef time.Time, withOlderVersions bool) { + if len(ctx.Args()) != 1 { + cli.ShowCommandHelpAndExit(ctx, "list", globalErrorExitStatus) + } + + targetURL = ctx.Args().Get(0) + versionID = ctx.String("version-id") + withOlderVersions = ctx.Bool("versions") + rewind := ctx.String("rewind") + + if versionID != "" && rewind != "" { + fatalIf(errDummy().Trace(), "You cannot specify both --version-id and --rewind flags at the same time") + } + + timeRef = parseRewindFlag(rewind) + return +} + +// showTags pretty prints tags of a bucket or a specified object/version +func showTags(ctx context.Context, clnt Client, versionID string, verbose bool) { + targetName := clnt.GetURL().String() + if versionID != "" { + targetName += " (" + versionID + ")" + } + + tagsMap, err := clnt.GetTags(ctx, versionID) + if err != nil { + if minio.ToErrorResponse(err.ToGoError()).Code == "NoSuchTagSet" { + fatalIf(probe.NewError(errors.New("check 'mc tag set --help' on how to set tags")), "No tags found for "+targetName) + } + fatalIf(err, "Unable to fetch tags for "+targetName) + return + } + + printMsg(tagListMessage{ + Tags: tagsMap, + Status: "success", + URL: clnt.GetURL().String(), + VersionID: versionID, + }) +} + func mainListTag(cliCtx *cli.Context) error { ctx, cancelListTag := context.WithCancel(globalContext) defer cancelListTag() - if len(cliCtx.Args()) != 1 { - cli.ShowCommandHelpAndExit(cliCtx, "list", globalErrorExitStatus) - } - - targetURL := cliCtx.Args().Get(0) - clnt, err := newClient(targetURL) - fatalIf(err, "Unable to initialize target "+targetURL) - - tagsMap, err := clnt.GetTags(ctx) - if err != nil { - if minio.ToErrorResponse(err.ToGoError()).Code == "NoSuchTagSet" { - fatalIf(probe.NewError(errors.New("check 'mc tag set --help' on how to set tags")), "No tags found for "+targetURL) - } - fatalIf(err, "Unable to fetch tags for "+targetURL) - } - - if len(tagsMap) == 0 { - fatalIf(probe.NewError(errors.New("check 'mc tag set --help' on how to set tags")), "No tags found for "+targetURL) - } - console.SetColor("Name", color.New(color.Bold, color.FgCyan)) console.SetColor("Key", color.New(color.FgGreen)) console.SetColor("Value", color.New(color.FgYellow)) + console.SetColor("NoTags", color.New(color.FgRed)) + + targetURL, versionID, timeRef, withVersions := parseTagListSyntax(cliCtx) + if timeRef.IsZero() && withVersions { + timeRef = time.Now().UTC() + } + + clnt, err := newClient(targetURL) + fatalIf(err, "Unable to initialize target "+targetURL) + + if timeRef.IsZero() && !withVersions { + showTags(ctx, clnt, versionID, true) + } else { + for content := range clnt.List(ctx, ListOptions{timeRef: timeRef, withOlderVersions: withVersions}) { + if content.Err != nil { + fatalIf(content.Err.Trace(), "Unable to list target "+targetURL) + } + showTags(ctx, clnt, content.VersionID, false) + } + } - printMsg(tagListMessage{ - Tags: tagsMap, - Status: "success", - URL: targetURL, - }) return nil } diff --git a/cmd/tag-main.go b/cmd/tag-main.go index 74be2cae..23745008 100644 --- a/cmd/tag-main.go +++ b/cmd/tag-main.go @@ -22,7 +22,7 @@ import ( var tagCmd = cli.Command{ Name: "tag", - Usage: "manage tags for bucket(s) and object(s)", + Usage: "manage tags for bucket and object(s)", Action: mainTag, Before: setGlobalsFromContext, Flags: globalFlags, diff --git a/cmd/tag-remove.go b/cmd/tag-remove.go index 58c587b2..26146d13 100644 --- a/cmd/tag-remove.go +++ b/cmd/tag-remove.go @@ -18,6 +18,7 @@ package cmd import ( "context" + "time" "github.com/fatih/color" "github.com/minio/cli" @@ -26,12 +27,27 @@ import ( "github.com/minio/minio/pkg/console" ) +var tagRemoveFlags = []cli.Flag{ + cli.StringFlag{ + Name: "version-id, vid", + Usage: "Remove tags of particular object version", + }, + cli.StringFlag{ + Name: "rewind", + Usage: "Go back in time", + }, + cli.BoolFlag{ + Name: "versions", + Usage: "Select multiple versions for each object", + }, +} + var tagRemoveCmd = cli.Command{ Name: "remove", Usage: "remove tags assigned to a bucket or an object", Action: mainRemoveTag, Before: setGlobalsFromContext, - Flags: globalFlags, + Flags: append(tagRemoveFlags, globalFlags...), CustomHelpTemplate: `NAME: {{.HelpName}} - {{.Usage}} @@ -48,20 +64,33 @@ EXAMPLES: 1. Remove the tags assigned to an object. {{.Prompt}} {{.HelpName}} myminio/testbucket/testobject - 2. Remove the tags assigned to a bucket. + 2. Remove the tags assigned to a particular version of an object. + {{.Prompt}} {{.HelpName}} --version-id "ieQq7aXsyhlhDt47YURGlrucYY3GxWHa" myminio/testbucket/testobject + + 3. Remove the tags assigned to an object versions that are older than one week + {{.Prompt}} {{.HelpName}} --versions --rewind 7d myminio/testbucket/testobject + + 4. Remove the tags assigned to a bucket. {{.Prompt}} {{.HelpName}} play/testbucket `, } // tagSetTagMessage structure will show message depending on the type of console. type tagRemoveMessage struct { - Status string `json:"status"` - Name string `json:"name"` + Status string `json:"status"` + Name string `json:"name"` + VersionID string `json:"versionID"` } // tagRemoveMessage console colorized output. func (t tagRemoveMessage) String() string { - return console.Colorize("Remove", "Tags removed for "+t.Name+".") + var msg string + msg += "Tags removed for " + t.Name + if t.VersionID != "" { + msg += " (" + t.VersionID + ")" + } + msg += "." + return console.Colorize("Remove", msg) } // JSON tagRemoveMessage. @@ -70,29 +99,68 @@ func (t tagRemoveMessage) JSON() string { fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(msgBytes) } -func checkRemoveTagSyntax(ctx *cli.Context) { + +func parseRemoveTagSyntax(ctx *cli.Context) (targetURL, versionID string, timeRef time.Time, withVersions bool) { if len(ctx.Args()) != 1 { cli.ShowCommandHelpAndExit(ctx, "remove", globalErrorExitStatus) } + + targetURL = ctx.Args().Get(0) + versionID = ctx.String("version-id") + withVersions = ctx.Bool("versions") + rewind := ctx.String("rewind") + + if versionID != "" && (rewind != "" || withVersions) { + fatalIf(errDummy().Trace(), "You cannot specify both --version-id and --rewind or --versions flags at the same time") + } + + timeRef = parseRewindFlag(rewind) + return +} + +// Delete tags of a bucket or a specified object/version +func deleteTags(ctx context.Context, clnt Client, versionID string, verbose bool) { + targetName := clnt.GetURL().String() + if versionID != "" { + targetName += " (" + versionID + ")" + } + + err := clnt.DeleteTags(ctx, versionID) + if err != nil { + fatalIf(err, "Unable to remove tags for "+targetName) + return + } + + printMsg(tagRemoveMessage{ + Status: "success", + Name: clnt.GetURL().String(), + VersionID: versionID, + }) } func mainRemoveTag(cliCtx *cli.Context) error { ctx, cancelList := context.WithCancel(globalContext) defer cancelList() - checkRemoveTagSyntax(cliCtx) - console.SetColor("Remove", color.New(color.FgGreen)) - targetURL := cliCtx.Args().Get(0) + targetURL, versionID, timeRef, withVersions := parseRemoveTagSyntax(cliCtx) + if timeRef.IsZero() && withVersions { + timeRef = time.Now().UTC() + } + clnt, pErr := newClient(targetURL) fatalIf(pErr, "Unable to initialize target "+targetURL) - pErr = clnt.DeleteTags(ctx) - fatalIf(pErr, "Unable to remove tags for "+targetURL) - printMsg(tagRemoveMessage{ - Status: "success", - Name: targetURL, - }) + if timeRef.IsZero() && !withVersions { + deleteTags(ctx, clnt, versionID, true) + } else { + for content := range clnt.List(ctx, ListOptions{timeRef: timeRef, withOlderVersions: withVersions}) { + if content.Err != nil { + fatalIf(content.Err.Trace(), "Unable to list target "+targetURL) + } + deleteTags(ctx, clnt, content.VersionID, false) + } + } return nil } diff --git a/cmd/tag-set.go b/cmd/tag-set.go index 6803e2f2..297a24a0 100644 --- a/cmd/tag-set.go +++ b/cmd/tag-set.go @@ -18,6 +18,7 @@ package cmd import ( "context" + "time" "github.com/fatih/color" "github.com/minio/cli" @@ -26,12 +27,25 @@ import ( "github.com/minio/minio/pkg/console" ) +var tagSetFlags = []cli.Flag{ + cli.StringFlag{ + Name: "version-id, vid", + Usage: "Set tags of particular object version", + }, + cli.StringFlag{ + Name: "rewind", + Usage: "Go back in time", + }, + cli.BoolFlag{ + Name: "versions", + Usage: "Select multiple versions for an object", + }, +} + var tagSetCmd = cli.Command{ - Name: "set", - Usage: "set tags for a bucket(s) and object(s)", - Action: mainSetTag, + Name: "set", Usage: "set tags for a bucket and object(s)", Action: mainSetTag, Before: setGlobalsFromContext, - Flags: globalFlags, + Flags: append(tagSetFlags, globalFlags...), CustomHelpTemplate: `NAME: {{.HelpName}} - {{.Usage}} @@ -48,20 +62,33 @@ EXAMPLES: 1. Assign tags to an object. {{.Prompt}} {{.HelpName}} play/testbucket/testobject "key1=value1&key2=value2&key3=value3" - 2. Assign tags to a bucket. + 2. Assign tags to a particuler version of an object. + {{.Prompt}} {{.HelpName}} --version-id "ieQq7aXsyhlhDt47YURGlrucYY3GxWHa" play/testbucket/testobject "key1=value1&key2=value2&key3=value3" + + 3. Assign tags to a object versions older than one week. + {{.Prompt}} {{.HelpName}} --versions --rewind 7d play/testbucket/testobject "key1=value1&key2=value2&key3=value3" + + 4. Assign tags to a bucket. {{.Prompt}} {{.HelpName}} myminio/testbucket "key1=value1&key2=value2&key3=value3" `, } // tagSetTagMessage structure will show message depending on the type of console. type tagSetMessage struct { - Status string `json:"status"` - Name string `json:"name"` + Status string `json:"status"` + Name string `json:"name"` + VersionID string `json:"versionID"` } // tagSetMessage console colorized output. func (t tagSetMessage) String() string { - return console.Colorize("List", "Tags set for "+t.Name+".") + var msg string + msg += "Tags set for " + t.Name + if t.VersionID != "" { + msg += " (" + t.VersionID + ")" + } + msg += "." + return console.Colorize("List", msg) } // JSON tagSetMessage. @@ -71,30 +98,69 @@ func (t tagSetMessage) JSON() string { return string(msgBytes) } -func checkSetTagSyntax(ctx *cli.Context) { +func parseSetTagSyntax(ctx *cli.Context) (targetURL, versionID string, timeRef time.Time, withVersions bool, tags string) { if len(ctx.Args()) != 2 || ctx.Args().Get(1) == "" { cli.ShowCommandHelpAndExit(ctx, "set", globalErrorExitStatus) } + + targetURL = ctx.Args().Get(0) + tags = ctx.Args().Get(1) + versionID = ctx.String("version-id") + withVersions = ctx.Bool("versions") + rewind := ctx.String("rewind") + + if versionID != "" && (rewind != "" || withVersions) { + fatalIf(errDummy().Trace(), "You cannot specify both --version-id and --rewind or --versions flags at the same time") + } + + timeRef = parseRewindFlag(rewind) + return +} + +// Set tags to a bucket or to a specified object/version +func setTags(ctx context.Context, clnt Client, versionID, tags string, verbose bool) { + targetName := clnt.GetURL().String() + if versionID != "" { + targetName += " (" + versionID + ")" + } + + err := clnt.SetTags(ctx, versionID, tags) + if err != nil { + fatalIf(err.Trace(tags), "Failed to set tags for "+targetName) + return + } + printMsg(tagSetMessage{ + Status: "success", + Name: clnt.GetURL().String(), + VersionID: versionID, + }) + } func mainSetTag(cliCtx *cli.Context) error { ctx, cancelSetTag := context.WithCancel(globalContext) defer cancelSetTag() - checkSetTagSyntax(cliCtx) console.SetColor("List", color.New(color.FgGreen)) - targetURL := cliCtx.Args().Get(0) - tags := cliCtx.Args().Get(1) + targetURL, versionID, timeRef, withVersions, tags := parseSetTagSyntax(cliCtx) + if timeRef.IsZero() && withVersions { + timeRef = time.Now().UTC() + } clnt, err := newClient(targetURL) fatalIf(err.Trace(cliCtx.Args()...), "Unable to initialize target "+targetURL) - fatalIf(clnt.SetTags(ctx, tags).Trace(tags), "Failed to set tags for "+targetURL) + if timeRef.IsZero() && !withVersions { + setTags(ctx, clnt, versionID, tags, true) + } else { + for content := range clnt.List(ctx, ListOptions{timeRef: timeRef, withOlderVersions: withVersions}) { + if content.Err != nil { + fatalIf(content.Err.Trace(), "Unable to list target "+targetURL) + } + setTags(ctx, clnt, content.VersionID, tags, false) + } + } - printMsg(tagSetMessage{ - Status: "success", - Name: targetURL, - }) return nil }