command/keys: list keys working end-to-end

This commit is contained in:
Ryan Uber 2014-09-08 20:09:35 -07:00
parent f771f2ef92
commit 9b4707a329
4 changed files with 59 additions and 28 deletions

Binary file not shown.

View File

@ -111,10 +111,11 @@ type joinResponse struct {
Num int32 Num int32
} }
type keysResponse struct { type keyResponse struct {
Messages map[string]string Messages map[string]string
NumNodes int NumNodes int
NumResp int NumResp int
NumErr int
Keys map[string]int Keys map[string]int
} }
@ -625,27 +626,41 @@ func (i *AgentRPC) handleReload(client *rpcClient, seq uint64) error {
} }
func (i *AgentRPC) handleListKeysLAN(client *rpcClient, seq uint64) error { func (i *AgentRPC) handleListKeysLAN(client *rpcClient, seq uint64) error {
header := responseHeader{ queryResp, err := i.agent.ListKeysLAN()
Seq: seq,
Error: "",
}
resp, err := i.agent.ListKeysLAN()
if err != nil {
return err
}
return client.Send(&header, resp)
}
func (i *AgentRPC) handleListKeysWAN(client *rpcClient, seq uint64) error {
resp, err := i.agent.ListKeysWAN()
header := responseHeader{ header := responseHeader{
Seq: seq, Seq: seq,
Error: errToString(err), Error: errToString(err),
} }
return client.Send(&header, resp) resp := keyResponse{
Messages: queryResp.Messages,
Keys: queryResp.Keys,
NumResp: queryResp.NumResp,
NumErr: queryResp.NumErr,
NumNodes: queryResp.NumNodes,
}
return client.Send(&header, &resp)
}
func (i *AgentRPC) handleListKeysWAN(client *rpcClient, seq uint64) error {
queryResp, err := i.agent.ListKeysWAN()
header := responseHeader{
Seq: seq,
Error: errToString(err),
}
resp := keyResponse{
Messages: queryResp.Messages,
Keys: queryResp.Keys,
NumResp: queryResp.NumResp,
NumErr: queryResp.NumErr,
NumNodes: queryResp.NumNodes,
}
return client.Send(&header, &resp)
} }
// Used to convert an error to a string representation // Used to convert an error to a string representation

View File

@ -176,26 +176,26 @@ func (c *RPCClient) WANMembers() ([]Member, error) {
return resp.Members, err return resp.Members, err
} }
func (c *RPCClient) ListKeysLAN() (map[string]int, error) { func (c *RPCClient) ListKeysLAN() (map[string]int, int, map[string]string, error) {
header := requestHeader{ header := requestHeader{
Command: listKeysLANCommand, Command: listKeysLANCommand,
Seq: c.getSeq(), Seq: c.getSeq(),
} }
resp := make(map[string]int) resp := new(keyResponse)
err := c.genericRPC(&header, nil, &resp) err := c.genericRPC(&header, nil, resp)
return resp, err return resp.Keys, resp.NumNodes, resp.Messages, err
} }
func (c *RPCClient) ListKeysWAN() (map[string]int, error) { func (c *RPCClient) ListKeysWAN() (map[string]int, int, map[string]string, error) {
header := requestHeader{ header := requestHeader{
Command: listKeysWANCommand, Command: listKeysWANCommand,
Seq: c.getSeq(), Seq: c.getSeq(),
} }
resp := make(map[string]int) resp := new(keyResponse)
err := c.genericRPC(&header, nil, &resp) err := c.genericRPC(&header, nil, resp)
return resp, err return resp.Keys, resp.NumNodes, resp.Messages, err
} }
// Leave is used to trigger a graceful leave and shutdown // Leave is used to trigger a graceful leave and shutdown

View File

@ -6,6 +6,7 @@ import (
"strings" "strings"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
"github.com/ryanuber/columnize"
) )
// KeysCommand is a Command implementation that handles querying, installing, // KeysCommand is a Command implementation that handles querying, installing,
@ -51,20 +52,35 @@ func (c *KeysCommand) Run(args []string) int {
if listKeys { if listKeys {
var keys map[string]int var keys map[string]int
var numNodes int
var messages map[string]string
var err error var err error
var out []string
if wan { if wan {
keys, err = client.ListKeysWAN() keys, numNodes, messages, err = client.ListKeysWAN()
} else { } else {
keys, err = client.ListKeysLAN() keys, numNodes, messages, err = client.ListKeysLAN()
} }
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Error: %s", err)) for node, msg := range messages {
out = append(out, fmt.Sprintf("failed: %s | %s", node, msg))
}
c.Ui.Error(columnize.SimpleFormat(out))
c.Ui.Error("")
c.Ui.Error(fmt.Sprintf("Failed gathering member keys: %s", err))
return 1 return 1
} }
fmt.Println(keys) c.Ui.Info("Keys gathered, listing cluster keys...")
c.Ui.Output("")
for key, num := range keys {
out = append(out, fmt.Sprintf("%s | [%d/%d]", key, num, numNodes))
}
c.Ui.Output(columnize.SimpleFormat(out))
return 0 return 0
} }