Skip to content

Commit

Permalink
Add common CLI function to get a server admin client
Browse files Browse the repository at this point in the history
Change-Id: Idbe7cdcd56081f7d468a8f95c2335a4fb1d0efa9
Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
  • Loading branch information
wlahti committed Dec 5, 2016
1 parent 2dc82e0 commit 9baa4eb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 33 deletions.
18 changes: 5 additions & 13 deletions peer/clilogging/getlevel.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ limitations under the License.
package clilogging

import (
"fmt"

"github.com/hyperledger/fabric/core/errors"
"github.com/hyperledger/fabric/core/peer"
"github.com/hyperledger/fabric/peer/common"
pb "github.com/hyperledger/fabric/protos/peer"

"github.com/spf13/cobra"
"golang.org/x/net/context"
)
Expand All @@ -45,19 +43,13 @@ func getLevel(cmd *cobra.Command, args []string) (err error) {
if err != nil {
logger.Warningf("Error: %s", err)
} else {
clientConn, err := peer.NewPeerClientConnection()
adminClient, err := common.GetAdminClient()
if err != nil {
//logger.Infof("Error trying to connect to local peer: %s", err)
err = errors.ErrorWithCallstack(errors.Peer, errors.PeerConnectionError, err.Error())
logger.Infof("%s", err)
//err = fmt.Errorf("Error trying to connect to local peer: %s", err)
fmt.Println(&pb.ServerStatus{Status: pb.ServerStatus_UNKNOWN})
logger.Warningf("%s", err)
return err
}

serverClient := pb.NewAdminClient(clientConn)

logResponse, err := serverClient.GetModuleLogLevel(context.Background(), &pb.LogLevelRequest{LogModule: args[0]})
logResponse, err := adminClient.GetModuleLogLevel(context.Background(), &pb.LogLevelRequest{LogModule: args[0]})

if err != nil {
logger.Warningf("Error retrieving log level")
Expand Down
14 changes: 4 additions & 10 deletions peer/clilogging/setlevel.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ limitations under the License.
package clilogging

import (
"fmt"

"golang.org/x/net/context"

"github.com/hyperledger/fabric/core/errors"
"github.com/hyperledger/fabric/core/peer"
"github.com/hyperledger/fabric/peer/common"
pb "github.com/hyperledger/fabric/protos/peer"

"github.com/spf13/cobra"
)

Expand All @@ -46,17 +44,13 @@ func setLevel(cmd *cobra.Command, args []string) (err error) {
if err != nil {
logger.Warningf("Error: %s", err)
} else {
clientConn, err := peer.NewPeerClientConnection()
adminClient, err := common.GetAdminClient()
if err != nil {
err = errors.ErrorWithCallstack(errors.Peer, errors.PeerConnectionError, err.Error())
logger.Warningf("%s", err)
fmt.Println(&pb.ServerStatus{Status: pb.ServerStatus_UNKNOWN})
return err
}

serverClient := pb.NewAdminClient(clientConn)

logResponse, err := serverClient.SetModuleLogLevel(context.Background(), &pb.LogLevelRequest{LogModule: args[0], LogLevel: args[1]})
logResponse, err := adminClient.SetModuleLogLevel(context.Background(), &pb.LogLevelRequest{LogModule: args[0], LogLevel: args[1]})

if err != nil {
logger.Warningf("%s", err)
Expand Down
17 changes: 14 additions & 3 deletions peer/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ limitations under the License.
package common

import (
"fmt"

"github.com/hyperledger/fabric/core/errors"
"github.com/hyperledger/fabric/core/peer"
"github.com/hyperledger/fabric/flogging"
pb "github.com/hyperledger/fabric/protos/peer"
Expand All @@ -33,12 +32,24 @@ const UndefinedParamValue = ""
func GetEndorserClient(cmd *cobra.Command) (pb.EndorserClient, error) {
clientConn, err := peer.NewPeerClientConnection()
if err != nil {
return nil, fmt.Errorf("Error trying to connect to local peer: %s", err)
err = errors.ErrorWithCallstack(errors.Peer, errors.PeerConnectionError, err.Error())
return nil, err
}
endorserClient := pb.NewEndorserClient(clientConn)
return endorserClient, nil
}

// GetAdminClient returns a new admin client connection for this peer
func GetAdminClient() (pb.AdminClient, error) {
clientConn, err := peer.NewPeerClientConnection()
if err != nil {
err = errors.ErrorWithCallstack(errors.Peer, errors.PeerConnectionError, err.Error())
return nil, err
}
adminClient := pb.NewAdminClient(clientConn)
return adminClient, nil
}

// SetErrorLoggingLevel sets the 'error' module's logger to the value in
// core.yaml
func SetErrorLoggingLevel() error {
Expand Down
12 changes: 5 additions & 7 deletions peer/node/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"fmt"

"github.com/golang/protobuf/ptypes/empty"
"github.com/hyperledger/fabric/core/peer"
"github.com/hyperledger/fabric/peer/common"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/spf13/cobra"
"golang.org/x/net/context"
Expand All @@ -40,17 +40,15 @@ var nodeStatusCmd = &cobra.Command{
}

func status() (err error) {
clientConn, err := peer.NewPeerClientConnection()

adminClient, err := common.GetAdminClient()
if err != nil {
logger.Infof("Error trying to connect to local peer: %s", err)
err = fmt.Errorf("Error trying to connect to local peer: %s", err)
logger.Warningf("%s", err)
fmt.Println(&pb.ServerStatus{Status: pb.ServerStatus_UNKNOWN})
return err
}

serverClient := pb.NewAdminClient(clientConn)

status, err := serverClient.GetStatus(context.Background(), &empty.Empty{})
status, err := adminClient.GetStatus(context.Background(), &empty.Empty{})
if err != nil {
logger.Infof("Error trying to get status from local peer: %s", err)
err = fmt.Errorf("Error trying to connect to local peer: %s", err)
Expand Down

0 comments on commit 9baa4eb

Please sign in to comment.