allow mc event remove to remove individual events (#3145)

This commit is contained in:
Daniel Valdivia
2020-04-14 18:44:29 -07:00
committed by GitHub
parent 150bc75a2c
commit 7565f9a96b
2 changed files with 75 additions and 13 deletions
+47 -10
View File
@@ -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
+28 -3
View File
@@ -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