Skip to content

Commit

Permalink
[FAB-7827] Use ConfigProvider interface
Browse files Browse the repository at this point in the history
This patch allows fabsdk.New to capture configuration errors. By capturing
these errors, boiler plate is reduced.

Change-Id: Ib7226d61b35783044cb6e5307ebee769aac4eb99
Signed-off-by: Troy Ronda <[email protected]>
  • Loading branch information
troyronda committed Jan 20, 2018
1 parent 5843985 commit 199cc9d
Show file tree
Hide file tree
Showing 29 changed files with 219 additions and 352 deletions.
3 changes: 3 additions & 0 deletions api/apiconfig/configprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type Config interface {
TLSClientCerts() ([]tls.Certificate, error)
}

// ConfigProvider enables creation of a Config instance
type ConfigProvider func() (Config, error)

// TimeoutType enumerates the different types of outgoing connections
type TimeoutType int

Expand Down
14 changes: 5 additions & 9 deletions def/fabapi/deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type StateStoreOpts struct {
Path string
}

func configFromOptions(options *Options) (apiconfig.Config, error) {
func configFromOptions(options *Options) apiconfig.ConfigProvider {
if options.ConfigByte != nil {
return config.FromRaw(options.ConfigByte, options.ConfigType)
}
Expand All @@ -54,19 +54,15 @@ func configFromOptions(options *Options) (apiconfig.Config, error) {
return config.FromFile(options.ConfigFile)
}

return nil, errors.New("No configuration provided")
return func() (apiconfig.Config, error) {
return nil, errors.New("No configuration provided")
}
}

// NewSDK wraps the NewSDK func moved to the pkg folder.
// Notice: this wrapper is deprecated and will be removed.
func NewSDK(options Options) (*fabsdk.FabricSDK, error) {
configProvider, err := configFromOptions(&options)
if err != nil {
return nil, err
}

sdk, err := fabsdk.New(
configProvider,
sdk, err := fabsdk.New(configFromOptions(&options),
sdkOptionsFromDeprecatedOptions(options)...)

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion def/factory/defclient/orgfactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestNewMSPClient(t *testing.T) {
func TestNewCredentialManager(t *testing.T) {
factory := NewOrgClientFactory()

config, err := config.FromFile("../../../test/fixtures/config/config_test.yaml")
config, err := config.FromFile("../../../test/fixtures/config/config_test.yaml")()
if err != nil {
t.Fatalf(err.Error())
}
Expand Down
6 changes: 2 additions & 4 deletions def/factory/defclient/sessfactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/api/apicryptosuite"
"github.com/hyperledger/fabric-sdk-go/api/apifabclient"
"github.com/hyperledger/fabric-sdk-go/def/factory/defcore"
"github.com/hyperledger/fabric-sdk-go/pkg/config"
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/channel"
fabmocks "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/mocks"
chImpl "github.com/hyperledger/fabric-sdk-go/pkg/fabric-txn/chclient"
Expand Down Expand Up @@ -187,10 +188,7 @@ func newMockProviders(t *testing.T) *mockProviders {
coreFactory := defcore.NewProviderFactory()
svcFactory := defsvc.NewProviderFactory()

opts := defcore.ConfigOpts{
FileName: "../../../test/fixtures/config/config_test.yaml",
}
config, err := coreFactory.NewConfigProvider(opts)
config, err := config.FromFile("../../../test/fixtures/config/config_test.yaml")()
if err != nil {
t.Fatalf(err.Error())
}
Expand Down
21 changes: 0 additions & 21 deletions def/factory/defcore/corefactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/hyperledger/fabric-sdk-go/api/apilogging"

"github.com/hyperledger/fabric-sdk-go/def/provider/fabpvdr"
configImpl "github.com/hyperledger/fabric-sdk-go/pkg/config"
cryptosuiteimpl "github.com/hyperledger/fabric-sdk-go/pkg/cryptosuite/bccsp/sw"
"github.com/hyperledger/fabric-sdk-go/pkg/errors"
kvs "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/keyvaluestore"
Expand All @@ -35,26 +34,6 @@ func NewProviderFactory() *ProviderFactory {
return &f
}

// ConfigOpts provides bootstrap setup
type ConfigOpts struct {
//FileName to load from a predefined path
FileName string
//Raw to load from an bytes array
Raw []byte
//Format to specify the type of the config (mainly used with ConfigBytes as ConfigFile has a file extension to specify the type)
// valid values: yaml, json, etc.
Format string
}

// NewConfigProvider creates a Config using the SDK's default implementation
func (f *ProviderFactory) NewConfigProvider(opts ConfigOpts) (apiconfig.Config, error) {
// configBytes takes precedence over configFile
if opts.Raw != nil && len(opts.Raw) > 0 {
return configImpl.FromRaw(opts.Raw, opts.Format)
}
return configImpl.FromFile(opts.FileName)
}

// NewStateStoreProvider creates a KeyValueStore using the SDK's default implementation
func (f *ProviderFactory) NewStateStoreProvider(config apiconfig.Config) (fab.KeyValueStore, error) {

Expand Down
19 changes: 0 additions & 19 deletions def/factory/defcore/corefactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,13 @@ import (
"github.com/hyperledger/fabric-sdk-go/api/apiconfig/mocks"
"github.com/hyperledger/fabric-sdk-go/api/apifabclient"
"github.com/hyperledger/fabric-sdk-go/def/provider/fabpvdr"
configImpl "github.com/hyperledger/fabric-sdk-go/pkg/config"
cryptosuitewrapper "github.com/hyperledger/fabric-sdk-go/pkg/cryptosuite/bccsp/wrapper"
kvs "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/keyvaluestore"
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/mocks"
signingMgr "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/signingmgr"
"github.com/hyperledger/fabric-sdk-go/pkg/logging/modlog"
)

func TestNewConfigProvider(t *testing.T) {
factory := NewProviderFactory()

opts := ConfigOpts{
FileName: "../../../test/fixtures/config/config_test.yaml",
}

config, err := factory.NewConfigProvider(opts)
if err != nil {
t.Fatalf("Unexpected error creating config provider %v", err)
}

_, ok := config.(*configImpl.Config)
if !ok {
t.Fatalf("Unexpected config provider created")
}
}

func TestNewStateStoreProvider(t *testing.T) {
factory := NewProviderFactory()

Expand Down
73 changes: 36 additions & 37 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,56 +61,55 @@ type Option func(opts *options) error

// FromReader loads configuration from in.
// configType can be "json" or "yaml".
func FromReader(in io.Reader, configType string, opts ...Option) (*Config, error) {
c, err := newConfig(opts...)
if err != nil {
return nil, err
}
func FromReader(in io.Reader, configType string, opts ...Option) apiconfig.ConfigProvider {
return func() (apiconfig.Config, error) {
c, err := newConfig(opts...)
if err != nil {
return nil, err
}

if configType == "" {
return nil, errors.New("empty config type")
}
if configType == "" {
return nil, errors.New("empty config type")
}

// read config from bytes array, but must set ConfigType
// for viper to properly unmarshal the bytes array
c.configViper.SetConfigType(configType)
c.configViper.MergeConfig(in)
// read config from bytes array, but must set ConfigType
// for viper to properly unmarshal the bytes array
c.configViper.SetConfigType(configType)
c.configViper.MergeConfig(in)

return initConfig(c)
return initConfig(c)
}
}

// FromFile reads from named config file
func FromFile(name string, opts ...Option) (*Config, error) {
c, err := newConfig(opts...)
if err != nil {
return nil, err
}
func FromFile(name string, opts ...Option) apiconfig.ConfigProvider {
return func() (apiconfig.Config, error) {
c, err := newConfig(opts...)
if err != nil {
return nil, err
}

if name == "" {
return nil, errors.New("filename is required")
}
if name == "" {
return nil, errors.New("filename is required")
}

// create new viper
c.configViper.SetConfigFile(name)
// create new viper
c.configViper.SetConfigFile(name)

// If a config file is found, read it in.
err = c.configViper.MergeInConfig()
if err == nil {
logger.Debugf("Using config file: %s", c.configViper.ConfigFileUsed())
} else {
return nil, errors.Wrap(err, "loading config file failed")
}
// If a config file is found, read it in.
err = c.configViper.MergeInConfig()
if err == nil {
logger.Debugf("Using config file: %s", c.configViper.ConfigFileUsed())
} else {
return nil, errors.Wrap(err, "loading config file failed")
}

return initConfig(c)
return initConfig(c)
}
}

// FromRaw will initialize the configs from a byte array
func FromRaw(configBytes []byte, configType string, opts ...Option) (*Config, error) {

if len(configBytes) == 0 {
return nil, errors.New("empty config byte array")
}

func FromRaw(configBytes []byte, configType string, opts ...Option) apiconfig.ConfigProvider {
buf := bytes.NewBuffer(configBytes)
logger.Debugf("config.FromRaw buf Len is %d, Cap is %d: %s", buf.Len(), buf.Cap(), buf)

Expand Down
Loading

0 comments on commit 199cc9d

Please sign in to comment.