Skip to content

Commit

Permalink
[FAB-5294] Retrieve peer config by name
Browse files Browse the repository at this point in the history
Change-Id: I73cc8761e7f44de0c3cfa6677e82a4bede259ce5
Signed-off-by: Divyank Katira <Divyank.Katira@securekey.com>
  • Loading branch information
d1vyank committed Jul 12, 2017
1 parent 6450c43 commit a24a856
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 4 deletions.
1 change: 1 addition & 0 deletions api/apiconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Config interface {
RandomOrdererConfig() (*OrdererConfig, error)
OrdererConfig(name string) (*OrdererConfig, error)
PeersConfig(org string) ([]PeerConfig, error)
PeerConfig(org string, name string) (*PeerConfig, error)
NetworkConfig() (*NetworkConfig, error)
IsTLSEnabled() bool
SetTLSCACertPool(*x509.CertPool)
Expand Down
13 changes: 13 additions & 0 deletions api/apiconfig/mocks/mockconfig.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,21 @@ func (c *Config) PeersConfig(org string) ([]apiconfig.PeerConfig, error) {
return peers, nil
}

// PeerConfig Retrieves a specific peer from the configuration by org and name
func (c *Config) PeerConfig(org string, name string) (*apiconfig.PeerConfig, error) {
config, err := c.NetworkConfig()
if err != nil {
return nil, err
}

peersConfig := config.Organizations[org].Peers
peerConfig := peersConfig[name]
peerConfig.TLS.Certificate = strings.Replace(peerConfig.TLS.Certificate, "$GOPATH",
os.Getenv("GOPATH"), -1)

return &peerConfig, nil
}

// NetworkConfig returns the network configuration defined in the config file
func (c *Config) NetworkConfig() (*apiconfig.NetworkConfig, error) {
if c.networkConfigCached {
Expand Down
25 changes: 23 additions & 2 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,17 @@ func TestCAConfigFailsByNetworkConfig(t *testing.T) {
}

//Testing PeersConfig failure scenario
pConfig, err := sampleConfig.PeersConfig("peerorg1")
if pConfig != nil || err == nil {
pConfigs, err := sampleConfig.PeersConfig("peerorg1")
if pConfigs != nil || err == nil {
t.Fatal("Testing PeersConfig supposed to fail")
}

//Testing PeersConfig failure scenario
pConfig, err := sampleConfig.PeerConfig("peerorg1", "peer1")
if pConfig != nil || err == nil {
t.Fatal("Testing PeerConfig supposed to fail")
}

//Set it back to valid one, otherwise other tests may fail
myViper.Set("client.network", clientNetworks)
}
Expand Down Expand Up @@ -273,6 +279,21 @@ func TestPeersConfig(t *testing.T) {

}

func TestPeerConfig(t *testing.T) {
pc, err := configImpl.PeerConfig(org1, "peer0")
if err != nil {
t.Fatalf(err.Error())
}

if pc.Host == "" {
t.Fatalf("Host value is empty")
}

if !filepath.IsAbs(pc.TLS.Certificate) {
t.Fatalf("Expected cert path to be absolute")
}
}

func TestInitConfig(t *testing.T) {
//Test init config
//...Positive case
Expand Down
5 changes: 5 additions & 0 deletions pkg/fabric-ca-client/mocks/mockconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func (c *MockConfig) PeersConfig(org string) ([]apiconfig.PeerConfig, error) {
return nil, nil
}

// PeerConfig Retrieves a specific peer from the configuration by org and name
func (c *MockConfig) PeerConfig(org string, name string) (*apiconfig.PeerConfig, error) {
return nil, nil
}

// IsTLSEnabled ...
func (c *MockConfig) IsTLSEnabled() bool {
return false
Expand Down
2 changes: 1 addition & 1 deletion pkg/fabric-client/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (c *Channel) PrimaryPeer() fab.Peer {
// When no primary peer has been set default to the first peer
// from map range - order is not guaranteed
for _, peer := range c.peers {
logger.Infof("Primary peer was not set, using %s", peer.URL())
logger.Debugf("Primary peer was not set, using %s", peer.URL())
return peer
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/fabric-client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,11 @@ func (c *Client) queryBySystemChaincodeByTarget(chaincodeID string, fcn string,
}
responses, err := channel.QueryBySystemChaincode(request, c)

if err != nil {
return nil, fmt.Errorf("Error from QueryBySystemChaincode: %s", err)
}
// we are only querying one peer hence one result
if err != nil || len(responses) != 1 {
if len(responses) != 1 {
return nil, fmt.Errorf("QueryBySystemChaincode should have one result only - result number: %d", len(responses))
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/fabric-client/mocks/mockconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func (c *MockConfig) PeersConfig(org string) ([]config.PeerConfig, error) {
return nil, nil
}

// PeerConfig Retrieves a specific peer from the configuration by org and name
func (c *MockConfig) PeerConfig(org string, name string) (*config.PeerConfig, error) {
return nil, nil
}

// IsTLSEnabled ...
func (c *MockConfig) IsTLSEnabled() bool {
return c.tlsEnabled
Expand Down

0 comments on commit a24a856

Please sign in to comment.