diff --git a/api/apitxn/resmgmtclient/opts.go b/api/apitxn/resmgmtclient/opts.go new file mode 100644 index 0000000000..ae35bc6715 --- /dev/null +++ b/api/apitxn/resmgmtclient/opts.go @@ -0,0 +1,37 @@ +/* +Copyright SecureKey Technologies Inc. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package resmgmtclient + +import ( + "time" + + fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient" +) + +//WithTargets encapsulates fab.Peer targets to resmgmtclient Option +func WithTargets(targets ...fab.Peer) Option { + return func(opts *Opts) error { + opts.Targets = targets + return nil + } +} + +//WithTargetFilter encapsulates resmgmtclient TargetFilter targets to resmgmtclient Option +func WithTargetFilter(targetFilter TargetFilter) Option { + return func(opts *Opts) error { + opts.TargetFilter = targetFilter + return nil + } +} + +//WithTimeout encapsulates time.Duration to resmgmtclient Option +func WithTimeout(timeout time.Duration) Option { + return func(opts *Opts) error { + opts.Timeout = timeout + return nil + } +} diff --git a/api/apitxn/resmgmtclient/resmgmt.go b/api/apitxn/resmgmtclient/resmgmt.go index cce9b4d8ca..425fc7ecb7 100644 --- a/api/apitxn/resmgmtclient/resmgmt.go +++ b/api/apitxn/resmgmtclient/resmgmt.go @@ -19,12 +19,6 @@ type TargetFilter interface { Accept(peer fab.Peer) bool } -// JoinChannelOpts contains options for peers joining channel -type JoinChannelOpts struct { - Targets []fab.Peer // target peers - TargetFilter TargetFilter // peer filter -} - // InstallCCRequest contains install chaincode request parameters type InstallCCRequest struct { Name string @@ -41,12 +35,6 @@ type InstallCCResponse struct { Err error } -// InstallCCOpts contains options for installing chaincode -type InstallCCOpts struct { - Targets []fab.Peer // target peers - TargetFilter TargetFilter // target filter -} - // InstantiateCCRequest contains instantiate chaincode request parameters type InstantiateCCRequest struct { Name string @@ -57,13 +45,6 @@ type InstantiateCCRequest struct { CollConfig []*common.CollectionConfig } -// InstantiateCCOpts contains options for instantiating chaincode -type InstantiateCCOpts struct { - Targets []fab.Peer // target peers - TargetFilter TargetFilter // target filter - Timeout time.Duration -} - // UpgradeCCRequest contains upgrade chaincode request parameters type UpgradeCCRequest struct { Name string @@ -74,37 +55,28 @@ type UpgradeCCRequest struct { CollConfig []*common.CollectionConfig } -// UpgradeCCOpts contains options for upgrading chaincode -type UpgradeCCOpts struct { - Targets []fab.Peer // target peers - TargetFilter TargetFilter // target filter - Timeout time.Duration +//Opts contains options for operations performed by ResourceMgmtClient +type Opts struct { + Targets []fab.Peer // target peers + TargetFilter TargetFilter // target filter + Timeout time.Duration //timeout options for instantiate and upgrade CC } +//Option func for each Opts argument +type Option func(opts *Opts) error + // ResourceMgmtClient is responsible for managing resources: peers joining channels, and installing and instantiating chaincodes(TODO). type ResourceMgmtClient interface { - // InstallCC installs chaincode - InstallCC(req InstallCCRequest) ([]InstallCCResponse, error) - - // InstallCCWithOpts installs chaincode with custom options (specific peers, filtered peers) - InstallCCWithOpts(req InstallCCRequest, opts InstallCCOpts) ([]InstallCCResponse, error) - - // InstantiateCC instantiates chaincode using default settings - InstantiateCC(channelID string, req InstantiateCCRequest) error - - // InstantiateCCWithOpts instantiates chaincode with custom options (target peers, filtered peers) - InstantiateCCWithOpts(channelID string, req InstantiateCCRequest, opts InstantiateCCOpts) error - - // UpgradeCC upgrades chaincode using default settings - UpgradeCC(channelID string, req UpgradeCCRequest) error + // InstallCC installs chaincode with optional custom options (specific peers, filtered peers) + InstallCC(req InstallCCRequest, options ...Option) ([]InstallCCResponse, error) - // UpgradeCCWithOpts upgrades chaincode with custom options (target peers, filtered peers) - UpgradeCCWithOpts(channelID string, req UpgradeCCRequest, opts UpgradeCCOpts) error + // InstantiateCC instantiates chaincode with optional custom options (specific peers, filtered peers, timeout) + InstantiateCC(channelID string, req InstantiateCCRequest, options ...Option) error - // JoinChannel allows for peers to join existing channel - JoinChannel(channelID string) error + // UpgradeCC upgrades chaincode with optional custom options (specific peers, filtered peers, timeout) + UpgradeCC(channelID string, req UpgradeCCRequest, options ...Option) error - //JoinChannelWithOpts allows for customizing set of peers about to join the channel (specific peers/filtered peers) - JoinChannelWithOpts(channelID string, opts JoinChannelOpts) error + // JoinChannel allows for peers to join existing channel with optional custom options (specific peers, filtered peers) + JoinChannel(channelID string, options ...Option) error } diff --git a/pkg/fabric-txn/resmgmtclient/resmgmt.go b/pkg/fabric-txn/resmgmtclient/resmgmt.go index 9adea7f5fc..698a74d5b5 100644 --- a/pkg/fabric-txn/resmgmtclient/resmgmt.go +++ b/pkg/fabric-txn/resmgmtclient/resmgmt.go @@ -93,30 +93,24 @@ func New(ctx Context, filter resmgmt.TargetFilter) (*ResourceMgmtClient, error) return resourceClient, nil } -// JoinChannel allows for default peers to join existing channel. Default peers are selected by applying default filter to all network peers. -func (rc *ResourceMgmtClient) JoinChannel(channelID string) error { +// JoinChannel allows for peers to join existing channel with optional custom options (specific peers, filtered peers) +func (rc *ResourceMgmtClient) JoinChannel(channelID string, options ...resmgmt.Option) error { if channelID == "" { return errors.New("must provide channel ID") } - targets, err := rc.getDefaultTargets(rc.discovery) + opts, err := rc.prepareResmgmtOpts(options...) if err != nil { - return errors.WithMessage(err, "failed to get default targets for JoinChannel") + return errors.WithMessage(err, "failed to get opts for JoinChannel") } - if len(targets) == 0 { - return errors.New("No default targets available") - } - - return rc.JoinChannelWithOpts(channelID, resmgmt.JoinChannelOpts{Targets: targets}) -} - -//JoinChannelWithOpts allows for customizing set of peers about to join the channel (specific peers or custom 'filtered' peers) -func (rc *ResourceMgmtClient) JoinChannelWithOpts(channelID string, opts resmgmt.JoinChannelOpts) error { - - if channelID == "" { - return errors.New("must provide channel ID") + //Default targets when targets are not provided in options + if len(opts.Targets) == 0 { + opts.Targets, err = rc.getDefaultTargets(rc.discovery) + if err != nil { + return errors.WithMessage(err, "failed to get default targets for JoinChannel") + } } targets, err := rc.calculateTargets(rc.discovery, opts.Targets, opts.TargetFilter) @@ -232,27 +226,8 @@ func (rc *ResourceMgmtClient) isChaincodeInstalled(req resmgmt.InstallCCRequest, return false, nil } -// InstallCC - install chaincode -func (rc *ResourceMgmtClient) InstallCC(req resmgmt.InstallCCRequest) ([]resmgmt.InstallCCResponse, error) { - - if err := checkRequiredInstallCCParams(req); err != nil { - return nil, err - } - - targets, err := rc.getDefaultTargets(rc.discovery) - if err != nil { - return nil, errors.WithMessage(err, "failed to get default targets for InstallCC") - } - - if len(targets) == 0 { - return nil, errors.New("No default targets available for install cc") - } - - return rc.InstallCCWithOpts(req, resmgmt.InstallCCOpts{Targets: targets}) -} - -// InstallCCWithOpts installs chaincode with custom options -func (rc *ResourceMgmtClient) InstallCCWithOpts(req resmgmt.InstallCCRequest, opts resmgmt.InstallCCOpts) ([]resmgmt.InstallCCResponse, error) { +// InstallCC installs chaincode with optional custom options (specific peers, filtered peers) +func (rc *ResourceMgmtClient) InstallCC(req resmgmt.InstallCCRequest, options ...resmgmt.Option) ([]resmgmt.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. @@ -262,6 +237,19 @@ func (rc *ResourceMgmtClient) InstallCCWithOpts(req resmgmt.InstallCCRequest, op return nil, err } + opts, err := rc.prepareResmgmtOpts(options...) + if err != nil { + return nil, errors.WithMessage(err, "failed to get opts for InstallCC") + } + + //Default targets when targets are not provided in options + if len(opts.Targets) == 0 { + opts.Targets, err = rc.getDefaultTargets(rc.discovery) + if err != nil { + return nil, errors.WithMessage(err, "failed to get default targets for InstallCC") + } + } + targets, err := rc.calculateTargets(rc.discovery, opts.Targets, opts.TargetFilter) if err != nil { return nil, errors.WithMessage(err, "failed to determine target peers for install cc") @@ -326,74 +314,29 @@ func checkRequiredInstallCCParams(req resmgmt.InstallCCRequest) error { } // InstantiateCC instantiates chaincode using default settings -func (rc *ResourceMgmtClient) InstantiateCC(channelID string, req resmgmt.InstantiateCCRequest) error { - - if err := checkRequiredCCProposalParams(channelID, req); err != nil { - return err - } - - // per channel discovery service - discovery, err := rc.discoveryProvider.NewDiscoveryService(channelID) - if err != nil { - return errors.WithMessage(err, "failed to create channel discovery service") - } - - targets, err := rc.getDefaultTargets(discovery) - if err != nil { - return errors.WithMessage(err, "failed to get default targets for InstantiateCC") - } +func (rc *ResourceMgmtClient) InstantiateCC(channelID string, req resmgmt.InstantiateCCRequest, options ...resmgmt.Option) error { - if len(targets) == 0 { - return errors.New("No default targets available for instantiate cc") - } + return rc.sendCCProposal(Instantiate, channelID, req, options...) - return rc.InstantiateCCWithOpts(channelID, req, resmgmt.InstantiateCCOpts{Targets: targets}) } -// InstantiateCCWithOpts instantiates chaincode with custom options -func (rc *ResourceMgmtClient) InstantiateCCWithOpts(channelID string, req resmgmt.InstantiateCCRequest, opts resmgmt.InstantiateCCOpts) error { +// UpgradeCC upgrades chaincode with optional custom options (specific peers, filtered peers, timeout) +func (rc *ResourceMgmtClient) UpgradeCC(channelID string, req resmgmt.UpgradeCCRequest, options ...resmgmt.Option) error { - return rc.sendCCProposalWithOpts(Instantiate, channelID, req, opts) + return rc.sendCCProposal(Upgrade, channelID, resmgmt.InstantiateCCRequest(req), options...) } -// UpgradeCC upgrades chaincode using default settings -func (rc *ResourceMgmtClient) UpgradeCC(channelID string, req resmgmt.UpgradeCCRequest) error { +// sendCCProposal sends proposal for type Instantiate, Upgrade +func (rc *ResourceMgmtClient) sendCCProposal(ccProposalType CCProposalType, channelID string, req resmgmt.InstantiateCCRequest, options ...resmgmt.Option) error { - if err := checkRequiredCCProposalParams(channelID, resmgmt.InstantiateCCRequest(req)); err != nil { + if err := checkRequiredCCProposalParams(channelID, req); err != nil { return err } - // per channel discovery service - discovery, err := rc.discoveryProvider.NewDiscoveryService(channelID) + opts, err := rc.prepareResmgmtOpts(options...) if err != nil { - return errors.WithMessage(err, "failed to create channel discovery service") - } - - targets, err := rc.getDefaultTargets(discovery) - if err != nil { - return errors.WithMessage(err, "failed to get default targets for UpgradeCC") - } - - if len(targets) == 0 { - return errors.New("No default targets available for upgrade cc") - } - - return rc.UpgradeCCWithOpts(channelID, req, resmgmt.UpgradeCCOpts{Targets: targets}) -} - -// UpgradeCCWithOpts upgrades chaincode with custom options -func (rc *ResourceMgmtClient) UpgradeCCWithOpts(channelID string, req resmgmt.UpgradeCCRequest, opts resmgmt.UpgradeCCOpts) error { - - return rc.sendCCProposalWithOpts(Upgrade, channelID, resmgmt.InstantiateCCRequest(req), resmgmt.InstantiateCCOpts(opts)) - -} - -// InstantiateCCWithOpts instantiates chaincode with custom options -func (rc *ResourceMgmtClient) sendCCProposalWithOpts(ccProposalType CCProposalType, channelID string, req resmgmt.InstantiateCCRequest, opts resmgmt.InstantiateCCOpts) error { - - if err := checkRequiredCCProposalParams(channelID, req); err != nil { - return err + return errors.WithMessage(err, "failed to get opts for cc proposal") } // per channel discovery service @@ -402,6 +345,14 @@ func (rc *ResourceMgmtClient) sendCCProposalWithOpts(ccProposalType CCProposalTy return errors.WithMessage(err, "failed to create channel discovery service") } + //Default targets when targets are not provided in options + if len(opts.Targets) == 0 { + opts.Targets, err = rc.getDefaultTargets(discovery) + if err != nil { + return errors.WithMessage(err, "failed to get default targets for cc proposal") + } + } + targets, err := rc.calculateTargets(discovery, opts.Targets, opts.TargetFilter) if err != nil { return errors.WithMessage(err, "failed to determine target peers for cc proposal") @@ -508,3 +459,15 @@ func (rc *ResourceMgmtClient) getChannel(channelID string) (fab.Channel, error) } return channel, nil } + +//prepareResmgmtOpts Reads resmgmt.Opts from resmgmt.Option array +func (rc *ResourceMgmtClient) prepareResmgmtOpts(options ...resmgmt.Option) (resmgmt.Opts, error) { + resmgmtOpts := resmgmt.Opts{} + for _, option := range options { + err := option(&resmgmtOpts) + if err != nil { + return resmgmtOpts, errors.WithMessage(err, "Failed to read resource management opts") + } + } + return resmgmtOpts, nil +} diff --git a/pkg/fabric-txn/resmgmtclient/resmgmt_test.go b/pkg/fabric-txn/resmgmtclient/resmgmt_test.go index c99d1d9f3b..79d48cb5f8 100644 --- a/pkg/fabric-txn/resmgmtclient/resmgmt_test.go +++ b/pkg/fabric-txn/resmgmtclient/resmgmt_test.go @@ -41,12 +41,10 @@ func TestJoinChannelFail(t *testing.T) { rc := setupResMgmtClient(ctx, nil, t) // Setup target peers - var peers []fab.Peer peer1, _ := peer.New(fcmocks.NewMockConfig()) - peers = append(peers, peer1) // Test fail genesis block retrieval (no orderer) - err := rc.JoinChannelWithOpts("mychannel", resmgmt.JoinChannelOpts{Targets: peers}) + err := rc.JoinChannel("mychannel", resmgmt.WithTargets(peer1)) if err == nil { t.Fatal("Should have failed to get genesis block") } @@ -84,7 +82,7 @@ func TestJoinChannel(t *testing.T) { peers = append(peers, peer1) // Test valid join channel request (success) - err = rc.JoinChannelWithOpts("mychannel", resmgmt.JoinChannelOpts{Targets: peers}) + err = rc.JoinChannel("mychannel", resmgmt.WithTargets(peer1)) if err != nil { t.Fatal(err) } @@ -92,7 +90,7 @@ func TestJoinChannel(t *testing.T) { orderer.(fcmocks.MockOrderer).EnqueueForSendDeliver(fcmocks.NewSimpleMockBlock()) // Test fails because configured peer is not running - err = rc.JoinChannelWithOpts("mychannel", resmgmt.JoinChannelOpts{}) + err = rc.JoinChannel("mychannel") if err == nil { t.Fatal("Should have failed due to configured peer is not running") } @@ -103,7 +101,7 @@ func TestJoinChannel(t *testing.T) { endorserServer.ProposalError = errors.New("Test Error") // Test proposal error - err = rc.JoinChannelWithOpts("mychannel", resmgmt.JoinChannelOpts{Targets: peers}) + err = rc.JoinChannel("mychannel", resmgmt.WithTargets(peer1)) if err == nil { t.Fatal("Should have failed with proposal error") } @@ -179,8 +177,8 @@ func TestJoinChannelWithOptsRequiredParameters(t *testing.T) { rc := setupDefaultResMgmtClient(t) - // Test empty channel name for request with opts - err := rc.JoinChannelWithOpts("", resmgmt.JoinChannelOpts{}) + // Test empty channel name for request with no opts + err := rc.JoinChannel("") if err == nil { t.Fatalf("Should have failed for empty channel name") } @@ -190,13 +188,13 @@ func TestJoinChannelWithOptsRequiredParameters(t *testing.T) { peers = append(peers, &peer) // Test both targets and filter provided (error condition) - err = rc.JoinChannelWithOpts("mychannel", resmgmt.JoinChannelOpts{Targets: peers, TargetFilter: &MSPFilter{mspID: "MspID"}}) + err = rc.JoinChannel("mychannel", resmgmt.WithTargets(peers...), resmgmt.WithTargetFilter(&MSPFilter{mspID: "MspID"})) if err == nil { t.Fatalf("Should have failed if both target and filter provided") } // Test missing default targets - err = rc.JoinChannelWithOpts("mychannel", resmgmt.JoinChannelOpts{TargetFilter: &MSPFilter{mspID: "MspID"}}) + err = rc.JoinChannel("mychannel", resmgmt.WithTargetFilter(&MSPFilter{mspID: "MspID"})) if err == nil { t.Fatalf("InstallCC should have failed with no targets error") } @@ -219,7 +217,7 @@ func TestJoinChannelDiscoveryError(t *testing.T) { } // If targets are not provided discovery service is used - err = rc.JoinChannelWithOpts("mychannel", resmgmt.JoinChannelOpts{}) + err = rc.JoinChannel("mychannel") if err == nil { t.Fatalf("Should have failed to join channel with discovery error") } @@ -319,7 +317,7 @@ func TestInstallCCWithOpts(t *testing.T) { // Already installed chaincode request req := resmgmt.InstallCCRequest{Name: "name", Version: "version", Path: "path", Package: &fab.CCPackage{Type: 1, Code: []byte("code")}} - responses, err := rc.InstallCCWithOpts(req, resmgmt.InstallCCOpts{Targets: peers}) + responses, err := rc.InstallCC(req, resmgmt.WithTargets(peers...)) if err != nil { t.Fatal(err) } @@ -338,7 +336,7 @@ func TestInstallCCWithOpts(t *testing.T) { // Chaincode not found request (it will be installed) req = resmgmt.InstallCCRequest{Name: "ID", Version: "v0", Path: "path", Package: &fab.CCPackage{Type: 1, Code: []byte("code")}} - responses, err = rc.InstallCCWithOpts(req, resmgmt.InstallCCOpts{Targets: peers}) + responses, err = rc.InstallCC(req, resmgmt.WithTargets(peers...)) if err != nil { t.Fatal(err) } @@ -353,14 +351,14 @@ func TestInstallCCWithOpts(t *testing.T) { // Chaincode that causes generic (system) error in installed chaincodes req = resmgmt.InstallCCRequest{Name: "error", Version: "v0", Path: "path", Package: &fab.CCPackage{Type: 1, Code: []byte("code")}} - _, err = rc.InstallCCWithOpts(req, resmgmt.InstallCCOpts{Targets: peers}) + _, err = rc.InstallCC(req, resmgmt.WithTargets(peers...)) if err == nil { t.Fatalf("Should have failed since install cc returns an error in the client") } // Chaincode that causes response error in installed chaincodes req = resmgmt.InstallCCRequest{Name: "errorInResponse", Version: "v0", Path: "path", Package: &fab.CCPackage{Type: 1, Code: []byte("code")}} - responses, err = rc.InstallCCWithOpts(req, resmgmt.InstallCCOpts{Targets: peers}) + responses, err = rc.InstallCC(req, resmgmt.WithTargets(peers...)) if err != nil { t.Fatal(err) } @@ -459,36 +457,35 @@ func TestInstallCCWithOptsRequiredParameters(t *testing.T) { // Test missing required parameters req := resmgmt.InstallCCRequest{} - opts := resmgmt.InstallCCOpts{} - _, err := rc.InstallCCWithOpts(req, opts) + _, err := rc.InstallCC(req) if err == nil { t.Fatalf("Should have failed for empty install cc request") } // Test missing chaincode ID req = resmgmt.InstallCCRequest{Name: "", Version: "v0", Path: "path"} - _, err = rc.InstallCCWithOpts(req, opts) + _, err = rc.InstallCC(req) if err == nil { t.Fatalf("Should have failed for empty cc ID") } // Test missing chaincode version req = resmgmt.InstallCCRequest{Name: "ID", Version: "", Path: "path"} - _, err = rc.InstallCCWithOpts(req, opts) + _, err = rc.InstallCC(req) if err == nil { t.Fatalf("Should have failed for empty cc version") } // Test missing chaincode path req = resmgmt.InstallCCRequest{Name: "ID", Version: "v0", Path: ""} - _, err = rc.InstallCCWithOpts(req, opts) + _, err = rc.InstallCC(req) if err == nil { t.Fatalf("InstallCC should have failed for empty cc path") } // Test missing chaincode package req = resmgmt.InstallCCRequest{Name: "ID", Version: "v0", Path: "path"} - _, err = rc.InstallCCWithOpts(req, opts) + _, err = rc.InstallCC(req) if err == nil { t.Fatalf("InstallCC should have failed for nil chaincode package") } @@ -502,7 +499,7 @@ func TestInstallCCWithOptsRequiredParameters(t *testing.T) { peers = append(peers, &peer) // Test both targets and filter provided (error condition) - _, err = rc.InstallCCWithOpts(req, resmgmt.InstallCCOpts{Targets: peers, TargetFilter: &MSPFilter{mspID: "Org1MSP"}}) + _, err = rc.InstallCC(req, resmgmt.WithTargets(peers...), resmgmt.WithTargetFilter(&MSPFilter{mspID: "Org1MSP"})) if err == nil { t.Fatalf("Should have failed if both target and filter provided") } @@ -516,13 +513,13 @@ func TestInstallCCWithOptsRequiredParameters(t *testing.T) { rc = setupResMgmtClient(ctx, nil, t) // No targets and no filter -- default filter msp doesn't match discovery service peer msp - _, err = rc.InstallCCWithOpts(req, resmgmt.InstallCCOpts{}) + _, err = rc.InstallCC(req) if err == nil { t.Fatalf("Should have failed with no targets error") } // Test filter only provided (filter rejects discovery service peer msp) - _, err = rc.InstallCCWithOpts(req, resmgmt.InstallCCOpts{TargetFilter: &MSPFilter{mspID: "Org2MSP"}}) + _, err = rc.InstallCC(req, resmgmt.WithTargetFilter(&MSPFilter{mspID: "Org2MSP"})) if err == nil { t.Fatalf("Should have failed with no targets since filter rejected all discovery targets") } @@ -547,7 +544,7 @@ func TestInstallCCDiscoveryError(t *testing.T) { // Test InstallCC discovery service error // if targets are not provided discovery service is used - _, err = rc.InstallCCWithOpts(req, resmgmt.InstallCCOpts{}) + _, err = rc.InstallCC(req) if err == nil { t.Fatalf("Should have failed to install cc with opts with discovery error") } @@ -627,44 +624,43 @@ func TestInstantiateCCWithOptsRequiredParameters(t *testing.T) { // Test missing required parameters req := resmgmt.InstantiateCCRequest{} - opts := resmgmt.InstantiateCCOpts{} // Test empty channel name - err := rc.InstantiateCCWithOpts("", req, opts) + err := rc.InstantiateCC("", req) if err == nil { t.Fatalf("Should have failed for empty channel name") } // Test empty request - err = rc.InstantiateCCWithOpts("mychannel", req, opts) + err = rc.InstantiateCC("mychannel", req) if err == nil { t.Fatalf("Should have failed for empty instantiate cc request") } // Test missing chaincode ID req = resmgmt.InstantiateCCRequest{Name: "", Version: "v0", Path: "path"} - err = rc.InstantiateCCWithOpts("mychannel", req, opts) + err = rc.InstantiateCC("mychannel", req) if err == nil { t.Fatalf("Should have failed for empty cc name") } // Test missing chaincode version req = resmgmt.InstantiateCCRequest{Name: "ID", Version: "", Path: "path"} - err = rc.InstantiateCCWithOpts("mychannel", req, opts) + err = rc.InstantiateCC("mychannel", req) if err == nil { t.Fatalf("Should have failed for empty cc version") } // Test missing chaincode path req = resmgmt.InstantiateCCRequest{Name: "ID", Version: "v0", Path: ""} - err = rc.InstantiateCCWithOpts("mychannel", req, opts) + err = rc.InstantiateCC("mychannel", req) if err == nil { t.Fatalf("Should have failed for empty cc path") } // Test missing chaincode policy req = resmgmt.InstantiateCCRequest{Name: "ID", Version: "v0", Path: "path"} - err = rc.InstantiateCCWithOpts("mychannel", req, opts) + err = rc.InstantiateCC("mychannel", req) if err == nil { t.Fatalf("Should have failed for missing chaincode policy") } @@ -679,7 +675,7 @@ func TestInstantiateCCWithOptsRequiredParameters(t *testing.T) { peers = append(peers, &peer) // Test both targets and filter provided (error condition) - err = rc.InstantiateCCWithOpts("mychannel", req, resmgmt.InstantiateCCOpts{Targets: peers, TargetFilter: &MSPFilter{mspID: "Org1MSP"}}) + err = rc.InstantiateCC("mychannel", req, resmgmt.WithTargets(peers...), resmgmt.WithTargetFilter(&MSPFilter{mspID: "Org1MSP"})) if err == nil { t.Fatalf("Should have failed if both target and filter provided") } @@ -693,13 +689,13 @@ func TestInstantiateCCWithOptsRequiredParameters(t *testing.T) { rc = setupResMgmtClient(ctx, nil, t) // No targets and no filter -- default filter msp doesn't match discovery service peer msp - err = rc.InstantiateCCWithOpts("mychannel", req, resmgmt.InstantiateCCOpts{}) + err = rc.InstantiateCC("mychannel", req) if err == nil { t.Fatalf("Should have failed with no targets error") } // Test filter only provided (filter rejects discovery service peer msp) - err = rc.InstantiateCCWithOpts("mychannel", req, resmgmt.InstantiateCCOpts{TargetFilter: &MSPFilter{mspID: "Org2MSP"}}) + err = rc.InstantiateCC("mychannel", req, resmgmt.WithTargetFilter(&MSPFilter{mspID: "Org2MSP"})) if err == nil { t.Fatalf("Should have failed with no targets since filter rejected all discovery targets") } @@ -731,14 +727,14 @@ func TestInstantiateCCDiscoveryError(t *testing.T) { } // Test InstantiateCCWithOpts create new discovery service per channel error - err = rc.InstantiateCCWithOpts("error", req, resmgmt.InstantiateCCOpts{}) + err = rc.InstantiateCC("error", req) if err == nil { t.Fatalf("Should have failed to instantiate cc with opts with create discovery service error") } // Test InstantiateCCWithOpts discovery service get peers error // if targets are not provided discovery service is used - err = rc.InstantiateCCWithOpts("mychannel", req, resmgmt.InstantiateCCOpts{}) + err = rc.InstantiateCC("mychannel", req) if err == nil { t.Fatalf("Should have failed to instantiate cc with opts with get peers discovery error") } @@ -818,44 +814,43 @@ func TestUpgradeCCWithOptsRequiredParameters(t *testing.T) { // Test missing required parameters req := resmgmt.UpgradeCCRequest{} - opts := resmgmt.UpgradeCCOpts{} // Test empty channel name - err := rc.UpgradeCCWithOpts("", req, opts) + err := rc.UpgradeCC("", req) if err == nil { t.Fatalf("Should have failed for empty channel name") } // Test empty request - err = rc.UpgradeCCWithOpts("mychannel", req, opts) + err = rc.UpgradeCC("mychannel", req) if err == nil { t.Fatalf("Should have failed for empty upgrade cc request") } // Test missing chaincode ID req = resmgmt.UpgradeCCRequest{Name: "", Version: "v0", Path: "path"} - err = rc.UpgradeCCWithOpts("mychannel", req, opts) + err = rc.UpgradeCC("mychannel", req) if err == nil { t.Fatalf("Should have failed for empty cc name") } // Test missing chaincode version req = resmgmt.UpgradeCCRequest{Name: "ID", Version: "", Path: "path"} - err = rc.UpgradeCCWithOpts("mychannel", req, opts) + err = rc.UpgradeCC("mychannel", req) if err == nil { t.Fatalf("Should have failed for empty cc version") } // Test missing chaincode path req = resmgmt.UpgradeCCRequest{Name: "ID", Version: "v0", Path: ""} - err = rc.UpgradeCCWithOpts("mychannel", req, opts) + err = rc.UpgradeCC("mychannel", req) if err == nil { t.Fatalf("UpgradeCC should have failed for empty cc path") } // Test missing chaincode policy req = resmgmt.UpgradeCCRequest{Name: "ID", Version: "v0", Path: "path"} - err = rc.UpgradeCCWithOpts("mychannel", req, opts) + err = rc.UpgradeCC("mychannel", req) if err == nil { t.Fatalf("UpgradeCC should have failed for missing chaincode policy") } @@ -870,7 +865,7 @@ func TestUpgradeCCWithOptsRequiredParameters(t *testing.T) { peers = append(peers, &peer) // Test both targets and filter provided (error condition) - err = rc.UpgradeCCWithOpts("mychannel", req, resmgmt.UpgradeCCOpts{Targets: peers, TargetFilter: &MSPFilter{mspID: "Org1MSP"}}) + err = rc.UpgradeCC("mychannel", req, resmgmt.WithTargets(peers...), resmgmt.WithTargetFilter(&MSPFilter{mspID: "Org1MSP"})) if err == nil { t.Fatalf("Should have failed if both target and filter provided") } @@ -884,13 +879,13 @@ func TestUpgradeCCWithOptsRequiredParameters(t *testing.T) { rc = setupResMgmtClient(ctx, nil, t) // No targets and no filter -- default filter msp doesn't match discovery service peer msp - err = rc.UpgradeCCWithOpts("mychannel", req, resmgmt.UpgradeCCOpts{}) + err = rc.UpgradeCC("mychannel", req) if err == nil { t.Fatalf("Should have failed with no targets error") } // Test filter only provided (filter rejects discovery service peer msp) - err = rc.UpgradeCCWithOpts("mychannel", req, resmgmt.UpgradeCCOpts{TargetFilter: &MSPFilter{mspID: "Org2MSP"}}) + err = rc.UpgradeCC("mychannel", req, resmgmt.WithTargetFilter(&MSPFilter{mspID: "Org2MSP"})) if err == nil { t.Fatalf("Should have failed with no targets since filter rejected all discovery targets") } @@ -923,14 +918,14 @@ func TestUpgradeCCDiscoveryError(t *testing.T) { } // Test UpgradeCCWithOpts discovery service error when creating discovery service for channel 'error' - err = rc.UpgradeCCWithOpts("error", req, resmgmt.UpgradeCCOpts{}) + err = rc.UpgradeCC("error", req) if err == nil { t.Fatalf("Should have failed to upgrade cc with opts with discovery error") } // Test UpgradeCCWithOpts discovery service error // if targets are not provided discovery service is used to get targets - err = rc.UpgradeCCWithOpts("mychannel", req, resmgmt.UpgradeCCOpts{}) + err = rc.UpgradeCC("mychannel", req) if err == nil { t.Fatalf("Should have failed to upgrade cc with opts with discovery error") } @@ -983,13 +978,13 @@ func TestCCProposal(t *testing.T) { // Test failed proposal error handling (endorser returns an error) endorserServer.ProposalError = errors.New("Test Error") - err = rc.InstantiateCCWithOpts("mychannel", instantiateReq, resmgmt.InstantiateCCOpts{Targets: peers}) + err = rc.InstantiateCC("mychannel", instantiateReq, resmgmt.WithTargets(peers...)) if err == nil { t.Fatalf("Should have failed to instantiate cc due to endorser error") } upgradeRequest := resmgmt.UpgradeCCRequest{Name: "name", Version: "version", Path: "path", Policy: ccPolicy} - err = rc.UpgradeCCWithOpts("mychannel", upgradeRequest, resmgmt.UpgradeCCOpts{Targets: peers}) + err = rc.UpgradeCC("mychannel", upgradeRequest, resmgmt.WithTargets(peers...)) if err == nil { t.Fatalf("Should have failed to upgrade cc due to endorser error") } @@ -1017,7 +1012,7 @@ func TestCCProposal(t *testing.T) { } // Test invalid function (only 'instatiate' and 'upgrade' are supported) - err = rc.sendCCProposalWithOpts(3, "mychannel", instantiateReq, resmgmt.InstantiateCCOpts{Targets: peers}) + err = rc.sendCCProposal(3, "mychannel", instantiateReq, resmgmt.WithTargets(peers...)) if err == nil { t.Fatalf("Should have failed for invalid function name") }