Skip to content

Commit

Permalink
Merge pull request #3122 from hashicorp/b-even-uuids
Browse files Browse the repository at this point in the history
status commands handle uuid prefixes with hyphens
  • Loading branch information
dadgar committed Aug 29, 2017
2 parents 6fb08b8 + e3710a9 commit f5d80c6
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 38 deletions.
6 changes: 1 addition & 5 deletions command/alloc_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,8 @@ func (c *AllocStatusCommand) Run(args []string) int {
c.Ui.Error(fmt.Sprintf("Identifier must contain at least two characters."))
return 1
}
if len(allocID)%2 == 1 {
// Identifiers must be of even length, so we strip off the last byte
// to provide a consistent user experience.
allocID = allocID[:len(allocID)-1]
}

allocID = sanatizeUUIDPrefix(allocID)
allocs, _, err := client.Allocations().PrefixList(allocID)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying allocation: %v", err))
Expand Down
19 changes: 19 additions & 0 deletions command/alloc_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,25 @@ func TestAllocStatusCommand_Run(t *testing.T) {
t.Fatalf("expected to have 'Created At' but saw: %s", out)
}
ui.OutputWriter.Reset()

// Try the query with an even prefix that includes the hyphen
if code := cmd.Run([]string{"-address=" + url, allocId1[:13]}); code != 0 {
t.Fatalf("expected exit 0, got: %d", code)
}
out = ui.OutputWriter.String()
if !strings.Contains(out, "Created At") {
t.Fatalf("expected to have 'Created At' but saw: %s", out)
}
ui.OutputWriter.Reset()

if code := cmd.Run([]string{"-address=" + url, "-verbose", allocId1}); code != 0 {
t.Fatalf("expected exit 0, got: %d", code)
}
out = ui.OutputWriter.String()
if !strings.Contains(out, allocId1) {
t.Fatal("expected to find alloc id in output")
}
ui.OutputWriter.Reset()
}

func TestAllocStatusCommand_AutocompleteArgs(t *testing.T) {
Expand Down
6 changes: 1 addition & 5 deletions command/eval_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,8 @@ func (c *EvalStatusCommand) Run(args []string) int {
c.Ui.Error(fmt.Sprintf("Identifier must contain at least two characters."))
return 1
}
if len(evalID)%2 == 1 {
// Identifiers must be of even length, so we strip off the last byte
// to provide a consistent user experience.
evalID = evalID[:len(evalID)-1]
}

evalID = sanatizeUUIDPrefix(evalID)
evals, _, err := client.Evaluations().PrefixList(evalID)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying evaluation: %v", err))
Expand Down
6 changes: 1 addition & 5 deletions command/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,8 @@ func (f *FSCommand) Run(args []string) int {
f.Ui.Error(fmt.Sprintf("Alloc ID must contain at least two characters."))
return 1
}
if len(allocID)%2 == 1 {
// Identifiers must be of even length, so we strip off the last byte
// to provide a consistent user experience.
allocID = allocID[:len(allocID)-1]
}

allocID = sanatizeUUIDPrefix(allocID)
allocs, _, err := client.Allocations().PrefixList(allocID)
if err != nil {
f.Ui.Error(fmt.Sprintf("Error querying allocation: %v", err))
Expand Down
11 changes: 11 additions & 0 deletions command/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"strconv"
"strings"
"time"

gg "github.com/hashicorp/go-getter"
Expand Down Expand Up @@ -330,3 +331,13 @@ func mergeAutocompleteFlags(flags ...complete.Flags) complete.Flags {
}
return merged
}

// sanatizeUUIDPrefix is used to sanatize a UUID prefix. The returned result
// will be a truncated version of the prefix if the prefix would not be
// queriable.
func sanatizeUUIDPrefix(prefix string) string {
hyphens := strings.Count(prefix, "-")
length := len(prefix) - hyphens
remainder := length % 2
return prefix[:len(prefix)-remainder]
}
6 changes: 1 addition & 5 deletions command/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,8 @@ func (l *LogsCommand) Run(args []string) int {
l.Ui.Error(fmt.Sprintf("Alloc ID must contain at least two characters."))
return 1
}
if len(allocID)%2 == 1 {
// Identifiers must be of even length, so we strip off the last byte
// to provide a consistent user experience.
allocID = allocID[:len(allocID)-1]
}

allocID = sanatizeUUIDPrefix(allocID)
allocs, _, err := client.Allocations().PrefixList(allocID)
if err != nil {
l.Ui.Error(fmt.Sprintf("Error querying allocation: %v", err))
Expand Down
6 changes: 1 addition & 5 deletions command/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,8 @@ func (m *monitor) monitor(evalID string, allowPrefix bool) int {
m.ui.Error(fmt.Sprintf("Identifier must contain at least two characters."))
return 1
}
if len(evalID)%2 == 1 {
// Identifiers must be of even length, so we strip off the last byte
// to provide a consistent user experience.
evalID = evalID[:len(evalID)-1]
}

evalID = sanatizeUUIDPrefix(evalID)
evals, _, err := m.client.Evaluations().PrefixList(evalID)
if err != nil {
m.ui.Error(fmt.Sprintf("Error reading evaluation: %s", err))
Expand Down
2 changes: 1 addition & 1 deletion command/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func TestMonitor_MonitorWithPrefix(t *testing.T) {
doneCh := make(chan struct{})
go func() {
defer close(doneCh)
code = mon.monitor(resp.EvalID[:8], true)
code = mon.monitor(resp.EvalID[:13], true)
}()

// Wait for completion
Expand Down
6 changes: 1 addition & 5 deletions command/node_drain.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,8 @@ func (c *NodeDrainCommand) Run(args []string) int {
c.Ui.Error(fmt.Sprintf("Identifier must contain at least two characters."))
return 1
}
if len(nodeID)%2 == 1 {
// Identifiers must be of even length, so we strip off the last byte
// to provide a consistent user experience.
nodeID = nodeID[:len(nodeID)-1]
}

nodeID = sanatizeUUIDPrefix(nodeID)
nodes, _, err := client.Nodes().PrefixList(nodeID)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error toggling drain mode: %s", err))
Expand Down
6 changes: 1 addition & 5 deletions command/node_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,8 @@ func (c *NodeStatusCommand) Run(args []string) int {
c.Ui.Error(fmt.Sprintf("Identifier must contain at least two characters."))
return 1
}
if len(nodeID)%2 == 1 {
// Identifiers must be of even length, so we strip off the last byte
// to provide a consistent user experience.
nodeID = nodeID[:len(nodeID)-1]
}

nodeID = sanatizeUUIDPrefix(nodeID)
nodes, _, err := client.Nodes().PrefixList(nodeID)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying node info: %s", err))
Expand Down
4 changes: 2 additions & 2 deletions command/node_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ func TestNodeStatusCommand_Run(t *testing.T) {
t.Fatalf("should not dump allocations")
}

// Query a single node based on prefix
if code := cmd.Run([]string{"-address=" + url, nodeID[:4]}); code != 0 {
// Query a single node based on a prefix that is even without the hyphen
if code := cmd.Run([]string{"-address=" + url, nodeID[:13]}); code != 0 {
t.Fatalf("expected exit 0, got: %d", code)
}
out = ui.OutputWriter.String()
Expand Down

0 comments on commit f5d80c6

Please sign in to comment.