Skip to content

Commit

Permalink
autocomplete for allocation ids
Browse files Browse the repository at this point in the history
  • Loading branch information
chelseakomlo committed Aug 9, 2017
1 parent d9091cc commit 3630cbc
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
28 changes: 28 additions & 0 deletions api/job_resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package api

import (
"github.com/hashicorp/nomad/nomad/structs"
)

type JobResources struct {
client *Client
}

// JobResources returns a handle on the JobResources endpoints
func (c *Client) JobResources() *JobResources {
return &JobResources{client: c}
}

// List returns a list of all resources for a particular context. If a
// context is not specified, matches for all contezts are returned.
func (j *JobResources) List(prefix string, context string) (*structs.ResourceListResponse, error) {
var resp structs.ResourceListResponse
req := &structs.ResourceListRequest{Prefix: prefix, Context: context}

_, err := j.client.write("/v1/resources/", req, &resp, nil)
if err != nil {
return nil, err
}

return &resp, nil
}
29 changes: 29 additions & 0 deletions api/job_resources_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package api

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestJobResource_PrefixList(t *testing.T) {
assert := assert.New(t)
t.Parallel()

c, s := makeClient(t, nil, nil)
defer s.Stop()

job := testJob()
_, _, err := c.Jobs().Register(job, nil)
assert.Nil(err)

id := *job.ID
prefix := id[:len(id)-2]
resp, err := c.JobResources().List(prefix, "jobs")

assert.Nil(err)
assert.NotEqual(0, resp.Index)

jobMatches := resp.Matches["jobs"]
assert.Equal(1, len(jobMatches))
assert.Equal(job.ID, jobMatches[0])
}
16 changes: 16 additions & 0 deletions command/alloc_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/client"
"github.com/posener/complete"
)

type AllocStatusCommand struct {
Expand Down Expand Up @@ -192,6 +193,21 @@ func (c *AllocStatusCommand) Run(args []string) int {
return 0
}

func (c *AllocStatusCommand) AutocompleteFlags() complete.Flags {
return nil
}

func (c *AllocStatusCommand) AutocompleteArgs() complete.Predictor {
client, _ := c.Meta.Client()
return complete.PredictFunc(func(a complete.Args) []string {
resp, err := client.JobResources().List(a.Last, "allocs")
if err != nil {
return []string{}
}
return resp.Matches["allocs"]
})
}

func formatAllocBasicInfo(alloc *api.Allocation, client *api.Client, uuidLength int, verbose bool) (string, error) {
basic := []string{
fmt.Sprintf("ID|%s", limit(alloc.ID, uuidLength)),
Expand Down
2 changes: 1 addition & 1 deletion nomad/resources_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func getResourceIter(context, prefix string, ws memdb.WatchSet, state *state.Sta
}

// If the length of a string is odd, return a subset of the string to the last
// even character (n-1)
// even character
func roundDownIfOdd(s string) string {
if len(s)%2 == 0 {
return s
Expand Down
2 changes: 1 addition & 1 deletion nomad/resources_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func TestResourcesEndpoint_List_RoundDownToEven(t *testing.T) {
testutil.WaitForLeader(t, s.RPC)

jobID := registerAndVerifyJob(s, t, id, 0)
jobID := registerAndVerifyJob(s, t, "bbafaaaa-e8f7-fd38-c855-ab94ceb89", 50)
registerAndVerifyJob(s, t, "bbafaaaa-e8f7-fd38-c855-ab94ceb89", 50)

req := &structs.ResourceListRequest{
Prefix: prefix,
Expand Down

0 comments on commit 3630cbc

Please sign in to comment.