Search respects QueryOptions and returns QueryMeta

@chelseakomlo heads up on this change
This commit is contained in:
Alex Dadgar 2017-08-27 22:17:51 -07:00
parent 7ce6c4f6f6
commit d7d8661485
23 changed files with 55 additions and 27 deletions

View File

@ -479,7 +479,7 @@ func (c *Client) rawQuery(endpoint string, q *QueryOptions) (io.ReadCloser, erro
return resp.Body, nil
}
// Query is used to do a GET request against an endpoint
// query is used to do a GET request against an endpoint
// and deserialize the response into an interface using
// standard Nomad conventions.
func (c *Client) query(endpoint string, out interface{}, q *QueryOptions) (*QueryMeta, error) {
@ -504,6 +504,32 @@ func (c *Client) query(endpoint string, out interface{}, q *QueryOptions) (*Quer
return qm, nil
}
// putQuery is used to do a PUT request when doing a read against an endpoint
// and deserialize the response into an interface using standard Nomad
// conventions.
func (c *Client) putQuery(endpoint string, in, out interface{}, q *QueryOptions) (*QueryMeta, error) {
r, err := c.newRequest("PUT", endpoint)
if err != nil {
return nil, err
}
r.setQueryOptions(q)
r.obj = in
rtt, resp, err := requireOK(c.doRequest(r))
if err != nil {
return nil, err
}
defer resp.Body.Close()
qm := &QueryMeta{}
parseQueryMeta(resp, qm)
qm.RequestTime = rtt
if err := decodeBody(resp, out); err != nil {
return nil, err
}
return qm, nil
}
// write is used to do a PUT request against an endpoint
// and serialize/deserialized using the standard Nomad conventions.
func (c *Client) write(endpoint string, in, out interface{}, q *WriteOptions) (*WriteMeta, error) {

View File

@ -14,21 +14,22 @@ func (c *Client) Search() *Search {
}
// PrefixSearch returns a list of matches for a particular context and prefix.
func (s *Search) PrefixSearch(prefix string, context contexts.Context) (*SearchResponse, error) {
func (s *Search) PrefixSearch(prefix string, context contexts.Context, q *QueryOptions) (*SearchResponse, *QueryMeta, error) {
var resp SearchResponse
req := &SearchRequest{Prefix: prefix, Context: context}
_, err := s.client.write("/v1/search", req, &resp, nil)
qm, err := s.client.putQuery("/v1/search", req, &resp, q)
if err != nil {
return nil, err
return nil, nil, err
}
return &resp, nil
return &resp, qm, nil
}
type SearchRequest struct {
Prefix string
Context contexts.Context
QueryOptions
}
type SearchResponse struct {

View File

@ -20,9 +20,10 @@ func TestSearch_List(t *testing.T) {
id := *job.ID
prefix := id[:len(id)-2]
resp, err := c.Search().PrefixSearch(prefix, contexts.Jobs)
resp, qm, err := c.Search().PrefixSearch(prefix, contexts.Jobs, nil)
assert.Nil(err)
assert.NotNil(qm)
jobMatches := resp.Matches[contexts.Jobs]
assert.Equal(1, len(jobMatches))

View File

@ -78,7 +78,7 @@ func (c *AllocStatusCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Allocs)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Allocs, nil)
if err != nil {
return []string{}
}

View File

@ -57,7 +57,7 @@ func (c *DeploymentFailCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Deployments)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Deployments, nil)
if err != nil {
return []string{}
}

View File

@ -49,7 +49,7 @@ func (c *DeploymentPauseCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Deployments)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Deployments, nil)
if err != nil {
return []string{}
}

View File

@ -67,7 +67,7 @@ func (c *DeploymentPromoteCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Deployments)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Deployments, nil)
if err != nil {
return []string{}
}

View File

@ -55,7 +55,7 @@ func (c *DeploymentResumeCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Deployments)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Deployments, nil)
if err != nil {
return []string{}
}

View File

@ -58,7 +58,7 @@ func (c *DeploymentStatusCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Deployments)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Deployments, nil)
if err != nil {
return []string{}
}

View File

@ -65,7 +65,7 @@ func (c *EvalStatusCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Evals)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Evals, nil)
if err != nil {
return []string{}
}

View File

@ -101,7 +101,7 @@ func (f *FSCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Allocs)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Allocs, nil)
if err != nil {
return []string{}
}

View File

@ -57,7 +57,7 @@ func (c *InspectCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Jobs)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Jobs, nil)
if err != nil {
return []string{}
}

View File

@ -60,7 +60,7 @@ func (c *JobDeploymentsCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Jobs)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Jobs, nil)
if err != nil {
return []string{}
}

View File

@ -72,7 +72,7 @@ func (c *JobDispatchCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Jobs)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Jobs, nil)
if err != nil {
return []string{}
}

View File

@ -72,7 +72,7 @@ func (c *JobHistoryCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Jobs)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Jobs, nil)
if err != nil {
return []string{}
}

View File

@ -68,7 +68,7 @@ func (c *JobPromoteCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Jobs)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Jobs, nil)
if err != nil {
return []string{}
}

View File

@ -55,7 +55,7 @@ func (c *JobRevertCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Jobs)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Jobs, nil)
if err != nil {
return []string{}
}

View File

@ -77,7 +77,7 @@ func (c *JobStatusCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Jobs)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Jobs, nil)
if err != nil {
return []string{}
}

View File

@ -82,7 +82,7 @@ func (l *LogsCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Allocs)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Allocs, nil)
if err != nil {
return []string{}
}

View File

@ -62,7 +62,7 @@ func (c *NodeDrainCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Nodes)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Nodes, nil)
if err != nil {
return []string{}
}

View File

@ -105,7 +105,7 @@ func (c *NodeStatusCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Nodes)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Nodes, nil)
if err != nil {
return []string{}
}

View File

@ -43,7 +43,7 @@ func (c *StatusCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.All)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.All, nil)
if err != nil {
return []string{}
}
@ -93,7 +93,7 @@ func (c *StatusCommand) Run(args []string) int {
id := args[len(args)-1]
// Query for the context associated with the id
res, err := client.Search().PrefixSearch(id, contexts.All)
res, _, err := client.Search().PrefixSearch(id, contexts.All, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying search with id: %q", err))
return 1

View File

@ -68,7 +68,7 @@ func (c *StopCommand) AutocompleteArgs() complete.Predictor {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Jobs)
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Jobs, nil)
if err != nil {
return []string{}
}