From a718b9265293ea4334257f95ab8b489f6f56bdc0 Mon Sep 17 00:00:00 2001 From: Angira Kekteeva Date: Thu, 31 Mar 2022 10:24:57 +0400 Subject: [PATCH] [#357] Add ObjectDelete notifications Signed-off-by: Angira Kekteeva --- api/handler/delete.go | 38 +++++++++++++++++++++++++++++++++++++- api/layer/layer.go | 4 ++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/api/handler/delete.go b/api/handler/delete.go index e43bd75b7..ac75c1fc1 100644 --- a/api/handler/delete.go +++ b/api/handler/delete.go @@ -57,9 +57,10 @@ type DeleteObjectsResponse struct { func (h *handler) DeleteObjectHandler(w http.ResponseWriter, r *http.Request) { reqInfo := api.GetReqInfo(r.Context()) + versionID := reqInfo.URL.Query().Get(api.QueryVersionID) versionedObject := []*layer.VersionedObject{{ Name: reqInfo.ObjectName, - VersionID: reqInfo.URL.Query().Get(api.QueryVersionID), + VersionID: versionID, }} bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName) @@ -96,6 +97,41 @@ func (h *handler) DeleteObjectHandler(w http.ResponseWriter, r *http.Request) { zap.Error(err)) } + var m *layer.SendNotificationParams + + if bktSettings.VersioningEnabled && len(versionID) == 0 { + m = &layer.SendNotificationParams{ + Event: layer.EventObjectRemovedDeleteMarkerCreated, + ObjInfo: &data.ObjectInfo{ + Name: reqInfo.ObjectName, + HashSum: deletedObject.DeleteMarkerEtag, + }, + BktInfo: bktInfo, + ReqInfo: reqInfo, + } + } else { + oid := oid.NewID() + if len(versionID) != 0 { + if err := oid.Parse(versionID); err != nil { + h.log.Error("couldn't send notification: %w", zap.Error(err)) + } + } + + m = &layer.SendNotificationParams{ + Event: layer.EventObjectRemovedDelete, + ObjInfo: &data.ObjectInfo{ + Name: reqInfo.ObjectName, + ID: oid, + }, + BktInfo: bktInfo, + ReqInfo: reqInfo, + } + } + + if err := h.obj.SendNotifications(r.Context(), m); err != nil { + h.log.Error("couldn't send notification: %w", zap.Error(err)) + } + if deletedObject.DeleteMarkVersion != "" { w.Header().Set(api.AmzDeleteMarker, strconv.FormatBool(true)) w.Header().Set(api.AmzVersionID, deletedObject.DeleteMarkVersion) diff --git a/api/layer/layer.go b/api/layer/layer.go index 79866ee9f..3842ae5d4 100644 --- a/api/layer/layer.go +++ b/api/layer/layer.go @@ -181,6 +181,7 @@ type ( Name string VersionID string DeleteMarkVersion string + DeleteMarkerEtag string Error error } @@ -617,6 +618,9 @@ func (n *layer) deleteObject(ctx context.Context, bkt *data.BucketInfo, settings } if len(obj.VersionID) == 0 { obj.DeleteMarkVersion = objInfo.Version() + if versioningEnabled { + obj.DeleteMarkerEtag = objInfo.HashSum + } } return obj