Skip to content

Commit

Permalink
[FAB-8320] Resource Mgmt: Query Channels for Peer
Browse files Browse the repository at this point in the history
Change-Id: Ib3bab65b103e450207f58df01bf1bf60ca419296
Signed-off-by: Sandra Vrtikapa <sandra.vrtikapa@securekey.com>
  • Loading branch information
sandrask committed Feb 23, 2018
1 parent 05b5ab9 commit 7fbc960
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 24 deletions.
16 changes: 8 additions & 8 deletions pkg/client/resmgmt/opts.go
Expand Up @@ -12,32 +12,32 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab"
)

//WithTargets encapsulates fab.Peer targets to resmgmtclient Option
func WithTargets(targets ...fab.Peer) Option {
//WithTargets encapsulates fab.Peer targets to resmgmtclient RequestOption
func WithTargets(targets ...fab.Peer) RequestOption {
return func(opts *Opts) error {
opts.Targets = targets
return nil
}
}

//WithTargetFilter encapsulates resmgmtclient TargetFilter targets to resmgmtclient Option
func WithTargetFilter(targetFilter TargetFilter) Option {
//WithTargetFilter encapsulates resmgmtclient TargetFilter targets to resmgmtclient RequestOption
func WithTargetFilter(targetFilter TargetFilter) RequestOption {
return func(opts *Opts) error {
opts.TargetFilter = targetFilter
return nil
}
}

//WithTimeout encapsulates time.Duration to resmgmtclient Option
func WithTimeout(timeout time.Duration) Option {
//WithTimeout encapsulates time.Duration to resmgmtclient RequestOption
func WithTimeout(timeout time.Duration) RequestOption {
return func(opts *Opts) error {
opts.Timeout = timeout
return nil
}
}

//WithOrdererID encapsulates OrdererID to Option
func WithOrdererID(ordererID string) Option {
//WithOrdererID encapsulates OrdererID to RequestOption
func WithOrdererID(ordererID string) RequestOption {
return func(opts *Opts) error {
opts.OrdererID = ordererID
return nil
Expand Down
30 changes: 16 additions & 14 deletions pkg/client/resmgmt/resmgmt.go
Expand Up @@ -88,8 +88,8 @@ type SaveChannelRequest struct {
SigningIdentity context.IdentityContext
}

//Option func for each Opts argument
type Option func(opts *Opts) error
//RequestOption func for each Opts argument
type RequestOption func(opts *Opts) error

var logger = logging.NewLogger("fabric_sdk_go")

Expand Down Expand Up @@ -178,7 +178,7 @@ func New(ctx Context, opts ...ClientOption) (*Client, error) {
}

// JoinChannel allows for peers to join existing channel with optional custom options (specific peers, filtered peers)
func (rc *Client) JoinChannel(channelID string, options ...Option) error {
func (rc *Client) JoinChannel(channelID string, options ...RequestOption) error {

if channelID == "" {
return errors.New("must provide channel ID")
Expand Down Expand Up @@ -313,7 +313,7 @@ func (rc *Client) isChaincodeInstalled(req InstallCCRequest, peer fab.Peer) (boo
}

// InstallCC installs chaincode with optional custom options (specific peers, filtered peers)
func (rc *Client) InstallCC(req InstallCCRequest, options ...Option) ([]InstallCCResponse, error) {
func (rc *Client) InstallCC(req InstallCCRequest, options ...RequestOption) ([]InstallCCResponse, error) {

// For each peer query if chaincode installed. If cc is installed treat as success with message 'already installed'.
// If cc is not installed try to install, and if that fails add to the list with error and peer name.
Expand Down Expand Up @@ -397,17 +397,13 @@ func checkRequiredInstallCCParams(req InstallCCRequest) error {
}

// InstantiateCC instantiates chaincode using default settings
func (rc *Client) InstantiateCC(channelID string, req InstantiateCCRequest, options ...Option) error {

func (rc *Client) InstantiateCC(channelID string, req InstantiateCCRequest, options ...RequestOption) error {
return rc.sendCCProposal(channel.InstantiateChaincode, channelID, req, options...)

}

// UpgradeCC upgrades chaincode with optional custom options (specific peers, filtered peers, timeout)
func (rc *Client) UpgradeCC(channelID string, req UpgradeCCRequest, options ...Option) error {

func (rc *Client) UpgradeCC(channelID string, req UpgradeCCRequest, options ...RequestOption) error {
return rc.sendCCProposal(channel.UpgradeChaincode, channelID, InstantiateCCRequest(req), options...)

}

// QueryInstalledChaincodes queries the installed chaincodes on a peer.
Expand All @@ -416,8 +412,14 @@ func (rc *Client) QueryInstalledChaincodes(proposalProcessor fab.ProposalProcess
return rc.resource.QueryInstalledChaincodes(proposalProcessor)
}

// QueryChannels queries the names of all the channels that a peer has joined.
// Returns the details of all channels that peer has joined.
func (rc *Client) QueryChannels(proposalProcessor fab.ProposalProcessor) (*pb.ChannelQueryResponse, error) {
return rc.resource.QueryChannels(proposalProcessor)
}

// sendCCProposal sends proposal for type Instantiate, Upgrade
func (rc *Client) sendCCProposal(ccProposalType channel.ChaincodeProposalType, channelID string, req InstantiateCCRequest, options ...Option) error {
func (rc *Client) sendCCProposal(ccProposalType channel.ChaincodeProposalType, channelID string, req InstantiateCCRequest, options ...RequestOption) error {

if err := checkRequiredCCProposalParams(channelID, req); err != nil {
return err
Expand Down Expand Up @@ -536,7 +538,7 @@ func checkRequiredCCProposalParams(channelID string, req InstantiateCCRequest) e
}

//prepareResmgmtOpts Reads Opts from Option array
func (rc *Client) prepareResmgmtOpts(options ...Option) (Opts, error) {
func (rc *Client) prepareResmgmtOpts(options ...RequestOption) (Opts, error) {
resmgmtOpts := Opts{}
for _, option := range options {
err := option(&resmgmtOpts)
Expand Down Expand Up @@ -578,7 +580,7 @@ func peersToTxnProcessors(peers []fab.Peer) []fab.ProposalProcessor {
}

// SaveChannel creates or updates channel
func (rc *Client) SaveChannel(req SaveChannelRequest, options ...Option) error {
func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption) error {

opts, err := rc.prepareSaveChannelOpts(options...)
if err != nil {
Expand Down Expand Up @@ -660,7 +662,7 @@ func (rc *Client) SaveChannel(req SaveChannelRequest, options ...Option) error {
}

//prepareSaveChannelOpts Reads chmgmt.Opts from chmgmt.Option array
func (rc *Client) prepareSaveChannelOpts(options ...Option) (Opts, error) {
func (rc *Client) prepareSaveChannelOpts(options ...RequestOption) (Opts, error) {
saveChannelOpts := Opts{}
for _, option := range options {
err := option(&saveChannelOpts)
Expand Down
30 changes: 30 additions & 0 deletions pkg/client/resmgmt/resmgmt_test.go
Expand Up @@ -362,6 +362,36 @@ func TestQueryInstalledChaincodes(t *testing.T) {

}

func TestQueryChannels(t *testing.T) {

rc := setupDefaultResMgmtClient(t)

// Test error
_, err := rc.QueryChannels(nil)
if err == nil {
t.Fatalf("QueryChannels: peer cannot be nil")
}

peer := &fcmocks.MockPeer{MockName: "Peer1", MockURL: "http://peer1.com", MockRoles: []string{}, MockCert: nil, MockMSP: "Org1MSP"}

// Test success (valid peer)
found := false
response, err := rc.QueryChannels(peer)
if err != nil {
t.Fatalf("failed to query channel for peer: %s", err)
}
for _, responseChannel := range response.Channels {
if responseChannel.ChannelId == "test" {
found = true
}
}

if !found {
t.Fatal("Peer has not joined 'test' channel")
}

}

func TestInstallCCWithOpts(t *testing.T) {

rc := setupDefaultResMgmtClient(t)
Expand Down
9 changes: 7 additions & 2 deletions pkg/fab/mocks/mockresource.go
Expand Up @@ -50,9 +50,14 @@ func (c *MockResource) CreateChannel(request api.CreateChannelRequest) (fab.Tran
return fab.TransactionID{}, nil
}

//QueryChannels ...
//QueryChannels mocks query channels
func (c *MockResource) QueryChannels(peer fab.ProposalProcessor) (*pb.ChannelQueryResponse, error) {
return nil, errors.New("Not implemented yet")
if peer == nil {
return nil, errors.New("Generate Error")
}
ci := &pb.ChannelInfo{ChannelId: "test"}
response := &pb.ChannelQueryResponse{Channels: []*pb.ChannelInfo{ci}}
return response, nil
}

// GenesisBlockFromOrderer returns the genesis block from the defined orderer that may be
Expand Down
23 changes: 23 additions & 0 deletions test/integration/sdk/resmgmt_queries_test.go
Expand Up @@ -54,6 +54,8 @@ func TestResMgmtClientQueries(t *testing.T) {

testInstalledChaincodes(t, ccID, target, client)

testQueryChannels(t, testSetup.ChannelID, target, client)

}

func testInstalledChaincodes(t *testing.T, ccID string, target fab.ProposalProcessor, client *resmgmt.Client) {
Expand All @@ -75,3 +77,24 @@ func testInstalledChaincodes(t *testing.T, ccID string, target fab.ProposalProce
t.Fatalf("QueryInstalledChaincodes failed to find installed %s chaincode", ccID)
}
}

func testQueryChannels(t *testing.T, channelID string, target fab.ProposalProcessor, client *resmgmt.Client) {

channelQueryResponse, err := client.QueryChannels(target)
if err != nil {
t.Fatalf("QueryChannels return error: %v", err)
}

found := false
for _, channel := range channelQueryResponse.Channels {
t.Logf("**Channel: %s", channel)
if channel.ChannelId == channelID {
found = true
}
}

if !found {
t.Fatalf("QueryChannels failed, peer did not join '%s' channel", channelID)
}

}

0 comments on commit 7fbc960

Please sign in to comment.