Search respects QueryOptions and returns QueryMeta
@chelseakomlo heads up on this change
This commit is contained in:
parent
7ce6c4f6f6
commit
d7d8661485
28
api/api.go
28
api/api.go
|
@ -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) {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue