Skip to content

Commit

Permalink
[FAB-9929] refactor pkg/fab/resource/api
Browse files Browse the repository at this point in the history
- move pkg/fab/resource/api to  pkg/fab/resource
- just refactoring to move packages around,
no logic updates.



Change-Id: I7ccf85267b0ad1b27796322bcd9336521edd3a56
Signed-off-by: Sudesh Shetty <[email protected]>
  • Loading branch information
sudeshrshetty committed May 7, 2018
1 parent 914555e commit db6ac7b
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 40 deletions.
6 changes: 3 additions & 3 deletions pkg/client/resmgmt/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
sdkCtx "github.com/hyperledger/fabric-sdk-go/pkg/context"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource/api"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource"
mspmocks "github.com/hyperledger/fabric-sdk-go/pkg/msp/test/mockmsp"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
Expand Down Expand Up @@ -50,7 +50,7 @@ func Example() {
}

// Install example chaincode to peer
installReq := InstallCCRequest{Name: "ExampleCC", Version: "v0", Path: "path", Package: &api.CCPackage{Type: 1, Code: []byte("bytes")}}
installReq := InstallCCRequest{Name: "ExampleCC", Version: "v0", Path: "path", Package: &resource.CCPackage{Type: 1, Code: []byte("bytes")}}
_, err = c.InstallCC(installReq, WithTargets(peer))
if err != nil {
fmt.Printf("failed to install chaincode: %v", err)
Expand Down Expand Up @@ -263,7 +263,7 @@ func ExampleClient_InstallCC() {
fmt.Println("failed to create client")
}

req := InstallCCRequest{Name: "ExampleCC", Version: "v0", Path: "path", Package: &api.CCPackage{Type: 1, Code: []byte("bytes")}}
req := InstallCCRequest{Name: "ExampleCC", Version: "v0", Path: "path", Package: &resource.CCPackage{Type: 1, Code: []byte("bytes")}}
responses, err := c.InstallCC(req, WithTargets(mockPeer()))
if err != nil {
fmt.Printf("failed to install chaincode: %v", err)
Expand Down
9 changes: 4 additions & 5 deletions pkg/client/resmgmt/resmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import (
contextImpl "github.com/hyperledger/fabric-sdk-go/pkg/context"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/peer"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource/api"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/txn"
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
"github.com/pkg/errors"
Expand All @@ -54,7 +53,7 @@ type InstallCCRequest struct {
Name string
Path string
Version string
Package *api.CCPackage
Package *resource.CCPackage
}

// InstallCCResponse contains install chaincode response status
Expand Down Expand Up @@ -235,7 +234,7 @@ func (rc *Client) JoinChannel(channelID string, options ...RequestOption) error
return errors.WithMessage(err, "genesis block retrieval failed")
}

joinChannelRequest := api.JoinChannelRequest{
joinChannelRequest := resource.JoinChannelRequest{
GenesisBlock: genesisBlock,
}

Expand Down Expand Up @@ -421,7 +420,7 @@ func (rc *Client) InstallCC(req InstallCCRequest, options ...RequestOption) ([]I
}

func (rc *Client) sendIntallCCRequest(req InstallCCRequest, reqCtx reqContext.Context, newTargets []fab.Peer, responses []InstallCCResponse) []InstallCCResponse {
icr := api.InstallChaincodeRequest{Name: req.Name, Path: req.Path, Version: req.Version, Package: req.Package}
icr := resource.InstallChaincodeRequest{Name: req.Name, Path: req.Path, Version: req.Version, Package: req.Package}
transactionProposalResponse, _, _ := resource.InstallChaincode(reqCtx, icr, peer.PeersToTxnProcessors(newTargets))
for _, v := range transactionProposalResponse {
logger.Debugf("Install chaincode '%s' endorser '%s' returned ProposalResponse status:%v", req.Name, v.Endorser, v.Status)
Expand Down Expand Up @@ -873,7 +872,7 @@ func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption)
return SaveChannelResponse{}, err
}

request := api.CreateChannelRequest{
request := resource.CreateChannelRequest{
Name: req.ChannelID,
Orderer: orderer,
Config: chConfig,
Expand Down
16 changes: 8 additions & 8 deletions pkg/client/resmgmt/resmgmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
fabImpl "github.com/hyperledger/fabric-sdk-go/pkg/fab"
fcmocks "github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/peer"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource/api"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/provider/fabpvdr"
mspmocks "github.com/hyperledger/fabric-sdk-go/pkg/msp/test/mockmsp"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/common/cauthdsl"
Expand Down Expand Up @@ -523,7 +523,7 @@ func TestInstallCCWithOpts(t *testing.T) {
Status: http.StatusOK, MockRoles: []string{}, MockCert: nil, MockMSP: "Org1MSP", Payload: responseBytes}

// Already installed chaincode request
req := InstallCCRequest{Name: "name", Version: "version", Path: "path", Package: &api.CCPackage{Type: 1, Code: []byte("code")}}
req := InstallCCRequest{Name: "name", Version: "version", Path: "path", Package: &resource.CCPackage{Type: 1, Code: []byte("code")}}
responses, err := rc.InstallCC(req, WithTargets(&peer1))
if err != nil {
t.Fatal(err)
Expand All @@ -542,7 +542,7 @@ func TestInstallCCWithOpts(t *testing.T) {
}

// Chaincode not found request (it will be installed)
req = InstallCCRequest{Name: "ID", Version: "v0", Path: "path", Package: &api.CCPackage{Type: 1, Code: []byte("code")}}
req = InstallCCRequest{Name: "ID", Version: "v0", Path: "path", Package: &resource.CCPackage{Type: 1, Code: []byte("code")}}
responses, err = rc.InstallCC(req, WithTargets(&peer1))
if err != nil {
t.Fatal(err)
Expand All @@ -566,7 +566,7 @@ func TestInstallCCWithOptsError(t *testing.T) {
peer1 := fcmocks.MockPeer{MockName: "Peer1", MockURL: "http://peer1.com",
Status: http.StatusOK, MockRoles: []string{}, MockCert: nil, MockMSP: "Org1MSP"}

req := InstallCCRequest{Name: "error", Version: "v0", Path: "path", Package: &api.CCPackage{Type: 1, Code: []byte("code")}}
req := InstallCCRequest{Name: "error", Version: "v0", Path: "path", Package: &resource.CCPackage{Type: 1, Code: []byte("code")}}

// Test both targets and filter provided (error condition)
_, err := rc.InstallCC(req, WithTargets(&peer1), WithTargetFilter(&mspFilter{mspID: "Org1MSP"}))
Expand All @@ -585,7 +585,7 @@ func TestInstallError(t *testing.T) {
peer2 := fcmocks.MockPeer{MockName: "Peer1", MockURL: "http://peer1.com",
Status: http.StatusOK, MockRoles: []string{}, MockCert: nil, MockMSP: "Org1MSP"}

req := InstallCCRequest{Name: "ID", Version: "v0", Path: "path", Package: &api.CCPackage{Type: 1, Code: []byte("code")}}
req := InstallCCRequest{Name: "ID", Version: "v0", Path: "path", Package: &resource.CCPackage{Type: 1, Code: []byte("code")}}
_, err := rc.InstallCC(req, WithTargets(&peer1, &peer2))
assert.NotNil(t, err)
assert.Contains(t, err.Error(), testErr.Error())
Expand All @@ -598,7 +598,7 @@ func TestInstallCC(t *testing.T) {
Status: http.StatusOK, MockRoles: []string{}, MockCert: nil, MockMSP: "Org1MSP"}

// Chaincode that is not installed already (it will be installed)
req := InstallCCRequest{Name: "ID", Version: "v0", Path: "path", Package: &api.CCPackage{Type: 1, Code: []byte("code")}}
req := InstallCCRequest{Name: "ID", Version: "v0", Path: "path", Package: &resource.CCPackage{Type: 1, Code: []byte("code")}}
responses, err := rc.InstallCC(req, WithTargets(&peer2))
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -665,7 +665,7 @@ func TestInstallCCWithDifferentMSP(t *testing.T) {
rc := setupResMgmtClient(t, ctx)

// Valid request
req := InstallCCRequest{Name: "name", Version: "version", Path: "path", Package: &api.CCPackage{Type: 1, Code: []byte("code")}}
req := InstallCCRequest{Name: "name", Version: "version", Path: "path", Package: &resource.CCPackage{Type: 1, Code: []byte("code")}}

// No targets and no filter -- default filter msp doesn't match discovery service peer msp
_, err := rc.InstallCC(req)
Expand Down Expand Up @@ -713,7 +713,7 @@ func TestInstallCCDiscoveryError(t *testing.T) {
rc := setupResMgmtClientWithDiscoveryError(t, ctx, errors.New("Test Error"))

// Test InstallCC discovery service error
req := InstallCCRequest{Name: "ID", Version: "v0", Path: "path", Package: &api.CCPackage{Type: 1, Code: []byte("code")}}
req := InstallCCRequest{Name: "ID", Version: "v0", Path: "path", Package: &resource.CCPackage{Type: 1, Code: []byte("code")}}

// Test InstallCC discovery service error
// if targets are not provided discovery service is used
Expand Down
6 changes: 3 additions & 3 deletions pkg/fab/ccpackager/gopackager/packager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"time"

"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource/api"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource"
"github.com/pkg/errors"

"fmt"
Expand All @@ -42,7 +42,7 @@ var keep = []string{".c", ".h", ".s", ".go", ".yaml", ".json"}
var logger = logging.NewLogger("fabsdk/fab")

// NewCCPackage creates new go lang chaincode package
func NewCCPackage(chaincodePath string, goPath string) (*api.CCPackage, error) {
func NewCCPackage(chaincodePath string, goPath string) (*resource.CCPackage, error) {

if chaincodePath == "" {
return nil, errors.New("chaincode path must be provided")
Expand Down Expand Up @@ -75,7 +75,7 @@ func NewCCPackage(chaincodePath string, goPath string) (*api.CCPackage, error) {
return nil, err
}

ccPkg := &api.CCPackage{Type: pb.ChaincodeSpec_GOLANG, Code: tarBytes}
ccPkg := &resource.CCPackage{Type: pb.ChaincodeSpec_GOLANG, Code: tarBytes}

return ccPkg, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/fab/resource/api/api.go → pkg/fab/resource/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package api
package resource

import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
Expand Down
11 changes: 5 additions & 6 deletions pkg/fab/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
contextImpl "github.com/hyperledger/fabric-sdk-go/pkg/context"
ccomm "github.com/hyperledger/fabric-sdk-go/pkg/core/config/comm"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource/api"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/txn"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
Expand Down Expand Up @@ -77,7 +76,7 @@ func SignChannelConfig(ctx context.Client, config []byte, signer msp.SigningIden
}

// CreateChannel calls the orderer to start building the new channel.
func CreateChannel(reqCtx reqContext.Context, request api.CreateChannelRequest, opts ...Opt) (fab.TransactionID, error) {
func CreateChannel(reqCtx reqContext.Context, request CreateChannelRequest, opts ...Opt) (fab.TransactionID, error) {
if request.Orderer == nil {
return fab.EmptyTransactionID, errors.New("missing orderer request parameter for the initialize channel")
}
Expand Down Expand Up @@ -118,7 +117,7 @@ func CreateChannel(reqCtx reqContext.Context, request api.CreateChannelRequest,
}

// TODO: this function was extracted from createOrUpdateChannel, but needs a closer examination.
func createChannelFromEnvelope(reqCtx reqContext.Context, request api.CreateChannelRequest) (fab.TransactionID, error) {
func createChannelFromEnvelope(reqCtx reqContext.Context, request CreateChannelRequest) (fab.TransactionID, error) {
env, err := extractSignedEnvelope(request.Envelope)
if err != nil {
return fab.EmptyTransactionID, errors.WithMessage(err, "signed envelope not valid")
Expand Down Expand Up @@ -176,7 +175,7 @@ func LastConfigFromOrderer(reqCtx reqContext.Context, channelName string, ordere
// JoinChannel sends a join channel proposal to the target peer.
//
// TODO extract targets from request into parameter.
func JoinChannel(reqCtx reqContext.Context, request api.JoinChannelRequest, targets []fab.ProposalProcessor, opts ...Opt) error {
func JoinChannel(reqCtx reqContext.Context, request JoinChannelRequest, targets []fab.ProposalProcessor, opts ...Opt) error {

if request.GenesisBlock == nil {
return errors.New("missing block input parameter with the required genesis block")
Expand Down Expand Up @@ -226,7 +225,7 @@ func extractSignedEnvelope(reqEnvelope []byte) (*fab.SignedEnvelope, error) {
}

// createOrUpdateChannel creates a new channel or updates an existing channel.
func createOrUpdateChannel(reqCtx reqContext.Context, txh *txn.TransactionHeader, request api.CreateChannelRequest) error {
func createOrUpdateChannel(reqCtx reqContext.Context, txh *txn.TransactionHeader, request CreateChannelRequest) error {

configUpdateEnvelope := &common.ConfigUpdateEnvelope{
ConfigUpdate: request.Config,
Expand Down Expand Up @@ -311,7 +310,7 @@ func QueryInstalledChaincodes(reqCtx reqContext.Context, peer fab.ProposalProces
}

// InstallChaincode sends an install proposal to one or more endorsing peers.
func InstallChaincode(reqCtx reqContext.Context, req api.InstallChaincodeRequest, targets []fab.ProposalProcessor, opts ...Opt) ([]*fab.TransactionProposalResponse, fab.TransactionID, error) {
func InstallChaincode(reqCtx reqContext.Context, req InstallChaincodeRequest, targets []fab.ProposalProcessor, opts ...Opt) ([]*fab.TransactionProposalResponse, fab.TransactionID, error) {

if req.Name == "" {
return nil, fab.EmptyTransactionID, errors.New("chaincode name required")
Expand Down
15 changes: 7 additions & 8 deletions pkg/fab/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
contextImpl "github.com/hyperledger/fabric-sdk-go/pkg/context"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/peer"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource/api"
mspmocks "github.com/hyperledger/fabric-sdk-go/pkg/msp/test/mockmsp"
"github.com/hyperledger/fabric-sdk-go/test/metadata"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
Expand Down Expand Up @@ -66,7 +65,7 @@ func TestCreateChannel(t *testing.T) {
// Create channel without envelope
reqCtx, cancel := contextImpl.NewRequest(ctx, contextImpl.WithTimeout(10*time.Second))
defer cancel()
_, err = CreateChannel(reqCtx, api.CreateChannelRequest{
_, err = CreateChannel(reqCtx, CreateChannelRequest{
Orderer: orderer,
Name: "mychannel",
})
Expand All @@ -75,7 +74,7 @@ func TestCreateChannel(t *testing.T) {
}

// Create channel without orderer
_, err = CreateChannel(reqCtx, api.CreateChannelRequest{
_, err = CreateChannel(reqCtx, CreateChannelRequest{
Envelope: configTx,
Name: "mychannel",
})
Expand All @@ -84,7 +83,7 @@ func TestCreateChannel(t *testing.T) {
}

// Create channel without name
_, err = CreateChannel(reqCtx, api.CreateChannelRequest{
_, err = CreateChannel(reqCtx, CreateChannelRequest{
Envelope: configTx,
Orderer: orderer,
})
Expand All @@ -93,7 +92,7 @@ func TestCreateChannel(t *testing.T) {
}

// Test with valid cofiguration
request := api.CreateChannelRequest{
request := CreateChannelRequest{
Envelope: configTx,
Orderer: orderer,
Name: "mychannel",
Expand Down Expand Up @@ -124,7 +123,7 @@ func TestJoinChannel(t *testing.T) {

genesisBlock := mocks.NewSimpleMockBlock()

request := api.JoinChannelRequest{}
request := JoinChannelRequest{}
reqCtx, cancel := contextImpl.NewRequest(ctx, contextImpl.WithTimeout(10*time.Second))
defer cancel()
err := JoinChannel(reqCtx, request, peers)
Expand All @@ -133,7 +132,7 @@ func TestJoinChannel(t *testing.T) {
}

// Test join channel with valid arguments
request = api.JoinChannelRequest{
request = JoinChannelRequest{
GenesisBlock: genesisBlock,
}
err = JoinChannel(reqCtx, request, peers)
Expand All @@ -143,7 +142,7 @@ func TestJoinChannel(t *testing.T) {

// Test failed proposal error handling
endorserServer.ProposalError = errors.New("Test Error")
request = api.JoinChannelRequest{}
request = JoinChannelRequest{}
err = JoinChannel(reqCtx, request, peers)
if err == nil {
t.Fatalf("Expected error")
Expand Down
5 changes: 2 additions & 3 deletions test/integration/fab/install_chaincode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/context"
packager "github.com/hyperledger/fabric-sdk-go/pkg/fab/ccpackager/gopackager"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource/api"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/hyperledger/fabric-sdk-go/test/integration"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
Expand Down Expand Up @@ -151,9 +150,9 @@ func testChaincodeInstallUsingChaincodePackage(t *testing.T, sdk *fabsdk.FabricS
}

// installCC use low level client to install chaincode
func installCC(t *testing.T, reqCtx reqContext.Context, name string, path string, version string, ccPackage *api.CCPackage, targets []fab.ProposalProcessor) error {
func installCC(t *testing.T, reqCtx reqContext.Context, name string, path string, version string, ccPackage *resource.CCPackage, targets []fab.ProposalProcessor) error {

icr := api.InstallChaincodeRequest{Name: name, Path: path, Version: version, Package: ccPackage}
icr := resource.InstallChaincodeRequest{Name: name, Path: path, Version: version, Package: ccPackage}

r, _, err := resource.InstallChaincode(reqCtx, icr, targets, resource.WithRetry(retry.DefaultResMgmtOpts))
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions test/integration/orgs/multiple_orgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
selection "github.com/hyperledger/fabric-sdk-go/pkg/client/common/selection/dynamicselection"

"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource/api"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/common/cauthdsl"
)

Expand Down Expand Up @@ -241,7 +241,7 @@ func testCCPolicy(chClientOrg2User *channel.Client, t *testing.T) {
}
}

func upgradeCC(ccPkg *api.CCPackage, org1ResMgmt *resmgmt.Client, t *testing.T, org2ResMgmt *resmgmt.Client) {
func upgradeCC(ccPkg *resource.CCPackage, org1ResMgmt *resmgmt.Client, t *testing.T, org2ResMgmt *resmgmt.Client) {
installCCReq := resmgmt.InstallCCRequest{Name: "exampleCC", Path: "github.com/example_cc", Version: "1", Package: ccPkg}
// Install example cc version '1' to Org1 peers
_, err := org1ResMgmt.InstallCC(installCCReq, resmgmt.WithRetry(retry.DefaultResMgmtOpts))
Expand Down Expand Up @@ -341,7 +341,7 @@ func verifyErrorFromCC(chClientOrg1User *channel.Client, t *testing.T) {
}
}

func createCC(t *testing.T, org1ResMgmt *resmgmt.Client, org2ResMgmt *resmgmt.Client, ccPkg *api.CCPackage) {
func createCC(t *testing.T, org1ResMgmt *resmgmt.Client, org2ResMgmt *resmgmt.Client, ccPkg *resource.CCPackage) {
installCCReq := resmgmt.InstallCCRequest{Name: "exampleCC", Path: "github.com/example_cc", Version: "0", Package: ccPkg}
// Install example cc to Org1 peers
_, err := org1ResMgmt.InstallCC(installCCReq, resmgmt.WithRetry(retry.DefaultResMgmtOpts))
Expand Down

0 comments on commit db6ac7b

Please sign in to comment.