Skip to content

Commit

Permalink
Add a registry for app registry pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
ishank011 committed Jun 11, 2021
1 parent 7c238c5 commit f18baeb
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 47 deletions.
1 change: 1 addition & 0 deletions cmd/revad/runtime/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
_ "github.com/cs3org/reva/internal/http/interceptors/loader"
_ "github.com/cs3org/reva/internal/http/services/loader"
_ "github.com/cs3org/reva/pkg/app/provider/loader"
_ "github.com/cs3org/reva/pkg/app/registry/loader"
_ "github.com/cs3org/reva/pkg/appauth/manager/loader"
_ "github.com/cs3org/reva/pkg/auth/manager/loader"
_ "github.com/cs3org/reva/pkg/auth/registry/loader"
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ require (
go 1.16

replace (
//github.com/cs3org/go-cs3apis => ../cs3apis/build/go-cs3apis
github.com/cs3org/go-cs3apis => github.com/ishank011/go-cs3apis v0.0.0-20210611101519-1497ab8e8312
github.com/eventials/go-tus => github.com/andrewmostello/go-tus v0.0.0-20200314041820-904a9904af9a
github.com/oleiade/reflections => github.com/oleiade/reflections v1.0.1
Expand Down
30 changes: 17 additions & 13 deletions internal/grpc/services/appregistry/appregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

registrypb "github.com/cs3org/go-cs3apis/cs3/app/registry/v1beta1"
"github.com/cs3org/reva/pkg/app"
"github.com/cs3org/reva/pkg/app/registry/static"
"github.com/cs3org/reva/pkg/app/registry/registry"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/rgrpc"
"github.com/cs3org/reva/pkg/rgrpc/status"
Expand All @@ -37,7 +37,7 @@ func init() {
}

type svc struct {
registry app.Registry
reg app.Registry
}

func (s *svc) Close() error {
Expand All @@ -53,8 +53,14 @@ func (s *svc) Register(ss *grpc.Server) {
}

type config struct {
Driver string `mapstructure:"driver"`
Static map[string]interface{} `mapstructure:"static"`
Driver string `mapstructure:"driver"`
Drivers map[string]map[string]interface{} `mapstructure:"drivers"`
}

func (c *config) init() {
if c.Driver == "" {
c.Driver = "static"
}
}

// New creates a new StorageRegistryService
Expand All @@ -65,13 +71,13 @@ func New(m map[string]interface{}, ss *grpc.Server) (rgrpc.Service, error) {
return nil, err
}

registry, err := getRegistry(c)
reg, err := getRegistry(c)
if err != nil {
return nil, err
}

svc := &svc{
registry: registry,
reg: reg,
}

return svc, nil
Expand All @@ -86,16 +92,14 @@ func parseConfig(m map[string]interface{}) (*config, error) {
}

func getRegistry(c *config) (app.Registry, error) {
switch c.Driver {
case "static":
return static.New(c.Static)
default:
return nil, errtypes.NotFound("driver not found: " + c.Driver)
if f, ok := registry.NewFuncs[c.Driver]; ok {
return f(c.Drivers[c.Driver])
}
return nil, errtypes.NotFound("appregistrysvc: driver not found: " + c.Driver)
}

func (s *svc) GetAppProviders(ctx context.Context, req *registrypb.GetAppProvidersRequest) (*registrypb.GetAppProvidersResponse, error) {
p, err := s.registry.FindProvider(ctx, req.ResourceInfo.MimeType)
p, err := s.reg.FindProvider(ctx, req.ResourceInfo.MimeType)
if err != nil {
return &registrypb.GetAppProvidersResponse{
Status: status.NewInternal(ctx, err, "error looking for the app provider"),
Expand All @@ -116,7 +120,7 @@ func (s *svc) AddAppProvider(ctx context.Context, req *registrypb.AddAppProvider
}

func (s *svc) ListAppProviders(ctx context.Context, req *registrypb.ListAppProvidersRequest) (*registrypb.ListAppProvidersResponse, error) {
providers, err := s.registry.ListProviders(ctx)
providers, err := s.reg.ListProviders(ctx)
if err != nil {
return &registrypb.ListAppProvidersResponse{
Status: status.NewInternal(ctx, err, "error listing the app providers"),
Expand Down
8 changes: 4 additions & 4 deletions internal/grpc/services/appregistry/appregistry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func Test_ListAppProviders(t *testing.T) {
}

ss := &svc{
registry: rr,
reg: rr,
}
got, err := ss.ListAppProviders(context.Background(), nil)

Expand Down Expand Up @@ -237,7 +237,7 @@ func Test_GetAppProviders(t *testing.T) {
}

ss := &svc{
registry: rr,
reg: rr,
}

for _, tt := range tests {
Expand Down Expand Up @@ -274,12 +274,12 @@ func TestNew(t *testing.T) {
{
name: "not existing driver",
m: map[string]interface{}{"Driver": "doesnotexist"},
wantErr: "error: not found: driver not found: doesnotexist",
wantErr: "error: not found: appregistrysvc: driver not found: doesnotexist",
},
{
name: "empty",
m: map[string]interface{}{},
wantErr: "error: not found: driver not found: ",
wantErr: "error: not found: appregistrysvc: driver not found: ",
},
{
name: "extra not existing field in setting",
Expand Down
1 change: 0 additions & 1 deletion internal/grpc/services/gateway/storageprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,6 @@ func (s *svc) initiateFileDownload(ctx context.Context, req *provider.InitiateFi

func (s *svc) InitiateFileUpload(ctx context.Context, req *provider.InitiateFileUploadRequest) (*gateway.InitiateFileUploadResponse, error) {
log := appctx.GetLogger(ctx)
log.Info().Msgf("InitiateFileUpload got %+v", req)
p, st := s.getPath(ctx, req.Ref)
if st.Code != rpc.Code_CODE_OK {
return &gateway.InitiateFileUploadResponse{
Expand Down
25 changes: 25 additions & 0 deletions pkg/app/registry/loader/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2018-2021 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 loader

import (
// Load core app registry drivers.
_ "github.com/cs3org/reva/pkg/app/registry/static"
// Add your own here
)
34 changes: 34 additions & 0 deletions pkg/app/registry/registry/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2018-2021 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 registry

import "github.com/cs3org/reva/pkg/app"

// NewFunc is the function that app provider implementations
// should register to at init time.
type NewFunc func(map[string]interface{}) (app.Registry, error)

// NewFuncs is a map containing all the registered app registry backends.
var NewFuncs = map[string]NewFunc{}

// Register registers a new app registry new function.
// Not safe for concurrent use. Safe for use from package init.
func Register(name string, f NewFunc) {
NewFuncs[name] = f
}
55 changes: 30 additions & 25 deletions pkg/app/registry/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ import (

registrypb "github.com/cs3org/go-cs3apis/cs3/app/registry/v1beta1"
"github.com/cs3org/reva/pkg/app"
"github.com/cs3org/reva/pkg/app/registry/registry"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/sharedconf"
"github.com/mitchellh/mapstructure"
)

type registry struct {
rules map[string]string
func init() {
registry.Register("static", New)
}

type config struct {
Rules map[string]string `mapstructure:"rules"`
}

func (c *config) init() {
Expand All @@ -41,7 +46,28 @@ func (c *config) init() {
}
}

func (b *registry) ListProviders(ctx context.Context) ([]*registrypb.ProviderInfo, error) {
func parseConfig(m map[string]interface{}) (*config, error) {
c := &config{}
if err := mapstructure.Decode(m, c); err != nil {
return nil, err
}
return c, nil
}

type reg struct {
rules map[string]string
}

func New(m map[string]interface{}) (app.Registry, error) {
c, err := parseConfig(m)
if err != nil {
return nil, err
}
c.init()
return &reg{rules: c.Rules}, nil
}

func (b *reg) ListProviders(ctx context.Context) ([]*registrypb.ProviderInfo, error) {
var providers = make([]*registrypb.ProviderInfo, 0, len(b.rules))
for _, address := range b.rules {
providers = append(providers, &registrypb.ProviderInfo{
Expand All @@ -51,7 +77,7 @@ func (b *registry) ListProviders(ctx context.Context) ([]*registrypb.ProviderInf
return providers, nil
}

func (b *registry) FindProvider(ctx context.Context, mimeType string) (*registrypb.ProviderInfo, error) {
func (b *reg) FindProvider(ctx context.Context, mimeType string) (*registrypb.ProviderInfo, error) {
// find the longest match
var match string

Expand All @@ -70,24 +96,3 @@ func (b *registry) FindProvider(ctx context.Context, mimeType string) (*registry
}
return p, nil
}

type config struct {
Rules map[string]string
}

func parseConfig(m map[string]interface{}) (*config, error) {
c := &config{}
if err := mapstructure.Decode(m, c); err != nil {
return nil, err
}
return c, nil
}

func New(m map[string]interface{}) (app.Registry, error) {
c, err := parseConfig(m)
if err != nil {
return nil, err
}
c.init()
return &registry{rules: c.Rules}, nil
}
6 changes: 3 additions & 3 deletions pkg/auth/registry/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ package registry

import "github.com/cs3org/reva/pkg/auth"

// NewFunc is the function that auth provider implementations
// NewFunc is the function that auth registry implementations
// should register at init time.
type NewFunc func(map[string]interface{}) (auth.Registry, error)

// NewFuncs is a map containing all the registered auth backends.
// NewFuncs is a map containing all the registered auth registries.
var NewFuncs = map[string]NewFunc{}

// Register registers a new auth provider new function.
// Register registers a new auth registry new function.
// Not safe for concurrent use. Safe for use from package init.
func Register(name string, f NewFunc) {
NewFuncs[name] = f
Expand Down

0 comments on commit f18baeb

Please sign in to comment.