From aa4d19dd9d28b67bd90b4264856c84f6645e0293 Mon Sep 17 00:00:00 2001 From: jkoberg Date: Fri, 18 Mar 2022 11:52:04 +0100 Subject: [PATCH 1/3] spaces events Signed-off-by: jkoberg --- .../eventsmiddleware/conversion.go | 52 +++++++++++++ .../interceptors/eventsmiddleware/events.go | 25 ++++++ pkg/events/spaces.go | 78 +++++++++++++++++++ pkg/utils/utils.go | 10 +++ 4 files changed, 165 insertions(+) create mode 100644 pkg/events/spaces.go diff --git a/internal/grpc/interceptors/eventsmiddleware/conversion.go b/internal/grpc/interceptors/eventsmiddleware/conversion.go index 2e934295c9..18d4ffc016 100644 --- a/internal/grpc/interceptors/eventsmiddleware/conversion.go +++ b/internal/grpc/interceptors/eventsmiddleware/conversion.go @@ -19,6 +19,7 @@ package eventsmiddleware import ( + user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1" link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1" provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" @@ -202,3 +203,54 @@ func FileVersionRestored(r *provider.RestoreFileVersionResponse, req *provider.R Key: req.Key, } } + +// SpaceCreated converts the response to an event +func SpaceCreated(r *provider.CreateStorageSpaceResponse) events.SpaceCreated { + return events.SpaceCreated{ + ID: r.StorageSpace.Id, + Owner: extractOwner(r.StorageSpace.Owner), + Root: r.StorageSpace.Root, + Name: r.StorageSpace.Name, + Type: r.StorageSpace.SpaceType, + Quota: r.StorageSpace.Quota, + MTime: r.StorageSpace.Mtime, + } +} + +// SpaceRenamed converts the response to an event +func SpaceRenamed(r *provider.UpdateStorageSpaceResponse, req *provider.UpdateStorageSpaceRequest) events.SpaceRenamed { + return events.SpaceRenamed{ + ID: r.StorageSpace.Id, + Owner: extractOwner(r.StorageSpace.Owner), + Name: r.StorageSpace.Name, + } +} + +// SpaceEnabled converts the response to an event +func SpaceEnabled(r *provider.UpdateStorageSpaceResponse, req *provider.UpdateStorageSpaceRequest) events.SpaceEnabled { + return events.SpaceEnabled{ + ID: r.StorageSpace.Id, + Owner: extractOwner(r.StorageSpace.Owner), + } +} + +// SpaceDisabled converts the response to an event +func SpaceDisabled(r *provider.DeleteStorageSpaceResponse, req *provider.DeleteStorageSpaceRequest) events.SpaceDisabled { + return events.SpaceDisabled{ + ID: req.Id, + } +} + +// SpaceDeleted converts the response to an event +func SpaceDeleted(r *provider.DeleteStorageSpaceResponse, req *provider.DeleteStorageSpaceRequest) events.SpaceDeleted { + return events.SpaceDeleted{ + ID: req.Id, + } +} + +func extractOwner(u *user.User) *user.UserId { + if u != nil { + return u.Id + } + return nil +} diff --git a/internal/grpc/interceptors/eventsmiddleware/events.go b/internal/grpc/interceptors/eventsmiddleware/events.go index 63259ac474..5e378f4b99 100644 --- a/internal/grpc/interceptors/eventsmiddleware/events.go +++ b/internal/grpc/interceptors/eventsmiddleware/events.go @@ -34,6 +34,7 @@ import ( "github.com/cs3org/reva/v2/pkg/events" "github.com/cs3org/reva/v2/pkg/events/server" "github.com/cs3org/reva/v2/pkg/rgrpc" + "github.com/cs3org/reva/v2/pkg/utils" ) const ( @@ -123,6 +124,30 @@ func NewUnary(m map[string]interface{}) (grpc.UnaryServerInterceptor, int, error if isSuccess(v) { ev = FileVersionRestored(v, req.(*provider.RestoreFileVersionRequest)) } + case *provider.CreateStorageSpaceResponse: + if isSuccess(v) { + ev = SpaceCreated(v) + } + case *provider.UpdateStorageSpaceResponse: + if isSuccess(v) { + r := req.(*provider.UpdateStorageSpaceRequest) + if r.StorageSpace.Name != "" { + ev = SpaceRenamed(v, r) + } + + if utils.ExistsInOpaque(r.Opaque, "restore") { + ev = SpaceEnabled(v, r) + } + } + case *provider.DeleteStorageSpaceResponse: + if isSuccess(v) { + r := req.(*provider.DeleteStorageSpaceRequest) + if utils.ExistsInOpaque(r.Opaque, "purge") { + ev = SpaceDeleted(v, r) + } else { + ev = SpaceDisabled(v, r) + } + } } if ev != nil { diff --git a/pkg/events/spaces.go b/pkg/events/spaces.go new file mode 100644 index 0000000000..38c693a059 --- /dev/null +++ b/pkg/events/spaces.go @@ -0,0 +1,78 @@ +package events + +import ( + "encoding/json" + + user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" + provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" + types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" +) + +// SpaceCreated is emitted when a space is created +type SpaceCreated struct { + ID *provider.StorageSpaceId + Owner *user.UserId + Root *provider.ResourceId + Name string + Type string + Quota *provider.Quota + MTime *types.Timestamp +} + +// Unmarshal to fulfill umarshaller interface +func (SpaceCreated) Unmarshal(v []byte) (interface{}, error) { + e := SpaceCreated{} + err := json.Unmarshal(v, &e) + return e, err +} + +// SpaceRenamed is emitted when a space is renamed +type SpaceRenamed struct { + ID *provider.StorageSpaceId + Owner *user.UserId + Name string +} + +// Unmarshal to fulfill umarshaller interface +func (SpaceRenamed) Unmarshal(v []byte) (interface{}, error) { + e := SpaceRenamed{} + err := json.Unmarshal(v, &e) + return e, err +} + +// SpaceDisabled is emitted when a space is disabled +type SpaceDisabled struct { + ID *provider.StorageSpaceId +} + +// Unmarshal to fulfill umarshaller interface +func (SpaceDisabled) Unmarshal(v []byte) (interface{}, error) { + e := SpaceDisabled{} + err := json.Unmarshal(v, &e) + return e, err +} + +// SpaceEnabled is emitted when a space is (re-)enabled +type SpaceEnabled struct { + ID *provider.StorageSpaceId + Owner *user.UserId +} + +// Unmarshal to fulfill umarshaller interface +func (SpaceEnabled) Unmarshal(v []byte) (interface{}, error) { + e := SpaceEnabled{} + err := json.Unmarshal(v, &e) + return e, err +} + +// SpaceDeleted is emitted when a space is deleted +type SpaceDeleted struct { + ID *provider.StorageSpaceId +} + +// Unmarshal to fulfill umarshaller interface +func (SpaceDeleted) Unmarshal(v []byte) (interface{}, error) { + e := SpaceDeleted{} + err := json.Unmarshal(v, &e) + return e, err +} diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 0420f082b9..4e31bd9c17 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -431,3 +431,13 @@ func ReadPlainFromOpaque(o *types.Opaque, key string) string { } return "" } + +// ExistsInOpaque returns true if the key exists in the opaque (ignoring the value) +func ExistsInOpaque(o *types.Opaque, key string) bool { + if o == nil || o.Map == nil { + return false + } + + _, ok := o.Map[key] + return ok +} From b87471c9d8de6476ed78932750a454f766ba343c Mon Sep 17 00:00:00 2001 From: jkoberg Date: Fri, 18 Mar 2022 16:17:04 +0100 Subject: [PATCH 2/3] add changelog Signed-off-by: jkoberg --- changelog/unreleased/space-events.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/unreleased/space-events.md diff --git a/changelog/unreleased/space-events.md b/changelog/unreleased/space-events.md new file mode 100644 index 0000000000..4b1fe3d157 --- /dev/null +++ b/changelog/unreleased/space-events.md @@ -0,0 +1,5 @@ +Enhancement: Add space specific events + +See `pkg/events/spaces.go` for full list + +https://github.com/cs3org/reva/pull/2647 From eebb4196d3744beb18986fb0d95e161498c3755e Mon Sep 17 00:00:00 2001 From: jkoberg Date: Mon, 21 Mar 2022 09:25:06 +0100 Subject: [PATCH 3/3] license header Signed-off-by: jkoberg --- pkg/events/spaces.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pkg/events/spaces.go b/pkg/events/spaces.go index 38c693a059..4f03d2a678 100644 --- a/pkg/events/spaces.go +++ b/pkg/events/spaces.go @@ -1,3 +1,21 @@ +// Copyright 2018-2022 CERN +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// In applying this license, CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + package events import (