github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/event-add.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 "strings" 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 eventAddFlags = []cli.Flag{ 32 cli.StringFlag{ 33 Name: "event", 34 Value: "put,delete,get", 35 Usage: "filter specific type of event. Defaults to all event", 36 }, 37 cli.StringFlag{ 38 Name: "prefix", 39 Usage: "filter event associated to the specified prefix", 40 }, 41 cli.StringFlag{ 42 Name: "suffix", 43 Usage: "filter event associated to the specified suffix", 44 }, 45 cli.BoolFlag{ 46 Name: "ignore-existing, p", 47 Usage: "ignore if event already exists", 48 }, 49 } 50 51 var eventAddCmd = cli.Command{ 52 Name: "add", 53 Usage: "add a new bucket notification", 54 Action: mainEventAdd, 55 OnUsageError: onUsageError, 56 Before: setGlobalsFromContext, 57 Flags: append(eventAddFlags, globalFlags...), 58 CustomHelpTemplate: `NAME: 59 {{.HelpName}} - {{.Usage}} 60 61 USAGE: 62 {{.HelpName}} TARGET ARN [FLAGS] 63 64 FLAGS: 65 {{range .VisibleFlags}}{{.}} 66 {{end}} 67 EXAMPLES: 68 1. Enable bucket notification with a specific ARN 69 {{.Prompt}} {{.HelpName}} myminio/mybucket arn:aws:sqs:us-west-2:444455556666:your-queue 70 71 2. Enable bucket notification with filters parameters 72 {{.Prompt}} {{.HelpName}} s3/mybucket arn:aws:sqs:us-west-2:444455556666:your-queue --event put,delete,get --prefix photos/ --suffix .jpg 73 74 3. Ignore duplicate bucket notification with -p flag 75 {{.Prompt}} {{.HelpName}} s3/mybucket arn:aws:sqs:us-west-2:444455556666:your-queue -p --event put,delete,get --prefix photos/ --suffix .jpg 76 77 4. Enable bucket notification for Replication and ILM transition events to a specific ARN 78 {{.Prompt}} {{.HelpName}} myminio/mysourcebucket arn:aws:sqs:us-west-2:444455556666:your-queue --event replica,ilm 79 `, 80 } 81 82 // checkEventAddSyntax - validate all the passed arguments 83 func checkEventAddSyntax(ctx *cli.Context) { 84 if len(ctx.Args()) != 2 { 85 showCommandHelpAndExit(ctx, 1) // last argument is exit code 86 } 87 } 88 89 // eventAddMessage container 90 type eventAddMessage struct { 91 ARN string `json:"arn"` 92 Event []string `json:"event"` 93 Prefix string `json:"prefix"` 94 Suffix string `json:"suffix"` 95 Status string `json:"status"` 96 } 97 98 // JSON jsonified update message. 99 func (u eventAddMessage) JSON() string { 100 u.Status = "success" 101 eventAddMessageJSONBytes, e := json.MarshalIndent(u, "", " ") 102 fatalIf(probe.NewError(e), "Unable to marshal into JSON.") 103 return string(eventAddMessageJSONBytes) 104 } 105 106 func (u eventAddMessage) String() string { 107 msg := console.Colorize("Event", "Successfully added "+u.ARN) 108 return msg 109 } 110 111 func mainEventAdd(cliCtx *cli.Context) error { 112 ctx, cancelEventAdd := context.WithCancel(globalContext) 113 defer cancelEventAdd() 114 115 console.SetColor("Event", color.New(color.FgGreen, color.Bold)) 116 117 checkEventAddSyntax(cliCtx) 118 119 args := cliCtx.Args() 120 path := args[0] 121 arn := args[1] 122 ignoreExisting := cliCtx.Bool("p") 123 124 event := strings.Split(cliCtx.String("event"), ",") 125 prefix := cliCtx.String("prefix") 126 suffix := cliCtx.String("suffix") 127 128 client, err := newClient(path) 129 if err != nil { 130 fatalIf(err.Trace(), "Unable to parse the provided url.") 131 } 132 133 s3Client, ok := client.(*S3Client) 134 if !ok { 135 fatalIf(errDummy().Trace(), "The provided url doesn't point to a S3 server.") 136 } 137 138 err = s3Client.AddNotificationConfig(ctx, arn, event, prefix, suffix, ignoreExisting) 139 fatalIf(err, "Unable to enable notification on the specified bucket.") 140 printMsg(eventAddMessage{ 141 ARN: arn, 142 Event: event, 143 Prefix: prefix, 144 Suffix: suffix, 145 }) 146 147 return nil 148 }