github.com/yimialmonte/fabric@v2.1.1+incompatible/integration/nwo/discover.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package nwo
     8  
     9  import (
    10  	"encoding/json"
    11  	"path/filepath"
    12  
    13  	"github.com/hyperledger/fabric/integration/nwo/commands"
    14  	. "github.com/onsi/gomega"
    15  	"github.com/onsi/gomega/gexec"
    16  )
    17  
    18  // DiscoveredPeer defines a struct for discovering peers using discovery service.
    19  // each peer in the result will have these fields
    20  type DiscoveredPeer struct {
    21  	MSPID      string   `yaml:"mspid,omitempty"`
    22  	Endpoint   string   `yaml:"endpoint,omitempty"`
    23  	Identity   string   `yaml:"identity,omitempty"`
    24  	Chaincodes []string `yaml:"chaincodes,omitempty"`
    25  }
    26  
    27  // running discovery service command discover peers against peer using channel name and user as specified in the
    28  // function arguments. return a slice of the discovered peers
    29  func DiscoverPeers(n *Network, p *Peer, user, channelName string) func() []DiscoveredPeer {
    30  	return func() []DiscoveredPeer {
    31  		peers := commands.Peers{
    32  			UserCert: n.PeerUserCert(p, user),
    33  			UserKey:  n.PeerUserKey(p, user),
    34  			MSPID:    n.Organization(p.Organization).MSPID,
    35  			Server:   n.PeerAddress(p, ListenPort),
    36  			Channel:  channelName,
    37  		}
    38  		if n.ClientAuthRequired {
    39  			peers.ClientCert = filepath.Join(n.PeerUserTLSDir(p, user), "client.crt")
    40  			peers.ClientKey = filepath.Join(n.PeerUserTLSDir(p, user), "client.key")
    41  		}
    42  		sess, err := n.Discover(peers)
    43  		Expect(err).NotTo(HaveOccurred())
    44  		Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
    45  
    46  		var discovered []DiscoveredPeer
    47  		err = json.Unmarshal(sess.Out.Contents(), &discovered)
    48  		Expect(err).NotTo(HaveOccurred())
    49  		return discovered
    50  	}
    51  }