-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2995 from hashicorp/f-allocation-autocomplete
Allocation autocomplete, client api
- Loading branch information
Showing
13 changed files
with
471 additions
and
334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package contexts | ||
|
||
// Context defines the scope in which a search for Nomad object operates | ||
type Context string | ||
|
||
const ( | ||
Allocs Context = "allocs" | ||
Evals Context = "evals" | ||
Jobs Context = "jobs" | ||
Nodes Context = "nodes" | ||
All Context = "" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/hashicorp/nomad/api/contexts" | ||
) | ||
|
||
type Search struct { | ||
client *Client | ||
} | ||
|
||
// Search returns a handle on the Search endpoints | ||
func (c *Client) Search() *Search { | ||
return &Search{client: c} | ||
} | ||
|
||
// PrefixSearch returns a list of matches for a particular context and prefix. | ||
func (s *Search) PrefixSearch(prefix string, context contexts.Context) (*SearchResponse, error) { | ||
var resp SearchResponse | ||
req := &SearchRequest{Prefix: prefix, Context: context} | ||
|
||
_, err := s.client.write("/v1/search", req, &resp, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &resp, nil | ||
} | ||
|
||
type SearchRequest struct { | ||
Prefix string | ||
Context contexts.Context | ||
} | ||
|
||
type SearchResponse struct { | ||
Matches map[contexts.Context][]string | ||
Truncations map[contexts.Context]bool | ||
QueryMeta | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package api | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/nomad/api/contexts" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSearch_List(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.Search().PrefixSearch(prefix, contexts.Jobs) | ||
|
||
assert.Nil(err) | ||
|
||
jobMatches := resp.Matches[contexts.Jobs] | ||
assert.Equal(1, len(jobMatches)) | ||
assert.Equal(id, jobMatches[0]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package agent | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/hashicorp/nomad/nomad/structs" | ||
) | ||
|
||
// SearchRequest accepts a prefix and context and returns a list of matching | ||
// IDs for that context. | ||
func (s *HTTPServer) SearchRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { | ||
if req.Method == "POST" || req.Method == "PUT" { | ||
return s.newSearchRequest(resp, req) | ||
} | ||
return nil, CodedError(405, ErrInvalidMethod) | ||
} | ||
|
||
func (s *HTTPServer) newSearchRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { | ||
args := structs.SearchRequest{} | ||
|
||
if err := decodeBody(req, &args); err != nil { | ||
return nil, CodedError(400, err.Error()) | ||
} | ||
|
||
var out structs.SearchResponse | ||
if err := s.agent.RPC("Search.PrefixSearch", &args, &out); err != nil { | ||
return nil, err | ||
} | ||
|
||
setMeta(resp, &out.QueryMeta) | ||
return out, nil | ||
} |
Oops, something went wrong.