Skip to content

Commit

Permalink
[FAB-9023] SaveChannel Response Struct
Browse files Browse the repository at this point in the history
This change wraps the transaction ID into a response struct to be
consistent with the other APIs in this package.

Change-Id: I279dc622d8a0969c25f46bdbff758ed7f50575bb
Signed-off-by: Troy Ronda <troy@troyronda.com>
  • Loading branch information
troyronda committed Mar 22, 2018
1 parent 13e434c commit 37f0a1a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
27 changes: 16 additions & 11 deletions pkg/client/resmgmt/resmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ type SaveChannelRequest struct {
// TODO: support pre-signed signature blocks
}

// SaveChannelResponse contains response parameters for Save
type SaveChannelResponse struct {
TransactionID fab.TransactionID
}

//RequestOption func for each Opts argument
type RequestOption func(ctx context.Client, opts *requestOptions) error

Expand Down Expand Up @@ -682,24 +687,24 @@ func peersToTxnProcessors(peers []fab.Peer) []fab.ProposalProcessor {
}

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

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

if req.ChannelConfigPath != "" {
configReader, err := os.Open(req.ChannelConfigPath)
if err != nil {
return fab.EmptyTransactionID, errors.Wrapf(err, "opening channel config file failed")
return SaveChannelResponse{}, errors.Wrapf(err, "opening channel config file failed")
}
defer loggedClose(configReader)
req.ChannelConfig = configReader
}

if req.ChannelID == "" || req.ChannelConfig == nil {
return fab.EmptyTransactionID, errors.New("must provide channel ID and channel config")
return SaveChannelResponse{}, errors.New("must provide channel ID and channel config")
}

logger.Debugf("saving channel: %s", req.ChannelID)
Expand All @@ -717,17 +722,17 @@ func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption)
} else if rc.ctx != nil {
signers = append(signers, rc.ctx)
} else {
return fab.EmptyTransactionID, errors.New("must provide signing user")
return SaveChannelResponse{}, errors.New("must provide signing user")
}

configTx, err := ioutil.ReadAll(req.ChannelConfig)
if err != nil {
return fab.EmptyTransactionID, errors.WithMessage(err, "reading channel config file failed")
return SaveChannelResponse{}, errors.WithMessage(err, "reading channel config file failed")
}

chConfig, err := resource.ExtractChannelConfig(configTx)
if err != nil {
return fab.EmptyTransactionID, errors.WithMessage(err, "extracting channel config failed")
return SaveChannelResponse{}, errors.WithMessage(err, "extracting channel config failed")
}

var configSignatures []*common.ConfigSignature
Expand All @@ -740,14 +745,14 @@ func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption)

configSignature, err := resource.CreateConfigSignature(&sigCtx, chConfig)
if err != nil {
return fab.EmptyTransactionID, errors.WithMessage(err, "signing configuration failed")
return SaveChannelResponse{}, errors.WithMessage(err, "signing configuration failed")
}
configSignatures = append(configSignatures, configSignature)
}

orderer, err := rc.requestOrderer(&opts, req.ChannelID)
if err != nil {
return fab.EmptyTransactionID, errors.WithMessage(err, "failed to find orderer for request")
return SaveChannelResponse{}, errors.WithMessage(err, "failed to find orderer for request")
}

request := api.CreateChannelRequest{
Expand All @@ -762,10 +767,10 @@ func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption)

txID, err := resource.CreateChannel(reqCtx, request)
if err != nil {
return fab.EmptyTransactionID, errors.WithMessage(err, "create channel failed")
return SaveChannelResponse{}, errors.WithMessage(err, "create channel failed")
}

return txID, nil
return SaveChannelResponse{TransactionID: txID}, nil
}

func loggedClose(c io.Closer) {
Expand Down
24 changes: 12 additions & 12 deletions pkg/client/resmgmt/resmgmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1366,14 +1366,14 @@ func TestSaveChannelSuccess(t *testing.T) {
assert.NotNil(t, err, "Should have failed to sign configuration")

// Test valid Save Channel request (success)
txID, err := cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r}, WithOrdererURL("example.com"))
resp, err := cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r}, WithOrdererURL("example.com"))
assert.Nil(t, err, "error should be nil")
assert.NotEmpty(t, txID, "transaction ID should be populated")
assert.NotEmpty(t, resp.TransactionID, "transaction ID should be populated")

// Test valid Save Channel request (success / filename)
txID, err = cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfigPath: channelConfig}, WithOrdererURL("example.com"))
resp, err = cc.SaveChannel(SaveChannelRequest{ChannelID: "mychannel", ChannelConfigPath: channelConfig}, WithOrdererURL("example.com"))
assert.Nil(t, err, "error should be nil")
assert.NotEmpty(t, txID, "transaction ID should be populated")
assert.NotEmpty(t, resp.TransactionID, "transaction ID should be populated")
}

func TestSaveChannelFailure(t *testing.T) {
Expand Down Expand Up @@ -1435,9 +1435,9 @@ func TestSaveChannelWithOpts(t *testing.T) {

// Test empty option (default order is random orderer from config)
opts := WithOrdererURL("")
txID, err := cc.SaveChannel(req, opts)
resp, err := cc.SaveChannel(req, opts)
assert.Nil(t, err, "error should be nil")
assert.NotEmpty(t, txID, "transaction ID should be populated")
assert.NotEmpty(t, resp.TransactionID, "transaction ID should be populated")

// Test valid orderer ID
r2, err := os.Open(channelConfig)
Expand All @@ -1447,9 +1447,9 @@ func TestSaveChannelWithOpts(t *testing.T) {
req = SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r2}

opts = WithOrdererURL("orderer.example.com")
txID, err = cc.SaveChannel(req, opts)
resp, err = cc.SaveChannel(req, opts)
assert.Nil(t, err, "error should be nil")
assert.NotEmpty(t, txID, "transaction ID should be populated")
assert.NotEmpty(t, resp.TransactionID, "transaction ID should be populated")

// Test invalid orderer ID
r3, err := os.Open(channelConfig)
Expand Down Expand Up @@ -1505,9 +1505,9 @@ func TestSaveChannelWithMultipleSigningIdenities(t *testing.T) {
defer r1.Close()

req := SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r1, SigningIdentities: []msp.SigningIdentity{}}
txID, err := cc.SaveChannel(req, WithOrdererURL(""))
resp, err := cc.SaveChannel(req, WithOrdererURL(""))
assert.Nil(t, err, "Failed to save channel with default signing identity: %s", err)
assert.NotEmpty(t, txID, "transaction ID should be populated")
assert.NotEmpty(t, resp.TransactionID, "transaction ID should be populated")

// multiple signing identities
r2, err := os.Open(channelConfig)
Expand All @@ -1516,9 +1516,9 @@ func TestSaveChannelWithMultipleSigningIdenities(t *testing.T) {

secondCtx := fcmocks.NewMockContext(mspmocks.NewMockSigningIdentity("second", "second"))
req = SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: r2, SigningIdentities: []msp.SigningIdentity{cc.ctx, secondCtx}}
txID, err = cc.SaveChannel(req, WithOrdererURL(""))
resp, err = cc.SaveChannel(req, WithOrdererURL(""))
assert.Nil(t, err, "Failed to save channel with multiple signing identities: %s", err)
assert.NotEmpty(t, txID, "transaction ID should be populated")
assert.NotEmpty(t, resp.TransactionID, "transaction ID should be populated")
}

func createClientContext(fabCtx context.Client) context.ClientProvider {
Expand Down

0 comments on commit 37f0a1a

Please sign in to comment.