Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Space Events #2647

Merged
merged 3 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/space-events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: Add space specific events

See `pkg/events/spaces.go` for full list

https://github.com/cs3org/reva/pull/2647
52 changes: 52 additions & 0 deletions internal/grpc/interceptors/eventsmiddleware/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
25 changes: 25 additions & 0 deletions internal/grpc/interceptors/eventsmiddleware/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down
96 changes: 96 additions & 0 deletions pkg/events/spaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// 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 (
"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
}
10 changes: 10 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}