Skip to content

Commit

Permalink
[FAB-7948] Removing *WithOpts functions in chmgmtclient
Browse files Browse the repository at this point in the history
Change-Id: I0e8e441921adc6edb51fb33226daadcfba37012a
Signed-off-by: Sudesh Shetty <[email protected]>
  • Loading branch information
sudeshrshetty committed Jan 29, 2018
1 parent a1febf9 commit 99efc28
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
15 changes: 8 additions & 7 deletions api/apitxn/chmgmtclient/chmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ type SaveChannelRequest struct {
SigningIdentity fab.IdentityContext
}

// SaveChannelOpts contains options for saving channel
type SaveChannelOpts struct {
// Opts contains options for saving channel, this struct is intended for reference only.
// Will be used in form of Option to pass arguments
type Opts struct {
OrdererID string // use specific orderer
}

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

// ChannelMgmtClient supports creating new channels
type ChannelMgmtClient interface {

// SaveChannel creates or updates channel
SaveChannel(req SaveChannelRequest) error

// SaveChannel creates or updates channel with custom options
SaveChannelWithOpts(req SaveChannelRequest, opts SaveChannelOpts) error
// SaveChannel creates or updates channel with optional Opts options
SaveChannel(req SaveChannelRequest, options ...Option) error
}
15 changes: 15 additions & 0 deletions api/apitxn/chmgmtclient/opts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package chmgmtclient

//WithOrdererID encapsulates OrdererID to Option
func WithOrdererID(ordererID string) Option {
return func(opts *Opts) error {
opts.OrdererID = ordererID
return nil
}
}
22 changes: 17 additions & 5 deletions pkg/fabric-txn/chmgmtclient/chmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ func NewChannelMgmtClient(client fab.Resource, config config.Config) (*ChannelMg
}

// SaveChannel creates or updates channel
func (cc *ChannelMgmtClient) SaveChannel(req chmgmt.SaveChannelRequest) error {
return cc.SaveChannelWithOpts(req, chmgmt.SaveChannelOpts{})
}
func (cc *ChannelMgmtClient) SaveChannel(req chmgmt.SaveChannelRequest, options ...chmgmt.Option) error {

// SaveChannelWithOpts creates or updates channel with custom options
func (cc *ChannelMgmtClient) SaveChannelWithOpts(req chmgmt.SaveChannelRequest, opts chmgmt.SaveChannelOpts) error {
opts, err := cc.prepareSaveChannelOpts(options...)
if err != nil {
return err
}

if req.ChannelID == "" || req.ChannelConfig == "" {
return errors.New("must provide channel ID and channel config")
Expand Down Expand Up @@ -110,3 +110,15 @@ func (cc *ChannelMgmtClient) SaveChannelWithOpts(req chmgmt.SaveChannelRequest,

return nil
}

//prepareSaveChannelOpts Reads chmgmt.Opts from chmgmt.Option array
func (cc *ChannelMgmtClient) prepareSaveChannelOpts(options ...chmgmt.Option) (chmgmt.Opts, error) {
saveChannelOpts := chmgmt.Opts{}
for _, option := range options {
err := option(&saveChannelOpts)
if err != nil {
return saveChannelOpts, errors.WithMessage(err, "Failed to read save channel opts")
}
}
return saveChannelOpts, nil
}
12 changes: 6 additions & 6 deletions pkg/fabric-txn/chmgmtclient/chmgmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,22 @@ func TestSaveChannelWithOpts(t *testing.T) {
req := chmgmtclient.SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: channelConfig}

// Test empty option (default order is random orderer from config)
opts := chmgmtclient.SaveChannelOpts{}
err := cc.SaveChannelWithOpts(req, opts)
opts := chmgmtclient.WithOrdererID("")
err := cc.SaveChannel(req, opts)
if err != nil {
t.Fatal(err)
}

// Test valid orderer ID
opts.OrdererID = "orderer.example.com"
err = cc.SaveChannelWithOpts(req, opts)
opts = chmgmtclient.WithOrdererID("orderer.example.com")
err = cc.SaveChannel(req, opts)
if err != nil {
t.Fatal(err)
}

// Test invalid orderer ID
opts.OrdererID = "Invalid"
err = cc.SaveChannelWithOpts(req, opts)
opts = chmgmtclient.WithOrdererID("Invalid")
err = cc.SaveChannel(req, opts)
if err == nil {
t.Fatal("Should have failed for invalid orderer ID")
}
Expand Down

0 comments on commit 99efc28

Please sign in to comment.