github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/ilm-rule-edit.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 minio "github.com/minio/minio-go/v7" 28 "github.com/minio/minio-go/v7/pkg/lifecycle" 29 "github.com/minio/pkg/v2/console" 30 ) 31 32 var ilmEditCmd = cli.Command{ 33 Name: "edit", 34 Usage: "modify a lifecycle configuration rule with given id", 35 Action: mainILMEdit, 36 OnUsageError: onUsageError, 37 Before: setGlobalsFromContext, 38 Flags: append(ilmEditFlags, globalFlags...), 39 CustomHelpTemplate: `NAME: 40 {{.HelpName}} - {{.Usage}} 41 42 USAGE: 43 {{.HelpName}} [COMMAND FLAGS] TARGET 44 45 FLAGS: 46 {{range .VisibleFlags}}{{.}} 47 {{end}} 48 DESCRIPTION: 49 Modify a lifecycle configuration rule with given id. 50 51 EXAMPLES: 52 1. Modify the expiration date for an existing rule with id "rHTY.a123". 53 {{.Prompt}} {{.HelpName}} --id "rHTY.a123" --expiry-date "2020-09-17" s3/mybucket 54 55 2. Modify the expiration and transition days for an existing rule with id "hGHKijqpo123". 56 {{.Prompt}} {{.HelpName}} --id "hGHKijqpo123" --expiry-days "300" \ 57 --transition-days "200" --storage-class "GLACIER" s3/mybucket 58 59 3. Disable the rule with id "rHTY.a123". 60 {{.Prompt}} {{.HelpName}} --id "rHTY.a123" --disable s3/mybucket 61 62 `, 63 } 64 65 var ilmEditFlags = append( 66 // Start by showing --id in edit command 67 []cli.Flag{ 68 cli.StringFlag{ 69 Name: "id", 70 Usage: "id of the rule to be modified", 71 }, 72 cli.BoolFlag{ 73 Name: "disable", 74 Usage: "disable the rule", 75 }, 76 cli.BoolFlag{ 77 Name: "enable", 78 Usage: "enable the rule", 79 }, 80 }, 81 ilmAddFlags..., 82 ) 83 84 type ilmEditMessage struct { 85 Status string `json:"status"` 86 Target string `json:"target"` 87 ID string `json:"id"` 88 } 89 90 func (i ilmEditMessage) String() string { 91 return console.Colorize(ilmThemeResultSuccess, "Lifecycle configuration rule with ID `"+i.ID+"` modified to "+i.Target+".") 92 } 93 94 func (i ilmEditMessage) JSON() string { 95 msgBytes, e := json.MarshalIndent(i, "", " ") 96 fatalIf(probe.NewError(e), "Unable to marshal into JSON.") 97 return string(msgBytes) 98 } 99 100 // Validate user given arguments 101 func checkILMEditSyntax(ctx *cli.Context) { 102 if len(ctx.Args()) != 1 { 103 showCommandHelpAndExit(ctx, globalErrorExitStatus) 104 } 105 id := ctx.String("id") 106 if id == "" { 107 fatalIf(errInvalidArgument(), "ID for lifecycle rule cannot be empty, please refer mc "+ctx.Command.FullName()+" --help for more details") 108 } 109 } 110 111 // Calls SetBucketLifecycle with the XML representation of lifecycleConfiguration type. 112 func mainILMEdit(cliCtx *cli.Context) error { 113 ctx, cancelILMEdit := context.WithCancel(globalContext) 114 defer cancelILMEdit() 115 116 checkILMEditSyntax(cliCtx) 117 setILMDisplayColorScheme() 118 args := cliCtx.Args() 119 urlStr := args.Get(0) 120 121 client, err := newClient(urlStr) 122 fatalIf(err.Trace(urlStr), "Unable to initialize client for "+urlStr) 123 124 // Configuration that is already set. 125 lfcCfg, _, err := client.GetLifecycle(ctx) 126 if err != nil { 127 if e := err.ToGoError(); minio.ToErrorResponse(e).Code == "NoSuchLifecycleConfiguration" { 128 lfcCfg = lifecycle.NewConfiguration() 129 } else { 130 fatalIf(err.Trace(args...), "Unable to fetch lifecycle rules for "+urlStr) 131 } 132 } 133 134 // Configuration that needs to be set is returned by ilm.GetILMConfigToSet. 135 // A new rule is added or the rule (if existing) is replaced 136 opts, err := ilm.GetLifecycleOptions(cliCtx) 137 fatalIf(err.Trace(args...), "Unable to generate new lifecycle rules for the input") 138 139 var rule *lifecycle.Rule 140 for i := range lfcCfg.Rules { 141 if lfcCfg.Rules[i].ID != opts.ID { 142 continue 143 } 144 rule = &lfcCfg.Rules[i] 145 break 146 } 147 148 if rule == nil { 149 fatalIf(errDummy(), "Unable to find rule id") 150 } 151 152 err = ilm.ApplyRuleFields(rule, opts) 153 fatalIf(err.Trace(args...), "Unable to generate new lifecycle rules for the input") 154 155 fatalIf(client.SetLifecycle(ctx, lfcCfg).Trace(urlStr), "Unable to set new lifecycle rules") 156 157 printMsg(ilmEditMessage{ 158 Status: "success", 159 Target: urlStr, 160 ID: opts.ID, 161 }) 162 163 return nil 164 }