Skip to content

Commit

Permalink
[FAB-7948] Removing *WithOpts functions in resgmtclient
Browse files Browse the repository at this point in the history
Change-Id: I1a10c4c961f3068b00c85afed97588a5045eb1ce
Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
  • Loading branch information
sudeshrshetty committed Jan 29, 2018
1 parent 4345a28 commit b0efb7e
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 188 deletions.
37 changes: 37 additions & 0 deletions api/apitxn/resmgmtclient/opts.go
Original file line number Diff line number Diff line change
@@ -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
}
}
60 changes: 16 additions & 44 deletions api/apitxn/resmgmtclient/resmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}
147 changes: 55 additions & 92 deletions pkg/fabric-txn/resmgmtclient/resmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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
}
Loading

0 comments on commit b0efb7e

Please sign in to comment.