github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/ilm-remove.go (about) 1 // Copyright (c) 2015-2022 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package cmd 19 20 import ( 21 "context" 22 23 "github.com/minio/cli" 24 json "github.com/minio/colorjson" 25 "github.com/minio/mc/cmd/ilm" 26 "github.com/minio/mc/pkg/probe" 27 "github.com/minio/pkg/v2/console" 28 ) 29 30 var ilmRemoveFlags = []cli.Flag{ 31 cli.StringFlag{ 32 Name: "id", 33 Usage: "id of the lifecycle rule", 34 }, 35 cli.BoolFlag{ 36 Name: "force", 37 Usage: "force flag is to be used when deleting all lifecycle configuration rules for the bucket", 38 }, 39 cli.BoolFlag{ 40 Name: "all", 41 Usage: "delete all lifecycle configuration rules of the bucket, force flag enforced", 42 }, 43 } 44 45 var ilmRmCmd = cli.Command{ 46 Name: "remove", 47 ShortName: "rm", 48 Usage: "remove (if any) existing lifecycle configuration rule", 49 Action: mainILMRemove, 50 OnUsageError: onUsageError, 51 Before: setGlobalsFromContext, 52 Flags: append(ilmRemoveFlags, globalFlags...), 53 CustomHelpTemplate: `NAME: 54 {{.HelpName}} - {{.Usage}} 55 56 USAGE: 57 {{.HelpName}} [FLAGS] TARGET 58 59 FLAGS: 60 {{range .VisibleFlags}}{{.}} 61 {{end}} 62 DESCRIPTION: 63 Remove a lifecycle configuration rule for the bucket by ID, optionally you can remove 64 all the lifecycle rules on a bucket with '--all --force' option. 65 66 EXAMPLES: 67 1. Remove the lifecycle management configuration rule given by ID "bgrt1ghju" for mybucket on alias 'myminio'. ID is case sensitive. 68 {{.Prompt}} {{.HelpName}} --id "bgrt1ghju" myminio/mybucket 69 70 2. Remove ALL the lifecycle management configuration rules for mybucket on alias 'myminio'. 71 Because the result is complete removal, the use of --force flag is enforced. 72 {{.Prompt}} {{.HelpName}} --all --force myminio/mybucket 73 `, 74 } 75 76 type ilmRmMessage struct { 77 Status string `json:"status"` 78 ID string `json:"id"` 79 Target string `json:"target"` 80 All bool `json:"all"` 81 } 82 83 func (i ilmRmMessage) String() string { 84 msg := "Rule ID `" + i.ID + "` from target " + i.Target + " removed." 85 if i.All { 86 msg = "Rules for `" + i.Target + "` removed." 87 } 88 return console.Colorize(ilmThemeResultSuccess, msg) 89 } 90 91 func (i ilmRmMessage) JSON() string { 92 msgBytes, e := json.MarshalIndent(i, "", " ") 93 fatalIf(probe.NewError(e), "Unable to marshal into JSON.") 94 return string(msgBytes) 95 } 96 97 func checkILMRemoveSyntax(ctx *cli.Context) { 98 if len(ctx.Args()) != 1 { 99 showCommandHelpAndExit(ctx, globalErrorExitStatus) 100 } 101 102 ilmAll := ctx.Bool("all") 103 ilmForce := ctx.Bool("force") 104 forceChk := (ilmAll && ilmForce) || (!ilmAll && !ilmForce) 105 if !forceChk { 106 fatalIf(errInvalidArgument(), 107 "It is mandatory to specify --all and --force flag together for mc "+ctx.Command.FullName()+".") 108 } 109 if ilmAll && ilmForce { 110 return 111 } 112 113 ilmID := ctx.String("id") 114 if ilmID == "" { 115 fatalIf(errInvalidArgument().Trace(ilmID), "ilm ID cannot be empty") 116 } 117 } 118 119 func mainILMRemove(cliCtx *cli.Context) error { 120 ctx, cancelILMImport := context.WithCancel(globalContext) 121 defer cancelILMImport() 122 123 checkILMRemoveSyntax(cliCtx) 124 setILMDisplayColorScheme() 125 args := cliCtx.Args() 126 urlStr := args.Get(0) 127 128 client, err := newClient(urlStr) 129 fatalIf(err.Trace(args...), "Unable to initialize client for "+urlStr+".") 130 131 ilmCfg, _, err := client.GetLifecycle(ctx) 132 fatalIf(err.Trace(urlStr), "Unable to fetch lifecycle rules") 133 134 ilmAll := cliCtx.Bool("all") 135 ilmForce := cliCtx.Bool("force") 136 137 if ilmAll && ilmForce { 138 ilmCfg.Rules = nil // Remove all rules 139 } else { 140 ilmCfg, err = ilm.RemoveILMRule(ilmCfg, cliCtx.String("id")) 141 fatalIf(err.Trace(urlStr, cliCtx.String("id")), "Unable to remove rule by id") 142 } 143 144 fatalIf(client.SetLifecycle(ctx, ilmCfg).Trace(urlStr), "Unable to set lifecycle rules") 145 146 printMsg(ilmRmMessage{ 147 Status: "success", 148 ID: cliCtx.String("id"), 149 All: ilmAll, 150 Target: urlStr, 151 }) 152 153 return nil 154 }