From 681cbb06defe8c61841d24e9b3e7c409aef97d7b Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 30 Nov 2020 09:10:34 -0800 Subject: [PATCH] use directly S3 event types instead of re-defining them (#3511) --- cmd/client-fs.go | 7 ++++--- cmd/client-s3.go | 34 ++++++---------------------------- cmd/mirror-main.go | 11 +++++------ cmd/watch-main.go | 11 ++++++----- cmd/watch.go | 32 ++------------------------------ 5 files changed, 23 insertions(+), 72 deletions(-) diff --git a/cmd/client-fs.go b/cmd/client-fs.go index 83971a7c..e116180a 100644 --- a/cmd/client-fs.go +++ b/cmd/client-fs.go @@ -41,6 +41,7 @@ import ( minio "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/encrypt" "github.com/minio/minio-go/v7/pkg/lifecycle" + "github.com/minio/minio-go/v7/pkg/notification" "github.com/minio/minio-go/v7/pkg/replication" "github.com/minio/minio/pkg/console" ) @@ -200,19 +201,19 @@ func (f *fsClient) Watch(ctx context.Context, options WatchOptions) (*WatchObjec Time: UTCNow().Format(timeFormatFS), Size: i.Size(), Path: event.Path(), - Type: EventCreate, + Type: notification.ObjectCreatedPut, }} } else if IsDeleteEvent(event.Event()) { eventChan <- []EventInfo{{ Time: UTCNow().Format(timeFormatFS), Path: event.Path(), - Type: EventRemove, + Type: notification.ObjectRemovedDelete, }} } else if IsGetEvent(event.Event()) { eventChan <- []EventInfo{{ Time: UTCNow().Format(timeFormatFS), Path: event.Path(), - Type: EventAccessed, + Type: notification.ObjectAccessedGet, }} } } diff --git a/cmd/client-s3.go b/cmd/client-s3.go index 6b3a1677..8742bba8 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -652,7 +652,7 @@ func (c *S3Client) notificationToEventsInfo(ninfo notification.Info) []EventInfo Size: record.S3.Object.Size, UserMetadata: record.S3.Object.UserMetadata, Path: u.String(), - Type: EventCreateCopy, + Type: notification.ObjectCreatedCopy, Host: record.Source.Host, Port: record.Source.Port, UserAgent: record.Source.UserAgent, @@ -663,7 +663,7 @@ func (c *S3Client) notificationToEventsInfo(ninfo notification.Info) []EventInfo Size: record.S3.Object.Size, UserMetadata: record.S3.Object.UserMetadata, Path: u.String(), - Type: EventCreatePutRetention, + Type: notification.EventType("s3:ObjectCreated:PutRetention"), Host: record.Source.Host, Port: record.Source.Port, UserAgent: record.Source.UserAgent, @@ -674,7 +674,7 @@ func (c *S3Client) notificationToEventsInfo(ninfo notification.Info) []EventInfo Size: record.S3.Object.Size, UserMetadata: record.S3.Object.UserMetadata, Path: u.String(), - Type: EventCreatePutLegalHold, + Type: notification.EventType("s3:ObjectCreated:PutLegalHold"), Host: record.Source.Host, Port: record.Source.Port, UserAgent: record.Source.UserAgent, @@ -685,41 +685,19 @@ func (c *S3Client) notificationToEventsInfo(ninfo notification.Info) []EventInfo Size: record.S3.Object.Size, UserMetadata: record.S3.Object.UserMetadata, Path: u.String(), - Type: EventCreate, + Type: notification.ObjectCreatedPut, Host: record.Source.Host, Port: record.Source.Port, UserAgent: record.Source.UserAgent, } } - } else if strings.HasPrefix(record.EventName, "s3:ObjectRemoved:") { + } else { eventsInfo[i] = EventInfo{ Time: record.EventTime, Size: record.S3.Object.Size, UserMetadata: record.S3.Object.UserMetadata, Path: u.String(), - Type: EventRemove, - Host: record.Source.Host, - Port: record.Source.Port, - UserAgent: record.Source.UserAgent, - } - } else if record.EventName == notification.ObjectAccessedGet { - eventsInfo[i] = EventInfo{ - Time: record.EventTime, - Size: record.S3.Object.Size, - UserMetadata: record.S3.Object.UserMetadata, - Path: u.String(), - Type: EventAccessedRead, - Host: record.Source.Host, - Port: record.Source.Port, - UserAgent: record.Source.UserAgent, - } - } else if record.EventName == notification.ObjectAccessedHead { - eventsInfo[i] = EventInfo{ - Time: record.EventTime, - Size: record.S3.Object.Size, - UserMetadata: record.S3.Object.UserMetadata, - Path: u.String(), - Type: EventAccessedStat, + Type: notification.EventType(record.EventName), Host: record.Source.Host, Port: record.Source.Port, UserAgent: record.Source.UserAgent, diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index ea05f293..ab443bc8 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -34,6 +34,7 @@ import ( "github.com/minio/mc/pkg/probe" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/encrypt" + "github.com/minio/minio-go/v7/pkg/notification" "github.com/minio/minio/pkg/console" ) @@ -451,16 +452,14 @@ func (mj *mirrorJob) watchMirrorEvents(ctx context.Context, events []EventInfo) targetURL := newClientURL(expandedTargetPath) tgtSSE := getSSE(targetPath, mj.opts.encKeyDB[targetAlias]) - if (event.Type == EventCreate) || - (event.Type == EventCreateCopy) || - (event.Type == EventCreatePutRetention) { + if strings.HasPrefix(string(event.Type), "s3:ObjectCreated:") { sourceModTime, _ := time.Parse(time.RFC3339Nano, event.Time) mirrorURL := URLs{ SourceAlias: sourceAlias, SourceContent: &ClientContent{ URL: *sourceURL, - RetentionEnabled: event.Type == EventCreatePutRetention, - LegalHoldEnabled: event.Type == EventCreatePutLegalHold, + RetentionEnabled: event.Type == notification.EventType("s3:ObjectCreated:PutRetention"), + LegalHoldEnabled: event.Type == notification.EventType("s3:ObjectCreated:PutLegalHold"), Size: event.Size, Time: sourceModTime, Metadata: event.UserMetadata, @@ -482,7 +481,7 @@ func (mj *mirrorJob) watchMirrorEvents(ctx context.Context, events []EventInfo) mj.parallel.queueTask(func() URLs { return mj.doMirrorWatch(ctx, targetPath, tgtSSE, mirrorURL) }) - } else if event.Type == EventRemove { + } else if event.Type == notification.ObjectRemovedDelete { if strings.Contains(event.UserAgent, uaMirrorAppName) { continue } diff --git a/cmd/watch-main.go b/cmd/watch-main.go index 777d2c16..6f052095 100644 --- a/cmd/watch-main.go +++ b/cmd/watch-main.go @@ -27,6 +27,7 @@ import ( "github.com/minio/cli" json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/probe" + "github.com/minio/minio-go/v7/pkg/notification" "github.com/minio/minio/pkg/console" ) @@ -99,10 +100,10 @@ func checkWatchSyntax(ctx *cli.Context) { type watchMessage struct { Status string `json:"status"` Event struct { - Time string `json:"time"` - Size int64 `json:"size"` - Path string `json:"path"` - Type EventType `json:"type"` + Time string `json:"time"` + Size int64 `json:"size"` + Path string `json:"path"` + Type notification.EventType `json:"type"` } `json:"events"` Source struct { Host string `json:"host,omitempty"` @@ -120,7 +121,7 @@ func (u watchMessage) JSON() string { func (u watchMessage) String() string { msg := console.Colorize("Time", fmt.Sprintf("[%s] ", u.Event.Time)) - if u.Event.Type == EventCreate { + if strings.HasPrefix(string(u.Event.Type), "s3:ObjectCreated:") { msg += console.Colorize("Size", fmt.Sprintf("%6s ", humanize.IBytes(uint64(u.Event.Size)))) } else { msg += fmt.Sprintf("%6s ", "") diff --git a/cmd/watch.go b/cmd/watch.go index bff3cb0c..a15dfd1b 100644 --- a/cmd/watch.go +++ b/cmd/watch.go @@ -22,35 +22,7 @@ import ( "time" "github.com/minio/mc/pkg/probe" -) - -// EventType represents the type of the event occurred. -type EventType string - -const ( - // EventCreate notifies when a new object is created - EventCreate EventType = "ObjectCreated" - - // EventCreateCopy notifies when there was a server side copy - EventCreateCopy EventType = "ObjectCreated:Copy" - - // EventRemove notifies when a new object is deleted - EventRemove = "ObjectRemoved" - - // Following are MinIO server specific events - - // EventCreatePutRetention notifies when a retention configuration is added to an object - EventCreatePutRetention EventType = "ObjectCreated:PutRetention" - - // EventCreatePutLegalHold notifies when a legal hold configuration is added to an object - EventCreatePutLegalHold EventType = "ObjectCreated:PutLegalHold" - - // EventAccessed notifies when an object is accessed. - EventAccessed = "ObjectAccessed" - // EventAccessedRead notifies when an object is accessed (specifically read/get). - EventAccessedRead = "ObjectAccessed:Read" - // EventAccessedStat notifies when an object is accessed (specifically stat). - EventAccessedStat = "ObjectAccessed:Stat" + "github.com/minio/minio-go/v7/pkg/notification" ) // EventInfo contains the information of the event that occurred and the source @@ -60,10 +32,10 @@ type EventInfo struct { Size int64 UserMetadata map[string]string Path string - Type EventType Host string Port string UserAgent string + Type notification.EventType } // WatchOptions contains watch configuration options