2017-04-04 16:33:22 +00:00
|
|
|
---
|
|
|
|
layout: api
|
|
|
|
page_title: Prepared Queries - HTTP API
|
2020-04-07 18:55:19 +00:00
|
|
|
description: The /query endpoints manage and execute prepared queries in Consul.
|
2017-04-04 16:33:22 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
# Prepared Query HTTP Endpoint
|
|
|
|
|
|
|
|
The `/query` endpoints create, update, destroy, and execute prepared queries.
|
|
|
|
|
|
|
|
Prepared queries allow you to register a complex service query and then execute
|
|
|
|
it later via its ID or name to get a set of healthy nodes that provide a given
|
|
|
|
service. This is particularly useful in combination with Consul's
|
2023-01-25 16:52:43 +00:00
|
|
|
[DNS Interface](/consul/docs/discovery/dns#prepared-query-lookups) as it allows for much richer queries than
|
2017-04-04 16:33:22 +00:00
|
|
|
would be possible given the limited entry points exposed by DNS.
|
|
|
|
|
2023-01-25 16:52:43 +00:00
|
|
|
Check the [Geo Failover tutorial](/consul/tutorials/developer-discovery/automate-geo-failover) for details and
|
2017-07-26 22:40:01 +00:00
|
|
|
examples for using prepared queries to implement geo failover for services.
|
|
|
|
|
2023-01-25 16:52:43 +00:00
|
|
|
Check the [prepared query rules](/consul/docs/security/acl/acl-rules#prepared-query-rules)
|
2019-03-07 20:10:40 +00:00
|
|
|
section of the agent ACL documentation for more details about how prepared
|
|
|
|
queries work with Consul's ACL system.
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
### Prepared Query Templates
|
|
|
|
|
|
|
|
Consul 0.6.4 and later support prepared query templates. These are created
|
2018-04-10 12:28:27 +00:00
|
|
|
similar to static queries, except with some additional fields and features.
|
2017-04-04 16:33:22 +00:00
|
|
|
Here is an example prepared query template:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"Template": {
|
|
|
|
"Type": "name_prefix_match",
|
2017-07-27 05:46:43 +00:00
|
|
|
"Regexp": "^geo-db-(.*?)-([^\\-]+?)$",
|
|
|
|
"RemoveEmptyTags": false
|
2017-04-04 16:33:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The `Template` structure configures a prepared query as a template instead of a
|
|
|
|
static query. It has two fields:
|
|
|
|
|
|
|
|
- `Type` is the query type, which must be `name_prefix_match`. This means that
|
|
|
|
the template will apply to any query lookup with a name whose prefix matches
|
|
|
|
the `Name` field of the template. In this example, any query for `geo-db` will
|
|
|
|
match this query. Query templates are resolved using a longest prefix match,
|
|
|
|
so it's possible to have high-level templates that are overridden for specific
|
|
|
|
services. Static queries are always resolved first, so they can also override
|
|
|
|
templates.
|
|
|
|
|
|
|
|
- `Regexp` is an optional regular expression which is used to extract fields
|
|
|
|
from the entire name, once this template is selected. In this example, the
|
|
|
|
regular expression takes the first item after the "-" as the database name and
|
|
|
|
everything else after as a tag. See the
|
|
|
|
[RE2](https://github.com/google/re2/wiki/Syntax) reference for syntax of this
|
|
|
|
regular expression.
|
|
|
|
|
2017-07-27 05:46:43 +00:00
|
|
|
- `RemoveEmptyTags` is optional, and if set to true, will cause the `Tags` list
|
|
|
|
inside the `Service` structure to be stripped of any empty strings. This defaults
|
|
|
|
to false, meaning that empty strings will remain in the list. This is useful
|
|
|
|
when interpolating into tags in a way where the tag is optional, and where
|
|
|
|
searching for an empty tag would yield no results from the query.
|
|
|
|
|
2017-04-04 16:33:22 +00:00
|
|
|
All other fields of the query have the same meanings as for a static query,
|
|
|
|
except that several interpolation variables are available to dynamically
|
|
|
|
populate the query before it is executed. All of the string fields inside the
|
|
|
|
`Service` structure are interpolated, with the following variables available:
|
|
|
|
|
|
|
|
- `${name.full}` has the entire name that was queried. For example, a DNS lookup
|
|
|
|
for `geo-db-customer-primary.query.consul` in the example above would set this
|
|
|
|
variable to `geo-db-customer-primary`.
|
|
|
|
|
|
|
|
- `${name.prefix}` has the prefix that matched. This would always be `geo-db`
|
|
|
|
for the example above.
|
|
|
|
|
|
|
|
- `${name.suffix}` has the suffix after the prefix. For example, a DNS lookup
|
|
|
|
for `geo-db-customer-primary.query.consul` in the example above would set this
|
|
|
|
variable to `-customer-primary`.
|
|
|
|
|
|
|
|
- `${match(N)}` returns the regular expression match at the given index N. The 0
|
|
|
|
index will have the entire match, and >0 will have the results of each match
|
|
|
|
group. For example, a DNS lookup for `geo-db-customer-primary.query.consul` in
|
|
|
|
the example above with a `Regexp` field set to `^geo-db-(.*?)-([^\-]+?)$`
|
|
|
|
would return `geo-db-customer-primary` for `${match(0)}`, `customer` for
|
|
|
|
`${match(1)}`, and `primary` for `${match(2)}`. If the regular expression
|
|
|
|
doesn't match, or an invalid index is given, then `${match(N)}` will return an
|
|
|
|
empty string.
|
|
|
|
|
2020-04-23 22:13:18 +00:00
|
|
|
- `${agent.segment}` <EnterpriseAlert inline /> - the network segment of the agent that
|
2022-09-15 16:03:12 +00:00
|
|
|
initiated the query. This variable can be used with the `NodeMeta` field to limit the results
|
2017-08-30 00:02:50 +00:00
|
|
|
of a query to service instances within its own network segment:
|
|
|
|
|
2020-04-06 20:27:35 +00:00
|
|
|
```json
|
|
|
|
{
|
|
|
|
"Name": "",
|
|
|
|
"Template": {
|
|
|
|
"Type": "name_prefix_match"
|
|
|
|
},
|
|
|
|
"Service": {
|
|
|
|
"Service": "${name.full}",
|
|
|
|
"NodeMeta": { "consul-network-segment": "${agent.segment}" }
|
2017-08-30 00:02:50 +00:00
|
|
|
}
|
2020-04-06 20:27:35 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-07-26 23:30:24 +00:00
|
|
|
This will map all names of the form `<service>.query.consul` over DNS to a query
|
2017-08-30 00:02:50 +00:00
|
|
|
that will select an instance of the service in the agent's own network segment.
|
|
|
|
|
2017-04-04 16:33:22 +00:00
|
|
|
Using templates, it is possible to apply prepared query behaviors to many
|
|
|
|
services with a single template. Here's an example template that matches any
|
|
|
|
query and applies a failover policy to it:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"Name": "",
|
|
|
|
"Template": {
|
|
|
|
"Type": "name_prefix_match"
|
|
|
|
},
|
|
|
|
"Service": {
|
|
|
|
"Service": "${name.full}",
|
|
|
|
"Failover": {
|
|
|
|
"NearestN": 3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
This will match any lookup for `*.query.consul` and will attempt to find the
|
|
|
|
service locally, and otherwise attempt to find that service in the next three
|
|
|
|
closest datacenters. If ACLs are enabled, a catch-all template like this with
|
|
|
|
an empty `Name` requires an ACL token that can write to any query prefix. Also,
|
|
|
|
only a single catch-all template can be registered at any time.
|
|
|
|
|
|
|
|
## Create Prepared Query
|
|
|
|
|
|
|
|
This endpoint creates a new prepared query and returns its ID if it is created
|
|
|
|
successfully.
|
|
|
|
|
2020-04-06 20:27:35 +00:00
|
|
|
| Method | Path | Produces |
|
|
|
|
| ------ | -------- | ------------------ |
|
|
|
|
| `POST` | `/query` | `application/json` |
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
The table below shows this endpoint's support for
|
2023-01-25 16:52:43 +00:00
|
|
|
[blocking queries](/consul/api-docs/features/blocking),
|
|
|
|
[consistency modes](/consul/api-docs/features/consistency),
|
|
|
|
[agent caching](/consul/api-docs/features/caching), and
|
|
|
|
[required ACLs](/consul/api-docs/api-structure#authentication).
|
2017-04-04 16:33:22 +00:00
|
|
|
|
2018-09-06 10:34:28 +00:00
|
|
|
| Blocking Queries | Consistency Modes | Agent Caching | ACL Required |
|
|
|
|
| ---------------- | ----------------- | ------------- | ------------- |
|
|
|
|
| `NO` | `none` | `none` | `query:write` |
|
2017-04-04 16:33:22 +00:00
|
|
|
|
2022-05-10 15:51:11 +00:00
|
|
|
### Query Parameters
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
- `dc` `(string: "")` - Specifies the datacenter to query. This will default to
|
2022-05-10 15:51:11 +00:00
|
|
|
the datacenter of the agent being queried.
|
|
|
|
|
|
|
|
### JSON Request Body Schema
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
- `Name` `(string: "")` - Specifies an optional friendly name that can be used
|
|
|
|
to execute a query instead of using its ID.
|
|
|
|
|
|
|
|
- `Session` `(string: "")` - Specifies the ID of an existing session. This
|
|
|
|
provides a way to automatically remove a prepared query when the given session
|
|
|
|
is invalidated. If not given the prepared query must be manually removed when
|
|
|
|
no longer needed.
|
|
|
|
|
|
|
|
- `Token` `(string: "")` - Specifies the ACL token to use each time the query is
|
|
|
|
executed. This allows queries to be executed by clients with lesser or even no
|
|
|
|
ACL Token, so this should be used with care. The token itself can only be seen
|
|
|
|
by clients with a management token. If the `Token` field is left blank or
|
|
|
|
omitted, the client's ACL Token will be used to determine if they have access
|
|
|
|
to the service being queried. If the client does not supply an ACL Token, the
|
|
|
|
anonymous token will be used.
|
|
|
|
|
|
|
|
- `Service` `(Service: <required>)` - Specifies the structure to define the query's behavior.
|
|
|
|
|
|
|
|
- `Service` `(string: <required>)` - Specifies the name of the service to
|
|
|
|
query.
|
2021-08-09 21:29:43 +00:00
|
|
|
|
2020-11-05 07:48:04 +00:00
|
|
|
- `Namespace` `(string: "")` <EnterpriseAlert inline /> - Specifies the Consul namespace
|
2021-08-09 21:29:43 +00:00
|
|
|
to query. If not provided the query will use Consul default namespace for resolution.
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
- `Failover` contains two fields, both of which are optional, and determine
|
|
|
|
what happens if no healthy nodes are available in the local datacenter when
|
|
|
|
the query is executed. It allows the use of nodes in other datacenters with
|
|
|
|
very little configuration.
|
|
|
|
|
2020-04-06 20:27:35 +00:00
|
|
|
- `NearestN` `(int: 0)` - Specifies that the query will be forwarded to up
|
|
|
|
to `NearestN` other datacenters based on their estimated network round
|
2023-01-25 16:52:43 +00:00
|
|
|
trip time using [Network Coordinates](/consul/docs/architecture/coordinates)
|
2020-04-06 20:27:35 +00:00
|
|
|
from the WAN gossip pool. The median round trip time from the server
|
|
|
|
handling the query to the servers in the remote datacenter is used to
|
|
|
|
determine the priority.
|
|
|
|
|
2022-08-12 15:06:16 +00:00
|
|
|
- `Datacenters` `(array<string>: nil)` - Specifies a fixed list of WAN federated
|
|
|
|
datacenters to forward the query to when there are no healthy nodes in the
|
2020-04-06 20:27:35 +00:00
|
|
|
local datacenter. Datacenters are queried in the order given in the
|
|
|
|
list. If this option is combined with `NearestN`, then the `NearestN`
|
|
|
|
queries will be performed first, followed by the list given by
|
|
|
|
`Datacenters`. A given datacenter will only be queried one time during a
|
|
|
|
failover, even if it is selected by both `NearestN` and is listed in
|
2022-08-12 15:06:16 +00:00
|
|
|
`Datacenters`. Use `Targets` to failover to cluster peers.
|
|
|
|
|
|
|
|
- `Targets` `(array<Target>: nil)` - Specifies a sequential list of remote
|
|
|
|
datacenters and cluster peers to failover to if there are no healthy
|
|
|
|
service instances in the local datacenter.
|
|
|
|
This option cannot be used with `NearestN` or `Datacenters`.
|
|
|
|
|
2023-01-25 16:52:43 +00:00
|
|
|
- `Peer` `(string: "")` - Specifies a [cluster peer](/consul/docs/connect/cluster-peering) to use for
|
2022-08-12 15:06:16 +00:00
|
|
|
failover.
|
|
|
|
|
|
|
|
- `Datacenter` `(string: "")` - Specifies a WAN federated datacenter to forward the
|
|
|
|
query to.
|
2017-04-04 16:33:22 +00:00
|
|
|
|
2018-04-10 12:28:27 +00:00
|
|
|
- `IgnoreCheckIDs` `(array<string>: nil)` - Specifies a list of check IDs that
|
|
|
|
should be ignored when filtering unhealthy instances. This is mostly useful
|
|
|
|
in an emergency or as a temporary measure when a health check is found to be
|
|
|
|
unreliable. Being able to ignore it in centrally-defined queries can be
|
2020-05-08 14:03:45 +00:00
|
|
|
simpler than deregistering the check as an interim solution until the check
|
2018-04-10 12:28:27 +00:00
|
|
|
can be fixed.
|
|
|
|
|
2017-04-04 16:33:22 +00:00
|
|
|
- `OnlyPassing` `(bool: false)` - Specifies the behavior of the query's health
|
|
|
|
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.
|
2018-06-05 23:15:59 +00:00
|
|
|
|
2018-05-23 10:32:09 +00:00
|
|
|
- `Near` `(string: "")` - Specifies a node to sort near based on distance
|
2023-01-25 16:52:43 +00:00
|
|
|
sorting using [Network Coordinates](/consul/docs/architecture/coordinates). The
|
2020-04-06 20:27:35 +00:00
|
|
|
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.
|
|
|
|
- `_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
|
|
|
|
header taking precedence. For DNS the source IP is the remote peer's IP
|
|
|
|
address or the value of the EDNS client IP with the EDNS client IP
|
|
|
|
taking precedence.
|
|
|
|
|
|
|
|
* `Tags` `(array<string>: nil)` - Specifies a list of service tags to filter
|
|
|
|
the query results. For a service to pass the tag filter it must have _all_
|
|
|
|
of the required tags, and _none_ of the excluded tags (prefixed with `!`).
|
|
|
|
|
|
|
|
* `NodeMeta` `(map<string|string>: nil)` - Specifies a list of user-defined
|
|
|
|
key/value pairs that will be used for filtering the query results to nodes
|
|
|
|
with the given metadata values present.
|
|
|
|
|
|
|
|
* `ServiceMeta` `(map<string|string>: nil)` - Specifies a list of user-defined
|
|
|
|
key/value pairs that will be used for filtering the query results to services
|
|
|
|
with the given metadata values present.
|
|
|
|
|
2023-01-25 16:52:43 +00:00
|
|
|
* `Connect` `(bool: false)` - If true, only [Connect-capable](/consul/docs/connect) services
|
2020-04-06 20:27:35 +00:00
|
|
|
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. Any
|
|
|
|
constrains beyond the service name such as `Near`, `Tags`, and `NodeMeta`
|
|
|
|
are applied to Connect-capable service.
|
|
|
|
|
|
|
|
* `DNS` `(DNS: nil)` - Specifies DNS configuration
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
- `TTL` `(string: "")` - Specifies the TTL duration when query results are
|
|
|
|
served over DNS. If this is specified, it will take precedence over any
|
|
|
|
Consul agent-specific configuration.
|
|
|
|
|
|
|
|
### Sample Payload
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"Name": "my-query",
|
|
|
|
"Session": "adf4238a-882b-9ddc-4a9d-5b6758e4159e",
|
|
|
|
"Token": "",
|
|
|
|
"Service": {
|
|
|
|
"Service": "redis",
|
|
|
|
"Failover": {
|
|
|
|
"NearestN": 3,
|
|
|
|
"Datacenters": ["dc1", "dc2"]
|
|
|
|
},
|
|
|
|
"Near": "node1",
|
|
|
|
"OnlyPassing": false,
|
|
|
|
"Tags": ["primary", "!experimental"],
|
2020-04-06 20:27:35 +00:00
|
|
|
"NodeMeta": { "instance_type": "m3.large" },
|
|
|
|
"ServiceMeta": { "environment": "production" }
|
2017-04-04 16:33:22 +00:00
|
|
|
},
|
|
|
|
"DNS": {
|
|
|
|
"TTL": "10s"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Sample Request
|
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
```shell-session
|
2017-04-04 16:33:22 +00:00
|
|
|
$ curl \
|
2017-04-27 08:29:03 +00:00
|
|
|
--request POST \
|
2017-04-04 16:33:22 +00:00
|
|
|
--data @payload.json \
|
2018-08-28 16:07:15 +00:00
|
|
|
http://127.0.0.1:8500/v1/query
|
2017-04-04 16:33:22 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### Sample Response
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"ID": "8f246b77-f3e1-ff88-5b48-8ec93abf3e05"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-07-17 07:35:13 +00:00
|
|
|
## List Prepared Queries
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
This endpoint returns a list of all prepared queries.
|
|
|
|
|
2022-03-19 20:32:38 +00:00
|
|
|
@include 'http_api_results_filtered_by_acls.mdx'
|
|
|
|
|
2020-04-06 20:27:35 +00:00
|
|
|
| Method | Path | Produces |
|
|
|
|
| ------ | -------- | ------------------ |
|
|
|
|
| `GET` | `/query` | `application/json` |
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
The table below shows this endpoint's support for
|
2023-01-25 16:52:43 +00:00
|
|
|
[blocking queries](/consul/api-docs/features/blocking),
|
|
|
|
[consistency modes](/consul/api-docs/features/consistency),
|
|
|
|
[agent caching](/consul/api-docs/features/caching), and
|
|
|
|
[required ACLs](/consul/api-docs/api-structure#authentication).
|
2017-04-04 16:33:22 +00:00
|
|
|
|
2018-09-06 10:34:28 +00:00
|
|
|
| Blocking Queries | Consistency Modes | Agent Caching | ACL Required |
|
|
|
|
| ---------------- | ----------------- | ------------- | ------------ |
|
2021-08-09 21:29:43 +00:00
|
|
|
| `NO` | `all` | `none` | `query:read` |
|
2017-04-04 16:33:22 +00:00
|
|
|
|
2022-05-10 15:51:11 +00:00
|
|
|
### Query Parameters
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
- `dc` `(string: "")` - Specifies the datacenter to query. This will default to
|
2022-05-10 15:51:11 +00:00
|
|
|
the datacenter of the agent being queried.
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
### Sample Request
|
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
```shell-session
|
2017-04-04 16:33:22 +00:00
|
|
|
$ curl \
|
2018-08-28 16:07:15 +00:00
|
|
|
http://127.0.0.1:8500/v1/query
|
2017-04-04 16:33:22 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### Sample Response
|
|
|
|
|
|
|
|
```json
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"ID": "8f246b77-f3e1-ff88-5b48-8ec93abf3e05",
|
|
|
|
"Name": "my-query",
|
|
|
|
"Session": "adf4238a-882b-9ddc-4a9d-5b6758e4159e",
|
|
|
|
"Token": "<hidden>",
|
|
|
|
"Service": {
|
|
|
|
"Service": "redis",
|
|
|
|
"Failover": {
|
|
|
|
"NearestN": 3,
|
|
|
|
"Datacenters": ["dc1", "dc2"]
|
|
|
|
},
|
|
|
|
"OnlyPassing": false,
|
|
|
|
"Tags": ["primary", "!experimental"],
|
2020-04-06 20:27:35 +00:00
|
|
|
"NodeMeta": { "instance_type": "m3.large" },
|
|
|
|
"ServiceMeta": { "environment": "production" }
|
2017-04-04 16:33:22 +00:00
|
|
|
},
|
|
|
|
"DNS": {
|
|
|
|
"TTL": "10s"
|
|
|
|
},
|
|
|
|
"RaftIndex": {
|
|
|
|
"CreateIndex": 23,
|
|
|
|
"ModifyIndex": 42
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
```
|
|
|
|
|
|
|
|
### Update Prepared Query
|
|
|
|
|
|
|
|
This endpoint updates an existing prepared query. If no query exists by the
|
|
|
|
given ID, an error is returned.
|
|
|
|
|
2020-04-06 20:27:35 +00:00
|
|
|
| Method | Path | Produces |
|
|
|
|
| ------ | -------------- | ------------------ |
|
|
|
|
| `PUT` | `/query/:uuid` | `application/json` |
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
The table below shows this endpoint's support for
|
2023-01-25 16:52:43 +00:00
|
|
|
[blocking queries](/consul/api-docs/features/blocking),
|
|
|
|
[consistency modes](/consul/api-docs/features/consistency),
|
|
|
|
[agent caching](/consul/api-docs/features/caching), and
|
|
|
|
[required ACLs](/consul/api-docs/api-structure#authentication).
|
2017-04-04 16:33:22 +00:00
|
|
|
|
2018-09-06 10:34:28 +00:00
|
|
|
| Blocking Queries | Consistency Modes | Agent Caching | ACL Required |
|
|
|
|
| ---------------- | ----------------- | ------------- | ------------- |
|
|
|
|
| `NO` | `none` | `none` | `query:write` |
|
2017-04-04 16:33:22 +00:00
|
|
|
|
2022-05-10 15:51:11 +00:00
|
|
|
### Path Parameters
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
- `uuid` `(string: <required>)` - Specifies the UUID of the query to update.
|
2022-05-10 15:51:11 +00:00
|
|
|
|
|
|
|
### Query Parameters
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
- `dc` `(string: "")` - Specifies the datacenter to query. This will default to
|
2022-05-10 15:51:11 +00:00
|
|
|
the datacenter of the agent being queried.
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
The body is the same as is used to create a prepared query. Please see above for
|
|
|
|
more information.
|
|
|
|
|
2017-09-21 19:13:47 +00:00
|
|
|
### Sample Request
|
2017-04-04 16:33:22 +00:00
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
```shell-session
|
2017-04-04 16:33:22 +00:00
|
|
|
$ curl \
|
|
|
|
--request PUT \
|
|
|
|
--data @payload.json \
|
2018-08-28 16:07:15 +00:00
|
|
|
http://127.0.0.1:8500/v1/query/8f246b77-f3e1-ff88-5b48-8ec93abf3e05
|
2017-04-04 16:33:22 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Read Prepared Query
|
|
|
|
|
|
|
|
This endpoint reads an existing prepared query. If no query exists by the
|
|
|
|
given ID, an error is returned.
|
|
|
|
|
2020-04-06 20:27:35 +00:00
|
|
|
| Method | Path | Produces |
|
|
|
|
| ------ | -------------- | ------------------ |
|
|
|
|
| `GET` | `/query/:uuid` | `application/json` |
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
The table below shows this endpoint's support for
|
2023-01-25 16:52:43 +00:00
|
|
|
[blocking queries](/consul/api-docs/features/blocking),
|
|
|
|
[consistency modes](/consul/api-docs/features/consistency),
|
|
|
|
[agent caching](/consul/api-docs/features/caching), and
|
|
|
|
[required ACLs](/consul/api-docs/api-structure#authentication).
|
2017-04-04 16:33:22 +00:00
|
|
|
|
2018-09-06 10:34:28 +00:00
|
|
|
| Blocking Queries | Consistency Modes | Agent Caching | ACL Required |
|
|
|
|
| ---------------- | ----------------- | ------------- | ------------ |
|
2021-08-09 21:29:43 +00:00
|
|
|
| `NO` | `all` | `none` | `query:read` |
|
2017-04-04 16:33:22 +00:00
|
|
|
|
2022-05-10 15:51:11 +00:00
|
|
|
### Path Parameters
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
- `uuid` `(string: <required>)` - Specifies the UUID of the query to read.
|
2022-05-10 15:51:11 +00:00
|
|
|
|
|
|
|
### Query Parameters
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
- `dc` `(string: "")` - Specifies the datacenter to query. This will default to
|
2022-05-10 15:51:11 +00:00
|
|
|
the datacenter of the agent being queried.
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
### Sample Request
|
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
```shell-session
|
2017-04-04 16:33:22 +00:00
|
|
|
$ curl \
|
2018-08-28 16:07:15 +00:00
|
|
|
http://127.0.0.1:8500/v1/query/8f246b77-f3e1-ff88-5b48-8ec93abf3e05
|
2017-04-04 16:33:22 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### Sample Response
|
|
|
|
|
|
|
|
The returned response is the same as the list of prepared queries above, only
|
|
|
|
with a single item present.
|
|
|
|
|
|
|
|
## Delete Prepared Query
|
|
|
|
|
|
|
|
This endpoint deletes an existing prepared query. If no query exists by the
|
|
|
|
given ID, an error is returned.
|
|
|
|
|
2020-04-06 20:27:35 +00:00
|
|
|
| Method | Path | Produces |
|
|
|
|
| -------- | -------------- | ------------------ |
|
|
|
|
| `DELETE` | `/query/:uuid` | `application/json` |
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
The table below shows this endpoint's support for
|
2023-01-25 16:52:43 +00:00
|
|
|
[blocking queries](/consul/api-docs/features/blocking),
|
|
|
|
[consistency modes](/consul/api-docs/features/consistency),
|
|
|
|
[agent caching](/consul/api-docs/features/caching), and
|
|
|
|
[required ACLs](/consul/api-docs/api-structure#authentication).
|
2017-04-04 16:33:22 +00:00
|
|
|
|
2018-09-06 10:34:28 +00:00
|
|
|
| Blocking Queries | Consistency Modes | Agent Caching | ACL Required |
|
|
|
|
| ---------------- | ----------------- | ------------- | ------------- |
|
|
|
|
| `NO` | `none` | `none` | `query:write` |
|
2017-04-04 16:33:22 +00:00
|
|
|
|
2022-05-10 15:51:11 +00:00
|
|
|
### Path Parameters
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
- `uuid` `(string: <required>)` - Specifies the UUID of the query to delete.
|
2022-05-10 15:51:11 +00:00
|
|
|
|
|
|
|
### Query Parameters
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
- `dc` `(string: "")` - Specifies the datacenter to query. This will default to
|
2022-05-10 15:51:11 +00:00
|
|
|
the datacenter of the agent being queried.
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
### Sample Request
|
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
```shell-session
|
2017-04-04 16:33:22 +00:00
|
|
|
$ curl \
|
|
|
|
--request DELETE \
|
2018-08-28 16:07:15 +00:00
|
|
|
http://127.0.0.1:8500/v1/query/8f246b77-f3e1-ff88-5b48-8ec93abf3e05
|
2017-04-04 16:33:22 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Execute Prepared Query
|
|
|
|
|
|
|
|
This endpoint executes an existing prepared query. If no query exists by the
|
|
|
|
given ID, an error is returned.
|
|
|
|
|
2022-03-19 20:32:38 +00:00
|
|
|
The HTTP response includes the `X-Consul-Results-Filtered-By-ACLs: true` header
|
|
|
|
if the [`Nodes`](#nodes) response array excludes results due to ACL policy configuration.
|
2023-01-25 16:52:43 +00:00
|
|
|
Refer to the [HTTP API documentation](/consul/api-docs/api-structure#results-filtered-by-acls) for more information.
|
2022-03-19 20:32:38 +00:00
|
|
|
|
2020-04-06 20:27:35 +00:00
|
|
|
| Method | Path | Produces |
|
|
|
|
| ------ | ---------------------- | ------------------ |
|
|
|
|
| `GET` | `/query/:uuid/execute` | `application/json` |
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
The table below shows this endpoint's support for
|
2023-01-25 16:52:43 +00:00
|
|
|
[blocking queries](/consul/api-docs/features/blocking),
|
|
|
|
[consistency modes](/consul/api-docs/features/consistency),
|
|
|
|
[agent caching](/consul/api-docs/features/caching), and
|
|
|
|
[required ACLs](/consul/api-docs/api-structure#authentication).
|
2017-04-04 16:33:22 +00:00
|
|
|
|
2020-04-06 20:27:35 +00:00
|
|
|
| Blocking Queries | Consistency Modes | Agent Caching | ACL Required |
|
|
|
|
| ---------------- | ----------------- | ------------- | --------------------- |
|
2021-08-09 21:29:43 +00:00
|
|
|
| `NO` | `all` | `simple` | `depends`<sup>1</sup> |
|
2017-04-04 16:33:22 +00:00
|
|
|
|
2020-04-07 18:55:19 +00:00
|
|
|
<sup>1</sup> If an ACL Token was bound to the query when it was defined then it will
|
|
|
|
be used when executing the request. Otherwise, the client's supplied ACL Token will
|
|
|
|
be used.
|
2017-04-04 16:33:22 +00:00
|
|
|
|
2022-05-10 15:51:11 +00:00
|
|
|
### Path Parameters
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
- `uuid` `(string: <required>)` - Specifies the UUID of the query to execute.
|
2022-05-10 15:51:11 +00:00
|
|
|
This parameter can also be the name of an existing prepared query,
|
|
|
|
or a name that matches a prefix name for a prepared query template.
|
|
|
|
|
|
|
|
### Query Parameters
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
- `dc` `(string: "")` - Specifies the datacenter to query. This will default to
|
2022-05-10 15:51:11 +00:00
|
|
|
the datacenter of the agent being queried.
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
- `near` `(string: "")` - Specifies to sort the resulting list in ascending
|
|
|
|
order based on the estimated round trip time from that node. Passing
|
2018-04-11 14:34:13 +00:00
|
|
|
`?near=_agent` will use the agent's node for the sort. Passing `?near=_ip`
|
|
|
|
will use the source IP of the request or the value of the X-Forwarded-For
|
|
|
|
header to lookup the node to use for the sort. If this is not present,
|
2017-04-04 16:33:22 +00:00
|
|
|
the default behavior will shuffle the nodes randomly each time the query is
|
|
|
|
executed.
|
|
|
|
|
|
|
|
- `limit` `(int: 0)` - Limit the size of the list to the given number of nodes.
|
|
|
|
This is applied after any sorting or shuffling.
|
|
|
|
|
2018-06-05 23:15:59 +00:00
|
|
|
- `connect` `(bool: false)` - If true, limit results to nodes that are
|
2022-05-10 15:51:11 +00:00
|
|
|
Connect-capable only. This parameter can also be specified directly on the template
|
2018-06-05 23:15:59 +00:00
|
|
|
itself to force all executions of a query to be Connect-only. See the
|
|
|
|
template documentation for more information.
|
|
|
|
|
2017-04-04 16:33:22 +00:00
|
|
|
### Sample Request
|
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
```shell-session
|
2017-04-04 16:33:22 +00:00
|
|
|
$ curl \
|
2018-08-28 16:07:15 +00:00
|
|
|
http://127.0.0.1:8500/v1/query/8f246b77-f3e1-ff88-5b48-8ec93abf3e05/execute?near=_agent
|
2017-04-04 16:33:22 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### Sample Response
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"Service": "redis",
|
|
|
|
"Nodes": [
|
|
|
|
{
|
|
|
|
"Node": {
|
|
|
|
"ID": "40e4a748-2192-161a-0510-9bf59fe950b5",
|
|
|
|
"Node": "foobar",
|
|
|
|
"Address": "10.1.10.12",
|
2017-04-18 12:02:24 +00:00
|
|
|
"Datacenter": "dc1",
|
2017-04-04 16:33:22 +00:00
|
|
|
"TaggedAddresses": {
|
|
|
|
"lan": "10.1.10.12",
|
|
|
|
"wan": "10.1.10.12"
|
|
|
|
},
|
2020-04-06 20:27:35 +00:00
|
|
|
"NodeMeta": { "instance_type": "m3.large" }
|
2017-04-04 16:33:22 +00:00
|
|
|
},
|
|
|
|
"Service": {
|
|
|
|
"ID": "redis",
|
|
|
|
"Service": "redis",
|
|
|
|
"Tags": null,
|
2020-04-06 20:27:35 +00:00
|
|
|
"Meta": { "redis_version": "4.0" },
|
2017-04-04 16:33:22 +00:00
|
|
|
"Port": 8000
|
|
|
|
},
|
|
|
|
"Checks": [
|
|
|
|
{
|
|
|
|
"Node": "foobar",
|
|
|
|
"CheckID": "service:redis",
|
|
|
|
"Name": "Service 'redis' check",
|
|
|
|
"Status": "passing",
|
|
|
|
"Notes": "",
|
|
|
|
"Output": "",
|
|
|
|
"ServiceID": "redis",
|
|
|
|
"ServiceName": "redis"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Node": "foobar",
|
|
|
|
"CheckID": "serfHealth",
|
|
|
|
"Name": "Serf Health Status",
|
|
|
|
"Status": "passing",
|
|
|
|
"Notes": "",
|
|
|
|
"Output": "",
|
|
|
|
"ServiceID": "",
|
|
|
|
"ServiceName": ""
|
|
|
|
}
|
|
|
|
],
|
2020-04-06 20:27:35 +00:00
|
|
|
"DNS": {
|
|
|
|
"TTL": "10s"
|
|
|
|
},
|
|
|
|
"Datacenter": "dc3",
|
|
|
|
"Failovers": 2
|
|
|
|
}
|
|
|
|
]
|
2017-04-04 16:33:22 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
- `Nodes` contains the list of healthy nodes providing the given service, as
|
|
|
|
specified by the constraints of the prepared query.
|
|
|
|
|
|
|
|
- `Service` has the service name that the query was selecting. This is useful
|
|
|
|
for context in case an empty list of nodes is returned.
|
|
|
|
|
|
|
|
- `DNS` has information used when serving the results over DNS. This is just a
|
|
|
|
copy of the structure given when the prepared query was created.
|
|
|
|
|
|
|
|
- `Datacenter` has the datacenter that ultimately provided the list of nodes and
|
|
|
|
`Failovers` has the number of remote datacenters that were queried while
|
|
|
|
executing the query. This provides some insight into where the data came from.
|
|
|
|
This will be zero during non-failover operations where there were healthy
|
|
|
|
nodes found in the local datacenter.
|
|
|
|
|
|
|
|
## Explain Prepared Query
|
|
|
|
|
|
|
|
This endpoint generates a fully-rendered query for a given name, post
|
|
|
|
interpolation.
|
|
|
|
|
2020-04-06 20:27:35 +00:00
|
|
|
| Method | Path | Produces |
|
|
|
|
| ------ | ---------------------- | ------------------ |
|
|
|
|
| `GET` | `/query/:uuid/explain` | `application/json` |
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
The table below shows this endpoint's support for
|
2023-01-25 16:52:43 +00:00
|
|
|
[blocking queries](/consul/api-docs/features/blocking),
|
|
|
|
[consistency modes](/consul/api-docs/features/consistency),
|
|
|
|
[agent caching](/consul/api-docs/features/caching), and
|
|
|
|
[required ACLs](/consul/api-docs/api-structure#authentication).
|
2017-04-04 16:33:22 +00:00
|
|
|
|
2018-09-06 10:34:28 +00:00
|
|
|
| Blocking Queries | Consistency Modes | Agent Caching | ACL Required |
|
|
|
|
| ---------------- | ----------------- | ------------- | ------------ |
|
2021-08-09 21:29:43 +00:00
|
|
|
| `NO` | `all` | `none` | `query:read` |
|
2017-04-04 16:33:22 +00:00
|
|
|
|
2022-05-10 15:51:11 +00:00
|
|
|
### Path Parameters
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
- `uuid` `(string: <required>)` - Specifies the UUID of the query to explain.
|
2022-05-10 15:51:11 +00:00
|
|
|
This parameter can also be the name of an existing prepared query,
|
|
|
|
or a name that matches a prefix name for a prepared query template.
|
|
|
|
|
|
|
|
### Query Parameters
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
- `dc` `(string: "")` - Specifies the datacenter to query. This will default to
|
2022-05-10 15:51:11 +00:00
|
|
|
the datacenter of the agent being queried.
|
2017-04-04 16:33:22 +00:00
|
|
|
|
|
|
|
### Sample Request
|
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
```shell-session
|
2017-04-04 16:33:22 +00:00
|
|
|
$ curl \
|
2018-08-28 16:07:15 +00:00
|
|
|
http://127.0.0.1:8500/v1/query/8f246b77-f3e1-ff88-5b48-8ec93abf3e05/explain
|
2017-04-04 16:33:22 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### Sample Response
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"Query": {
|
|
|
|
"ID": "8f246b77-f3e1-ff88-5b48-8ec93abf3e05",
|
|
|
|
"Name": "my-query",
|
|
|
|
"Session": "adf4238a-882b-9ddc-4a9d-5b6758e4159e",
|
|
|
|
"Token": "<hidden>",
|
|
|
|
"Name": "geo-db",
|
|
|
|
"Template": {
|
|
|
|
"Type": "name_prefix_match",
|
|
|
|
"Regexp": "^geo-db-(.*?)-([^\\-]+?)$"
|
|
|
|
},
|
|
|
|
"Service": {
|
|
|
|
"Service": "mysql-customer",
|
|
|
|
"Failover": {
|
|
|
|
"NearestN": 3,
|
|
|
|
"Datacenters": ["dc1", "dc2"]
|
|
|
|
},
|
|
|
|
"OnlyPassing": true,
|
|
|
|
"Tags": ["primary"],
|
2018-03-28 14:04:50 +00:00
|
|
|
"Meta": { "mysql_version": "5.7.20" },
|
2020-04-06 20:27:35 +00:00
|
|
|
"NodeMeta": { "instance_type": "m3.large" }
|
2017-04-04 16:33:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|