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

pkg: add plugable storage broker drivers #12

Merged
merged 1 commit into from
Feb 7, 2019
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
1 change: 1 addition & 0 deletions cmd/revad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/cernbox/reva/pkg/log"

_ "github.com/cernbox/reva/cmd/revad/svcs/httpsvcs/loader"
_ "github.com/cernbox/reva/pkg/storage/broker/loader"
_ "github.com/cernbox/reva/pkg/storage/fs/loader"

"github.com/cernbox/reva/cmd/revad/config"
Expand Down
2 changes: 1 addition & 1 deletion cmd/revad/revad.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ driver = "demo"
[grpc.services.storagebrokersvc]
driver = "static"

[grpc.services.storagebrokersvc.static.rules]
[grpc.services.storagebrokersvc.drivers.static.rules]
"/" = "localhost:9999"
"123e4567-e89b-12d3-a456-426655440000" = "localhost:9999"

Expand Down
14 changes: 6 additions & 8 deletions cmd/revad/svcs/grpcsvcs/storagebrokersvc/storagebrokersvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/cernbox/reva/pkg/err"
"github.com/cernbox/reva/pkg/log"
"github.com/cernbox/reva/pkg/storage"
"github.com/cernbox/reva/pkg/storage/broker/static"
"github.com/cernbox/reva/pkg/storage/broker/registry"
"github.com/mitchellh/mapstructure"
)

Expand All @@ -22,8 +22,8 @@ type service struct {
}

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"`
}

// New creates a new StorageBrokerService
Expand Down Expand Up @@ -55,12 +55,10 @@ func parseConfig(m map[string]interface{}) (*config, error) {
}

func getBroker(c *config) (storage.Broker, error) {
switch c.Driver {
case "static":
return static.New(c.Static)
default:
return nil, fmt.Errorf("driver not found: %s", c.Driver)
if f, ok := registry.NewFuncs[c.Driver]; ok {
return f(c.Drivers[c.Driver])
}
return nil, fmt.Errorf("driver not found: %s", c.Driver)
}

func (s *service) Discover(ctx context.Context, req *storagebrokerv0alphapb.DiscoverRequest) (*storagebrokerv0alphapb.DiscoverResponse, error) {
Expand Down
7 changes: 7 additions & 0 deletions pkg/storage/broker/loader/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package loader

import (
// Load core storage broker drivers.
_ "github.com/cernbox/reva/pkg/storage/broker/static"
// Add your own here
)
16 changes: 16 additions & 0 deletions pkg/storage/broker/registry/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package registry

import "github.com/cernbox/reva/pkg/storage"

// NewFunc is the function that storage broker implementations
// should register at init time.
type NewFunc func(map[string]interface{}) (storage.Broker, error)

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

// Register registers a new storage broker new function.
// Not safe for concurrent use. Safe for use from package init.
func Register(name string, f NewFunc) {
NewFuncs[name] = f
}
5 changes: 5 additions & 0 deletions pkg/storage/broker/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import (
"strings"

"github.com/cernbox/reva/pkg/log"
"github.com/cernbox/reva/pkg/storage/broker/registry"

"github.com/cernbox/reva/pkg/storage"
"github.com/mitchellh/mapstructure"
)

func init() {
registry.Register("static", New)
}

var logger = log.New("static")

type broker struct {
Expand Down