github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/event-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 "errors" 23 24 "github.com/fatih/color" 25 "github.com/minio/cli" 26 json "github.com/minio/colorjson" 27 "github.com/minio/mc/pkg/probe" 28 "github.com/minio/pkg/v2/console" 29 ) 30 31 var eventRemoveFlags = []cli.Flag{ 32 cli.BoolFlag{ 33 Name: "force", 34 Usage: "force removing all bucket notifications", 35 }, 36 cli.StringFlag{ 37 Name: "event", 38 Value: "put,delete,get", 39 Usage: "filter specific type of event. Defaults to all event", 40 }, 41 cli.StringFlag{ 42 Name: "prefix", 43 Usage: "filter event associated to the specified prefix", 44 }, 45 cli.StringFlag{ 46 Name: "suffix", 47 Usage: "filter event associated to the specified suffix", 48 }, 49 } 50 51 var eventRemoveCmd = cli.Command{ 52 Name: "remove", 53 ShortName: "rm", 54 Usage: "remove a bucket notification; '--force' removes all bucket notifications", 55 Action: mainEventRemove, 56 OnUsageError: onUsageError, 57 Before: setGlobalsFromContext, 58 Flags: append(eventRemoveFlags, globalFlags...), 59 CustomHelpTemplate: `NAME: 60 {{.HelpName}} - {{.Usage}} 61 62 USAGE: 63 {{.HelpName}} TARGET [ARN] [FLAGS] 64 65 FLAGS: 66 {{range .VisibleFlags}}{{.}} 67 {{end}} 68 EXAMPLES: 69 1. Remove bucket notification associated to a specific arn 70 {{.Prompt}} {{.HelpName}} myminio/mybucket arn:aws:sqs:us-west-2:444455556666:your-queue 71 72 2. Remove all bucket notifications. --force flag is mandatory here 73 {{.Prompt}} {{.HelpName}} myminio/mybucket --force 74 `, 75 } 76 77 // checkEventRemoveSyntax - validate all the passed arguments 78 func checkEventRemoveSyntax(ctx *cli.Context) { 79 if len(ctx.Args()) == 0 || len(ctx.Args()) > 2 { 80 showCommandHelpAndExit(ctx, 1) // last argument is exit code 81 } 82 if len(ctx.Args()) == 1 && !ctx.Bool("force") { 83 fatalIf(probe.NewError(errors.New("")), "--force flag needs to be passed to remove all bucket notifications.") 84 } 85 } 86 87 // eventRemoveMessage container 88 type eventRemoveMessage struct { 89 ARN string `json:"arn"` 90 Status string `json:"status"` 91 } 92 93 // JSON jsonified remove message. 94 func (u eventRemoveMessage) JSON() string { 95 u.Status = "success" 96 eventRemoveMessageJSONBytes, e := json.MarshalIndent(u, "", " ") 97 fatalIf(probe.NewError(e), "Unable to marshal into JSON.") 98 return string(eventRemoveMessageJSONBytes) 99 } 100 101 func (u eventRemoveMessage) String() string { 102 msg := console.Colorize("Event", "Successfully removed "+u.ARN) 103 return msg 104 } 105 106 func mainEventRemove(cliCtx *cli.Context) error { 107 ctx, cancelEventRemove := context.WithCancel(globalContext) 108 defer cancelEventRemove() 109 110 console.SetColor("Event", color.New(color.FgGreen, color.Bold)) 111 112 checkEventRemoveSyntax(cliCtx) 113 114 args := cliCtx.Args() 115 path := args.Get(0) 116 117 arn := "" 118 if len(args) == 2 { 119 arn = args.Get(1) 120 } 121 122 client, err := newClient(path) 123 if err != nil { 124 fatalIf(err.Trace(), "Unable to parse the provided url.") 125 } 126 127 s3Client, ok := client.(*S3Client) 128 if !ok { 129 fatalIf(errDummy().Trace(), "The provided url doesn't point to a S3 server.") 130 } 131 132 // flags for the attributes of the even 133 event := cliCtx.String("event") 134 prefix := cliCtx.String("prefix") 135 suffix := cliCtx.String("suffix") 136 137 err = s3Client.RemoveNotificationConfig(ctx, arn, event, prefix, suffix) 138 if err != nil { 139 fatalIf(err, "Unable to disable notification on the specified bucket.") 140 } 141 142 printMsg(eventRemoveMessage{ARN: arn}) 143 144 return nil 145 }