Skip to content

Commit

Permalink
Merge pull request #2995 from hashicorp/f-allocation-autocomplete
Browse files Browse the repository at this point in the history
Allocation autocomplete, client api
  • Loading branch information
chelseakomlo authored Aug 14, 2017
2 parents bd1154b + ffdea26 commit 49bf7f9
Show file tree
Hide file tree
Showing 13 changed files with 471 additions and 334 deletions.
12 changes: 12 additions & 0 deletions api/contexts/contexts.go
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 = ""
)
38 changes: 38 additions & 0 deletions api/search.go
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
}
30 changes: 30 additions & 0 deletions api/search_test.go
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])
}
2 changes: 1 addition & 1 deletion command/agent/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (s *HTTPServer) registerHandlers(enableDebug bool) {
s.mux.HandleFunc("/v1/evaluations", s.wrap(s.EvalsRequest))
s.mux.HandleFunc("/v1/evaluation/", s.wrap(s.EvalSpecificRequest))

s.mux.HandleFunc("/v1/resources/", s.wrap(s.ResourceListRequest))
s.mux.HandleFunc("/v1/search", s.wrap(s.SearchRequest))

s.mux.HandleFunc("/v1/deployments", s.wrap(s.DeploymentsRequest))
s.mux.HandleFunc("/v1/deployment/", s.wrap(s.DeploymentSpecificRequest))
Expand Down
32 changes: 0 additions & 32 deletions command/agent/resources_endpoint.go

This file was deleted.

32 changes: 32 additions & 0 deletions command/agent/search_endpoint.go
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
}
Loading

0 comments on commit 49bf7f9

Please sign in to comment.