Skip to content

Commit

Permalink
[FAB-8127] FabricProvider based on context or cfg
Browse files Browse the repository at this point in the history
Change-Id: Icd183424c1ed107a3cd068bb86508a3b6ba769b9
Signed-off-by: Troy Ronda <troy@troyronda.com>
  • Loading branch information
troyronda committed Feb 8, 2018
1 parent cc5fd66 commit 966fa03
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 87 deletions.
21 changes: 9 additions & 12 deletions api/apicore/fabric.go
Expand Up @@ -7,23 +7,20 @@ SPDX-License-Identifier: Apache-2.0
package apicore

import (
"crypto/x509"

"github.com/hyperledger/fabric-sdk-go/api/apiconfig"
"github.com/hyperledger/fabric-sdk-go/api/apifabca"
"github.com/hyperledger/fabric-sdk-go/api/apifabclient"
)

// FabricProvider enables access to fabric objects such as peer and user
// FabricProvider enables access to fabric objects such as peer and user based on config or context.
type FabricProvider interface {
NewChannelClient(user apifabclient.IdentityContext, cfg apifabclient.ChannelCfg) (apifabclient.Channel, error)
NewChannelConfig(user apifabclient.IdentityContext, name string) (apifabclient.ChannelConfig, error)
NewResourceClient(user apifabclient.IdentityContext) (apifabclient.Resource, error)
NewEventHub(ic apifabclient.IdentityContext, channelID string) (apifabclient.EventHub, error)
NewCAClient(orgID string) (apifabca.FabricCAClient, error)
CreateChannelClient(user apifabclient.IdentityContext, cfg apifabclient.ChannelCfg) (apifabclient.Channel, error)
CreateChannelConfig(user apifabclient.IdentityContext, name string) (apifabclient.ChannelConfig, error)
CreateResourceClient(user apifabclient.IdentityContext) (apifabclient.Resource, error)
CreateEventHub(ic apifabclient.IdentityContext, channelID string) (apifabclient.EventHub, error)
CreateCAClient(orgID string) (apifabca.FabricCAClient, error)

NewPeer(url string, certificate *x509.Certificate, serverHostOverride string) (apifabclient.Peer, error)
NewPeerFromConfig(peerCfg *apiconfig.NetworkPeer) (apifabclient.Peer, error)
NewOrdererFromConfig(cfg *apiconfig.OrdererConfig) (apifabclient.Orderer, error)
NewUser(name string, signingIdentity *apifabclient.SigningIdentity) (apifabclient.User, error)
CreatePeerFromConfig(peerCfg *apiconfig.NetworkPeer) (apifabclient.Peer, error)
CreateOrdererFromConfig(cfg *apiconfig.OrdererConfig) (apifabclient.Orderer, error)
CreateUser(name string, signingIdentity *apifabclient.SigningIdentity) (apifabclient.User, error)
}
2 changes: 1 addition & 1 deletion pkg/fabric-txn/resmgmtclient/resmgmt.go
Expand Up @@ -140,7 +140,7 @@ func (rc *ResourceMgmtClient) JoinChannel(channelID string, options ...resmgmt.O
}

// TODO: handle more than the first orderer.
orderer, err := rc.fabricProvider.NewOrdererFromConfig(&oConfig[0])
orderer, err := rc.fabricProvider.CreateOrdererFromConfig(&oConfig[0])
if err != nil {
return errors.WithMessage(err, "failed to create orderers from config")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/fabsdk/deprecated.go
Expand Up @@ -138,5 +138,5 @@ func (sdk *FabricSDK) newSessionFromIdentityName(orgID string, id string) (*sess
//
// Deprecated: the system client is being replaced with the interfaces supplied by NewClient()
func (sdk *FabricSDK) NewSystemClient(s apisdk.SessionContext) (apifabclient.Resource, error) {
return sdk.FabricProvider().NewResourceClient(s)
return sdk.FabricProvider().CreateResourceClient(s)
}
2 changes: 1 addition & 1 deletion pkg/fabsdk/fabsdk.go
Expand Up @@ -266,7 +266,7 @@ func (sdk *FabricSDK) newUser(orgID string, userName string) (apifabclient.Ident
return nil, errors.WithMessage(err, "failed to get signing identity")
}

user, err := sdk.fabricProvider.NewUser(userName, signingIdentity)
user, err := sdk.fabricProvider.CreateUser(userName, signingIdentity)
if err != nil {
return nil, errors.WithMessage(err, "NewPreEnrolledUser returned error")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/fabsdk/factory/defclient/sessfactory.go
Expand Up @@ -32,7 +32,7 @@ func NewSessionClientFactory() *SessionClientFactory {
// NewChannelMgmtClient returns a client that manages channels (create/join channel)
func (f *SessionClientFactory) NewChannelMgmtClient(providers apisdk.Providers, session apisdk.SessionContext) (apichmgmt.ChannelMgmtClient, error) {
// For now settings are the same as for system client
resource, err := providers.FabricProvider().NewResourceClient(session)
resource, err := providers.FabricProvider().CreateResourceClient(session)
if err != nil {
return nil, err
}
Expand All @@ -48,7 +48,7 @@ func (f *SessionClientFactory) NewChannelMgmtClient(providers apisdk.Providers,
func (f *SessionClientFactory) NewResourceMgmtClient(providers apisdk.Providers, session apisdk.SessionContext, filter apiresmgmt.TargetFilter) (apiresmgmt.ResourceMgmtClient, error) {

fabProvider := providers.FabricProvider()
resource, err := fabProvider.NewResourceClient(session)
resource, err := fabProvider.CreateResourceClient(session)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/fabsdk/provider/chpvdr/chprovider.go
Expand Up @@ -38,7 +38,7 @@ func (cp *ChannelProvider) NewChannelService(ic apifabclient.IdentityContext, ch
if channelID != "" {
v, ok := cp.chCfgMap.Load(channelID)
if !ok {
p, err := cp.fabricProvider.NewChannelConfig(ic, channelID)
p, err := cp.fabricProvider.CreateChannelConfig(ic, channelID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -80,15 +80,15 @@ type ChannelService struct {

// Channel returns the named Channel client.
func (cs *ChannelService) Channel() (apifabclient.Channel, error) {
return cs.fabricProvider.NewChannelClient(cs.identityContext, cs.cfg)
return cs.fabricProvider.CreateChannelClient(cs.identityContext, cs.cfg)
}

// EventHub returns the EventHub for the named channel.
func (cs *ChannelService) EventHub() (apifabclient.EventHub, error) {
return cs.fabricProvider.NewEventHub(cs.identityContext, cs.cfg.Name())
return cs.fabricProvider.CreateEventHub(cs.identityContext, cs.cfg.Name())
}

// ChannelConfig returns the ChannelConfig for the named channel
func (cs *ChannelService) ChannelConfig() (apifabclient.ChannelConfig, error) {
return cs.fabricProvider.NewChannelConfig(cs.identityContext, cs.cfg.Name())
return cs.fabricProvider.CreateChannelConfig(cs.identityContext, cs.cfg.Name())
}
11 changes: 7 additions & 4 deletions pkg/fabsdk/provider/chpvdr/chprovider_test.go
Expand Up @@ -64,12 +64,12 @@ type MockProviderFactory struct {

// CustomFabricProvider overrides channel config default implementation
type MockFabricProvider struct {
fabpvdr.FabricProvider
*fabpvdr.FabricProvider
providerContext apifabclient.ProviderContext
}

// NewChannelConfig initializes the channel config
func (f *MockFabricProvider) NewChannelConfig(ic apifabclient.IdentityContext, channelID string) (apifabclient.ChannelConfig, error) {
// CreateChannelConfig initializes the channel config
func (f *MockFabricProvider) CreateChannelConfig(ic apifabclient.IdentityContext, channelID string) (apifabclient.ChannelConfig, error) {

ctx := chconfig.Context{
ProviderContext: f.providerContext,
Expand All @@ -80,7 +80,8 @@ func (f *MockFabricProvider) NewChannelConfig(ic apifabclient.IdentityContext, c

}

func (f *MockFabricProvider) NewChannelClient(ic apifabclient.IdentityContext, cfg apifabclient.ChannelCfg) (apifabclient.Channel, error) {
// CreateChannelClient overrides the default.
func (f *MockFabricProvider) CreateChannelClient(ic apifabclient.IdentityContext, cfg apifabclient.ChannelCfg) (apifabclient.Channel, error) {
ctx := chconfig.Context{
ProviderContext: f.providerContext,
IdentityContext: ic,
Expand All @@ -95,8 +96,10 @@ func (f *MockFabricProvider) NewChannelClient(ic apifabclient.IdentityContext, c

// NewFabricProvider mocks new default implementation of fabric primitives
func (f *MockProviderFactory) NewFabricProvider(context apifabclient.ProviderContext) (apicore.FabricProvider, error) {
fabProvider := fabpvdr.New(context)

cfp := MockFabricProvider{
FabricProvider: fabProvider,
providerContext: context,
}
return &cfp, nil
Expand Down
45 changes: 16 additions & 29 deletions pkg/fabsdk/provider/fabpvdr/fabpvdr.go
Expand Up @@ -7,8 +7,6 @@ SPDX-License-Identifier: Apache-2.0
package fabpvdr

import (
"crypto/x509"

"github.com/hyperledger/fabric-sdk-go/api/apiconfig"
"github.com/hyperledger/fabric-sdk-go/api/apifabca"
"github.com/hyperledger/fabric-sdk-go/api/apifabclient"
Expand Down Expand Up @@ -41,12 +39,8 @@ func New(ctx apifabclient.ProviderContext) *FabricProvider {
return &f
}

//
// TODO - the methods in this package should be Create rather than New.
//

// NewResourceClient returns a new client initialized for the current instance of the SDK.
func (f *FabricProvider) NewResourceClient(ic apifabclient.IdentityContext) (apifabclient.Resource, error) {
// CreateResourceClient returns a new client initialized for the current instance of the SDK.
func (f *FabricProvider) CreateResourceClient(ic apifabclient.IdentityContext) (apifabclient.Resource, error) {
ctx := &fabContext{
ProviderContext: f.providerContext,
IdentityContext: ic,
Expand All @@ -56,8 +50,8 @@ func (f *FabricProvider) NewResourceClient(ic apifabclient.IdentityContext) (api
return client, nil
}

// NewChannelClient returns a new client initialized for the current instance of the SDK.
func (f *FabricProvider) NewChannelClient(ic apifabclient.IdentityContext, cfg apifabclient.ChannelCfg) (apifabclient.Channel, error) {
// CreateChannelClient returns a new client initialized for the current instance of the SDK.
func (f *FabricProvider) CreateChannelClient(ic apifabclient.IdentityContext, cfg apifabclient.ChannelCfg) (apifabclient.Channel, error) {
ctx := &fabContext{
ProviderContext: f.providerContext,
IdentityContext: ic,
Expand All @@ -70,8 +64,8 @@ func (f *FabricProvider) NewChannelClient(ic apifabclient.IdentityContext, cfg a
return channel, nil
}

// NewEventHub initilizes the event hub.
func (f *FabricProvider) NewEventHub(ic apifabclient.IdentityContext, channelID string) (apifabclient.EventHub, error) {
// CreateEventHub initilizes the event hub.
func (f *FabricProvider) CreateEventHub(ic apifabclient.IdentityContext, channelID string) (apifabclient.EventHub, error) {
peerConfig, err := f.providerContext.Config().ChannelPeers(channelID)
if err != nil {
return nil, errors.WithMessage(err, "read configuration for channel peers failed")
Expand All @@ -97,8 +91,8 @@ func (f *FabricProvider) NewEventHub(ic apifabclient.IdentityContext, channelID
return events.FromConfig(eventCtx, &eventSource.PeerConfig)
}

// NewChannelConfig initializes the channel config
func (f *FabricProvider) NewChannelConfig(ic apifabclient.IdentityContext, channelID string) (apifabclient.ChannelConfig, error) {
// CreateChannelConfig initializes the channel config
func (f *FabricProvider) CreateChannelConfig(ic apifabclient.IdentityContext, channelID string) (apifabclient.ChannelConfig, error) {

ctx := chconfig.Context{
ProviderContext: f.providerContext,
Expand All @@ -108,13 +102,13 @@ func (f *FabricProvider) NewChannelConfig(ic apifabclient.IdentityContext, chann
return chconfig.New(ctx, channelID)
}

// NewCAClient returns a new FabricCAClient initialized for the current instance of the SDK.
func (f *FabricProvider) NewCAClient(orgID string) (apifabca.FabricCAClient, error) {
// CreateCAClient returns a new FabricCAClient initialized for the current instance of the SDK.
func (f *FabricProvider) CreateCAClient(orgID string) (apifabca.FabricCAClient, error) {
return fabricCAClient.NewFabricCAClient(orgID, f.providerContext.Config(), f.providerContext.CryptoSuite())
}

// NewUser returns a new default implementation of a User.
func (f *FabricProvider) NewUser(name string, signingIdentity *apifabclient.SigningIdentity) (apifabclient.User, error) {
// CreateUser returns a new default implementation of a User.
func (f *FabricProvider) CreateUser(name string, signingIdentity *apifabclient.SigningIdentity) (apifabclient.User, error) {

user := identityImpl.NewUser(name, signingIdentity.MspID)

Expand All @@ -124,20 +118,13 @@ func (f *FabricProvider) NewUser(name string, signingIdentity *apifabclient.Sign
return user, nil
}

// NewPeer returns a new default implementation of Peer
//
// TODO: This should be FromConfig like the others.
func (f *FabricProvider) NewPeer(url string, certificate *x509.Certificate, serverHostOverride string) (apifabclient.Peer, error) {
return peerImpl.New(f.providerContext.Config(), peerImpl.WithURL(url), peerImpl.WithTLSCert(certificate), peerImpl.WithServerName(serverHostOverride))
}

// NewPeerFromConfig returns a new default implementation of Peer based configuration
func (f *FabricProvider) NewPeerFromConfig(peerCfg *apiconfig.NetworkPeer) (apifabclient.Peer, error) {
// CreatePeerFromConfig returns a new default implementation of Peer based configuration
func (f *FabricProvider) CreatePeerFromConfig(peerCfg *apiconfig.NetworkPeer) (apifabclient.Peer, error) {
return peerImpl.New(f.providerContext.Config(), peerImpl.FromPeerConfig(peerCfg))
}

// NewOrdererFromConfig creates a default implementation of Orderer based on configuration.
func (f *FabricProvider) NewOrdererFromConfig(cfg *apiconfig.OrdererConfig) (apifabclient.Orderer, error) {
// CreateOrdererFromConfig creates a default implementation of Orderer based on configuration.
func (f *FabricProvider) CreateOrdererFromConfig(cfg *apiconfig.OrdererConfig) (apifabclient.Orderer, error) {
orderer, err := orderer.New(f.providerContext.Config(), orderer.FromOrdererConfig(cfg))
if err != nil {
return nil, errors.WithMessage(err, "creating orderer failed")
Expand Down
34 changes: 10 additions & 24 deletions pkg/fabsdk/provider/fabpvdr/fabpvdr_test.go
Expand Up @@ -10,7 +10,6 @@ import (
"testing"

"github.com/hyperledger/fabric-sdk-go/api/apiconfig"
"github.com/hyperledger/fabric-sdk-go/api/apiconfig/mocks"
"github.com/hyperledger/fabric-sdk-go/api/apifabclient"
fabricCAClient "github.com/hyperledger/fabric-sdk-go/pkg/fabric-ca-client"
channelImpl "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/channel"
Expand All @@ -24,11 +23,11 @@ func TestNewFabricProvider(t *testing.T) {
newMockFabricProvider(t)
}

func TestNewChannelClient(t *testing.T) {
func TestCreateChannelClient(t *testing.T) {
p := newMockFabricProvider(t)

user := mocks.NewMockUser("user")
client, err := p.NewChannelClient(user, mocks.NewMockChannelCfg("mychannel"))
client, err := p.CreateChannelClient(user, mocks.NewMockChannelCfg("mychannel"))
if err != nil {
t.Fatalf("Unexpected error creating client %v", err)
}
Expand All @@ -39,11 +38,11 @@ func TestNewChannelClient(t *testing.T) {
}
}

func TestNewResourceClient(t *testing.T) {
func TestCreateResourceClient(t *testing.T) {
p := newMockFabricProvider(t)

user := mocks.NewMockUser("user")
client, err := p.NewResourceClient(user)
client, err := p.CreateResourceClient(user)
if err != nil {
t.Fatalf("Unexpected error creating client %v", err)
}
Expand All @@ -54,12 +53,12 @@ func TestNewResourceClient(t *testing.T) {
}
}

func TestNewCAClient(t *testing.T) {
func TestCreateCAClient(t *testing.T) {
p := newMockFabricProvider(t)

org := "org1"

client, err := p.NewCAClient(org)
client, err := p.CreateCAClient(org)
if err != nil {
t.Fatalf("Unexpected error creating client %v", err)
}
Expand All @@ -83,19 +82,6 @@ func TestNewCAClient(t *testing.T) {
}
}

func TestNewPeer(t *testing.T) {
p := newMockFabricProvider(t)

url := "grpcs://localhost:8080"

peer, err := p.NewPeer(url, mock_apiconfig.GoodCert, "")
if err != nil {
t.Fatalf("Unexpected error creating peer %v", err)
}

verifyPeer(t, peer, url)
}

func verifyPeer(t *testing.T, peer apifabclient.Peer, url string) {
_, ok := peer.(*peerImpl.Peer)
if !ok {
Expand All @@ -110,7 +96,7 @@ func verifyPeer(t *testing.T, peer apifabclient.Peer, url string) {
}
}

func TestNewPeerFromConfig(t *testing.T) {
func TestCreatePeerFromConfig(t *testing.T) {
p := newMockFabricProvider(t)

url := "grpc://localhost:8080"
Expand All @@ -121,7 +107,7 @@ func TestNewPeerFromConfig(t *testing.T) {
},
}

peer, err := p.NewPeerFromConfig(&peerCfg)
peer, err := p.CreatePeerFromConfig(&peerCfg)

if err != nil {
t.Fatalf("Unexpected error creating peer %v", err)
Expand All @@ -130,7 +116,7 @@ func TestNewPeerFromConfig(t *testing.T) {
verifyPeer(t, peer, url)
}

func TestNewUser(t *testing.T) {
func TestCreateUser(t *testing.T) {
org := "org1"

p := newMockFabricProvider(t)
Expand All @@ -144,7 +130,7 @@ func TestNewUser(t *testing.T) {
t.Fatalf("Unexpected error getting signing identity %v", err)
}

user, err := p.NewUser("user", signingIdentity)
user, err := p.CreateUser("user", signingIdentity)
if err != nil {
t.Fatalf("Unexpected error getting user %v", err)
}
Expand Down
10 changes: 5 additions & 5 deletions test/integration/base_test_setup.go
Expand Up @@ -94,7 +94,7 @@ func (setup *BaseSetupImpl) Initialize(t *testing.T) error {

setup.Identity = session

sc, err := sdk.FabricProvider().NewResourceClient(setup.Identity)
sc, err := sdk.FabricProvider().CreateResourceClient(setup.Identity)
if err != nil {
return errors.WithMessage(err, "NewResourceClient failed")
}
Expand Down Expand Up @@ -146,7 +146,7 @@ func (setup *BaseSetupImpl) Initialize(t *testing.T) error {
}

// At this point we are able to retrieve channel configuration
configProvider, err := sdk.FabricProvider().NewChannelConfig(setup.Identity, setup.ChannelID)
configProvider, err := sdk.FabricProvider().CreateChannelConfig(setup.Identity, setup.ChannelID)
if err != nil {
return err
}
Expand All @@ -170,7 +170,7 @@ func (setup *BaseSetupImpl) Initialize(t *testing.T) error {
// GetChannel initializes and returns a channel based on config
func (setup *BaseSetupImpl) GetChannel(sdk *fabsdk.FabricSDK, ic fab.IdentityContext, config apiconfig.Config, chCfg fab.ChannelCfg, orgs []string) (fab.Channel, error) {

channel, err := sdk.FabricProvider().NewChannelClient(ic, chCfg)
channel, err := sdk.FabricProvider().CreateChannelClient(ic, chCfg)
if err != nil {
return nil, errors.WithMessage(err, "NewChannel failed")
}
Expand All @@ -181,7 +181,7 @@ func (setup *BaseSetupImpl) GetChannel(sdk *fabsdk.FabricSDK, ic fab.IdentityCon
return nil, errors.WithMessage(err, "reading peer config failed")
}
for _, p := range peerConfig {
endorser, err := sdk.FabricProvider().NewPeerFromConfig(&apiconfig.NetworkPeer{PeerConfig: p})
endorser, err := sdk.FabricProvider().CreatePeerFromConfig(&apiconfig.NetworkPeer{PeerConfig: p})
if err != nil {
return nil, errors.WithMessage(err, "NewPeer failed")
}
Expand All @@ -196,7 +196,7 @@ func (setup *BaseSetupImpl) GetChannel(sdk *fabsdk.FabricSDK, ic fab.IdentityCon
}

func (setup *BaseSetupImpl) setupEventHub(t *testing.T, client *fabsdk.FabricSDK, identity fab.IdentityContext) error {
eventHub, err := client.FabricProvider().NewEventHub(identity, setup.ChannelID)
eventHub, err := client.FabricProvider().CreateEventHub(identity, setup.ChannelID)
if err != nil {
return err
}
Expand Down

0 comments on commit 966fa03

Please sign in to comment.