Skip to content

Commit

Permalink
[FAB-8324]: Resource Mgmt: Query Config
Browse files Browse the repository at this point in the history
Change-Id: Id7ece54c45b82c75e1da66da71517cb56e138ee3
Signed-off-by: Sandra Vrtikapa <[email protected]>
  • Loading branch information
sandrask committed Mar 5, 2018
1 parent 4b576fd commit aefe5a5
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
63 changes: 60 additions & 3 deletions pkg/client/resmgmt/resmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/core"
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/chconfig"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"

"github.com/hyperledger/fabric-sdk-go/pkg/context"
Expand Down Expand Up @@ -444,8 +445,7 @@ func (rc *Client) QueryInstantiatedChaincodes(channelID string, options ...Reque
}

// select random channel peer
r := rand.New(rand.NewSource(time.Now().Unix()))
randomNumber := r.Intn(len(targets))
randomNumber := rand.Intn(len(targets))
target = targets[randomNumber]
}

Expand Down Expand Up @@ -707,7 +707,64 @@ func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption)
return nil
}

//prepareRequestOpts prepares rrequest options
// QueryConfigFromOrderer config returns channel configuration from orderer
// Valid request option is WithOrdererID
// If orderer id is not provided orderer will be defaulted to channel orderer (if configured) or random orderer from config
func (rc *Client) QueryConfigFromOrderer(channelID string, options ...RequestOption) (fab.ChannelCfg, error) {

opts, err := rc.prepareRequestOpts(options...)
if err != nil {
return nil, err
}

chCfg, err := rc.provider.Config().ChannelConfig(channelID)
if err != nil {
return nil, err
}

var ordererCfg *core.OrdererConfig

// Figure out orderer configuration (first try opts, then random channel orderer, then random orderer )
if opts.OrdererID != "" {

ordererCfg, err = rc.provider.Config().OrdererConfig(opts.OrdererID)

} else if chCfg != nil && len(chCfg.Orderers) > 0 {

// random channel orderer
randomNumber := rand.Intn(len(chCfg.Orderers))
ordererCfg, err = rc.provider.Config().OrdererConfig(chCfg.Orderers[randomNumber])

} else {
// random orderer from configuration
ordererCfg, err = rc.provider.Config().RandomOrdererConfig()
}

// Check if retrieving orderer configuration went ok
if err != nil || ordererCfg == nil {
return nil, errors.Errorf("failed to retrieve orderer config: %s", err)
}

orderer, err := orderer.New(rc.provider.Config(), orderer.FromOrdererConfig(ordererCfg))
if err != nil {
return nil, errors.WithMessage(err, "failed to resolve orderer")
}

ctx := &fabContext{
Providers: rc.provider,
Identity: rc.identity,
}

channelConfig, err := chconfig.New(ctx, channelID, chconfig.WithOrderer(orderer))
if err != nil {
return nil, errors.WithMessage(err, "QueryConfig failed")
}

return channelConfig.Query()

}

// prepareRequestOpts prepares request options
func (rc *Client) prepareRequestOpts(options ...RequestOption) (Opts, error) {
opts := Opts{}
for _, option := range options {
Expand Down
39 changes: 39 additions & 0 deletions test/integration/sdk/resmgmt_queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ func TestResMgmtClientQueries(t *testing.T) {
// Our target for queries will be primary peer on this channel
target := testSetup.Targets[0]

testQueryConfigFromOrderer(t, testSetup.ChannelID, client)

testInstalledChaincodes(t, ccID, target, client)

testInstantiatedChaincodes(t, testSetup.ChannelID, ccID, target, client)

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

}

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

chaincodeQueryResponse, err := client.QueryInstantiatedChaincodes(channelID, resmgmt.WithTarget(target.(fab.Peer)))
Expand Down Expand Up @@ -120,3 +123,39 @@ func testQueryChannels(t *testing.T, channelID string, target fab.ProposalProces
}

}

func testQueryConfigFromOrderer(t *testing.T, channelID string, client *resmgmt.Client) {

channelCfg, err := client.QueryConfigFromOrderer(channelID)
if err != nil {
t.Fatalf("QueryConfig return error: %v", err)
}

expected := "orderer.example.com:7050"
if !contains(channelCfg.Orderers(), expected) {
t.Fatalf("Expected orderer %s, got %s", expected, channelCfg.Orderers())
}

channelCfg, err = client.QueryConfigFromOrderer(channelID, resmgmt.WithOrdererID("orderer.example.com"))
if err != nil {
t.Fatalf("QueryConfig return error: %v", err)
}
if !contains(channelCfg.Orderers(), expected) {
t.Fatalf("Expected orderer %s, got %s", expected, channelCfg.Orderers())
}

channelCfg, err = client.QueryConfigFromOrderer(channelID, resmgmt.WithOrdererID("non-existent"))
if err == nil {
t.Fatalf("QueryConfig should have failed for invalid orderer")
}

}

func contains(list []string, value string) bool {
for _, e := range list {
if e == value {
return true
}
}
return false
}

0 comments on commit aefe5a5

Please sign in to comment.