From f18baebbfcbe073996f5fea56222c451f9cad9d5 Mon Sep 17 00:00:00 2001 From: Ishank Arora Date: Fri, 11 Jun 2021 12:37:26 +0200 Subject: [PATCH] Add a registry for app registry pkg --- cmd/revad/runtime/loader.go | 1 + go.mod | 1 - .../grpc/services/appregistry/appregistry.go | 30 +++++----- .../services/appregistry/appregistry_test.go | 8 +-- .../grpc/services/gateway/storageprovider.go | 1 - pkg/app/registry/loader/loader.go | 25 +++++++++ pkg/app/registry/registry/registry.go | 34 ++++++++++++ pkg/app/registry/static/static.go | 55 ++++++++++--------- pkg/auth/registry/registry/registry.go | 6 +- 9 files changed, 114 insertions(+), 47 deletions(-) create mode 100644 pkg/app/registry/loader/loader.go create mode 100644 pkg/app/registry/registry/registry.go diff --git a/cmd/revad/runtime/loader.go b/cmd/revad/runtime/loader.go index ba169a1d923..241f625baec 100644 --- a/cmd/revad/runtime/loader.go +++ b/cmd/revad/runtime/loader.go @@ -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" diff --git a/go.mod b/go.mod index e41875fb617..faebfd4ad9b 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/internal/grpc/services/appregistry/appregistry.go b/internal/grpc/services/appregistry/appregistry.go index 93ba74382c5..74449f183b9 100644 --- a/internal/grpc/services/appregistry/appregistry.go +++ b/internal/grpc/services/appregistry/appregistry.go @@ -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" @@ -37,7 +37,7 @@ func init() { } type svc struct { - registry app.Registry + reg app.Registry } func (s *svc) Close() error { @@ -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 @@ -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 @@ -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 ®istrypb.GetAppProvidersResponse{ Status: status.NewInternal(ctx, err, "error looking for the app provider"), @@ -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 ®istrypb.ListAppProvidersResponse{ Status: status.NewInternal(ctx, err, "error listing the app providers"), diff --git a/internal/grpc/services/appregistry/appregistry_test.go b/internal/grpc/services/appregistry/appregistry_test.go index b2c7cb4e9c5..158e1e282d0 100644 --- a/internal/grpc/services/appregistry/appregistry_test.go +++ b/internal/grpc/services/appregistry/appregistry_test.go @@ -120,7 +120,7 @@ func Test_ListAppProviders(t *testing.T) { } ss := &svc{ - registry: rr, + reg: rr, } got, err := ss.ListAppProviders(context.Background(), nil) @@ -237,7 +237,7 @@ func Test_GetAppProviders(t *testing.T) { } ss := &svc{ - registry: rr, + reg: rr, } for _, tt := range tests { @@ -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", diff --git a/internal/grpc/services/gateway/storageprovider.go b/internal/grpc/services/gateway/storageprovider.go index 920d5bbc157..90a5a042725 100644 --- a/internal/grpc/services/gateway/storageprovider.go +++ b/internal/grpc/services/gateway/storageprovider.go @@ -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{ diff --git a/pkg/app/registry/loader/loader.go b/pkg/app/registry/loader/loader.go new file mode 100644 index 00000000000..7c06a89f925 --- /dev/null +++ b/pkg/app/registry/loader/loader.go @@ -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 +) diff --git a/pkg/app/registry/registry/registry.go b/pkg/app/registry/registry/registry.go new file mode 100644 index 00000000000..0650a2a75f4 --- /dev/null +++ b/pkg/app/registry/registry/registry.go @@ -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 +} diff --git a/pkg/app/registry/static/static.go b/pkg/app/registry/static/static.go index 0d3f7ecbc9d..bfde41d7ef3 100644 --- a/pkg/app/registry/static/static.go +++ b/pkg/app/registry/static/static.go @@ -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() { @@ -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 ®{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, ®istrypb.ProviderInfo{ @@ -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 @@ -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 ®istry{rules: c.Rules}, nil -} diff --git a/pkg/auth/registry/registry/registry.go b/pkg/auth/registry/registry/registry.go index 48e13898fce..0170885f378 100644 --- a/pkg/auth/registry/registry/registry.go +++ b/pkg/auth/registry/registry/registry.go @@ -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