Add flag to allow delete marker replication (#3395)

This commit is contained in:
Poorna Krishnamoorthy
2020-11-10 19:36:48 -08:00
committed by GitHub
parent 63ce578c46
commit 786a53672b
7 changed files with 135 additions and 45 deletions
+1 -1
View File
@@ -265,7 +265,7 @@ var completeCmds = map[string]complete.Predictor{
"/encrypt/clear": s3Complete{deepLevel: 2},
"/replicate/add": s3Complete{deepLevel: 2},
"/replicate/set": s3Complete{deepLevel: 2},
"/replicate/edit": s3Complete{deepLevel: 2},
"/replicate/ls": s3Complete{deepLevel: 2},
"/replicate/rm": s3Complete{deepLevel: 2},
"/replicate/export": s3Complete{deepLevel: 2},
+44 -12
View File
@@ -18,7 +18,9 @@ package cmd
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/fatih/color"
"github.com/minio/cli"
@@ -57,6 +59,10 @@ var replicateAddFlags = []cli.Flag{
Name: "remote-bucket",
Usage: "remote bucket, should be a unique value for the configuration",
},
cli.StringFlag{
Name: "replicate",
Usage: "comma separated list to enable replication of delete markers, and/or deletion of versioned objects.Valid options are \"delete-marker\", \"delete\" and \"\"",
},
}
var replicateAddCmd = cli.Command{
@@ -75,12 +81,15 @@ FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Add replication configuration rule on bucket "mybucket" for alias "myminio".
1. Add replication configuration rule on bucket "mybucket" for alias "myminio" to replicate
all objects with tags key1=value1, key2=value2 to destbucket, including delete markers and
versioned deletes.
{{.Prompt}} {{.HelpName}} myminio/mybucket/prefix --tags "key1=value1&key2=value2" \
--storage-class "STANDARD" \
--arn 'arn:minio:replication::c5be6b16-769d-432a-9ef1-4567081f3566:destbucket' \
--priority 1 \
--remote-bucket "destbucket"
--replicate "delete,delete-marker"
2. Add replication configuration rule with Disabled status on bucket "mybucket" for alias "myminio".
{{.Prompt}} {{.HelpName}} myminio/mybucket/prefix --tags "key1=value1&key2=value2" \
@@ -105,6 +114,11 @@ type replicateAddMessage struct {
ID string `json:"id"`
}
const (
enableStatus = "enable"
disableStatus = "disable"
)
func (l replicateAddMessage) JSON() string {
l.Status = "success"
jsonMessageBytes, e := json.MarshalIndent(l, "", " ")
@@ -135,19 +149,37 @@ func mainReplicateAdd(cliCtx *cli.Context) error {
fatalIf(err, "Unable to initialize connection.")
rcfg, err := client.GetReplication(ctx)
fatalIf(err.Trace(args...), "Unable to get replication configuration")
ruleStatus := "enable"
if cliCtx.Bool("disable") {
ruleStatus = "disable"
ruleStatus := enableStatus
if cliCtx.Bool(disableStatus) {
ruleStatus = disableStatus
}
dmReplicateStatus := disableStatus
deleteReplicationStatus := disableStatus
if cliCtx.IsSet("replicate") {
replSlice := strings.Split(cliCtx.String("replicate"), ",")
for _, opt := range replSlice {
switch strings.TrimSpace(strings.ToLower(opt)) {
case "delete-marker":
dmReplicateStatus = enableStatus
case "delete":
deleteReplicationStatus = enableStatus
default:
fatalIf(probe.NewError(fmt.Errorf("invalid value for --replicate flag %s", cliCtx.String("replicate"))), "--replicate flag takes one or more comma separated string with values \"delete, delete-marker\" or \"\" to disable delete marker replication")
}
}
}
opts := replication.Options{
TagString: cliCtx.String("tags"),
RoleArn: cliCtx.String("arn"),
StorageClass: cliCtx.String("storage-class"),
Priority: strconv.Itoa(cliCtx.Int("priority")),
RuleStatus: ruleStatus,
ID: cliCtx.String("id"),
DestBucket: cliCtx.String("remote-bucket"),
Op: replication.AddOption,
TagString: cliCtx.String("tags"),
RoleArn: cliCtx.String("arn"),
StorageClass: cliCtx.String("storage-class"),
Priority: strconv.Itoa(cliCtx.Int("priority")),
RuleStatus: ruleStatus,
ID: cliCtx.String("id"),
DestBucket: cliCtx.String("remote-bucket"),
Op: replication.AddOption,
ReplicateDeleteMarkers: dmReplicateStatus,
ReplicateDeletes: deleteReplicationStatus,
}
fatalIf(client.SetReplication(ctx, &rcfg, opts), "Could not add replication rule")
printMsg(replicateAddMessage{
+53 -20
View File
@@ -18,6 +18,7 @@ package cmd
import (
"context"
"fmt"
"strconv"
"strings"
@@ -29,7 +30,7 @@ import (
"github.com/minio/minio/pkg/console"
)
var replicateSetFlags = []cli.Flag{
var replicateEditFlags = []cli.Flag{
cli.StringFlag{
Name: "id",
Usage: "id for the rule, should be a unique value",
@@ -54,14 +55,18 @@ var replicateSetFlags = []cli.Flag{
Name: "remote-bucket",
Usage: "destination bucket, should be a unique value for the configuration",
},
cli.StringFlag{
Name: "replicate",
Usage: "comma separated list to enable replication of delete markers, and/or deletion of versioned objects.Valid options are \"delete-marker\", \"delete\" and \"\"",
},
}
var replicateSetCmd = cli.Command{
Name: "set",
var replicateEditCmd = cli.Command{
Name: "edit",
Usage: "modify an existing server side replication configuration rule",
Action: mainReplicateSet,
Action: mainReplicateEdit,
Before: setGlobalsFromContext,
Flags: append(globalFlags, replicateSetFlags...),
Flags: append(globalFlags, replicateEditFlags...),
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
@@ -83,44 +88,51 @@ EXAMPLES:
--storage-class "STANDARD" --priority 2
4. Clear tags for replication configuration rule with ID "kMYD.491" on a target myminio/bucket.
{{.Prompt}} {{.HelpName}} myminio/mybucket --id "kMYD.491" --tags ""
5. Enable delete marker replication on a replication configuration rule with ID "kxYD.491" on a target myminio/bucket.
{{.Prompt}} {{.HelpName}} myminio/mybucket --id "kxYD.491" --replicate "delete-marker"
6. Disable delete marker and versioned delete replication on a replication configuration rule with ID "kxYD.491" on a target myminio/bucket.
{{.Prompt}} {{.HelpName}} myminio/mybucket --id "kxYD.491" --replicate ""
`,
}
// checkReplicateSetSyntax - validate all the passed arguments
func checkReplicateSetSyntax(ctx *cli.Context) {
// checkReplicateEditSyntax - validate all the passed arguments
func checkReplicateEditSyntax(ctx *cli.Context) {
if len(ctx.Args()) != 1 {
cli.ShowCommandHelpAndExit(ctx, "set", 1) // last argument is exit code
cli.ShowCommandHelpAndExit(ctx, "edit", 1) // last argument is exit code
}
}
type replicateSetMessage struct {
type replicateEditMessage struct {
Op string `json:"op"`
Status string `json:"status"`
URL string `json:"url"`
ID string `json:"id"`
}
func (l replicateSetMessage) JSON() string {
func (l replicateEditMessage) JSON() string {
l.Status = "success"
jsonMessageBytes, e := json.MarshalIndent(l, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(jsonMessageBytes)
}
func (l replicateSetMessage) String() string {
func (l replicateEditMessage) String() string {
if l.ID != "" {
return console.Colorize("replicateSetMessage", "Replication configuration rule with ID `"+l.ID+"` applied to "+l.URL+".")
return console.Colorize("replicateEditMessage", "Replication configuration rule with ID `"+l.ID+"` applied to "+l.URL+".")
}
return console.Colorize("replicateSetMessage", "Replication configuration rule applied to "+l.URL+" successfully.")
return console.Colorize("replicateEditMessage", "Replication configuration rule applied to "+l.URL+" successfully.")
}
func mainReplicateSet(cliCtx *cli.Context) error {
ctx, cancelReplicateSet := context.WithCancel(globalContext)
defer cancelReplicateSet()
func mainReplicateEdit(cliCtx *cli.Context) error {
ctx, cancelReplicateEdit := context.WithCancel(globalContext)
defer cancelReplicateEdit()
console.SetColor("replicateSetMessage", color.New(color.FgGreen))
console.SetColor("replicateEditMessage", color.New(color.FgGreen))
checkReplicateSetSyntax(cliCtx)
checkReplicateEditSyntax(cliCtx)
// Get the alias parameter from cli
args := cliCtx.Args()
@@ -141,6 +153,24 @@ func mainReplicateSet(cliCtx *cli.Context) error {
fatalIf(err.Trace(args...), "--state can be either `enable` or `disable`")
}
}
var vDeleteReplicate, dmReplicate string
if cliCtx.IsSet("replicate") {
replSlice := strings.Split(cliCtx.String("replicate"), ",")
vDeleteReplicate = disableStatus
dmReplicate = disableStatus
for _, opt := range replSlice {
switch strings.TrimSpace(strings.ToLower(opt)) {
case "delete-marker":
dmReplicate = enableStatus
case "delete":
vDeleteReplicate = enableStatus
default:
if opt != "" {
fatalIf(probe.NewError(fmt.Errorf("invalid value for --replicate flag %s", cliCtx.String("replicate"))), "--replicate flag takes one or more comma separated string with values \"delete, delete-marker\"")
}
}
}
}
opts := replication.Options{
TagString: cliCtx.String("tags"),
RoleArn: cliCtx.String("arn"),
@@ -155,9 +185,12 @@ func mainReplicateSet(cliCtx *cli.Context) error {
if cliCtx.IsSet("priority") {
opts.Priority = strconv.Itoa(cliCtx.Int("priority"))
}
if cliCtx.IsSet("replicate") {
opts.ReplicateDeletes = vDeleteReplicate
opts.ReplicateDeleteMarkers = dmReplicate
}
fatalIf(client.SetReplication(ctx, &rcfg, opts), "Could not modify replication rule")
printMsg(replicateSetMessage{
printMsg(replicateEditMessage{
Op: "set",
URL: aliasedURL,
ID: opts.ID,
+1 -1
View File
@@ -20,7 +20,7 @@ import "github.com/minio/cli"
var replicateSubcommands = []cli.Command{
replicateAddCmd,
replicateSetCmd,
replicateEditCmd,
replicateListCmd,
replicateExportCmd,
replicateImportCmd,