Skip to content

Commit

Permalink
Use resource path to decide mount ID instead of owner
Browse files Browse the repository at this point in the history
  • Loading branch information
ishank011 committed Apr 8, 2021
1 parent 4ce8069 commit 724b008
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
40 changes: 24 additions & 16 deletions pkg/cbox/storage/eoshomewrapper/eoshomewrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
package eoshome

import (
"bytes"
"context"
"text/template"

userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
"github.com/Masterminds/sprig"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/pkg/storage"
"github.com/cs3org/reva/pkg/storage/fs/registry"
"github.com/cs3org/reva/pkg/storage/utils/eosfs"
"github.com/cs3org/reva/pkg/storage/utils/templates"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
)
Expand All @@ -37,7 +38,7 @@ func init() {

type wrapper struct {
storage.FS
mountIDTemplate string
mountIDTemplate *template.Template
}

func parseConfig(m map[string]interface{}) (*eosfs.Config, string, error) {
Expand All @@ -54,7 +55,7 @@ func parseConfig(m map[string]interface{}) (*eosfs.Config, string, error) {

t, ok := m["mount_id_template"].(string)
if !ok || t == "" {
t = "eoshome-{{substr 0 1 .Id.OpaqueId}}"
t = "eoshome-{{ trimAll \"/\" .Path | substr 0 1 }}"
}

return c, t, nil
Expand All @@ -74,7 +75,12 @@ func New(m map[string]interface{}) (storage.FS, error) {
return nil, err
}

return &wrapper{FS: eos, mountIDTemplate: t}, nil
mountIDTemplate, err := template.New("mountID").Funcs(sprig.TxtFuncMap()).Parse(t)
if err != nil {
return nil, err
}

return &wrapper{FS: eos, mountIDTemplate: mountIDTemplate}, nil
}

// We need to override the two methods, GetMD and ListFolder to fill the
Expand All @@ -88,13 +94,10 @@ func (w *wrapper) GetMD(ctx context.Context, ref *provider.Reference, mdKeys []s

// We need to extract the mount ID based on the mapping template.
//
// Take the owner of the resource as the user to decide the mapping.
// If that is not present, leave it empty to be filled by storageprovider.
if res != nil && res.Owner != nil && res.Owner.OpaqueId != "" {
res.Id.StorageId = w.getMountID(ctx, res.Owner)
}
// Take the first letter of the resource path after the namespace has been removed.
// If it's empty, leave it empty to be filled by storageprovider.
res.Id.StorageId = w.getMountID(ctx, res)
return res, nil

}

func (w *wrapper) ListFolder(ctx context.Context, ref *provider.Reference, mdKeys []string) ([]*provider.ResourceInfo, error) {
Expand All @@ -103,13 +106,18 @@ func (w *wrapper) ListFolder(ctx context.Context, ref *provider.Reference, mdKey
return nil, err
}
for _, r := range res {
if r != nil && r.Owner != nil && r.Owner.OpaqueId != "" {
r.Id.StorageId = w.getMountID(ctx, r.Owner)
}
r.Id.StorageId = w.getMountID(ctx, r)
}
return res, nil
}

func (w *wrapper) getMountID(ctx context.Context, u *userpb.UserId) string {
return templates.WithUser(&userpb.User{Id: u}, w.mountIDTemplate)
func (w *wrapper) getMountID(ctx context.Context, r *provider.ResourceInfo) string {
if r == nil {
return ""
}
b := bytes.Buffer{}
if err := w.mountIDTemplate.Execute(&b, r); err != nil {
return ""
}
return b.String()
}
39 changes: 24 additions & 15 deletions pkg/cbox/storage/eoswrapper/eoswrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
package eoshome

import (
"bytes"
"context"
"text/template"

userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
"github.com/Masterminds/sprig"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/pkg/storage"
"github.com/cs3org/reva/pkg/storage/fs/registry"
"github.com/cs3org/reva/pkg/storage/utils/eosfs"
"github.com/cs3org/reva/pkg/storage/utils/templates"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
)
Expand All @@ -37,7 +38,7 @@ func init() {

type wrapper struct {
storage.FS
mountIDTemplate string
mountIDTemplate *template.Template
}

func parseConfig(m map[string]interface{}) (*eosfs.Config, string, error) {
Expand All @@ -54,7 +55,7 @@ func parseConfig(m map[string]interface{}) (*eosfs.Config, string, error) {

t, ok := m["mount_id_template"].(string)
if !ok || t == "" {
t = "eoshome-{{substr 0 1 .Id.OpaqueId}}"
t = "eoshome-{{ trimAll \"/\" .Path | substr 0 1 }}"
}

return c, t, nil
Expand All @@ -73,7 +74,12 @@ func New(m map[string]interface{}) (storage.FS, error) {
return nil, err
}

return &wrapper{FS: eos, mountIDTemplate: t}, nil
mountIDTemplate, err := template.New("mountID").Funcs(sprig.TxtFuncMap()).Parse(t)
if err != nil {
return nil, err
}

return &wrapper{FS: eos, mountIDTemplate: mountIDTemplate}, nil
}

// We need to override the two methods, GetMD and ListFolder to fill the
Expand All @@ -87,11 +93,9 @@ func (w *wrapper) GetMD(ctx context.Context, ref *provider.Reference, mdKeys []s

// We need to extract the mount ID based on the mapping template.
//
// Take the owner of the resource as the user to decide the mapping.
// If that is not present, leave it empty to be filled by storageprovider.
if res != nil && res.Owner != nil && res.Owner.OpaqueId != "" {
res.Id.StorageId = w.getMountID(ctx, res.Owner)
}
// Take the first letter of the resource path after the namespace has been removed.
// If it's empty, leave it empty to be filled by storageprovider.
res.Id.StorageId = w.getMountID(ctx, res)
return res, nil

}
Expand All @@ -102,13 +106,18 @@ func (w *wrapper) ListFolder(ctx context.Context, ref *provider.Reference, mdKey
return nil, err
}
for _, r := range res {
if r != nil && r.Owner != nil && r.Owner.OpaqueId != "" {
r.Id.StorageId = w.getMountID(ctx, r.Owner)
}
r.Id.StorageId = w.getMountID(ctx, r)
}
return res, nil
}

func (w *wrapper) getMountID(ctx context.Context, u *userpb.UserId) string {
return templates.WithUser(&userpb.User{Id: u}, w.mountIDTemplate)
func (w *wrapper) getMountID(ctx context.Context, r *provider.ResourceInfo) string {
if r == nil {
return ""
}
b := bytes.Buffer{}
if err := w.mountIDTemplate.Execute(&b, r); err != nil {
return ""
}
return b.String()
}

0 comments on commit 724b008

Please sign in to comment.