From 5bc17838f3e94891d3c227e1bbef1d697c9143bb Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 5 Jun 2018 16:15:59 -0700 Subject: [PATCH] api: support ExecuteConnect --- api/prepared_query.go | 31 +++++++++++++++++++++++++++++++ website/source/api/query.html.md | 16 +++++++++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/api/prepared_query.go b/api/prepared_query.go index d322dd867..4b4173d7f 100644 --- a/api/prepared_query.go +++ b/api/prepared_query.go @@ -54,6 +54,14 @@ type ServiceQuery struct { // pair is in this map it must be present on the node in order for the // service entry to be returned. NodeMeta map[string]string + + // Connect if true will filter the prepared query results to only + // include Connect-capable services. These include both native services + // and proxies for matching services. Note that if a proxy matches, + // the constraints in the query above (Near, OnlyPassing, etc.) apply + // to the _proxy_ and not the service being proxied. In practice, proxies + // should be directly next to their services so this isn't an issue. + Connect bool } // QueryTemplate carries the arguments for creating a templated query. @@ -202,3 +210,26 @@ func (c *PreparedQuery) Execute(queryIDOrName string, q *QueryOptions) (*Prepare } return out, qm, nil } + +// ExecuteConnect is used to execute a specific prepared query and return +// only Connect-capable nodes. +func (c *PreparedQuery) ExecuteConnect(queryIDOrName string, q *QueryOptions) (*PreparedQueryExecuteResponse, *QueryMeta, error) { + r := c.c.newRequest("GET", "/v1/query/"+queryIDOrName+"/execute") + r.setQueryOptions(q) + r.params.Set("connect", "true") + rtt, resp, err := requireOK(c.c.doRequest(r)) + if err != nil { + return nil, nil, err + } + defer resp.Body.Close() + + qm := &QueryMeta{} + parseQueryMeta(resp, qm) + qm.RequestTime = rtt + var out PreparedQueryExecuteResponse + if err := decodeBody(resp, &out); err != nil { + return nil, nil, err + } + + return &out, qm, nil +} diff --git a/website/source/api/query.html.md b/website/source/api/query.html.md index 1f42aabec..707c58397 100644 --- a/website/source/api/query.html.md +++ b/website/source/api/query.html.md @@ -208,15 +208,15 @@ The table below shows this endpoint's support for check filtering. If this is set to false, the results will include nodes with checks in the passing as well as the warning states. If this is set to true, only nodes with checks in the passing state will be returned. - + - `Near` `(string: "")` - Specifies a node to sort near based on distance sorting using [Network Coordinates](/docs/internals/coordinates.html). The nearest instance to the specified node will be returned first, and subsequent nodes in the response will be sorted in ascending order of estimated round-trip times. If the node given does not exist, the nodes in the response will be shuffled. If unspecified, the response will be shuffled by default. - - - `_agent` - Returns results nearest the agent servicing the request. + + - `_agent` - Returns results nearest the agent servicing the request. - `_ip` - Returns results nearest to the node associated with the source IP where the query was executed from. For HTTP the source IP is the remote peer's IP address or the value of the X-Forwarded-For header with the @@ -233,6 +233,11 @@ The table below shows this endpoint's support for key/value pairs that will be used for filtering the query results to nodes with the given metadata values present. + - `Connect` `(bool: false)` - If true, only [Connect-capable](/docs/connect/index.html) services + for the specified service name will be returned. This includes both + natively integrated services and proxies. For proxies, the proxy name + may not match `Service`, because the proxy destination will. + - `DNS` `(DNS: nil)` - Specifies DNS configuration - `TTL` `(string: "")` - Specifies the TTL duration when query results are @@ -497,6 +502,11 @@ Token will be used. - `limit` `(int: 0)` - Limit the size of the list to the given number of nodes. This is applied after any sorting or shuffling. +- `connect` `(bool: false)` - If true, limit results to nodes that are + Connect-capable only. This can also be specified directly on the template + itself to force all executions of a query to be Connect-only. See the + template documentation for more information. + ### Sample Request ```text