From 2b36376bbc6a5c68478d5e56af9ccbb1f82825b1 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Thu, 8 Oct 2020 19:04:33 +0100 Subject: [PATCH] retention: Fix status msg with info --default command (#3445) Make the output little more prettier as well. --- cmd/retention-clear.go | 4 ++-- cmd/retention-common.go | 47 +++++++++++++++++++++++++++-------------- cmd/retention-set.go | 4 ++-- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/cmd/retention-clear.go b/cmd/retention-clear.go index 081a251b..4defbabc 100644 --- a/cmd/retention-clear.go +++ b/cmd/retention-clear.go @@ -105,11 +105,11 @@ func parseClearRetentionArgs(cliCtx *cli.Context) (target, versionID string, tim // Clear Retention for one object/version or many objects within a given prefix, bypass governance is always enabled func clearRetention(ctx context.Context, target, versionID string, timeRef time.Time, withOlderVersions, isRecursive bool) error { - return applyRetention(ctx, "clear", target, versionID, timeRef, withOlderVersions, isRecursive, "", 0, minio.Days, true) + return applyRetention(ctx, lockOpClear, target, versionID, timeRef, withOlderVersions, isRecursive, "", 0, minio.Days, true) } func clearBucketLock(urlStr string) error { - return applyBucketLock("clear", urlStr, "", 0, "") + return applyBucketLock(lockOpClear, urlStr, "", 0, "") } // main for retention clear command. diff --git a/cmd/retention-common.go b/cmd/retention-common.go index 2adcce55..3383d822 100644 --- a/cmd/retention-common.go +++ b/cmd/retention-common.go @@ -30,7 +30,7 @@ import ( // Structured message depending on the type of console. type retentionCmdMessage struct { - Op string `json:"op"` + Op lockOpType `json:"op"` Mode minio.RetentionMode `json:"mode"` Validity string `json:"validity"` URLPath string `json:"urlpath"` @@ -43,7 +43,7 @@ type retentionCmdMessage struct { func (m retentionCmdMessage) String() string { var color, msg string ed := "" - if m.Op == "clear" { + if m.Op == lockOpClear { ed = "ed" } @@ -71,8 +71,17 @@ func (m retentionCmdMessage) JSON() string { return string(msgBytes) } +type lockOpType string + +const ( + lockOpInfo = "info" + lockOpClear = "clear" + lockOpSet = "set" +) + // Structured message depending on the type of console. -type lockCmdMessage struct { +type retentionBucketMessage struct { + Op lockOpType `json:"op"` Enabled string `json:"enabled"` Mode minio.RetentionMode `json:"mode"` Validity string `json:"validity"` @@ -80,16 +89,20 @@ type lockCmdMessage struct { } // Colorized message for console printing. -func (m lockCmdMessage) String() string { - if m.Mode == "" { - return console.Colorize("Clear", "Object lock configuration cleared successfully") +func (m retentionBucketMessage) String() string { + if m.Op == lockOpClear { + return console.Colorize("RetentionSuccess", "Object lock configuration cleared successfully.") } - - return fmt.Sprintf("%s mode is enabled for %s", console.Colorize("Mode", m.Mode), console.Colorize("Validity", m.Validity)) + // info/set command + if m.Mode == "" { + return console.Colorize("RetentionNotFound", "No locking mode is enabled.") + } + return console.Colorize("RetentionSuccess", fmt.Sprintf("%s mode is enabled for %s.", + console.Colorize("Mode", m.Mode), console.Colorize("Validity", m.Validity))) } // JSON'ified message for scripting. -func (m lockCmdMessage) JSON() string { +func (m retentionBucketMessage) JSON() string { msgBytes, e := json.MarshalIndent(m, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(msgBytes) @@ -110,7 +123,7 @@ func getRetainUntilDate(validity uint64, unit minio.ValidityUnit) (string, *prob return timeStr, nil } -func setRetentionSingle(ctx context.Context, op, alias, url, versionID string, mode minio.RetentionMode, retainUntil time.Time, bypassGovernance bool) *probe.Error { +func setRetentionSingle(ctx context.Context, op lockOpType, alias, url, versionID string, mode minio.RetentionMode, retainUntil time.Time, bypassGovernance bool) *probe.Error { newClnt, err := newClientFromAlias(alias, url) if err != nil { return err @@ -175,7 +188,7 @@ func checkObjectLockSupport(ctx context.Context, aliasedURL string) { } // Apply Retention for one object/version or many objects within a given prefix. -func applyRetention(ctx context.Context, op, target, versionID string, timeRef time.Time, withOlderVersions, isRecursive bool, +func applyRetention(ctx context.Context, op lockOpType, target, versionID string, timeRef time.Time, withOlderVersions, isRecursive bool, mode minio.RetentionMode, validity uint64, unit minio.ValidityUnit, bypassGovernance bool) error { clnt, err := newClient(target) if err != nil { @@ -246,7 +259,7 @@ func applyRetention(ctx context.Context, op, target, versionID string, timeRef t } if !atLeastOneRetentionApplied { - errorIf(errDummy().Trace(clnt.GetURL().String()), "Unable to find any object/version to "+op+" its retention.") + errorIf(errDummy().Trace(clnt.GetURL().String()), "Unable to find any object/version to "+string(op)+" its retention.") cErr = exitStatus(globalErrorExitStatus) // Set the exit status. } @@ -254,7 +267,7 @@ func applyRetention(ctx context.Context, op, target, versionID string, timeRef t } // applyBucketLock - set object lock configuration. -func applyBucketLock(op string, urlStr string, mode minio.RetentionMode, validity uint64, unit minio.ValidityUnit) error { +func applyBucketLock(op lockOpType, urlStr string, mode minio.RetentionMode, validity uint64, unit minio.ValidityUnit) error { client, err := newClient(urlStr) if err != nil { fatalIf(err.Trace(), "Unable to parse the provided url.") @@ -262,7 +275,7 @@ func applyBucketLock(op string, urlStr string, mode minio.RetentionMode, validit ctx, cancelLock := context.WithCancel(globalContext) defer cancelLock() - if op == "clear" || mode != "" { + if op == lockOpClear || mode != "" { err = client.SetObjectLockConfig(ctx, mode, validity, unit) fatalIf(err, "Unable to apply object lock configuration on the specified bucket.") } else { @@ -270,7 +283,8 @@ func applyBucketLock(op string, urlStr string, mode minio.RetentionMode, validit fatalIf(err, "Unable to apply object lock configuration on the specified bucket.") } - printMsg(lockCmdMessage{ + printMsg(retentionBucketMessage{ + Op: op, Enabled: "Enabled", Mode: mode, Validity: fmt.Sprintf("%d%s", validity, unit), @@ -293,7 +307,8 @@ func showBucketLock(urlStr string) error { status, mode, validity, unit, err := client.GetObjectLockConfig(ctx) fatalIf(err, "Unable to get object lock configuration on the specified bucket.") - printMsg(lockCmdMessage{ + printMsg(retentionBucketMessage{ + Op: lockOpInfo, Enabled: status, Mode: mode, Validity: fmt.Sprintf("%d%s", validity, unit), diff --git a/cmd/retention-set.go b/cmd/retention-set.go index 676ccd31..08ec8b49 100644 --- a/cmd/retention-set.go +++ b/cmd/retention-set.go @@ -120,11 +120,11 @@ func parseSetRetentionArgs(cliCtx *cli.Context) (target, versionID string, recur // Set Retention for one object/version or many objects within a given prefix. func setRetention(ctx context.Context, target, versionID string, timeRef time.Time, withOlderVersions, isRecursive bool, mode minio.RetentionMode, validity uint64, unit minio.ValidityUnit, bypassGovernance bool) error { - return applyRetention(ctx, "set", target, versionID, timeRef, withOlderVersions, isRecursive, mode, validity, unit, bypassGovernance) + return applyRetention(ctx, lockOpSet, target, versionID, timeRef, withOlderVersions, isRecursive, mode, validity, unit, bypassGovernance) } func setBucketLock(urlStr string, mode minio.RetentionMode, validity uint64, unit minio.ValidityUnit) error { - return applyBucketLock("set", urlStr, mode, validity, unit) + return applyBucketLock(lockOpSet, urlStr, mode, validity, unit) } // main for retention set command.