From 42023a1298764591cd2961aa743a62b751ab5f10 Mon Sep 17 00:00:00 2001 From: Daniel Valdivia Date: Tue, 14 Apr 2020 18:44:29 -0700 Subject: [PATCH] allow mc event remove to remove individual events (#3145) --- cmd/client-s3.go | 57 +++++++++++++++++++++++++++++++++++++-------- cmd/event-remove.go | 31 +++++++++++++++++++++--- 2 files changed, 75 insertions(+), 13 deletions(-) diff --git a/cmd/client-s3.go b/cmd/client-s3.go index c41ae5a7..75c10379 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -296,7 +296,7 @@ func (c *S3Client) AddNotificationConfig(arn string, events []string, prefix, su } // RemoveNotificationConfig - Remove bucket notification -func (c *S3Client) RemoveNotificationConfig(arn string) *probe.Error { +func (c *S3Client) RemoveNotificationConfig(arn string, event string, prefix string, suffix string) *probe.Error { bucket, _ := c.url2BucketAndObject() // Remove all notification configs if arn is empty if arn == "" { @@ -317,15 +317,52 @@ func (c *S3Client) RemoveNotificationConfig(arn string) *probe.Error { } accountArn := minio.NewArn(fields[1], fields[2], fields[3], fields[4], fields[5]) - switch fields[2] { - case "sns": - mb.RemoveTopicByArn(accountArn) - case "sqs": - mb.RemoveQueueByArn(accountArn) - case "lambda": - mb.RemoveLambdaByArn(accountArn) - default: - return errInvalidArgument().Trace(fields[2]) + // if we are passed filters for either events, suffix or prefix, then only delete the single event that matches + // the arguments + if event != "" || suffix != "" || prefix != "" { + // Translate events to type events for comparison + events := strings.Split(event, ",") + var eventsTyped []minio.NotificationEventType + for _, e := range events { + switch e { + case "put": + eventsTyped = append(eventsTyped, minio.ObjectCreatedAll) + case "delete": + eventsTyped = append(eventsTyped, minio.ObjectRemovedAll) + case "get": + eventsTyped = append(eventsTyped, minio.ObjectAccessedAll) + default: + return errInvalidArgument().Trace(events...) + } + } + var err error + // based on the arn type, we'll look for the event in the corresponding sublist and delete it if there's a match + switch fields[2] { + case "sns": + err = mb.RemoveTopicByArnEventsPrefixSuffix(accountArn, eventsTyped, prefix, suffix) + case "sqs": + err = mb.RemoveQueueByArnEventsPrefixSuffix(accountArn, eventsTyped, prefix, suffix) + case "lambda": + err = mb.RemoveLambdaByArnEventsPrefixSuffix(accountArn, eventsTyped, prefix, suffix) + default: + return errInvalidArgument().Trace(fields[2]) + } + if err != nil { + return probe.NewError(err) + } + + } else { + // remove all events for matching arn + switch fields[2] { + case "sns": + mb.RemoveTopicByArn(accountArn) + case "sqs": + mb.RemoveQueueByArn(accountArn) + case "lambda": + mb.RemoveLambdaByArn(accountArn) + default: + return errInvalidArgument().Trace(fields[2]) + } } // Set the new bucket configuration diff --git a/cmd/event-remove.go b/cmd/event-remove.go index 7f38a429..8b68076c 100644 --- a/cmd/event-remove.go +++ b/cmd/event-remove.go @@ -1,5 +1,5 @@ /* - * MinIO Client (C) 2016 MinIO, Inc. + * MinIO Client (C) 2016-2020 MinIO, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,8 @@ package cmd import ( "errors" + "github.com/minio/minio-go/v6" + "github.com/fatih/color" "github.com/minio/cli" json "github.com/minio/mc/pkg/colorjson" @@ -32,6 +34,18 @@ var ( Name: "force", Usage: "force removing all bucket notifications", }, + cli.StringFlag{ + Name: "event", + Usage: "filter specific type of event. Defaults to all event", + }, + cli.StringFlag{ + Name: "prefix", + Usage: "filter event associated to the specified prefix", + }, + cli.StringFlag{ + Name: "suffix", + Usage: "filter event associated to the specified suffix", + }, } ) @@ -111,8 +125,19 @@ func mainEventRemove(ctx *cli.Context) error { fatalIf(errDummy().Trace(), "The provided url doesn't point to a S3 server.") } - err = s3Client.RemoveNotificationConfig(arn) - fatalIf(err, "Cannot disable notification on the specified bucket.") + // flags for the attributes of the even + event := ctx.String("event") + prefix := ctx.String("prefix") + suffix := ctx.String("suffix") + + err = s3Client.RemoveNotificationConfig(arn, event, prefix, suffix) + if err != nil { + if err.Cause == minio.ErrNoNotificationConfigMatch { + fatalIf(err, "Remove event failed.") + } + fatalIf(err, "Cannot disable notification on the specified bucket.") + } + printMsg(eventRemoveMessage{ARN: arn}) return nil